In JavaScript, how would you construct a game where two players take turns picking stones from a collection, as in the "Pick up stones" problem?
Question Analysis
The "Pick up stones" problem is a classic example of a turn-based game involving two players. The objective is to analyze how you would construct a simple game in JavaScript where two players alternately pick stones from a collection. The challenge is to design the game logic that manages the state of the game, enforces the rules, and determines the winner. Key considerations include implementing the turn-based mechanism, ensuring that the game state is updated correctly after each turn, and possibly handling edge cases or variations in the rules (such as the maximum number of stones a player can pick on a turn).
Answer
To construct a game in JavaScript where two players take turns picking stones, follow these steps:
-
Initialize the Game State:
- Define the total number of stones and variables to track the number of stones remaining.
- Set up a mechanism to track which player's turn it is.
-
Game Logic:
- Create a function to handle a player's move, which will:
- Ensure the move is valid (e.g., check the number of stones picked is within allowed limits).
- Subtract the picked stones from the total.
- Switch the turn to the other player.
- Check for the end of the game condition (e.g., no stones left).
- Create a function to handle a player's move, which will:
-
User Interaction:
- Implement a way for players to input their moves (e.g., through prompts or a simple UI).
-
Determine the Winner:
- Decide the winning condition, such as the player who picks the last stone or some other winning rule.
Here is a simple implementation in JavaScript:
class PickUpStonesGame {
constructor(totalStones, maxPick) {
this.totalStones = totalStones;
this.maxPick = maxPick;
this.currentPlayer = 1;
}
playTurn(pick) {
if (pick < 1 || pick > this.maxPick) {
console.log("Invalid move. Pick at least 1 and at most " + this.maxPick + " stones.");
return;
}
if (pick > this.totalStones) {
console.log("Not enough stones to pick.");
return;
}
this.totalStones -= pick;
console.log(`Player ${this.currentPlayer} picked ${pick} stones. ${this.totalStones} stones left.`);
if (this.totalStones === 0) {
console.log(`Player ${this.currentPlayer} wins!`);
return;
}
this.currentPlayer = this.currentPlayer === 1 ? 2 : 1;
}
}
const game = new PickUpStonesGame(10, 3);
// Example: Playing a turn
game.playTurn(2); // Player 1 picks 2 stones
game.playTurn(3); // Player 2 picks 3 stones
Explanation:
- The
PickUpStonesGame
class initializes the game with a total number of stones and a maximum number of stones a player can pick in one turn. - The
playTurn
function handles player moves, checks for valid moves, updates the game state, and checks for a win condition. - The game alternates turns between two players and provides feedback on each move.
This implementation serves as a basic framework, which can be expanded with UI elements or additional rules as required.