/*
* Like all Arduino code - copied from somewhere else :)
* So don't claim it as your own
* Based on the code from -> https://www.instructables.com/Connect-4-Game-Using-Arduino-and-Neopixel/
* ---------------------------------
Modified version - 24/12 by skuydi
* adjusted matrix size from 8x8 to 10x10
* add a debounce for the buttons
* add a timer to make the game faster
* add a random pawn appearance on the first line
* add a start button and a timer
* add a switch to control the demo mode
* add random color during the demo mode
*/
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 6 //arduino pin
#define NUMPIXELS 100 //# of pixels
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
//button switches
#define LeftButton 4 //data pin for left button
#define DownButton 7 //data pin for down button
#define RightButton 5 //data pin for right button
#define DemoButton 10 //data pin for demo button
#define StartButton 8 //data pin for start button
int board[NUMPIXELS]; /* RGB Matrix Grid */
/*
00 01 02 ... 09
10 11 12 ... 19
...
90 91 92 ... 99
*/
//RGB LED values for player 1 (index 0) and player 2 (index 1)
int r[2];
int g[2];
int b[2];
boolean demoMode = false; //run in demo mode until reset
int whoGoesFirst = 1; // Player to go first.
int elapsingTime = 13000; // Elapsing time to play
int startTimer = 2000;
bool isDemoMode;
bool buttonState = false; // L'état du bouton (pressé ou non)
bool lastButtonState = false; // Dernier état du bouton pour détecter les changements
unsigned long lastPressTime = 0; // Moment du dernier appui sur le bouton
unsigned long timerStart = 0; // Moment du début du timer de 2 secondes
bool waitingForStart = false; // Si le bouton est pressé, démarrage du timer
void setup() {
//button switches - use "PULLUP" to set open/unpressed to HIGH
pinMode(LeftButton, INPUT_PULLUP);
pinMode(DownButton, INPUT_PULLUP);
pinMode(RightButton, INPUT_PULLUP);
pinMode(DemoButton, INPUT_PULLUP);
pinMode(StartButton, INPUT_PULLUP);
pixels.begin(); // This initializes the NeoPixel library.
//pixels.setBrightness(100); // 1/4 power max 255 (10 at dimm/night)
pixels.show(); // Initialize all pixels to 'off'
Serial.begin(11600); // open the serial port at 11600 bps:
randomSeed(analogRead(0));
//press any button during start up to enter demo mode
if (isButtonPressed() > 0) {
demoMode = true;
}
}
void loop() {
// Lire l'état du bouton
buttonState = digitalRead(StartButton) == LOW; // Le bouton est activé quand il est relié à GND (LOW)
bool demoMode = digitalRead(DemoButton); // Demo mode on/off
if (demoMode) { //run game demo
startDemo();
} else {
// Si le bouton a été pressé (changement d'état de "non pressé" à "pressé")
if (buttonState && !lastButtonState) {
lastPressTime = millis(); // Enregistrer le moment où le bouton a été pressé
waitingForStart = true; // On commence à attendre la durée du timer
timerStart = millis(); // Début du timer
delay(300); // Anti-rebond pour éviter les multiples détections du même a
}
lastButtonState = buttonState; // Sauvegarde l'état actuel du bouton
// Si on attend le timer de 2 secondes après le bouton pressé
if (waitingForStart) {
unsigned long currentTime = millis();
if (currentTime - timerStart >= startTimer) { // Si 2 secondes se sont écoulées
waitingForStart = false; // Arrêter le timer
twoPlayerGame(); //two player game
delay(300); // Anti-rebond pour éviter les multiples détections du même appui
}
}
}
}
void twoPlayerGame () {
pixels.setBrightness(200); // 1/4 power max 255 (10 at dimm/night)
r[0] = 255; // Exemple couleur fixe pour le joueur 1 (Rouge)
g[0] = 0;
b[0] = 0;
r[1] = 0; // Exemple couleur fixe pour le joueur 2 (Bleu)
g[1] = 0;
b[1] = 255;
int whoseTurn = whoGoesFirst; //global var
//fillBoard(); //for testing
//while (eachTurn(whoseTurn) == 0) { //returns 1 if end of game.. TODO recode this
while (!endGame(whoseTurn)) {
if (whoseTurn == 1) {
whoseTurn = 2; //next turn is player 2
} else {
whoseTurn = 1; //next turn is player 1
}
}
melt();
//who goes first next turn: first player has an advantage so loser goes first
if (whoseTurn == 1) {
whoGoesFirst = 2; //player 2 starts the next game
} else {
whoGoesFirst = 1; //player 1 starts the next game
}
}
void runDemo() {
// Vérifie si le programme est en mode DEMO
if (isDemoMode) {
pixels.setBrightness(255); // 1/4 power max 255 (10 at dimm/night)
// Générer des couleurs aléatoires pour chaque joueur
r[0] = random(0, 256); // Couleur aléatoire pour le joueur 1 (Red)
g[0] = random(0, 256); // Couleur aléatoire pour le joueur 1 (Green)
b[0] = random(0, 256); // Couleur aléatoire pour le joueur 1 (Blue)
r[1] = random(0, 256); // Couleur aléatoire pour le joueur 2 (Red)
g[1] = random(0, 256); // Couleur aléatoire pour le joueur 2 (Green)
b[1] = random(0, 256); // Couleur aléatoire pour le joueur 2 (Blue)
}
// Exécuter un jeu avec un nombre de tours aléatoire (par exemple, 50)
randomPlay(50); // jouer 50 tours ou jusqu'à la fin du jeu
delay(1000); // pause avant de "fondre" l'écran
melt();
delay(500); // pause après "melt"
}
void startDemo() {
isDemoMode = true; // Active le mode DEMO
runDemo(); // Lance le mode DEMO
isDemoMode = false; // Désactive le mode DEMO après l'exécution
}
/*
Find a starting spot for each player
Some of the top row columns may be played already so find an empty spot to start
If nothing found, return 11 (board full)
TODO: auto play last chip
*/
int startingSpot(int player) {
int i = i = randomColumn();
int pixelLoc;
int prevLoc;
boolean foundStartLoc = false;
if (player == 1) { //player1 start from left
int i = randomColumn();
//find a starting pixel: normally at 0
while ((i < 10) && (!foundStartLoc)) {
if (board[i] == 0) { //found an empty spot to start
pixelLoc = i;
showPixel(player, pixelLoc);
foundStartLoc = true;
} else { //occupied, go to next
i++;
}
}
} else { //player2 starts from the right side
int i = randomColumn();
while ((i >= 0) && (!foundStartLoc)) {
if (board[i] == 0) { //found an empty spot to start
pixelLoc = i;
showPixel(player, pixelLoc);
foundStartLoc = true;
} else { //occupied, go to next
i--;
}
}
}
if (foundStartLoc) {
return pixelLoc;
} else {
return 11; //11 means no more space to play
}
}
boolean endGame(int player) {
int prevLoc;
// Trouver un emplacement pour commencer le jeu pour le joueur actuel
int pixelLoc = startingSpot(player);
if (pixelLoc == 11) { // Pas de place pour jouer, le jeu est terminé
Serial.println("Pas de place pour jouer. Jeu terminé");
return true;
}
int WhichButton = isButtonPressed(); // Détecter quel bouton est appuyé
unsigned long startTime = millis(); // Démarrer le timer pour 3 secondes
while (WhichButton != 2) { // Tant que ce n'est pas le bouton bas (pour "laisser tomber" le jeton)
unsigned long currentTime = millis(); // Vérifier l'heure actuelle
if (currentTime - startTime > elapsingTime) { // Si plus de 3 secondes se sont écoulées
//Serial.println("Temps écoulé, au joueur suivant.");
// Effacer le pion du joueur dont le temps est écoulé (réinitialiser la position du pion)
board[pixelLoc] = 0; // Réinitialise la case à 0 (ou une autre valeur représentant une case vide)
movePixel(player, pixelLoc, -1); // Déplacer le pixel du joueur en dehors de l'écran ou à une position invalide
return false; // Passer au joueur suivant
}
boolean okToMove = false; // Réinitialiser à chaque boucle
if (WhichButton == 1) { // Si le bouton gauche est appuyé
// Vérifier à gauche
int chkLoc = pixelLoc - 1;
while ((chkLoc >= 0) && (!okToMove)) {
if (board[chkLoc] == 0) {
prevLoc = pixelLoc;
pixelLoc = chkLoc; // Se déplacer à gauche
okToMove = true;
} else {
chkLoc--;
}
}
movePixel(player, prevLoc, pixelLoc);
} else if (WhichButton == 3) { // Si le bouton droit est appuyé
// Vérifier à droite
int chkLoc = pixelLoc + 1;
while ((chkLoc <= 9) && (!okToMove)) {
if (board[chkLoc] == 0) {
prevLoc = pixelLoc;
pixelLoc = chkLoc; // Se déplacer à droite
okToMove = true;
} else {
chkLoc++;
}
}
movePixel(player, prevLoc, pixelLoc);
}
delay(1); // Petite pause avant de vérifier à nouveau
WhichButton = isButtonPressed(); // Vérifier l'état des boutons
}
// Si le bouton bas est appuyé, jouer le jeton dans la colonne sélectionnée
if (WhichButton == 2) {
int playedLoc = dropChip(pixelLoc, player);
if (isWinningMove(player, playedLoc)) {
return true; // Le joueur a gagné
} else {
return false; // La partie continue
}
}
return false; // Retourne false si aucun bouton n'est appuyé
}
void showPixel(int player, int location) {
pixels.setPixelColor(location, pixels.Color(r[player - 1], g[player - 1], b[player - 1])); //on
pixels.show(); // This sends the updated pixel color to the hardware.
}
void movePixel(int player, int currentLoc, int nextLoc) {
pixels.setPixelColor(nextLoc, pixels.Color(r[player - 1], g[player - 1], b[player - 1])); //on
pixels.setPixelColor(currentLoc, pixels.Color(0, 0, 0)); // off
pixels.show(); // This sends the updated pixel color to the hardware.
}
int isButtonPressed() {
// Délai de debounce en millisecondes
const int debounceDelay = 50;
// Helper function to wait for button release with debounce
auto waitForRelease = [](int buttonPin) {
while (digitalRead(buttonPin) == LOW) {
delay(10); // Petite pause pour éviter une surcharge
}
};
// Helper function for debouncing a button
auto isStablePress = [&](int buttonPin) {
if (digitalRead(buttonPin) == LOW) {
delay(debounceDelay); // Attendre un temps pour vérifier la stabilité
return digitalRead(buttonPin) == LOW; // Confirmer que l'état est toujours LOW
}
return false;
};
// Check each button with debounce
if (isStablePress(LeftButton)) {
//Serial.println("Left button pressed");
waitForRelease(LeftButton);
return 1;
} else if (isStablePress(DownButton)) {
//Serial.println("Down button pressed");
waitForRelease(DownButton);
return 2;
} else if (isStablePress(RightButton)) {
//Serial.println("Right button pressed");
waitForRelease(RightButton);
return 3;
}
return 0; // No button pressed
}
int dropChip(int currentLoc, int player) {
boolean notOccupied = true;
int checkLoc = currentLoc + 10; //check next location
int prevLoc;
//light the top row first
pixels.setPixelColor(currentLoc, pixels.Color(r[player - 1], g[player - 1], b[player - 1]));
pixels.show(); // This sends the updated pixel color to the hardware.
while ((checkLoc < 100) && (notOccupied)) {
if (board[checkLoc] == 0) { //not occupied
prevLoc = currentLoc;
currentLoc = checkLoc;
//move light
movePixel(player, prevLoc, currentLoc);
// pixels.setPixelColor(prevLoc, pixels.Color(0, 0, 0)); // off
// pixels.setPixelColor(currentLoc, pixels.Color(r[player - 1], g[player - 1], b[player - 1]));
// pixels.show(); // This sends the updated pixel color to the hardware.
checkLoc = checkLoc + 10; //check lower level
} else { //occupied
notOccupied = false;
}
delay(25); //slow down the drop
}
board[currentLoc] = player;
return currentLoc;
}
void showConnect4(int connect4[]) {
int player = board[connect4[0]]; //find out whose connect4
/*
Serial.print("showConnect4: Connected dots are :");
for (int l = 0; l <= 3; l++) {
Serial.print(connect4[l]);
Serial.print(" ");
}
Serial.println();
*/
//blink for 4 times
for (int i = 0; i <= 4; i++) {
//off all connect 4
for (int j = 0; j <= 3; j++) {
pixels.setPixelColor(connect4[j], pixels.Color(0, 0, 0));
}
// pixels.setPixelColor(connect4[0], pixels.Color(0, 0, 0));
// pixels.setPixelColor(connect4[1], pixels.Color(0, 0, 0));
// pixels.setPixelColor(connect4[2], pixels.Color(0, 0, 0));
// pixels.setPixelColor(connect4[3], pixels.Color(0, 0, 0));
pixels.show();
delay(200);
//turn on all connect 4
for (int j = 0; j <= 3; j++) {
pixels.setPixelColor(connect4[j], pixels.Color(r[player - 1], g[player - 1], b[player - 1]));
}
// pixels.setPixelColor(connect4[0], pixels.Color(r[player - 1], g[player - 1], b[player - 1]));
// pixels.setPixelColor(connect4[1], pixels.Color(r[player - 1], g[player - 1], b[player - 1]));
// pixels.setPixelColor(connect4[2], pixels.Color(r[player - 1], g[player - 1], b[player - 1]));
// pixels.setPixelColor(connect4[3], pixels.Color(r[player - 1], g[player - 1], b[player - 1]));
pixels.show();
delay(200);
}
}
/*
clear the board matrix with 0(not occupied) and no color
*/
void clearBoard() {
for (int i = 0; i < NUMPIXELS; i++) {
board[i] = 0;
pixels.setPixelColor(i, pixels.Color(0, 0, 0)); // off
}
pixels.show();
}
/*
used to simulate all the chips melting.
*/
void melt() { //melting effect
int color;
//Serial.println("initiate melting effect");
for (int k = 0; k <= 9; k++) {
//first time 55 to
for (int i = 55; i >= 10 * k; i--) { //100-11=55 always starts from the 2nd to the bottom row
color = board[i];
//change row below to the same color as top
if (color == 0) {
pixels.setPixelColor(i + 10, pixels.Color(0, 0, 0)); // black
board[i + 10] = 0;
} else {
pixels.setPixelColor(i + 10, pixels.Color(r[color - 1], g[color - 1], b[color - 1])); // set color
board[i + 10] = color;
}
}
pixels.show();
delay(100);
//change top rows to black //h = 0, from 9 to 0, h=1, from 15 to 10, when h=2, 23-16
for (int i = (9 + (10 * k)); i >= (10 * k); i--) {
pixels.setPixelColor(i, pixels.Color(0, 0, 0)); // black
board[i] = 0;
}
//Serial.println("outloop");
pixels.show();
//drawBoard();
//delay(50);
}
delay(500);
}
/*
Below are functions to check for connect 4
*/
boolean isWinningMove(int player, int playedLoc) {
/*
Serial.println("-----------------------------");
Serial.print("isWinningMove: playedLoc = ");
Serial.println(playedLoc);
*/
int connected[4]; //store all connected location
/*
//test
if (playedLoc < 40) {
Serial.println("Connect4 - veritcal");
connected[0] = playedLoc;
connected[1] = playedLoc + 10;
connected[2] = playedLoc + 16;
connected[3] = playedLoc + 24;
showConnect4(connected);
return true;
}
*/
//check for veritcal connect4
if (chkVericalWin(player, playedLoc)) {
return true;
} else if (chkHorizontalWin(player, playedLoc)) {
return true;
} else if (chkDiagonalWin(player, playedLoc)) { //up & right dirction TODO: combine with below
return true;
} else if (chkDiagonalWin2(player, playedLoc)) { //up and left direction
return true;
} else {
return false;
}
}
boolean chkVericalWin(int player, int playedLoc) {
int connected[4]; //init arrayf
//Serial.println("chkVericalWin: ");
if ((playedLoc < 70) && (board[playedLoc + 10] == player) && (board[playedLoc + 20] == player) && (board[playedLoc + 30] == player)) {
//Serial.println("********** Connect4 - veritcal**********");
//make array
connected[0] = playedLoc;
connected[1] = playedLoc + 10;
connected[2] = playedLoc + 20;
connected[3] = playedLoc + 30;
showConnect4(connected);
return true;
} else {
//Serial.println(" not connected");
return false;
}
}
boolean chkDiagonalWin2(int player, int playedLoc) {
int connected[4] = {0, 0, 0, 0}; //init array
int connectedIndex = 0; //starting with one connection index - self
connected[connectedIndex] = playedLoc;
//Serial.println("chkDiagonal reverse");
//check row above & left -> \ direction
boolean keepChecking = true;
int chkLoc;
//check for right edge condition
if ((playedLoc) % 10 == 0) {
//playedLoc is on the left edge, no need to check further
//Serial.println("left edge");
keepChecking = false;
} else {
chkLoc = playedLoc - 11; //56
}
while (keepChecking) {
//Serial.print(" chkloc(up & left) = ");
//Serial.println(chkLoc);
if (board[chkLoc] == player) {
connectedIndex++;
connected[connectedIndex] = chkLoc;
//Serial.println(" diag \ connected ");
//Serial.println(connected[0]);
//Serial.println(connected[1]);
//Serial.println(connected[2]);
//Serial.println(connected[3]);
if (connectedIndex >= 3) {
//Serial.println("**********Connect4 - diagonal**********");
showConnect4(connected);
return true;
}
//deal with boundary issue
if ((chkLoc) % 10 == 0) {
//chkLoc is on the left edge, no need to check further
//Serial.println("left edge");
keepChecking = false;
} else {
chkLoc = chkLoc - 11; //56
}
} else {
//Serial.println(" diag \ not connected ");
keepChecking = false;
}
}
// check going down diagonally in "\" direction
keepChecking = true;
//check for right edge condition ex: 55,49,1111
if ((playedLoc + 1) % 10 == 0) {
//playedLoc is on the left edge, no need to check further
//Serial.println("right edge");
keepChecking = false;
} else {
chkLoc = playedLoc + 11; //54 + 11 = 1111
}
while ((keepChecking) && (chkLoc <= 99)) {
//Serial.print(" chkloc(down & right) = ");
//Serial.println(chkLoc);
if (board[chkLoc] == player) {
connectedIndex++;
connected[connectedIndex] = chkLoc;
//Serial.println(" diag \ connected ");
//Serial.println(connected[0]);
//Serial.println(connected[1]);
//Serial.println(connected[2]);
//Serial.println(connected[3]);
if (connectedIndex >= 3) {
//Serial.println("**********Connect4 - diagonal**********");
showConnect4(connected);
return true;
}
//edge = (playedLoc) % 10;
if ((chkLoc + 1) % 10 == 0) { //ex 49
//chkLoc is on the right edge, no need to check further
//Serial.println("right edge");
keepChecking = false;
} else {
chkLoc = chkLoc + 11; //310+11=49
}
} else {
//Serial.println(" diag \ not connected ");
keepChecking = false;
}
}
//Serial.print("connectedIndex: ");
//Serial.println(connectedIndex);
//drawBoard();
return false;
}
boolean chkDiagonalWin(int player, int playedLoc) {
int connected[4] = {0, 0, 0, 0}; //init array
int connectedIndex = 0; //starting with one connection index - self
connected[connectedIndex] = playedLoc;
//Serial.println("chkDiagonal /");
//check row above & right -> / direction
boolean keepChecking = true;
int chkLoc;
//check for right edge condition
//int edge = (playedLoc + 1) % 10;
if ((playedLoc + 1) % 10 == 0) {
//playedLoc is on the right edge, no need to check further
//Serial.println("right edge");
keepChecking = false;
} else {
chkLoc = playedLoc - 9; //56
}
while (keepChecking) {
//Serial.print(" chkloc(up & right) = ");
//Serial.println(chkLoc);
if (board[chkLoc] == player) {
connectedIndex++;
connected[connectedIndex] = chkLoc;
//Serial.println(" diag / connected ");
//Serial.println(connected[0]);
//Serial.println(connected[1]);
//Serial.println(connected[2]);
//Serial.println(connected[3]);
if (connectedIndex >= 3) {
//Serial.println("**********Connect4 - diagonal**********");
showConnect4(connected);
return true;
}
//deal with boundary issue
if ((chkLoc + 1) % 10 == 0) {
//playedLoc is on the right edge, no need to check further
//Serial.println("right edge");
keepChecking = false;
} else {
chkLoc = chkLoc - 9; //56
}
} else {
//Serial.println(" diag / not connected ");
keepChecking = false;
}
}
keepChecking = true;
//check for left edge condition 56 is edge
//edge = (playedLoc) % 10;
if ((playedLoc) % 10 == 0) {
//playedLoc is on the left edge, no need to check further
//Serial.println("left edge");
keepChecking = false;
} else {
chkLoc = playedLoc + 9; //56
}
// check going down diagonally in "/" direction
while ((keepChecking) && (chkLoc <= 1111)) {
//Serial.print(" chkloc(down & left) = ");
//Serial.println(chkLoc);
if (board[chkLoc] == player) {
connectedIndex++;
connected[connectedIndex] = chkLoc;
//Serial.println(" diag / connected ");
//Serial.println(connected[0]);
//Serial.println(connected[1]);
//Serial.println(connected[2]);
//Serial.println(connected[3]);
if (connectedIndex >= 3) {
//Serial.println("**********Connect4 - diagonal**********");
showConnect4(connected);
return true;
}
//edge = (playedLoc) % 10;
if ((chkLoc) % 10 == 0) {
//playedLoc is on the left edge, no need to check further
//Serial.println("left edge");
keepChecking = false;
} else {
chkLoc = chkLoc + 9; //56
}
} else {
//Serial.println(" diag / not connected ");
keepChecking = false;
}
}
//drawBoard();
return false;
}
boolean chkHorizontalWin(int player, int playedLoc) {
int connected[4]; //init array
int connectedIndex = 0; //starting with one connection index - self
connected[connectedIndex] = playedLoc;
int leftWall = int(playedLoc / 10) * 10;
//Serial.print("chkHorizontalWin: leftWall = ");
//Serial.println(leftWall);
//until hit a wall, keep checking left side
int chkLoc = playedLoc - 1;
boolean keepChecking = true;
while ((leftWall <= chkLoc) && (keepChecking)) {
//Serial.print(" chkloc = ");
//Serial.println(chkLoc);
if ( board[chkLoc] == player ) {
//left connected
connectedIndex++;
connected[connectedIndex] = chkLoc;
//Serial.println(" left connected "); //33
//Serial.println(chkLoc);
if (connectedIndex >= 3) {
//Serial.println("**********Connect4 - horizontal**********");
showConnect4(connected);
return true;
}
} else {
//Serial.println(" left not connected");
keepChecking = false;
}
chkLoc--; //32
} //while to check left
//check right: ex: for 61, right wall is 1111
int rightWall = leftWall + 9;
//Serial.print("chkHorizontalWin: rightWall = ");
//Serial.println(rightWall);
//until hit a wall, keep checking left side
chkLoc = playedLoc + 1;
keepChecking = true;
while ((rightWall >= chkLoc) && (keepChecking)) {
//Serial.print(" chkloc = ");
//Serial.println(chkLoc);
if ( board[chkLoc] == player ) {
//right connected
connectedIndex++; //keep accumulating connected points
connected[connectedIndex] = chkLoc;
//Serial.println(" right connected "); //33
//Serial.println(chkLoc);
if (connectedIndex >= 3) {
//Serial.println("**********Connect4 - horizontal**********");
showConnect4(connected);
return true;
}
} else {
//Serial.println(" right not connected");
keepChecking = false;
}
chkLoc++; //32
} //while to check left
return false;
}
/*
utilities used in testing
*/
void fillBoard() { //for testing purpose only
int player = 2;
for (int i = 10; i < NUMPIXELS; i++) {
if (player == 1) {
player = 2;
} else {
player = 1;
}
board[i] = player;
pixels.setPixelColor(i, pixels.Color(r[player - 1], g[player - 1], b[player - 1])); // set color
}
pixels.show();
}
void drawBoard() {
for (int i = 0; i < 10; i++) { //row loop
for (int j = (0 + (10 * i)); j < (10 + (10 * i)); j++) {
//Serial.print(j);
Serial.print(board[j]);
//pixels.setPixelColor(i, pixels.Color(0,0,0)); // Moderately bright green color.
//pixels.show();
}
Serial.println();
}
Serial.println("--------");
}
/*
Functions for play demo. This has no "winning" algorithm; it just randomly drops a chip until
a player "accidently" wins the game.
*/
void randomPlay(int turns) {
boolean gameOver = false;
int player = 2;
int i = 1;
//Serial.println("random play");
while ((i < turns) && (!gameOver)) {
if (player == 1) {
player = 2;
} else {
player = 1;
}
//pick a column that's not full
int chooseColumn = randomColumn();
while (board[chooseColumn] > 0) { //occupied - pick another number
//Serial.println("@randomplay - column is full: ");
//delay(5000);
//pick another column
chooseColumn = randomColumn();
}
//wait for down button: for debugging one turn at a time
/*
while (isButtonPressed() != 2 ) {
//Serial.println("waiting for down button");
delay(10);
}*/
//Serial.println(chooseColumn);
int playedLoc = dropChip(chooseColumn, player);
if (isWinningMove(player, playedLoc)) {
gameOver = true;
}
i++;
//pause after each turn
delay(500);
}
//Serial.println("********** random play - game over **********");
}
//function to randomly pick a column to dropChip
int randomColumn() {
int randomNum;
//random(min, max)
//Parameters
//min - lower bound of the random value, inclusive (optional)
//max - upper bound of the random value, exclusive
//generate random # from 2 to 5 that matches led Pins
//2 = red, 3 = blue, 4 = yellow, 5=green
randomNum = random(0, 10);
return randomNum;
}