| New Reply |
program for a game similar to rock-paper-scissors |
Share Thread |
| Jan31-13, 01:36 PM | #1 |
|
|
program for a game similar to rock-paper-scissors
1. The problem statement, all variables and given/known data
I'm writing a program for a game similar to rock-paper-scissors. It is called proton-neutron-electron (P, N, E). There are two players. In the event of a tie, player 2 wins P1 denotes player 1, P2 denotes player 2, P = proton, N = neutron, E = electron. P1P means player 1 picks proton. There are 9 possibilities. Here they are: P1P + P2P = P2 wins P1P + P2N = P1 wins P1P + P2E = P1 wins P1N + P2P = P2 wins P1N + P2N = P2 wins P1N + P2E = P2 wins P1E + P2P = P1 wins P1E + P2N = P2 wins P1E + P2E = P2 wins 3. The attempt at a solution Here is what I have written in the compiler; it does not work. I am not looking for another solution, I would just like to know where I went wrong in my syntax. PHP Code:
|
| Jan31-13, 01:57 PM | #2 |
|
|
You could start by replacing all = with == and see what happens?
|
| Jan31-13, 02:13 PM | #3 |
|
|
That didn't work.
|
| Jan31-13, 02:14 PM | #4 |
|
|
program for a game similar to rock-paper-scissors
I managed to just do as such:
PHP Code:
|
| Jan31-13, 02:24 PM | #5 |
|
|
Can you post the changed code that you tried but didn't work?
|
| Jan31-13, 02:44 PM | #6 |
|
|
PHP Code:
PHP Code:
|
| Jan31-13, 03:05 PM | #7 |
|
Recognitions:
|
Note: '==' is a logical operator which you use, for example, in an 'if' statement to test if a certain condition is true:
if (user1 == 'P') {'some calculations'} If you wish to assign a certain value to a variable, then the following statement is used: { user1 = 'P'; } |
| Jan31-13, 03:16 PM | #8 |
|
|
Ah yes, that was where my issue was. Works now! Thanks! I will obviously just use the other one though because it is much shorter.
|
| Jan31-13, 03:19 PM | #9 |
|
Mentor
|
Your rule 5 violates what you said earlier about ties going to player 2. Can you summarize the rules any better than in the table? It looks like proton beats neutron (rules 2, 4) and electron beats neutron (rule 6) sometimes, but neutron beats electron sometimes (rule 8). Is that also a mistake? In rock paper scissors, there is a simple set of rules: rock covers (beats) paper, scissors cut (beat) paper, and paper covers (beats) rock. Is there a similar, simple rule here that you didn't mention? If so, it would make your logic much simpler. |
| Jan31-13, 03:22 PM | #10 |
|
|
Yeah, I noticed that as well. There doesn't seem to be any logicality with it, but that is how the table was given in the assignment. At that point I just made sure the algorithm obeyed the table.
PHP Code:
|
| Jan31-13, 03:24 PM | #11 |
|
Mentor
|
I redid the indentation in your code so that the lines didn't scroll so far to the right.
|
| Jan31-13, 03:30 PM | #12 |
|
Mentor
|
|
| Jan31-13, 03:35 PM | #13 |
|
|
Oops! I just edited that thanks.
|
| Jan31-13, 04:17 PM | #14 |
|
Mentor
|
Instead of one if block with a large condition, I would write three if blocks in which I check what player 1 has. I have implemented the first of these if blocks, and have added comments to suggest what the logic is.
Code:
if (user1 == 'P')
{
if (user2 == 'P')
{
cout << "Player 2 is the winner!";
}
else
{
cout << "Player 1 is the winner!";
}
}
else if (user1 == 'N')
{
// Player 2 wins only if player 1 has 'P'
}
else // user1 == 'E'
{
// Player 1 wins only if player 2 has 'N'
}
|
| New Reply |
Similar discussions for: program for a game similar to rock-paper-scissors
|
||||
| Thread | Forum | Replies | ||
| FTL via entanglement- paper discussing similar | Quantum Physics | 4 | ||
| log - rock - scissors | Earth | 26 | ||
| Rock Paper Scissors in Court! | General Discussion | 3 | ||
| rock, paper, scissors revisited | General Discussion | 645 | ||
| Rock, Paper, Scissors | General Discussion | 28 | ||