# Game of N stones where each player can remove 1, 3 or 4

Two players are playing a game with n stones where player 1 always plays first. The two players move in alternating turns and plays optimally. In a single move a player can remove either 1, 3 or 4 stones from the pile of stones. If a player is unable to make a move then that player loses the game. Given the number of stones where n is less than equal to 200, find and print the name of the winner.

Examples:

```Input : 4
Output : player 1

Input : 7
Output : player 2
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

To solve this problem, we need to find each possible value of n as a winning or losing position. Since above game is one of the impartial combinatorial games, therefore the characterization of losing and winning position is valid.

The characteristic properties of winning and losing states are:

• All terminal positions are losing positions.
• From every winning position, there is atleast one move to a losing position.
• From every losing position, every move is to a winning position.

If a player is able to make a move such that the next move is the losing state than the player is at winning state. Find the state of player 1, if player1 is in winning state then player 1 wins the game otherwise player 2 will win.

Consider the following base positions:

1. position 0 is the losing state, if the number of stones is 0 than the player1 will unable to make a move therefore player1 loses.
2. position 1 is the winning state, if the number of stones is 1 than the player1 will remove the stone and win the game.
3. position 2 is the losing state, if the number of stones is 2 than the player1 will remove 1 stone and then player2 will remove the second stone and win the game.
4. position 3 is the winning state, if the player is able to take a move such that the next move is the losing state than the player is at winning state, the palyer1 will remove all the 3 stones
5. position 4 is the winning state, if the player is able to take a move such that the next move is the losing state than the player is at winning state, the palyer1 will remove all the 4 stones
6. position 5 is the winning state, if the player is able to take a move such that the next move is the losing state than the player is at winning state, the palyer1 will remove 3 stones leaving 2 stones, which is the losing state
7. position 6 is the winning state, if the player is able to take a move such that the next move is the losing state than the player is at winning state, the palyer1 will remove 4 stones leaving 2 stones, which is the losing state
8. position 7 is the losing state, if the number of stones is 7 than the player1 can remove 1, 3 or 4 stones which all leads to the losing state, therefore player1 will lose.

Below is the implementation of above approach:

## CPP

 `// CPP program to find winner of ` `// the game of N stones ` `#include ` `using` `namespace` `std; ` ` `  `const` `int` `MAX = 200; ` ` `  `// finds the winning and losing ` `// states for the 200 stones. ` `void` `findStates(``int` `position[]) ` `{ ` `    ``// 0 means losing state ` `    ``// 1 means winning state ` `    ``position = 0; ` `    ``position = 1; ` `    ``position = 0; ` `    ``position = 1; ` `    ``position = 1; ` `    ``position = 1; ` `    ``position = 1; ` `    ``position = 0; ` ` `  `    ``// find states for other positions ` `    ``for` `(``int` `i = 8; i <= MAX; i++) { ` `        ``if` `(!position[i - 1] || !position[i - 3] ` `            ``|| !position[i - 4]) ` `            ``position[i] = 1; ` `        ``else` `            ``position[i] = 0; ` `    ``} ` `} ` ` `  `// driver function ` `int` `main() ` `{ ` `    ``int` `N = 100; ` `    ``int` `position[MAX] = { 0 }; ` ` `  `    ``findStates(position); ` ` `  `    ``if` `(position[N] == 1) ` `        ``cout << ``"Player 1"``; ` `    ``else` `        ``cout << ``"Player 2"``; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program for the variation ` `// in nim game ` `class` `GFG { ` `     `  `    ``static` `final` `int` `MAX = ``200``; ` `     `  `    ``// finds the winning and losing ` `    ``// states for the 200 stones. ` `    ``static` `void` `findStates(``int` `position[]) ` `    ``{ ` `         `  `        ``// 0 means losing state ` `        ``// 1 means winning state ` `        ``position[``0``] = ``0``; ` `        ``position[``1``] = ``1``; ` `        ``position[``2``] = ``0``; ` `        ``position[``3``] = ``1``; ` `        ``position[``4``] = ``1``; ` `        ``position[``5``] = ``1``; ` `        ``position[``6``] = ``1``; ` `        ``position[``7``] = ``0``; ` `     `  `        ``// find states for other positions ` `        ``for` `(``int` `i = ``8``; i < MAX; i++) { ` `             `  `            ``if` `(position[i - ``1``]!=``1` `||  ` `                    ``position[i - ``3``]!=``1` `                  ``|| position[i - ``4``]!=``1``) ` `                ``position[i] = ``1``; ` `            ``else` `                ``position[i] = ``0``; ` `        ``} ` `    ``} ` `     `  `    ``//Driver code ` `    ``public` `static` `void` `main (String[] args) ` `    ``{ ` `         `  `        ``int` `N = ``100``; ` `        ``int` `position[]=``new` `int``[MAX]; ` `     `  `        ``findStates(position); ` `     `  `        ``if` `(position[N] == ``1``) ` `            ``System.out.print(``"Player 1"``); ` `        ``else` `            ``System.out.print(``"Player 2"``); ` `    ``} ` `} ` ` `  `// This code is contributed by Anant Agarwal. `

