Board Logo
« Rock, Paper, Scissors game »

Welcome Guest. Please Login or Register.
Jan 21st, 2018, 12:48am


Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Please use the forums Search feature before asking.
Please post code using the code box described in Format Your Messages.
This will keep indentation, separate it better form the message and prevent gibberish.
If the code is too long for one post or additional files are needed, upload a ZIP archive to the Just BASIC Files Archive Site.

« Previous Topic | Next Topic »
Pages: 1 2 3  Notify Send Topic Print
 veryhotthread  Author  Topic: Rock, Paper, Scissors game  (Read 1953 times)
Welopez
Moderator
ImageImageImageImageImage


member is offline

Avatar

Never let your beliefs get in the way of learning.


PM

Gender: Male
Posts: 4407
xx Re: Rock, Paper, Scissors game
« Reply #15 on: Dec 31st, 2007, 2:38pm »

Quote:
help! nothing happens on my computer when you hit the button!??!

Ask yourself what happens if npc=1.465329, or npc=3.987652, or, well you get the idea.

It's also helpful when you have a trapclose statement.

wepon is spelled "weapon." Not that I'm correcting your spelling, but if you don't pay attention to the spelling of statements and variables, your program most likely will crash (if it will compile!). Words inside quotations can be spelled any way you like... misspelled words simply look unprofessional.
User IP Logged

JB 1.01
Win7 64bit, 4 GB RAM, Pentium 6200@2.13 GHz (laptop)
WinXP, 1 GB RAM, Intel N270@1.6 GHz (netbook)
Crusader
Senior Member
ImageImageImageImageImage


member is offline

Avatar

"love is the red the rose on your coffin door"

YIM YIM AIM
Homepage PM

Gender: Male
Posts: 581
xx Re: Rock, Paper, Scissors game
« Reply #16 on: Dec 31st, 2007, 5:12pm »

oh, so i should use integer? :o.

i did put in integer, but now it only works half the time 0.o
(trapclose added)

Code:
'paper beats rock
'rock beats scissor
'scissor beats paper

'rock is 1
'paper is 2
'scissors is 3
    nomainwin

    WindowWidth = 235
    WindowHeight = 172

    button #main.p, "Paper", [p], UL, 26, 16, 70, 27
    button #main.s, "Scissor", [s], UL, 26, 52, 70, 27
    button #main.r, "Rock", [r], UL, 26, 85, 70, 27
    statictext #main.statictext4, "Press A 'Weapon' Type to attack.", 27, 118, 248, 20

    open "Rock Paper Scissors" for window as #main
    print #main, "font ms_sans_serif 0 16"
print #main, "trapclose [quit]"

[main]
    wait


[p]
player$="Paper"
npc = int(rnd(1)*3)
if npc = 3 then goto [lose]
if npc = 1 then goto [win]

    wait

[s]
player$="Scissors"
npc = int(rnd(1)*3)
if npc = 1 then goto [lose]
if npc = 2 then goto [win]
    wait


[r]
player$="Rock"
npc = int(rnd(1)*3)
if npc = 2 then goto [lose]
if npc = 3 then goto [win]
    wait



[lose]
if npc = 1 then npc$ = "Rock"
if npc = 2 then npc$ = "Paper"
if npc = 3 then npc$ = "Scissors"

 notice "You Picked"; player$;" they picked ";npc$
 notice "You Lose :'( Try Again."

 goto [main]

[win]

if npc = 1 then npc$ = "Rock"
if npc = 2 then npc$ = "Paper"
if npc = 3 then npc$ = "Scissors"
 notice "You Picked"; player$;" they picked"; npc$
notice "You Win :) Good Job!"
goto [main]
 
User IP Logged

Mutant Turkey Talk

Welopez
Moderator
ImageImageImageImageImage


member is offline

Avatar

Never let your beliefs get in the way of learning.


PM

Gender: Male
Posts: 4407
xx Re: Rock, Paper, Scissors game
« Reply #17 on: Dec 31st, 2007, 5:17pm »

Yes.... you must use the INT function, unless you wish to use SELECT CASE and npc>0; npc>1; npc>2. To insure the possibilities are 1, 2, 3, use this form:
Code:
npc = INT(rnd(1)*3)+1
 

