V.Vidhya Logo

V.Vidhya

Nim Game


Create a text-based strategy game called Nimm, where two players alternately take stones from a shared pile until there are no stones left, the last one to take a stone loses.

Instructions
  • The game starts with a pile of 20 stones between the players.
  • The two players alternate turns (Player 1 goes first).
  • On a given turn, a player may take either 1 or 2 stones from the pile.
  • The two players continue until the center pile has run out of stones.
  • The last player to take a stone loses.
Expected Output
Welcome to Nimm (take 1 or 2 stones, last to take loses)!

Stones remaining: 20
Player 1, how many stones will you take? (1 or 2): 2

Stones remaining: 18
Player 2, how many stones will you take? (1 or 2): 2

Stones remaining: 16
Player 1, how many stones will you take? (1 or 2): 1

Stones remaining: 15
Player 2, how many stones will you take? (1 or 2): 2

Stones remaining: 13
Player 1, how many stones will you take? (1 or 2): 2

Stones remaining: 11
Player 2, how many stones will you take? (1 or 2): 1

Stones remaining: 10
Player 1, how many stones will you take? (1 or 2): 2

Stones remaining: 8
Player 2, how many stones will you take? (1 or 2): -1
Invalid choice. You may take only 1 or 2 stones at a time. Please try again: 2

Stones remaining: 6
Player 1, how many stones will you take? (1 or 2): 7
Invalid choice. You may take only 1 or 2 stones at a time. Please try again: 2

Stones remaining: 4
Player 2, how many stones will you take? (1 or 2): 2

Stones remaining: 2
Player 1, how many stones will you take? (1 or 2): 1

Stones remaining: 1
Player 2, how many stones will you take? (1 or 2): 1

Player 2 took the last stone and loses!
Player 1 wins the game. Congratulations!


Feel free to implement additional features to enhance the gameplay.


Prev Post
Lucky Number Game
Next Post
Loan Calculator