Forums Register Login

Challenge: rock, paper, scissors

+Pie Number of slices to send: Send
Hi everyone!
I need develop the algorithm of popular game.
Tournament "rock, paper, scissors"
Develop a program in any language, simulating the tournament playoffs, "rock, paper, scissors" among 2 players ^ K where K - the number of rounds set by the user.
● Before the tournament, each player is assigned a number from 1 to 2 ^ K and random tactics with equal probability, "scissors", "stone" or "paper", where the player with the tactics, such as "scissors" shows scissors with probability - 0.5 stone - 0.25, paper - 0.25. In other tactics similar to the probability distribution.
● In each round matches take place between the players before the two victories.
● The winner of the match is determined by the following rules:
○ Stone defeats scissors ("stone blunts or breaks scissors")
○ Scissors defeat Paper (the "scissors cut paper")
○ Paper beats rock ("Paper covers rock")
● If the players show the same sign, then the game is replayed.
● Results rounds are displayed.

Example:
Input data:
3

Output:
Player1: scissors
Player2: paper
Player3: paper
Player4 stone
Player5: scissors
Player6: paper
Player7: paper
Player8 stone

Round 1:
Player1 vs Player2 - Player1 won!
Player3 vs Player4 - won Player4!
Player5 vs Player6 - won Player5!
Player7 vs Player8 - won Player8!

Round 2:
Player1 vs Player4 - Player1 won!
Player5 vs Player8 - won Player5!

Final:
Player1 vs Player5 - won Player5!
+Pie Number of slices to send: Send
I decided to make three classes: Player, Game and GameTest.
In Player class I describe such fields as "name", "tactics" and method "showSign" to determine what sign player will show.
In Game class I describe two methods "wonInRound"(here determines winning player of two) and method "go" (here the main algorith of game, here is rounds until appers winner of game)
GameTest is the main class.

My issue: I can't solve draw problem when both players have the same signs. My program is hovering in the cycle


Below you can see Game class:
+Pie Number of slices to send: Send
 

Alex Chun wrote:
My issue: I can't solve draw problem when both players have the same signs. My program is hovering in the cycle



Without trying to run the code...Can't you just in a test for both being equal (a draw)? It might be easiest to add the test at the top so you don't need to change the original code that follows. You have three possible outcomes instead of your current two -- player 1 wins, 2 wins, or equal and nobody wins.
+Pie Number of slices to send: Send
 

Guillermo Ishi wrote:
You have three possible outcomes instead of your current two -- player 1 wins, 2 wins, or equal and nobody wins.



Guillermo, No, every round we must play like play off, If we have draw then we must replay until somebody wins.
+Pie Number of slices to send: Send
So, please tell us, to which code snippet you're referring to to.
Tell us the lines, where do you think your problem appears.
+Pie Number of slices to send: Send
 

Alex Chun wrote:every round we must play like play off, If we have draw then we must replay until somebody wins.




in that case something like this will do --



Actually I like this better because the other will eventually run out of stack.

+Pie Number of slices to send: Send
Liutauras, running is hovering in the cycle.
Here


+Pie Number of slices to send: Send
That code snippet would look nicer written with "do-while" loop, something like this, because you wouldn't need to repeat lines 2 and 3 before loop starts:

Alex Chun wrote:My issue: I can't solve draw problem when both players have the same signs. My program is hovering in the cycle

But this is what your code it says to do: repeat, until signs are equal.
And how do you want it to be?
+Pie Number of slices to send: Send
Repeat until signs are equal? That is not how you play rock paper and scissors.
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:Repeat until signs are equal? That is not how you play rock paper and scissors.

I am not that familiar with it, but if it is similar to the way is done in my country, you need to replay if signs are equal and keep doing it until while they are equal.

Agh, I expressed myself wrongly, you're right, sorry for my english.
+Pie Number of slices to send: Send
 

Alex Chun wrote:Liutauras, running is hovering in the cycle.
Here




You keep the loop going as long as there's a tie, and you exit it when there's no tie. That makes sense and is ok.

If I read what you're saying, your code stays in that loop. That means sign1 and sign2 are never equal. Breakpoint the code there or print their values and see what they actually are when they should be equal, and take it from there.

You haven't included the showSign() function which assigns the values to those two, so we can't see if it's broken. If that's not your problem it's at least A problem.

+Pie Number of slices to send: Send
 

Guillermo Ishi wrote:If I read what you're saying, your code stays in that loop. That means sign1 and sign2 are never equal.

That means opposite.

And please show us Alex, where and how you create instances "one" and "two" of Player class.
+Pie Number of slices to send: Send
 

Liutauras Vilda wrote:

Guillermo Ishi wrote:If I read what you're saying, your code stays in that loop. That means sign1 and sign2 are never equal.

That means opposite.

And please show us Alex, where and how you create instances "one" and "two" of Player class.



the way I read the while loop, while there's a tie keep playing until there's no tie. That's what you want.
+Pie Number of slices to send: Send
 

Liutauras Vilda wrote:

And please show us Alex, where and how you create instances "one" and "two" of Player class.



Have a look, please, in the method "go()". There are I create an array of players and they play each other.
+Pie Number of slices to send: Send
 

Alex Chun wrote:My issue: I can't solve draw problem when both players have the same signs. My program is hovering in the cycle


Ok, once again, your problem is, that this bit of code loops forever, when both players has the same sign:
But this is whats suppose to be. You didn't answer, how do you want it to behave?
Entire loop looks good, and should exit when signs becomes different. Please clarify if you want someone to help you.
+Pie Number of slices to send: Send
Output:



In this case is ok anywhere
But if we run else and if we'll see draw
Output:


And program is hovering.
I need that no cycle and players played else play off until somebody wins.

PS. камень - rock
бумага - paper
ножницы - scissors
игрок1 - player1
+Pie Number of slices to send: Send
Thank you everyone. I solved it.
1
+Pie Number of slices to send: Send
 

Alex Chun wrote:Thank you everyone. I solved it.


You can enlarge it into rock, paper, scissors, lizard, Spock...
+Pie Number of slices to send: Send
 

Guillermo Ishi wrote:

Alex Chun wrote:Thank you everyone. I solved it.


You can enlarge it into rock, paper, scissors, lizard, Spock...



Ok. I'll try it.
My problem was class Player. I mistaken in method "showSign()". There were 2 mistakes.
That is a challenge like offline exams to the university.
+Pie Number of slices to send: Send
 

Alex Chun wrote:
That is a challenge like offline exams to the university.


I took a class where we did the lizard, Spock version but in Python. There's a less obvious algorithm to find the winner besides the obvious way, but I can't remember what it is. Maybe you can look it up if you're interested.
+Pie Number of slices to send: Send
 

Guillermo Ishi wrote:
but in Python.



But I do not understand Python code.
Wink, wink, nudge, nudge, say no more, it's a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1601 times.
Similar Threads
Can someone please be bothered to try this, thanks Rock paper scissors
Deck of cards
While getting Input from User check a second condition
Using Methods
Question on arraylists
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 17:44:29.