That will insure your answer cannot be zero, neither can it be four. Study the explanation of RND() and INT() in the helpfile until you are certain you understand it.

Honest! The helpfile should be your best friend!


User IP Logged

JB 1.01
Win7 64bit, 4 GB RAM, Pentium 6200@2.13 GHz (laptop)
WinXP, 1 GB RAM, Intel N270@1.6 GHz (netbook)
Crusader
Senior Member
ImageImageImageImageImage


member is offline

Avatar

"love is the red the rose on your coffin door"

YIM YIM AIM
Homepage PM

Gender: Male
Posts: 581
xx Re: Rock, Paper, Scissors game
« Reply #18 on: Dec 31st, 2007, 5:29pm »

It is, wink but my helpfile is all screwed lately so i've held off opening it unless i want my comp to crash. and loose my work.

User IP Logged

Mutant Turkey Talk

Welopez
Moderator
ImageImageImageImageImage


member is offline

Avatar

Never let your beliefs get in the way of learning.


PM

Gender: Male
Posts: 4407
xx Re: Rock, Paper, Scissors game
« Reply #19 on: Dec 31st, 2007, 5:53pm »

Must be something very strange you've done. The helpfile cannot possibly crash your work. I've been keeping it open while programming for the past four years so I can refer to it frequently to insure the correct syntax of a command or control.

You can download and install Just Basic again (which includes the helpfile) without making any changes to the files contained in just basic. If you have one or one hundred "mySuperProg.bas" in your JB folder, it will not be over written or deleted. Only the files, help, and demo included with Just Basic will be over written with new and un-corrupted copies.

User IP Logged

JB 1.01
Win7 64bit, 4 GB RAM, Pentium 6200@2.13 GHz (laptop)
WinXP, 1 GB RAM, Intel N270@1.6 GHz (netbook)
StrongTower
Member in Training
ImageImage


member is offline

Avatar

No one is greater than God!


PM

Gender: Male
Posts: 36
xx Re: Rock, Paper, Scissors game
« Reply #20 on: Dec 31st, 2007, 9:21pm »

I fix it for you

Code:

'paper beats rock
'rock beats scissor
'scissor beats paper

'rock is 1
'paper is 2
'scissors is 3
    nomainwin

    WindowWidth = 235
    WindowHeight = 172

    button #main.p, "Paper", [p], UL, 26, 16, 70, 27
    button #main.s, "Scissor", [s], UL, 26, 52, 70, 27
    button #main.r, "Rock", [r], UL, 26, 85, 70, 27
    statictext #main.statictext4, "Press A 'Wepon' Type to attack.", 27, 118, 248, 20

    open "Rock Paper Scissors" for window as #main
    print #main, "font ms_sans_serif 0 16"
#main, "trapclose [quit]"


[main]
    wait


[p]
player$="Paper"
npc = int(rnd(1)*3) + 1
notice npc
if npc = 3 then goto [lose]
if npc = 1 then goto [win]

    wait

[s]
player$="Scissors"
npc = int(rnd(1)*3) + 1
if npc = 1 then goto [lose]
if npc = 2 then goto [win]
    wait


[r]
player$="Rock"
npc = int(rnd(1)*3) + 1
if npc = 2 then goto [lose]
if npc = 3 then goto [win]
    wait



[lose]
if npc = 1 then npc$ = "rock"
if npc = 2 then npc$ = "paper"
if npc = 3 then npc$ = "scissors"

 notice "You Picked ";player$;" they picked ";npc$
 notice "You Lose :'( Try Again."

 goto [quit]
wait
[win]
if npc = 1 then npc$ = "rock"
if npc = 2 then npc$ = "paper"
if npc = 3 then npc$ = "scissors"
 notice "You Picked ";player$;" they picked ";npc$


notice "You Win :) Good Job!"
goto [quit]
wait

[quit]
close#main
end



 
User IP Logged

JB/LB Programmer at work!



God Bless All!
Crusader
Senior Member
ImageImageImageImageImage


member is offline

Avatar

"love is the red the rose on your coffin door"

YIM YIM AIM
Homepage PM

Gender: Male
Posts: 581
xx Re: Rock, Paper, Scissors game
« Reply #21 on: Jan 1st, 2008, 10:15am »

