Welcome to Dream.In.Code
Getting Help is Easy!

Join 86,257 Programmers. There are 2,047 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

Gaming Question

 
Reply to this topicStart new topic

Gaming Question, Question on gaming results

Boot_Up
post 11 Apr, 2008 - 02:02 PM
Post #1


New D.I.C Head

*
Joined: 4 Apr, 2008
Posts: 3



I'm to design a rock, paper, scissors game but I know there has to be a shorter way to display the results than to put each possible result in an If statement. I just need help in how to display the results in a simpler way or a way that Rock will always win over scissors, etc.

[Declare String player
Declare String comp
Declare String winner

Set rand = random (1, 3)

Display “Enter rock, paper or scissors.”
Input player
Display “You selected .” player

If random(1, 3) == 1 Then
Display “The computer chose rock."
If random(1, 3) == 2 Then
Display “The computer chose paper.”
If random(1, 3) == 3 Then
Display “The computer chose scissors.”

//This is where I'm having the issue because there are
//a lot of If's to input. Is there an easier way or do I
//just have to write out each if as an if statement?
If player == rock Then

Display “The winner is !”, winner

If player == comp Then
End If

//These are the winning determining factors and I was just going
//to write out each possible way for each to win.
//Rock smashes the scissors
//Scissors cuts paper
//Paper wraps rock
//If answer is the same, play again]
User is offlineProfile CardPM
Go to the top of the page
+Quote Post


girasquid
post 11 Apr, 2008 - 03:29 PM
Post #2


Barbarbar

Group Icon
Joined: 3 Oct, 2006
Posts: 953

Does your language support associative arrays?

If it does, try matching words to values. So array{rock} = 1, array{paper} = 2, etc.

Then just test something like this:
CODE

if answers{user_input_answer} > computer_answer then
// computer wins
else
// player wins
end if
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Boot_Up
post 11 Apr, 2008 - 05:21 PM
Post #3


New D.I.C Head

*
Joined: 4 Apr, 2008
Posts: 3

I understand what you're saying but how is that going to work if you have three values because you can't just put if one is larger than the other because there will be three values to consider.


QUOTE(girasquid @ 11 Apr, 2008 - 03:29 PM) *

Does your language support associative arrays?

If it does, try matching words to values. So array{rock} = 1, array{paper} = 2, etc.

Then just test something like this:
CODE

if answers{user_input_answer} > computer_answer then
// computer wins
else
// player wins
end if


User is offlineProfile CardPM
Go to the top of the page
+Quote Post

girasquid
post 11 Apr, 2008 - 08:28 PM
Post #4


Barbarbar

Group Icon
Joined: 3 Oct, 2006
Posts: 953

The two values you are comparing are the value inside the associative array and the computer's answer.

Rock always beats scissors, right? And scissors always beats paper? And paper beats rock. So something like this:
CODE

Rock = 3
Scissors = 2
Paper = 1
if((user_entered > computer_entered) && ((user_entered-2) != computer_entered) {}

Which would then fire whenever the user's entry won - which is the case of 3 vs. 2, 2 vs. 1, and 1 vs. 3 (which is the only case it doesn't work for). I leave the rest of the logic up to you.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 5/16/08 09:44AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month