Quick Search for:  in language:    
program,allows,user,play,simple,random,game,R
   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:
 
Login





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

Print
Email
 
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.
 
code:
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.    =**************************************
    
    #!/usr/bin/perl
    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=$main->Frame(-relief=>"sunken", 
    	-height=>400, 
    	-width=>600, 
    	-background=>"white");
    $mainFrame->pack(-expand=>1, -fill=>"both", -anchor=>"sw");
    $button1 = $mainFrame->Button(-height=>2,
    	-width=>10,
    	-text=>"Rock",
    	-justify=>"left",
    	-foreground=>"white",
    	-command=>\&pick;_rock);
    $button1->place(-relx=>0.0, -rely=>1.0, -anchor=>"sw");
    $button2 = $main->Button(-height=>2,
    	-width=>10,
    	-text=>"Paper",
    	-justify=>"left",
    	-foreground=>"white",
    	-command=>\&pick;_paper);
    $button2->place(-relx=>0.5, -rely=>1.0, -anchor=>"s");
    $button3 = $main->Button(-height=>2,
    	-width=>10,
    	-text=>"Scissors",
    	-justify=>"left",
    	-foreground=>"white",
    	-command=>\&pick;_scissors);
    $button3->place(-relx=>1.0, -rely=>1.0, -anchor=>"se");
    $filebutton=$menuBar->Menubutton(-text=>"File",
    	-underline=>3,
    	-foreground=>"white");
    $fileMenu = $filebutton->Menu();
    $filebutton->configure(-menu=>$fileMenu);
    $fileMenu->command(-command=>\&new;_game,
    	-label=>"New Game",
    	-foreground=>"white",
    	-activeforeground=>"light blue");
    $fileMenu->command(-command=>\&end;_game,
    	-label=>"End Current Game",
    	-foreground=>"white",
    	-activeforeground=>"light blue");
    $fileMenu->separator;
    $fileMenu->command(-command=>\&exit;,
    	-label=>"Exit",
    	-foreground=>"white",
    	-activeforeground=>"light blue");
    $filebutton->pack(-side=>"left");
    $menuBar->pack(-side=>"top", -fill=>"x");
    $text=$mainFrame->Text(-background=>"light blue", 
    	-relief=>"sunken", 
    	-wrap=>"word",
    	-height=>20, 
    	-width=>80);
    $text->place(-relx=>0.5, -rely=>0.5, -anchor=>"center");
    $win_disp=$mainFrame->Text(-height=>2, 
    	-width=>3, 
    	-relief=>"raised",
    	-background=>"gold", 
    	-foreground=>"maroon", 
    	-font=>"times-24");
    $win_disp->insert('0.0', "0");
    $win_disp->place(-relx=>'0.08', -rely=>'0.0', -anchor=>'n');
    $win_label=$mainFrame->Label(-height=>1, 
    	-width=>4.2, 
    	-background=>"white", 
    	-foreground=>"black", 
    	-text=>"Wins:");
    $win_label->place(-relx=>'0.0', -rely=>'0.0', -anchor=>'nw');
    $loss_disp=$mainFrame->Text(-height=>2, 
    	-width=>3, 
    	-relief=>"raised",
    	-background=>"red", 
    	-foreground=>"dark blue", 
    	-font=>"times-24");
    $loss_disp->insert('0.0', "0");
    $loss_disp->place(-relx=>'0.37', -rely=>'0.0', -anchor=>'n');
    $loss_label=$mainFrame->Label(-height=>1, 
    	-width=>6, 
    	-background=>"white", 
    	-foreground=>"black", 
    	-text=>"Losses:");
    $loss_label->place(-relx=>'0.3', -rely=>'0.0', -anchor=>'n');
    $draw_disp=$mainFrame->Text(-height=>2, 
    	-width=>3, 
    	-relief=>"raised",
    	-background=>"green", 
    	-foreground=>"blue", 
    	-font=>"times-24");
    $draw_disp->insert('0.0', "0");
    $draw_disp->place(-relx=>'0.62', -rely=>'0.0', -anchor=>'n');
    $draw_label=$mainFrame->Label(-height=>1, 
    	-width=>5.5, 
    	-background=>"white", 
    	-foreground=>"black", 
    	-text=>"Draws:");
    $draw_label->place(-relx=>'.55', -rely=>'0.0', -anchor=>'n');
    $node = {
    DATA => $value,
    NEXT => \$next,
    LAST => \$last,
    };
    createList();
    $text->insert('0.0', 'Click on Rock Paper or Scissors to start each turn.');
    $p=$head;
    $over=0;
    MainLoop;
    sub pick_rock{
    	$yourChoice = "Rock";
    	MAIN_FCN();
    	GAME();
    }
    sub pick_paper{
    	$yourChoice = "Paper";
    	MAIN_FCN();
    	GAME();
    }
    sub pick_scissors{
    	$yourChoice = "Scissors";
    	MAIN_FCN();
    	GAME();
    }
    sub exit{
    	$main->destroy;}
    sub MAIN_FCN{
    	srand;
    	$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";}
    	$over=0;
    	return;
    }
    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");
    				$losses++;
    				$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");
    				$wins++;
    				$win_disp->delete('0.0', 'end');
    				$win_disp->insert('0.0', $wins);
    				$over = 1;
    			}
    			else
    			{
    				$text->insert('0.0', "Computer chose $p->{DATA}. It's a Draw!\n\n");	
    				$draws++;
    				$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");
    			last;
    		}
    		else
    		{
    			$p = $p->{NEXT};
    		}
    	}
    }
    sub end_game{
    	$total = $wins+$losses+$draws;
    	$percent = ($wins/$total)*100;
    	$adjustedScore = $percent * $total;
    	$points = (($wins*3)+$draws)-($losses*2);
    	calcMessage();
    	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");
    	new_game();
    }
    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.).
Reason:
 
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:
Reason:

 
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.
 
Name:
Comment:

 

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.