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

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:**

**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.**position 1**is the winning state, if the number of stones is 1 than the player1 will remove the stone and win the game.**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.**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**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**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**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**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 <bits/stdc++.h> ` `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] = 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] || !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; ` `} ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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] = 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 () ` ` ` `{ ` ` ` `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. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find winner of ` `// the game of N stones ` `$MAX` `= 200; ` ` ` `// finds the winning and losing ` `// states for the 200 stones. ` `function` `findStates(` `$position` `) ` `{ ` ` ` `global` `$MAX` `; ` ` ` `// 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` `= 8; ` `$i` `<= ` `$MAX` `; ` `$i` `++) { ` ` ` `if` `(!` `$position` `[` `$i` `- 1] || !` `$position` `[` `$i` `- 3] ` ` ` `|| !` `$position` `[` `$i` `- 4]) ` ` ` `$position` `[` `$i` `] = 1; ` ` ` `else` ` ` `$position` `[` `$i` `] = 0; ` ` ` `} ` `} ` ` ` `// driver function ` ` ` ` ` `$N` `= 100; ` ` ` `$position` `[` `$MAX` `] = ` `array` `(0); ` ` ` ` ` `findStates(` `$position` `); ` ` ` ` ` `if` `(` `$position` `== 1) ` ` ` `echo` `"Player 1"` `; ` ` ` `else` ` ` `echo` `"Player 2"` `; ` ` ` ` ` `#This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**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.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

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.

## Recommended Posts:

- Two player game in which a player can remove all occurrences of a number
- Find the winner of a game of removing any number of stones from the least indexed non-empty pile from given N piles
- Game Theory (Normal-form game) | Set 3 (Game with Mixed Strategy)
- Game Theory (Normal-form Game) | Set 6 (Graphical Method [2 X N] Game)
- Game Theory (Normal-form Game) | Set 7 (Graphical Method [M X 2] Game)
- Find the player who will win the Coin game
- Largest odd divisor Game to check which player wins
- Find the player who wins the game by removing the last of given N cards
- Find the player who is the last to remove any character from the beginning of a Binary String
- Combinatorial Game Theory | Set 2 (Game of Nim)
- Game Theory (Normal - form game) | Set 1 (Introduction)
- Game Theory (Normal-form Game) | Set 4 (Dominance Property-Pure Strategy)
- Game Theory (Normal-form Game) | Set 5 (Dominance Property-Mixed Strategy)
- Find probability that a player wins when probabilities of hitting the target are given
- Minimum score possible for a player by selecting one or two consecutive array elements from given binary array
- Predict the winner of a card game of removing K cards in each turn such that Bitwise AND of K and size of pile is 0
- Count the numbers that can be reduced to zero or less in a game
- Optimal Strategy for a Game | DP-31
- Combinatorial Game Theory | Set 1 (Introduction)
- Combinatorial Game Theory | Set 4 (Sprague - Grundy Theorem)