thanks a bunch mate tongue,


to what welopez said; I reinstalled and everything works fine and my stuff is still there
« Last Edit: Jan 1st, 2008, 10:16am by Crusader » User IP Logged

Mutant Turkey Talk

Isaiah
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 2
xx Re: Rock, Paper, Scissors game
« Reply #22 on: Jan 28th, 2008, 12:03am »

Hello!

This is my very first post in the Just BASIC forums, and also one of my first programs....

So, it's not nearly as good as the others, but I gave it my best shot. =)

Code:
'By Isaiah Vallejo.  =D

'Here, the possible choices are mapped to numbers.
dim a$(2)
a$(0) = "rock"
a$(1) = "paper"
a$(2) = "scissors"

'Generates a random number.
generate = int(rnd(1)*100)

'Determines the computer's pick based on the randomly generated number.
if generate >= 0 and generate <= 33 _
 then comppick = 0
if generate >= 33 and generate <= 66 _
 then comppick = 1
if generate >= 66 and generate <= 100 _
 then comppick = 2


'Accepts input from the user.
input "Rock, Paper, Scissors:  " ; pick$

'Takes the user's input, and maps it to a possible choice.
select case lower$(pick$)
    case a$(0)
    userpick = 0
    case a$(1)
    userpick = 1
    case a$(2)
    userpick = 2
end select

'Compares the user's pick and the computer's pick by way of the
'detwin()--"determine winnner"-- function.
results = detwin(userpick, comppick)


'This part was added mostly to check the logic of the detwin() function
print "You chose:  " + a$(userpick)
print "And I chose:  " + a$(comppick)

if results = -1 then print "It's a tie!"
if results = 0 then print "You lose."
if results = 1 then print "You win!"


end

'Function compares the two inputs.  Since 0,1, and 2 have been mapped to
'possible choices, they are compared in this manner.
function detwin(x, y)

 if x = y then detwin = -1

'Select case for the variable x, which will be mapped to the variable
'userpick.  The output of this function depends on what the input for the
'variable y is, which is mapped to the variable comppick.
 select case x
  case 0
   if y = 2 then detwin = 1 else _
   if y = 1 then detwin = 0
  case 1
   if y = 2 then detwin = 0 else _
   if y = 0 then detwin = 1
  case 2
   if y = 0 then detwin = 0 else _
   if y = 1 then detwin = 1
 end select

end function



 


EDIT:

And, yes, I do realize that I don't have a loop in there, and that I have a bug just waiting to happen because I don't have a

Code:
select case lower$(pick)
 case else
 


line in there, but I was on the phone with my girlfriend, and also running out of time before I've got to go to sleep, etc.

But, any creative criticism will be greatly appreciated. =)
« Last Edit: Jan 28th, 2008, 12:13am by Isaiah » User IP Logged

I just got here, so work with me. =)
uncleBen
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1679
xx Re: Rock, Paper, Scissors game
« Reply #23 on: Jan 28th, 2008, 11:07am »

Hopefully this gets it right too:
Code:
    item$(1) = "Rock"
    item$(2) = "Scissors"
    item$(3) = "Paper"

    response$(0) = "Draw!"
    response$(1) = "You win!"
    response$(2) = "You lose!"
    nomainwin

    WindowWidth = 264
    WindowHeight = 215

    listbox #main.list, item$(, [UserSelect], 22, 46, 192, 50
    statictext #main, "Double-click to select", 26, 16, 176, 20
    statictext #main.result, "", 22, 111, 192, 40
    open "Rock Scissors Paper" for window as #main
    print #main, "trapclose [Quit]"

    wait

[Quit]
    close #main
    end

[UserSelect]
    #main.list, "selectionindex? humanSel"
    if humanSel = 0 then wait

    computerSel = int(rnd(1)*3) + 1
    result$ = "Computer selected: "; item$(computerSel); chr$(13)
    if computerSel < humanSel then computerSel = computerSel + 3
    result$ = result$ + response$(computerSel - humanSel)
    #main.result, result$
    wait
 
« Last Edit: Jan 28th, 2008, 11:14am by uncleBen » User IP Logged

Passing arrays to subroutines, functions that work with any types, quick string indexing and much more - JBExtensions.