## Python3

 `# Python3 program to find winner of ` `# the game of N stones ` ` `  `MAX` `=` `200` ` `  `# finds the winning and losing ` `# states for the 200 stones. ` `def` `findStates(position): ` ` `  `    ``# 0 means losing state ` `    ``# 1 means winning state ` `    ``position[``0``] ``=` `0``; ` `    ``position[``1``] ``=` `1``; ` `    ``position[``2``] ``=` `0``; ` `    ``position[``3``] ``=` `1``; ` `    ``position[``4``] ``=` `1``; ` `    ``position[``5``] ``=` `1``; ` `    ``position[``6``] ``=` `1``; ` `    ``position[``7``] ``=` `0` ` `  `    ``# find states for other positions ` `    ``for` `i ``in` `range``(``8``,``MAX``+``1``): ` `        ``if` `not``(position[i ``-` `1``]) ``or` `not``(position[i ``-` `3``]) ``or` `not``(position[i ``-` `4``]): ` `            ``position[i] ``=` `1``; ` `        ``else``: ` `            ``position[i] ``=` `0``; ` `     `  `#driver function ` `N ``=` `100` `position ``=` `[``0``] ``*` `(``MAX``+``1``) ` ` `  `findStates(position) ` ` `  `if` `(position[N] ``=``=` `1``): ` `        ``print``(``"Player 1"``) ` `else``: ` `        ``print``(``"Player 2"``) ` ` `  ` `  `# This code is contributed by ` `# Smitha Dinesh Semwal `

## C#

 `// C# program for the variation ` `// in nim game ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``static` `int` `MAX = 200; ` `     `  `    ``// finds the winning and losing ` `    ``// states for the 200 stones. ` `    ``static` `void` `findStates(``int` `[]position) ` `    ``{ ` `         `  `        ``// 0 means losing state ` `        ``// 1 means winning state ` `        ``position = 0; ` `        ``position = 1; ` `        ``position = 0; ` `        ``position = 1; ` `        ``position = 1; ` `        ``position = 1; ` `        ``position = 1; ` `        ``position = 0; ` `     `  `        ``// find states for other positions ` `        ``for` `(``int` `i = 8; i < MAX; i++) ` `        ``{ ` `            ``if` `(position[i - 1] != 1 ` `                  ``|| position[i - 3] != 1 ` `                   ``|| position[i - 4]!=1) ` `                ``position[i] = 1; ` `            ``else` `                ``position[i] = 0; ` `        ``} ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main () ` `    ``{ ` `        ``int` `N = 100; ` `        ``int` `[]position = ``new` `int``[MAX]; ` `     `  `        ``findStates(position); ` `     `  `        ``if` `(position[N] == 1) ` `            ``Console.WriteLine(``"Player 1"``); ` `        ``else` `            ``Console.WriteLine(``"Player 2"``); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 ` `

Output:

```Player 2
```

This article is contributed by ARSHPREET_SINGH. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

## Recommended Posts:

Improved By : vt_m, jit_t

Article Tags :
Practice Tags :

3

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.