Hey there! Today we'll be building a simple Game: Rock-Paper-Scissor in C++ programming language. You can check it out on Google .
But today we are going to build it to play with computer/bot. Are you excited? So let's go...
- LOGIC:
The logic for this game is very simple. First we'll make a void function which will execute the logic of the program and then we'll call it in our main function. In logic function, we'll include choices for user, score, number of moves and apply a while loop which will check the choices made by user and bot and increase the score accordingly. When the while loop finish we'll print the winner of the game. Now let's move on to the algorithm behind it.
- ALGORITHM:
- Start
- Make a void logic function
- In the function, declare variables for choices r = rock, p = paper and s = scissor and declare int variable for score initializing with 0.
- Also include a counter variable which will count the number of moves for user and let's initialize it with 6.
- And take one input from user and let's the first choice of the bot be 's'.
- Now apply while loop and input the condition ( comp and player score <5 & counter >0).
- In the loop first print the initial scores and then take input from user.
- Now apply if-else statements to check who wins and in it increase the score accordingly. Also decrease the number of moves for user.
- When the while loop completes, print the result.
- After coding the above logic function, call the function in main function.
- End
- CODE:
- OUTPUT:
Rock-Paper-Scissor
---------------------
Choose r:rock, p:paper, s:scissor
Score : Bot : 0 | Player : 0 | Moves Left : 6
Enter Choice : p
Bot Wins
Score : Bot : 1 | Player : 0 | Moves Left : 5
Enter Choice : r
Bot Wins
Score : Bot : 2 | Player : 0 | Moves Left : 4
Enter Choice : s
Bot Wins
Score : Bot : 3 | Player : 0 | Moves Left : 3
Enter Choice : p
Bot Wins
Score : Bot : 4 | Player : 0 | Moves Left : 2
Enter Choice : s
Player Wins
Score : Bot : 4 | Player : 1 | Moves Left : 1
Enter Choice : r
Draw
Score : Bot : 4 | Player : 1 | Moves Left : 0
You Ran Out Of Moves
Bot Is Winner, Try Again
That's all for today. I hope you enjoyed. Still if you've any problem then don't hesitate to ask.
If you enjoyed then don't forget to
Share, Subscribe and keep coding...
Thanks.
Comments
Post a Comment