Tired of Minesweeper? Try TomatoSweeper
Henry
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 757
xx Re: Rock, Paper, Scissors game
« Reply #24 on: Feb 3rd, 2008, 03:22am »

on Jan 28th, 2008, 12:03am, Isaiah wrote:
Hello!

This is my very first post in the Just BASIC forums, and also one of my first programs....

So, it's not nearly as good as the others, but I gave it my best shot. =)


Nice program Isaiah. Welcome to the JB conforums.
Hope we see and hear more from you. smiley
User IP Logged

Asmodeus
Junior Member
ImageImageImage


member is offline

Avatar




PM


Posts: 105
xx Re: Rock, Paper, Scissors game
« Reply #25 on: Apr 22nd, 2008, 08:22am »

I know the challenge is over, but I wanted to try writing the game anyways.

Code:
'Rock - Paper - Scissor Game
'       by Asmodeus

    [start]
    cls
    print "Please enter your choice"
    print
    print "[1] Rock"
    print "[2] Paper"
    print "[3] Scissor"
    print
    input "You Chose = "; choice
    if choice = 1 then print "Rock"
    if choice = 2 then print "Paper"
    if choice = 3 then print "Scissor"
    print

    print "The Computer Chose"
    computer = int(rnd(1)*3) + 1
    if computer = 1 then print "Rock"
    if computer = 2 then print "Paper"
    if computer = 3 then print "Scissor"

    if choice = 1 and computer = 1 then print "You Tied"
    if choice = 1 and computer = 2 then print "Computer Wins"
    if choice = 1 and computer = 3 then print "You Win"
    if choice = 2 and computer = 1 then print "You Win"
    if choice = 2 and computer = 2 then print "You Tied"
    if choice = 2 and computer = 3 then print "Computer Wins"
    if choice = 3 and computer = 1 then print "Computer Wins"
    if choice = 3 and computer = 2 then print "You Win"
    if choice = 3 and computer = 3 then print "You Tied"

    print
    input "Hit Enter to continue"; dummyvar$
    print
    input "Play again? 1-Yes / 2-No"; play
    if play = 1 then goto [start]
    if play = 2 then goto [quit]

    [quit]
    end

 


Asmodeus
User IP Logged

If at first you don't succeed; call it version 1.0
Welopez
Moderator
ImageImageImageImageImage


member is offline

Avatar

Never let your beliefs get in the way of learning.


PM

Gender: Male
Posts: 4407
xx Re: Rock, Paper, Scissors game
« Reply #26 on: Apr 22nd, 2008, 09:57am »

Very good, Asmodeus! Your program works correctly, and nothing else should be required. However....
Code:
'Rock - Paper - Scissor Game
'       by Asmodeus

    [start]
    cls
    print "Please enter your choice"
    print
    print "[1] Rock"
    print "[2] Paper"
    print "[3] Scissor"
    print
    input "Your selection? = "; choice
    SELECT CASE choice
        CASE 1
            PRINT "You chose ROCK."

        CASE 2
            PRINT "You chose PAPER."

        CASE 3
            PRINT "You chose SCISSORS."

        CASE ELSE
            PRINT "You goofed!"
            CALL pause
            GOTO [start]
        END SELECT

    print
    computer = int(rnd(1)*3) + 1
    SELECT CASE computer
        CASE 1
            PRINT "The computer chose ROCK."

        CASE 2
            PRINT "The computer chose PAPER."

        CASE 3
            PRINT "The computer chose SCISSORS."

        CASE ELSE
            PRINT "You goofed!"
            CALL pause
            GOTO [start]
        END SELECT

    SELECT CASE choice; computer
    CASE 1; 1 : PRINT "You tied."
    CASE 1; 2 : PRINT "Computer wins!"
    CASE 1; 3 : PRINT "You win!"
    CASE 2; 1 : PRINT "You win!"
    CASE 2; 2 : PRINT "You tied."
    CASE 2; 3 : PRINT "Computer wins!"
    CASE 3; 1 : PRINT "Computer wins!"
    CASE 3; 2 : PRINT "You win!"
    CASE 3; 3 : PRINT "You tied!"
    CASE ELSE : PRINT "You goofed!" : CALL pause : GOTO [start]
    END SELECT

    print
    input "Play again? (Y/N)"; reply$
    IF UPPER$(LEFT$(reply$,1))="Y" THEN [start]

    end

    SUB pause
        TIMER 2000, [resume]
        WAIT
        [resume]
        TIMER 0
    END SUB
 

