UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: geecee on April 27, 2009, 08:28:45 PM

Title: ROCK, PAPER, SCISSORS
Post by: geecee on April 27, 2009, 08:28:45 PM
Hi there!

ROCK crushes SCISSORS, SCISSORS cut PAPER and PAPER covers ROCK.

What was once a game to settle playground disputes has grown to be on the cusp of recognition as a global sport.

Here's a short programme that I converted from one that appeared in an old computer magazine.  The conversion wasn't difficult but I've spruced it up a little.

I've posted the code here but it and necessary media are in the attached .zip file.

[pbcode]
; PROJECT : Rock, Paper, Scissors
; AUTHOR  : geecee
; CREATED : 26/04/2009
; EDITED  : 28/04/2009
rem ---------------------------------------------------------------------

rem Tell PB to include the input support library  
#include "Input"

rem Load required media
RPSimage=loadnewimage("RPS.png")

rem Display image
drawimage RPSimage,0,0,false

rem This command will hide mouse pointer
mouse off

rem Load required fonts
rem Set text font "Arial" size 30 to bold
LoadFont "Arial",2,30,1
rem Set text font "Arial" size 24 to bold
LoadFont "Arial",3,24,1
rem Set text font "Arial" size 40 to bold
LoadFont "Arial",4,40,1
rem Set text font "Arial" size 18 to bold
LoadFont "Arial",5,18,1
 
rem Set Font
SetFont 2

rem Set ink colour
ink rgb(255,217,128)

rem Write text to screen
text 360,30, "ROCK, PAPER, SCISSORS"

rem Set font
setfont 5

rem Write text to screen
text 360,75,"ROCK crushes SCISSORS, SCISSORS cut"
text 360,93,"PAPER and PAPER covers ROCK."
text 360,129,"What was once a game to settle playground"
text 360,147,"disputes has grown to be on the cusp of"
text 360,165,"recognition as a global sport."

rem Set font
setfont 3

rem Write text to screen
centertext 400,550, "Press any key to begin"

rem Make screen visible and wait for a key press
sync
waitkey
waitnokey

rem Draw a box to hide previous text at location
boxc 2,550,798,599,1,rgb(255,0,0)

rem Set variable values to zero
wins=0
losses=0
ties=0
 
 rem ============================================================
 rem  Programme main loop
 rem ============================================================
 do
 
   rem Determine width of longest text string
   tw=400-gettextwidth("SCISSORS")/2
   
   rem Determine text height
   height=gettextheight("SCISSORS")
 
   rem Set across and down coordinates
   across=400-gettextwidth("SCISSORS beats PAPER")/2
   down=160
   
   rem Draw box to hide previous text at location
   boxc 2,down+height*3,798,down+height*7,1,rgb(255,0,0)          
 
   rem Write text to screen       
   text tw,down+height*3,"1 ... ROCK"
   text tw,down+height*4,"2 ... PAPER"
   text tw,down+height*5,"3 ... SCISSORS"
   text tw,down+height*6,"4 ... QUIT"   
   
   repeat       

     rem Computer random choice
     for a=1 to 10
      aichoice=rnd(2)+1
     next
 
     rem Alternative to if/then statements  
     select aichoice
       case 1
         aichoicetxt$ = "ROCK"
       case 2
         aichoicetxt$ = "PAPER"
       case 3
         aichoicetxt$ = "SCISSORS"
     endselect
     
     rem Write text to screen   
     centertext 400,down+height*11,"Wins: "+str$(wins)+"     Losses: "+str$(losses)+"     Ties: "+str$(ties)

     rem Set cursor position   
     setcursor 400-gettextwidth("Choose from any one of the above categories and key in its number > ")/2,500
 
     rem Enter player choice
     playerchoice=val(staticinput("Choose from any one of the above categories and key in its number > "))
 
     rem Alternative to if/then statements
     select playerchoice
       case 1
         playerchoicetxt$ = "ROCK"
       case 2
         playerchoicetxt$ = "PAPER"
       case 3
         playerchoicetxt$ = "SCISSORS"
       case 4  
         rem Draw a box to hide previous text at location
         boxc 2,200,798,580,1,rgb(255,0,0)
 
         rem Set Font
         SetFont 4
           
         rem Write text to screen
         centertext 400,300,"THANKS FOR PLAYING ... BYE!"

         rem Display screen
         sync

         rem A short wait before proceeding
         for times=1 to 2
           wait 1000
         next times

         rem End programme
         end        
       endselect
 
       rem Draw a box to hide previous text at location
       boxc 2,500,798,530,1,rgb(255,0,0)
 
   until playerchoice>0 and playerchoice<5
 
   rem Go to subroutine to compare choices
   gosub compare

   rem Make screen visible
   sync

   rem A short wait before proceeding
   for times=1 to 2
     wait 1000
   next times
 
   rem Draw a box to hide previous text at location
   boxc 2,down+height*8,798,down+height*10,1,rgb(255,0,0)
   boxc 2,down+height*11,798,down+height*12,1,rgb(255,0,0)

 rem End programme main loop    
 loop
 
rem ============================================================
rem  Subroutine to compare choices
rem ============================================================
compare:  
 
  rem Alternative to if/then statements
  select playerchoice
    case 1
        if aichoice = 2 then beats$="covers":gosub aiwin
        if aichoice = 3 then beats$="crushes":gosub playerwin
        if playerchoice = aichoice then gosub tie
    case 2
        if aichoice = 1 then beats$="covers":gosub playerwin
        if aichoice = 3 then beats$="cuts":gosub aiwin
        if playerchoice = aichoice then gosub tie
    case 3
        if aichoice = 1 then beats$="crushes":gosub aiwin
        if aichoice = 2 then beats$="cuts":gosub playerwin
        if playerchoice = aichoice then gosub tie
  endselect
 
return
 
rem ============================================================
rem  Subroutine to track computer losses
rem ============================================================
aiwin:

  losses=losses+1
  text across,down+height*8, "You Lose"
  text across,down+height*9, aichoicetxt$+" "+beats$+" "+playerchoicetxt$
 
return
 
rem ============================================================
rem  Subroutine to track player wins
rem ============================================================
playerwin:

  wins=wins+1
  text across,down+height*8, "You Win"
  text across,down+height*9,playerchoicetxt$+" "+beats$+" "+aichoicetxt$
 
return

rem ============================================================
rem  Subroutine to track ties
rem ============================================================
tie:

  ties=ties+1
  text across,down+height*8, "It's a Tie"
 
return
[/pbcode]

:)
Enjoy
geecee
Title: Re: ROCK, PAPER, SCISSORS
Post by: OldNESJunkie on April 28, 2009, 08:37:36 PM
Um,the media isn't in the zip file I d/l ?
Title: Re: ROCK, PAPER, SCISSORS
Post by: geecee on April 29, 2009, 12:32:07 AM
Sorry OldNESJunkie

My error ...... Now fixed.

:-[
geecee
Title: Re: ROCK, PAPER, SCISSORS
Post by: OldNESJunkie on April 29, 2009, 05:10:35 PM
No problemo, I just removed the references to the media to try it out, pretty good, some animations would be cool though. You are becoming a coding machine apparently, seems everyday you post something.  :o
Title: Re: ROCK, PAPER, SCISSORS
Post by: geecee on April 30, 2009, 04:47:58 AM
Hi OldNESJunkie

Quotesome animations would be cool though.

Not my forte I'm afraid ...... but I'm sure someone could do the necessary.

:)
geecee