int playerScore = 0;
int computerScore = 0;
int buttonPins[] = {13, 12, 11};
int currentPlayer = 0; // 0 for player, 1 for computer
boolean gameStarted = false;
void setup() {
Serial.begin(9600);
for (int i = 0; i < 3; i++) {
pinMode(buttonPins[i], INPUT);
}
Serial.println("\n===============================");
Serial.println("Stacking Game");
Serial.println("Press any button to start.");
Serial.println();
}
void loop() {
if (!gameStarted) {
// Wait for any button press to start the game
if (readButton() != 0) {
gameStarted = true;
Serial.println("Game started!");
delay(500); // Debounce delay
}
return;
}
if (currentPlayer == 0) {
Serial.println("Player's turn:");
int buttonState = readButton();
if (buttonState >= 1 && buttonState <= 3) {
playerScore += buttonState;
Serial.println("Player chose: " + String(buttonState));
delay(500);
currentPlayer = 1; // Switch to computer's turn
}
} else {
Serial.println("Computer's turn:");
int computerChoice = random(1, 4);
computerScore += computerChoice;
Serial.println("Computer chose: " + String(computerChoice));
delay(500);
currentPlayer = 0; // Switch to player's turn
}
if (playerScore == 11) {
Serial.println("PLAYER WINS");
resetGame();
} else if (computerScore == 11) {
Serial.println("COMPUTER WINS");
resetGame();
}
}
int readButton() {
for (int i = 0; i < 3; i++) {
if (digitalRead(buttonPins[i]) == HIGH) {
return i + 1; // Return 1, 2, or 3 based on button pressed
}
}
return 0; // No button pressed
}
void resetGame() {
playerScore = 0;
computerScore = 0;
currentPlayer = 0;
gameStarted = false;
Serial.println("Game over. Press any button to play again.");
Serial.println();
while (readButton() == 0) {
// Wait for any button press to restart the game
}
delay(500); // Debounce delay
}