• Post Reply Bookmark Topic Watch Topic
  • New Topic

Challenge: rock, paper, scissors  RSS feed

 
Alex Chun
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras, running is hovering in the cycle.
Here


 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Repeat until signs are equal? That is not how you play rock paper and scissors.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you everyone. I solved it.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:
but in Python.


But I do not understand Python code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!