programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Liutauras Vilda
• Knute Snortum
• Bear Bibeault
Sheriffs:
• Devaka Cooray
• Jeanne Boyarsky
• Junilu Lacar
Saloon Keepers:
• Ron McLeod
• Stephan van Hulst
• Tim Moores
• Carey Brown
• salvin francis
Bartenders:
• Tim Holloway
• Piet Souris
• Frits Walraven

Challenge: rock, paper, scissors

Ranch Hand
Posts: 41
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!

Alex Chun
Ranch Hand
Posts: 41
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:

Ranch Hand
Posts: 789

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.

Alex Chun
Ranch Hand
Posts: 41

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.

Marshal
Posts: 7493
509
So, please tell us, to which code snippet you're referring to to.
Tell us the lines, where do you think your problem appears.

Guillermo Ishi
Ranch Hand
Posts: 789

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.

Alex Chun
Ranch Hand
Posts: 41
Liutauras, running is hovering in the cycle.
Here

Liutauras Vilda
Marshal
Posts: 7493
509
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?

Marshal
Posts: 68095
258
Repeat until signs are equal? That is not how you play rock paper and scissors.

Liutauras Vilda
Marshal
Posts: 7493
509

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.

Guillermo Ishi
Ranch Hand
Posts: 789

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.

Liutauras Vilda
Marshal
Posts: 7493
509

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.

Guillermo Ishi
Ranch Hand
Posts: 789

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.

Alex Chun
Ranch Hand
Posts: 41

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.

Liutauras Vilda
Marshal
Posts: 7493
509

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.

Alex Chun
Ranch Hand
Posts: 41
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

Alex Chun
Ranch Hand
Posts: 41
Thank you everyone. I solved it.

Guillermo Ishi
Ranch Hand
Posts: 789
• 1

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

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

Alex Chun
Ranch Hand
Posts: 41

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.

Guillermo Ishi
Ranch Hand
Posts: 789

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.

Alex Chun
Ranch Hand
Posts: 41

Guillermo Ishi wrote:
but in Python.

But I do not understand Python code.

 Would anybody like some fudge? I made it an hour ago. And it goes well with a tiny ad ... Java file APIs (DOC, XLS, PDF, and many more) https://products.aspose.com/total/java