When you write a program which has many possible IF/THEN choices (I once wrote one with 119 possibilities), a SELECT CASE statement is much more practical. The CASE comparisons can be on two or more lines, which I do for clarity, or can be multiple statement lines, such as in the winner determination at the end of the code.

Why do you need "Press enter to continue."? Why not simply ask the player if he wants to do again?

If the player does not respond with "Y", then go ahead and end. You don't have to check for a no answer.

A good program, and good practice. Now you can add SELECT CASE to your toolbox, and simplify your "Do again?" comparison.

User IP Logged

JB 1.01
Win7 64bit, 4 GB RAM, Pentium 6200@2.13 GHz (laptop)
WinXP, 1 GB RAM, Intel N270@1.6 GHz (netbook)
Asmodeus
Junior Member
ImageImageImage


member is offline

Avatar




PM


Posts: 105
xx Re: Rock, Paper, Scissors game
« Reply #27 on: Apr 22nd, 2008, 10:10am »

Hi Welopez,

Thanks for the advice on this... I am reviewing your changes.

-Asmodues
User IP Logged

If at first you don't succeed; call it version 1.0
tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3636
xx Re: Rock, Paper, Scissors game
« Reply #28 on: Apr 23rd, 2008, 05:20am »

I just wand my 2 cents. Or may be 3.
1)
then I see things like
Code:
print "Rock"
print "Paper"
print "Scissor
 

3 times in a row, I think it could be typed once, stored in array, and used from there. See for example uncleBen's post.

2) checking all possible outcomes - with
Code:
if <condition 1> and <condition 2> 
, nine lines, or select case with much the same nine lines
while sertainly works (and easy to understand),
beats me as un-sportsmanlike ;)
Ok, write it to my whim, never mind.

3) SELECT CASE in JBasic is somewhat unique - you can check two variables at once. (I already forgot seeing that in HELP file.) In MS Basics you are out of luck - so if you use it here you end up with even longer code. (so far for advantages oF SELECT CASE over several IFs.)

4) Welopez, 119 CASE's? What was it, really?
Being a lazy person I would definitely try to shorten it somehow.

Though I have an example that has just same approach - and I do not see way to improve. It's processor emulation - having huge SELECT CASE for opcodes. Each case should work on one opcode, like MOV A, B. Now, if I used very very interpreted language (where I could execute programs in a string (like FoxPro)), I would create table with codes (like SUBS) to execute on each opcode. This I'd have small program and big data file ;) (actually containing programs). Not sure if it really any better then having one big program - but then you can fix action for opcodes without recompiling program.
User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
Welopez
Moderator
ImageImageImageImageImage


member is offline

Avatar

Never let your beliefs get in the way of learning.


PM

Gender: Male
Posts: 4407
xx Re: Rock, Paper, Scissors game
« Reply #29 on: Apr 23rd, 2008, 07:39am »

The game was Smarticus! Actually, I mis-spoke, there were 114 SELECT CASE choices, the remaining 6 default to a win for the player or Smarticus.

I needed to make three comparisons for each line and had not yet learned to use the ";" between variables, so I used the STR$() value of 3 variables to check the status of the game board. It was great fun... just to see if I could do it.

The paper, rock, scissors could also be stored in a string, then parsed using WORD$(), instead of an array. Some beginners seem to grasp the use of WORD$() before they grasp arrays.
Code:
choice$="Paper Rock Scissors"
FOR k=1 TO 3
     PRINT WORD$(choice$,k)
NEXT k

END
 

Uncle Ben's DEMO is very clear and concise also. I just wanted to point out another method. I think it's useful to put more "tools" in the beginners tool box which can be used the next time he is beginning a project.
User IP Logged

JB 1.01
Win7 64bit, 4 GB RAM, Pentium 6200@2.13 GHz (laptop)
WinXP, 1 GB RAM, Intel N270@1.6 GHz (netbook)
Pages: 1 2 3  Notify Send Topic Print
« Previous Topic | Next Topic »

Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls