We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

- Prepare
- Algorithms
- Game Theory
- Game of Stones
- Discussions

# Game of Stones

# Game of Stones

#### Sort by

recency

#### |

#### 185 Discussions

#### |

Please Login in order to post a comment

Solution in

python 3.x:C# efficient 1-liner

my c++ solution:

This problem describes a game where two players take turns removing stones from a pile, and the goal is to determine the winner based on the initial number of stones. The solution involves analyzing the optimal moves for each player and determining if the first player (Player 1) or the second player (Player 2 will win.

The gameOfStones function needs to consider the different scenarios for each test case and output "First" or "Second" accordingly, based on the optimal moves of the players. The key is to analyze the patterns and conditions that lead to a win for either player.

Overall, it's a dynamic programming problem that involves understanding the winning strategies and implementing the logic to determine the winner for each test case.

In this game, two players, First and Second, take turns removing either 1, 2, or 3 stones from a starting pile of stones. The player who cannot make a move loses the game. Both players play optimally to win.

To determine the winner for each test case, we need to analyze the number of stones initially present:

If the starting number of stones (n) is divisible by 4, then the First player will always win. If n is not divisible by 4, then the Second player will win. Here's the Python function to implement this logic:

python Copy code def gameOfStones(n): if n % 4 == 0: return "Second" else: return "First"

## Input: Number of test cases

t = int(input()) results = []

## Input: Number of stones for each test case

for _ in range(t): stones = int(input()) result = gameOfStones(stones) results.append(result)

## Output the results

for result in results: print(result) You can use this code to calculate and print the winner for each test case based on the starting number of stones.