Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Rock Paper Scissors Compiler Error.
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
barney




PostPosted: Tue May 16, 2006 10:18 pm   Post subject: Rock Paper Scissors Compiler Error.

Can anyone tell me whats wrong with my code? Cause it won't run.... :S
It's supposed to be just a simple rock-paper-scissors game.
code:

var p1 : char
var p2 : char
var exit_char : string (1)
var p : char
var P : char

put "Note: The 'ROCK-PAPER-SCISSORS' game is meant for two (2) players. \nPress any key to continue. \n\n"
    getch (exit_char)

put "Player 1, enter 'p' for paper, 'r' for rock, or 's' for scissors: "..
get p1
put "Player 2, enter 'p' for paper, 'r' for rock, or 's' for scissors: "..
get p2

if ((p1 = "p" or "P") and (p2 = "p" or "P")) then
    put "\nTie game, nobody wins."
elsif ((p1 = "s" or "S") and (p2 = "s" or "S")) then
    put "\nTie game, nobody wins."
elsif ((p1 = "r" or "R") and (p2 = "r" or "R")) then
    put "\nTie game, nobody wins."

elsif ((p1 = "p" or "P") and (p2 = "r" or "R")) then
     put "\nPlayer 1 wins, paper covers rock."
elsif ((p1 = "r" or "R") and (p2 = "s" or "S")) then
    put "\nPlayer 1 wins, rock breaks scissors."
elsif ((p1 = "s" or "S") and (p2 = "p" or "P")) then
    put "\nPlayer 1 wins, scissor cuts paper."

elsif ((p1 = "p" or "P") and (p2 = "s" or "S")) then
     put "\nPlayer 2 wins, scissor cuts paper."
elsif ((p1 = "r" or "R") and (p2 = "p" or "P")) then
    put "\nPlayer 2 wins, paper covers rock."
elsif ((p1 = "s" or "S") and (p2 = "r" or "R")) then
    put "\nPlayer 2 wins, rock breaks scissors."

else
     put "\nInvalid choice(s). Remember to enter only one letter; 'p', 'r', or 's'."
end if
Sponsor
Sponsor
Sponsor
sponsor
NikG




PostPosted: Tue May 16, 2006 10:42 pm   Post subject: (No subject)

You should probably read the rules when posting in a new forum if you want any help. If you read the rules, you'd know that you should not post topic titles such as HELP NEEDED, because people tend to ignore them. Second, you should have used the code /code tags to post your code.

Anyways, the problem is that Turing (and most languages I believe) is not smart enough to figure out
code:
p1 = "p" or "P"

Instead, you have to say:
code:
p1 = "p" or p1= "P"
NikG




PostPosted: Tue May 16, 2006 10:45 pm   Post subject: (No subject)

And another thing... 2 player rock paper scissors???
Doesn't player 2 have a major advantage?

Why don't you try a computer player.
Here's a hint as to how to do it:
code:
Rand.Int(1,3)
Clayton




PostPosted: Tue May 16, 2006 11:03 pm   Post subject: (No subject)

whats up with the brakets around your if conditions??? (is this just something that ive never seen b4 and actually works) that is probably a big problem too in case you havent figured that out yet
NikG




PostPosted: Tue May 16, 2006 11:16 pm   Post subject: (No subject)

SuperFreak82 wrote:
whats up with the brakets around your if conditions??? (is this just something that ive never seen b4 and actually works) that is probably a big problem too in case you havent figured that out yet

