Quick Search for:  in language:    
   Code/Articles  |  Newest/Best  |  Community  |  Jobs  |  Other  |  Goto  | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
Perl Stats

 Code: 74,273. lines
 Jobs: 25. postings

 How to support the site

Sponsored by:

You are in:

Latest Code Ticker for Perl.
Simple Perl Ping
By John Hass on 12/29

Very basic login script template with cookies
By Aaron L. Anderson on 12/29

Click here to put this ticker on your site!

Add this ticker to your desktop!

Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites


Rock, Paper, Scissors w/ GUI

VB icon
Submitted on: 11/19/2003 3:50:39 PM
By: Kurt Rudolph  
Level: Beginner
User Rating: By 2 Users
Compatibility:5.0 (all versions)

Users have accessed this code 627 times.
(About the author)
     This program allows the user to play a simple, random game of Rock Paper Scissors against the computer, have their score kept, and get a message appropriate to their score (read: canned insult) at the end of the game.
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
Terms of Agreement:   
By using this code, you agree to the following terms...   
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.

    = Name: Rock, Paper, Scissors w/ GUI
    = Description:This program allows the us
    =     er to play a simple, random game of Rock
    =     Paper Scissors against the computer, hav
    =     e their score kept, and get a message ap
    =     propriate to their score (read: canned i
    =     nsult) at the end of the game.
    = By: Kurt Rudolph
    = Inputs:User must have Perl::Tk module 
    =     installed
    = Returns:One word: Lots of fun.
    = Assumes:To reset a game without gettin
    =     g your score, go to File->New Game. T
    =     o stop the current game, go to File->
    =     End Current Game.
    = Side Effects:Crying, with both joy and
    =     pain
    =This code is copyrighted and has    = limited warranties.Please see http://w
    =     ww.Planet-Source-Code.com/vb/scripts/Sho
    =     wCode.asp?txtCodeId=550&lngWId;=6    =for details.    =**************************************
    use Tk;
    $main = new MainWindow(-height=>1000, -width=>1000, -background=>"black");
    $menuBar = $main->Frame(-relief=>"raised",
    		-background=>"light blue");
    $menuBar->pack(-side=>"top", -fill=>"x");
    $mainFrame->pack(-expand=>1, -fill=>"both", -anchor=>"sw");
    $button1 = $mainFrame->Button(-height=>2,
    $button1->place(-relx=>0.0, -rely=>1.0, -anchor=>"sw");
    $button2 = $main->Button(-height=>2,
    $button2->place(-relx=>0.5, -rely=>1.0, -anchor=>"s");
    $button3 = $main->Button(-height=>2,
    $button3->place(-relx=>1.0, -rely=>1.0, -anchor=>"se");
    $fileMenu = $filebutton->Menu();
    	-label=>"New Game",
    	-activeforeground=>"light blue");
    	-label=>"End Current Game",
    	-activeforeground=>"light blue");
    	-activeforeground=>"light blue");
    $menuBar->pack(-side=>"top", -fill=>"x");
    $text=$mainFrame->Text(-background=>"light blue", 
    $text->place(-relx=>0.5, -rely=>0.5, -anchor=>"center");
    $win_disp->insert('0.0', "0");
    $win_disp->place(-relx=>'0.08', -rely=>'0.0', -anchor=>'n');
    $win_label->place(-relx=>'0.0', -rely=>'0.0', -anchor=>'nw');
    	-foreground=>"dark blue", 
    $loss_disp->insert('0.0', "0");
    $loss_disp->place(-relx=>'0.37', -rely=>'0.0', -anchor=>'n');
    $loss_label->place(-relx=>'0.3', -rely=>'0.0', -anchor=>'n');
    $draw_disp->insert('0.0', "0");
    $draw_disp->place(-relx=>'0.62', -rely=>'0.0', -anchor=>'n');
    $draw_label->place(-relx=>'.55', -rely=>'0.0', -anchor=>'n');
    $node = {
    DATA => $value,
    NEXT => \$next,
    LAST => \$last,
    $text->insert('0.0', 'Click on Rock Paper or Scissors to start each turn.');
    sub pick_rock{
    	$yourChoice = "Rock";
    sub pick_paper{
    	$yourChoice = "Paper";
    sub pick_scissors{
    	$yourChoice = "Scissors";
    sub exit{
    sub MAIN_FCN{
    	$num = int (rand 3); # get a random integer, 1 through 3
    	if($num == 0){$compChoice = "Rock";}
    	elsif($num == 1){$compChoice = "Paper";}
    	elsif($num == 2){$compChoice = "Scissors";}
    sub GAME{
    	while($over == 0 && $yourChoice ne "exit")
    		if($yourChoice eq $p->{DATA})
    			if($p->{NEXT}->{DATA} eq $compChoice)
    				$text->insert('0.0', "Computer chose $p->{NEXT}->{DATA}. You lose.\n\n");
    				$loss_disp->delete('0.0', 'end');
    				$loss_disp->insert('0.0', $losses);
    				$over = 1;
    			elsif($p->{LAST}->{DATA} eq $compChoice)
    				$text->insert('0.0', "Computer chose $p->{LAST}->{DATA}. You win!\n\n");
    				$win_disp->delete('0.0', 'end');
    				$win_disp->insert('0.0', $wins);
    				$over = 1;
    				$text->insert('0.0', "Computer chose $p->{DATA}. It's a Draw!\n\n");	
    				$draw_disp->delete('0.0', 'end');
    				$draw_disp->insert('0.0', $draws);
    				$over = 1;
    		elsif($yourChoice ne "Rock" && $yourChoice ne "Paper" && $yourChoice ne "Scissors")
    			$text->insert('0.0', "Please enter 'Rock', 'Paper', or 'Scissors' next time\n");
    			$p = $p->{NEXT};
    sub end_game{
    	$total = $wins+$losses+$draws;
    	$percent = ($wins/$total)*100;
    	$adjustedScore = $percent * $total;
    	$points = (($wins*3)+$draws)-($losses*2);
    	my $button = $main->messageBox(-type =>'OK',
    		-title => 'Message',
    	-message=>"You racked up a $message $points points.\nBut your amazing rock, paper, scissors prowess aside, you$taunt\n");
    sub createList
    	$head = $node->{DATA};
    	$curr = \$head;
    	$head->{DATA} = "Rock";
    	$head->{NEXT} = $node->{DATA};
    	$head->{NEXT}->{DATA} = "Paper";
    	$curr = $head->{NEXT};
    	$curr->{LAST} = $head;
    	$curr->{NEXT} = $node->{DATA};
    	$curr->{NEXT}->{DATA} = "Scissors";
    	$curr = $curr->{NEXT};
    	$curr->{LAST} = $head->{NEXT};
    	$curr->{NEXT} = $head;
    	$head->{LAST} = $curr;
    sub calcMessage
    	if($points < 25){$message = "pathetic";
    	$taunt = "'re a bedwetter";}
    	elsif($points>26 && $points<50){$message = "quietly dignified";
    	$taunt = "r mom still breast feeds you";}
    	elsif($points>51 && $points<100){$message = "unbearably smug";
    	$taunt = " are a chronic masturbator";}
    	else{$message = "contemptibly competent";
    	$taunt = " captain the SS Anal Avenger as with your first mate Hot Karl";}
    sub new_game{
    	$wins = 0;
    	$losses = 0;
    	$draws = 0;
    	$text->delete('0.0', 'end');
    	$win_disp->delete('0.0', 'end');
    	$loss_disp->delete('0.0', 'end');
    	$draw_disp->delete('0.0', 'end');
    	$loss_disp->insert('0.0', $losses);
    	$draw_disp->insert('0.0', $draws);
    	$win_disp->insert('0.0', $wins);

Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Your Vote!

What do you think of this code(in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
Other User Comments
11/19/2003 5:16:46 PM:Randy McCleary
Nice coding. Its always good see others 
on here writing code with Perl/Tk. Keep 
up the good work. Look forward to more 
submissions from you.
Keep the Planet clean! If this comment was disrespectful, please report it:

Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.


Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | Perl Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.