Brackets are allowed.
And perhaps he put it in brackets because some languages require it, I believe (I'm thinking of javascript...)
barney




PostPosted: Wed May 17, 2006 4:17 pm   Post subject: (No subject)

NikG wrote:
And another thing... 2 player rock paper scissors???
Doesn't player 2 have a major advantage?

Why don't you try a computer player.
Here's a hint as to how to do it:
code:
Rand.Int(1,3)


its a class assignment, we have to make it for two players. but thanks! ill try what you said. Smile
barney




PostPosted: Wed May 17, 2006 4:24 pm   Post subject: (No subject)

OK, i did what NikG said...but now all that comes after I type in the choices, is "Tie game, nobody wins." Anymore help? And I changed the "P", "R", and "S" to constants, so this is what the code looks like now:
code:
const p := "p"
const P := "P"
const r := "r"
const R := "R"
const s := "s"
const S := "S"
var p1 : char
var p2 : char
var exit_char : string (1)

color (103)
put "\t\t\tROCK-PAPER-SCISSORS"
put "\nNote: The 'ROCK-PAPER-SCISSORS' game is meant for two (2) players. \nPress any key to continue. \n\n"
getch (exit_char)

put "Player 1, enter 'p' for paper, 'r' for rock, or 's' for scissors: " ..
get p1
put "Player 2, enter 'p' for paper, 'r' for rock, or 's' for scissors: " ..
get p2

if ((p1 = p) or (p1 = P) and (p2 = p) or (p2 = P)) then
    put "\nTie game, nobody wins."
elsif ((p1 = s) or (p1 = S) and (p2 = s) or (p2 = S)) then
    put "\nTie game, nobody wins."
elsif ((p1 = r) or (p1 = R) and (p2 = r) or (p2 = R)) then
    put "\nTie game, nobody wins."

elsif ((p1 = p) or (p1 = P) and (p2 = r) or (p2 = R)) then
    put "\nPlayer 1 wins, paper covers rock."
elsif ((p1 = r) or (p1 = R) and (p2 = s) or (p2 = S)) then
    put "\nPlayer 1 wins, rock breaks scissors."
elsif ((p1 = s) or (p1 = S) and (p2 = p) or (p2 = P)) then
    put "\nPlayer 1 wins, scissor cuts paper."
   
elsif ((p1 = p) or (p1 = P) and (p2 = s) or (p2 = S)) then
    put "\nPlayer 2 wins, scissor cuts paper."
elsif ((p1 = r) or (p1 = R) and (p2 = p) or (p2 = P)) then
    put "\nPlayer 2 wins, paper covers rock."
elsif ((p1 = s) or (p1 = S) and (p2 = r) or (p2 = R)) then
    put "\nPlayer 2 wins, rock breaks scissors."
else
    put "\nInvalid choice(s)."
end if
TheOneTrueGod




PostPosted: Wed May 17, 2006 5:08 pm   Post subject: (No subject)

One thing you need to keep in mind is order of operations. Yes, boolean algebra functions similar to regular math. Brackets are checked first, then (i believe) "and"s are checked, then "or"s.

So, the code

code:

elsif ((p1 = s) or (p1 = S) and (p2 = s) or (p2 = S)) then


will first check the brackets (nothing noteworthy there.)
then, it will check the "and"s. This is where you have made a mistake above.

Using bracket logic:
code:
(p1 = s or (p1 = S and p2 = s) or p2 = S)


if you look through your code, you have several of these, so it will be a tie game WHENEVER
p1 = s
p1 = r
p1 = p

So if player one doesn't type in upper case, or if player two doesn't type in lower case, it is ALLWAYS going to be a tie game.

Use brackets effectively to cure this problem.
Sponsor
Sponsor
Sponsor
sponsor
MysticVegeta




PostPosted: Wed May 17, 2006 6:08 pm   Post subject: (No subject)

Um may I make a suggestion? For tie games, why cant you just go
Pseudo:
if Str.Upper(p1) = Str.Upper(p2) then
          put "Tie"
end if
barney




PostPosted: Wed May 17, 2006 8:35 pm   Post subject: (No subject)

TheOneTrueGod wrote:
...Use brackets effectively to cure this problem.


OK, I'm not sure if I totally understood you. Is this what you mean?

code:
if (p1 = p or (p1 = P and p2 = p) or p2 = P) then
    put "\nTie game, nobody wins."
elsif (p1 = s or (p1 = S and p2 = s) or p2 = S) then
    put "\nTie game, nobody wins."
elsif (p1 = r or (p1 = R and p2 = r) or p2 = R) then
    put "\nTie game, nobody wins."

elsif (p1 = p or (p1 = P and p2 = r) or p2 = R) then
    put "\nPlayer 1 wins, paper covers rock."
elsif (p1 = r or (p1 = R and p2 = s) or p2 = S) then
    put "\nPlayer 1 wins, rock breaks scissors."
elsif (p1 = s or (p1 = S and p2 = p) or p2 = P) then
    put "\nPlayer 1 wins, scissor cuts paper."
   
elsif (p1 = p or (p1 = P and p2 = s) or p2 = S) then
    put "\nPlayer 2 wins, scissor cuts paper."
elsif (p1 = r or (p1 = R and p2 = p) or p2 = P) then
    put "\nPlayer 2 wins, paper covers rock."
elsif (p1 = s or (p1 = S and p2 = r) or p2 = R) then
    put "\nPlayer 2 wins, rock breaks scissors."
else
    put "\nInvalid choice(s)."
end if
TheOneTrueGod




PostPosted: Wed May 17, 2006 9:03 pm   Post subject: (No subject)

lol, no, I didn't give you the answer to the problem I gave you the problem itself. Read over my post carefully, and make sure you understand it. What you have done is, effectively, nothing. Since the "and" operator functions before the "or' operator, those brackets don't do anything the way you've placed them.

I'm tired of just giving out answers on the help. You shouldn't be posting here expecting other people to just give you the answer. What SHOULD happen, is that you should be given a clue as to what you did wrong, so you can think about it, and learn from your mistakes.

If all anyone ever did was just tell other people what to do, then no one would ever learn anything.
barney




PostPosted: Wed May 17, 2006 9:08 pm   Post subject: (No subject)

lol ohhh. haha, i think i get it....a bit. ok let me try it out. thanks! Very Happy
TheOneTrueGod




PostPosted: Wed May 17, 2006 9:19 pm   Post subject: (No subject)

No problem, sorry for the minor rant there. Post any success/failures Razz and good luck.
barney




PostPosted: Wed May 17, 2006 9:20 pm   Post subject: (No subject)

You know, for a while there, I though I totally got it. So I went and changed the coding....

code:
if ((p1 = p or p1 = P) and (p2 = p or p2 = P)) then
    put "\nTie game, nobody wins."
elsif ((p1 = s or p1 = S) and (p2 = s or p2 = S)) then
    put "\nTie game, nobody wins."
elsif ((p1 = r or p1 = R) and (p2 = r or p2 = R)) then
    put "\nTie game, nobody wins."

elsif ((p1 = p and p2 = r) or (p1 = P and p2 = R)) then
    put "\nPlayer 1 wins, paper covers rock."
elsif ((p1 = r and p2 = s) or (p1 = R and p2 = S)) then
    put "\nPlayer 1 wins, rock breaks scissors."
elsif ((p1 = s and p2 = p) or (p1 = S and p2 = P)) then
    put "\nPlayer 1 wins, scissor cuts paper."
   
elsif ((p1 = p and p2 = P) or (p1 = s and p2 = S)) then
    put "\nPlayer 2 wins, scissor cuts paper."
elsif ((p1 = r and p2 = R) or (p1 = p and p2 = P)) then
    put "\nPlayer 2 wins, paper covers rock."
elsif ((p1 = s and p2 = S) or (p1 = r and p2 = R)) then
    put "\nPlayer 2 wins, rock breaks scissors."
else
    put "\nInvalid choice(s)."
end if


and it turns out, it still doesn't work. =| I ran it and typed in "p" then "r", and it gave me "Invalid choice(s)."
TheOneTrueGod




PostPosted: Wed May 17, 2006 9:36 pm   Post subject: (No subject)

A'ight, i'll teach you some basic debugging techniques:

#1) put

If you try outputting values, you can see what they contain, and then you can determine whats wrong with it. In your case, I tried outputting p1, then p2. It turns out that, since you used chars, p1 held the first value you entered, and p2 held no value. I'm not sure why, but I believe its because of the "enter" key. I think p2 held the key "enter", but i'm not entirely sure why. When I replaced them with the variable type string(1), it worked fine (though you'll get an error if its a string greater than length of 1.

#2) Tracing through your code in your head.

Go through your code in your head, figure out what each line does, and why it does that.
code:

elsif ((p1 = p and p2 = P) or (p1 = s and p2 = S)) then

take a look at that line... Go through it logistically, and you should be able to figure out why it is wrong... Then apply that logic to all lines that are below the tie game line.

Finally, a couple hints. Its good that you used constants, but using them here actually makes this harder for you. As mysticVegeta said, why not just use Str.Upper? This will convert a string (or character in your case) to the upper case version of it (or if its upper case allready, itll leave it alone). Using that logic, it is quite easy to condense your if statements into roughly 7 lines (plus intermediate code), and less if you do it very efficiently (but I wouldn't suggest you go for the less lines just yet.)
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 19 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: