Intelligent Arduino Uno & Mega Tic Tac Toe (morpion) (4 / 5 étapes)

Étape 4: Le Code

Obtenir le code complet est en dessous. Cependant, prêter une attention particulière aux constantes déclarées dans la partie supérieure du code. Pour la version de Arduino Mega, vérifier les définitions de broche pour le bouton de réinitialisation, la victoire des rouge et verte LEDs, les boutons, les voyants rouges et verts. La plupart d'entre eux ont été définie sous forme de tableaux 3D et j’ai aménagé le code pour correspondre aux voyants et boutons comme montré sur la photo. Bouton gauche sur la photo du haut correspond au premier élément du tableau. Nous puis retravailler ensemble vers la droite, vers le bas d’une rangée, partout encore une fois en bas une rangée et partout. N’oubliez pas, le diagramme Fritzing est tourné de 90° vers la droite par rapport à des photos - mais j’ai noté la « numérotation » des voyants et boutons sur le diagramme.

Pour la version Arduino Uno, vérifier les broches Charlieplex en haut du code, la broche analogique (« bouton ») et la broche de bouton de réinitialisation. Vous ne devriez avoir aucun problème avec la numérotation de LED et il peut être laissé comme-est. Si pour une raison étrange, la matrice de touches ne fonctionne pas, vous devrez faire votre propre dépannage pour vérifier les valeurs de tension qui sont à venir dans la broche analogique sur chaque touche et ajustez le tableau « resButtons » en conséquence.

N’oubliez pas, pour la version Arduino Uno, vous aurez besoin de la bibliothèque de Charlieplex jointe en tant qu’un fichier le code ci-dessous. Décompressez-le et placez-le dans le dossier bibliothèques de votre IDE Arduino (vous pouvez effectuer une recherche sur le web si vous ne savez pas comment).

Le peu d’intelligence n’y a rien de plus que l’Arduino vérifiant chaque colonne, la ligne et la diagonale pour voir s’il y a déjà deux LED rouge allumée et un espace libre de placer un morceau. Cela signifierait l’Arduino pouvait gagner et c’est prioritaire. Si l’Arduino ne trouve une telle ligne, il fera ensuite la même chose à nouveau mais cette fois à la recherche de deux voyants verts et un espace de rechange. Cela signifie que l’homme pouvait gagner et l’Arduino placera donc un morceau pour bloquer l’humain. Enfin, s’il ne peut pas gagner ou bloquer, il choisira un endroit au hasard en choisissant l’un des espaces libres au Conseil d’administration.

J’ai commenté le code autant que possible pour vous aider à comprendre ce qui se passe. Je sais que le code pourrait être beaucoup plus efficace. Je pense que ce n’est pas si mal pour une première tentative. Bonne chance!

Pour la Arduino Mega : (Version de l’Arduino Uno est ci-dessous)

 // TIC TAC TOE for Arduino Mega // by Nick Harvey // Include any libraries needed #include <liquidcrystal.h> // For the LCD // Define pins const int green[3][3] = { // Green is the player {30, 31, 32}, {33, 34, 35}, {36, 37, 38} }; const int red[3][3] = { // Red is the Arduino {40, 41, 42}, {43, 44, 45}, {46, 47, 48} }; const int button[3][3] = { // Buttons to choose position {2, 3, 4}, {5, 6, 7}, {8, 9, 10} }; const int greenWin = 50; // Lights if the player wins const int redWin = 51; // Lights if the Arduino wins const int resetButton = 11; // Button to start a new game const int win[8][3][3] = { // This 4D array defines all possible winning combinations { {1, 1, 1}, {0, 0, 0}, {0, 0, 0} }, { {0, 0, 0}, {1, 1, 1}, {0, 0, 0} }, { {0, 0, 0}, {0, 0, 0}, {1, 1, 1} }, { {1, 0, 0}, {1, 0, 0}, {1, 0, 0} }, { {0, 1, 0}, {0, 1, 0}, {0, 1, 0} }, { {0, 0, 1}, {0, 0, 1}, {0, 0, 1} }, { {1, 0, 0}, {0, 1, 0}, {0, 0, 1} }, { {0, 0, 1}, {0, 1, 0}, {1, 0, 0} } }; LiquidCrystal lcd(A4, A5, A3, A2, A1, A0); // Pins used for the LCD display (standard Arduino LCD library) // Global variables int gamePlay[3][3] = { // Holds the current game {0, 0, 0}, {0, 0, 0}, {0, 0, 0} }; int squaresLeft = 9; // The number of free squares left on the board int played = 0; // Has the Arduino played or not // Global constants const int startupFlashSpeed = 100; // The time in milliseconds the LEDs light for on startup const int arduinoDelay = 3000; // How long the Arduino waits before playing (simulates thought) void setup() { // put your setup code here, to run once: // Start serial comms Serial.begin(9600); // Initialise LCD lcd.begin(16, 2); // Define green and red pins as outputs for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { pinMode(green[i][j], OUTPUT); pinMode(red[i][j], OUTPUT); } } // Define green and red win lights as outputs pinMode(greenWin, OUTPUT); pinMode(redWin, OUTPUT); // Define buttons as inputs for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { pinMode(button[i][j], INPUT); } } //Define reset button as input pinMode(resetButton, INPUT); initialise(); // Do startup flash startupFlash(); } void initialise() { // Prepare the board for a game disp("TIC TAC TOE"); // Set green and red LEDs off for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { digitalWrite(green[i][j], LOW); digitalWrite(red[i][j], LOW); gamePlay[i][j] = 0; } } // Set win LEDs off digitalWrite(greenWin, LOW); digitalWrite(redWin, LOW); // Reset variables squaresLeft = 9; // Tell the player it's their turn disp("Your turn..."); } void loop() { // put your main code here, to run repeatedly: // Wait for an input and call the buttonPress routine if pressed for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if ((digitalRead(button[i][j]) == HIGH) && (gamePlay[i][j] == 0)) { buttonPress(i, j); // Pass the x and y of the button pressed break; } } } } void buttonPress(int i, int j) { // Button pressed, light the green LED and note the square as taken Serial.print("Button press "); Serial.print(i); Serial.println(j); digitalWrite(green[i][j], HIGH); gamePlay[i][j] = 1; // Note the square played squaresLeft -= 1; // Update number of squares left printGame(); // Print the game to serial monitor checkGame(); // Check for a winner arduinosTurn(); // Arduino's turn } void arduinosTurn() { // Arduino takes a turn Serial.println("Arduino's turn"); disp("My turn..."); checkPossiblities(); // Check to see if a winning move can be played if (played == 0) { checkBlockers(); // If no winning move played, check to see if we can block a win } if (played == 0) { randomPlay(); // Otherwise, pick a random square } squaresLeft -= 1; // Update number of squares left played = 0; // Reset if played or not printGame(); // Print the games to serial monitor checkGame(); // Check for a winner disp("Your turn..."); // Tell the player it's their turn } void checkPossiblities() { // Check all rows, then columns, then diagonals to see if there are two reds lit and a free square to make a line of three Serial.println("Checking possibilities to win..."); disp("Can I win?"); int poss = 0; // Used to count if possible - if it gets to 2 then its a possiblity int x = 0; // The X position to play int y = 0; // The Y position to play int space = 0; // Is there a free square or not to play // Check rows for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (gamePlay[i][j] == 2) { poss += 1; // Square is red. Increment the possiblity counter } if (gamePlay[i][j] == 0) { space = 1; // Square is empty. Note this and the position x = i; y = j; } if ((poss == 2) && (space == 1)) { // 2 red squares and a free square Serial.print("Found an obvious row! "); Serial.print(x); Serial.println(y); playPoss(x, y); } } poss = 0; x = 0; y = 0; space = 0; } // Check columns - same as for rows but the "for" loops have been reversed to go to columns for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (gamePlay[j][i] == 2) { poss += 1; } if (gamePlay[j][i] == 0) { space = 1; x = j; y = i; } if ((poss == 2) && (space == 1) && (played == 0)) { // This time also check if we've already played Serial.print("Found an obvious column! "); Serial.print(x); Serial.println(y); playPoss(x, y); } } // Reset variables poss = 0; x = 0; y = 0; space = 0; } // Check crosses - as for rows and columns but "for" loops changed // Check diagonal top left to bottom right for (int i = 0; i < 3; i++) { if (gamePlay[i][i] == 2) { poss += 1; } if (gamePlay[i][i] == 0) { space = 1; x = i; y = i; } if ((poss == 2) && (space == 1) && (played == 0)) { Serial.print("Found an obvious cross! "); Serial.print(x); Serial.println(y); playPoss(x, y); } } // Reset variables poss = 0; x = 0; y = 0; space = 0; // Check diagonal top right to bottom left int row = 0; // Used to count up the rows for (int i = 2; i >= 0; i--) { // We count DOWN the columns if (gamePlay[row][i] == 2) { poss += 1; } if (gamePlay[row][i] == 0) { space = 1; x = row; y = i; } if ((poss == 2) && (space == 1) && (played == 0)) { Serial.print("Found an obvious cross! "); Serial.print(x); Serial.println(y); playPoss(x, y); } row += 1; // Increment the row counter } // Reset variables poss = 0; x = 0; y = 0; space = 0; } void checkBlockers() { // As for checkPossibilites() but this time checking the players squares for places to block a line of three Serial.println("Checking possibilities to block..."); disp("Can I block?"); int poss = 0; int x = 0; int y = 0; int space = 0; // Check rows for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (gamePlay[i][j] == 1) { poss += 1; } if (gamePlay[i][j] == 0) { space = 1; x = i; y = j; } if ((poss == 2) && (space == 1) && (played == 0)) { Serial.print("Found an blocker row! "); Serial.print(x); Serial.println(y); playPoss(x, y); } } poss = 0; x = 0; y = 0; space = 0; } // Check columns for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (gamePlay[j][i] == 1) { poss += 1; } if (gamePlay[j][i] == 0) { space = 1; x = j; y = i; } if ((poss == 2) && (space == 1) && (played == 0)) { Serial.print("Found an blocker column! "); Serial.print(x); Serial.println(y); playPoss(x, y); } } poss = 0; x = 0; y = 0; space = 0; } // Check crosses for (int i = 0; i < 3; i++) { if (gamePlay[i][i] == 1) { poss += 1; } if (gamePlay[i][i] == 0) { space = 1; x = i; y = i; } if ((poss == 2) && (space == 1) && (played == 0)) { Serial.print("Found an blocker cross! "); Serial.print(x); Serial.println(y); playPoss(x, y); } } poss = 0; x = 0; y = 0; space = 0; int row = 0; for (int i = 2; i >= 0; i--) { if (gamePlay[row][i] == 1) { poss += 1; } if (gamePlay[row][i] == 0) { space = 1; x = row; y = i; } if ((poss == 2) && (space == 1) && (played == 0)) { Serial.print("Found an blocker cross! "); Serial.print(x); Serial.println(y); playPoss(x, y); } row += 1; } poss = 0; x = 0; y = 0; space = 0; } void randomPlay() { // No win or block to play... Let's just pick a square at random Serial.println("Choosing randomly..."); int choice = random(1, squaresLeft); // We pick a number from 0 to the number of squares left on the board Serial.print("Arduino chooses "); Serial.println(choice); int pos = 1; // Stores the free square we're currently on for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (gamePlay[i][j] == 0) { // Check to see if square empty if (pos == choice) { // Play the empty square that corresponds to the random number playPoss(i, j); } pos += 1; // Increment the free square counter } } } } void playPoss(int x, int y) { // Simulate thought and then play the chosen square disp("Hmmm..."); delay(arduinoDelay); disp("OK"); digitalWrite(red[x][y], HIGH); gamePlay[x][y] = 2; // Update the game play array played = 1; // Note that we've played } void checkGame() { // Check the game for a winner // Check if the player has won Serial.println("Checking for a winner"); disp("Checking..."); int player = 1; int winner = 0; for (int i = 0; i < 8; i++) { // We cycle through all winning combinations in the 4D array and check if they correspond to the current game //Check game int winCheck = 0; for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { if ((win[i][j][k] == 1) && (gamePlay[j][k] == player)) { winCheck += 1; } } } if (winCheck == 3) { winner = 1; Serial.print("Player won game "); Serial.println(i); endGame(1); } } // Do the same for to check if the Arduino has won player = 2; winner = 0; for (int i = 0; i < 8; i++) { //Check game int winCheck = 0; for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { if ((win[i][j][k] == 1) && (gamePlay[j][k] == player)) { winCheck += 1; } } } if (winCheck == 3) { winner = 1; Serial.print("Arduino won game "); Serial.println(i); endGame(2); } } if (squaresLeft == -1) { endGame(0); } } void printGame() { // Prints the game to the serial monitor for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { Serial.print(gamePlay[i][j]); Serial.print(" "); } Serial.println(""); } Serial.print(squaresLeft); Serial.println(" squares left"); } void endGame(int winner) { // Is called when a winner is found switch (winner) { case 0: Serial.println("It's a draw"); digitalWrite(greenWin, HIGH); digitalWrite(redWin, HIGH); disp("It's a draw!"); break; case 1: Serial.println("Player wins"); digitalWrite(greenWin, HIGH); disp("You win!!!"); break; case 2: Serial.println("Arduino wins"); digitalWrite(redWin, HIGH); disp("I win!!!"); break; } lcd.setCursor(0, 1); lcd.print("Press reset..."); while (digitalRead(resetButton) == LOW) { } initialise(); } void disp(String message) { // Used to quickly display a message on the LCD lcd.clear(); lcd.setCursor(0, 0); lcd.print(message); } void startupFlash() { // Flash at the start for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { digitalWrite(green[i][j], HIGH); digitalWrite(greenWin, HIGH); delay(startupFlashSpeed); digitalWrite(green[i][j], LOW); digitalWrite(greenWin, LOW); digitalWrite(red[i][j], HIGH); digitalWrite(redWin, HIGH); delay(startupFlashSpeed); digitalWrite(red[i][j], LOW); digitalWrite(redWin, LOW); } } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { digitalWrite(green[j][i], HIGH); delay(startupFlashSpeed); digitalWrite(green[j][i], LOW); } } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { digitalWrite(red[i][j], HIGH); delay(startupFlashSpeed); digitalWrite(red[i][j], LOW); } } for (int i = 0; i < 3; i++) { digitalWrite(red[i][i], HIGH); delay(startupFlashSpeed); digitalWrite(red[i][i], LOW); } int row = 0; for (int i = 2; i >= 0; i--) { digitalWrite(green[row][i], HIGH); delay(startupFlashSpeed); digitalWrite(green[row][i], LOW); row += 1; } } 

Pour l’Arduino Uno :

 <p>// TIC TAC TOE for Arduino Uno<br>// by Nick Harvey</p><p>#include <charlieplex.h> byte pins[5] = {5, 6, 9, 10, 11}; Charlieplex charlie(pins, sizeof(pins));</charlieplex.h></p><p>// Define LEDs const int green[3][3] = { // Green is the player {0, 8, 14}, {2, 10, 16}, {4, 12, 6} }; const int red[3][3] = { // Red is the Arduino {1, 9, 15}, {3, 11, 17}, {5, 13, 7} }; const int button = A0; // The analog pin the button matrix is connected to</p><p>const int resButtons[3][3] = { // The resistance thresholds for the buttons {800, 400, 200}, {160, 140, 120}, {90, 85, 70} };</p><p>const int greenWin = 18; // Lights if the player wins const int redWin = 19; // Lights if the Arduino wins const int resetButton = 13; // Button to start a new game</p><p>const int win[8][3][3] = { // This 4D array defines all possible winning combinations { {1, 1, 1}, {0, 0, 0}, {0, 0, 0} }, { {0, 0, 0}, {1, 1, 1}, {0, 0, 0} }, { {0, 0, 0}, {0, 0, 0}, {1, 1, 1} }, { {1, 0, 0}, {1, 0, 0}, {1, 0, 0} }, { {0, 1, 0}, {0, 1, 0}, {0, 1, 0} }, { {0, 0, 1}, {0, 0, 1}, {0, 0, 1} }, { {1, 0, 0}, {0, 1, 0}, {0, 0, 1} }, { {0, 0, 1}, {0, 1, 0}, {1, 0, 0} } };</p><p>// Global variables int gamePlay[3][3] = { // Holds the current game {0, 0, 0}, {0, 0, 0}, {0, 0, 0} }; int squaresLeft = 9; // The number of free squares left on the board int played = 0; // Has the Arduino played or not</p><p>// Global constants const int arduinoDelay = 3000; // How long the Arduino waits before playing (simulates thought)</p><p>void setup() { // put your setup code here, to run once:</p><p> // Start serial comms Serial.begin(9600);</p><p> // Define buttons as inputs pinMode(button, INPUT);</p><p> //Define reset button as input pinMode(resetButton, INPUT);</p><p> initialise(); }</p><p>void initialise() { // Prepare the board for a game Serial.println("Initialising..."); // Set green and red LEDs off for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { charlie.setLed(green[i][j], false); charlie.setLed(red[i][j], false); gamePlay[i][j] = 0; } } // Set win LEDs off charlie.setLed(greenWin, false); charlie.setLed(redWin, false);</p><p> // Reset variables squaresLeft = 9; } void loop() { // put your main code here, to run repeatedly: // Wait for an input and call the buttonPress routine if pressed int upper = 10000; if (analogRead(button) != 0) { int x; int y; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (gamePlay[i][j] == 0) { if ((analogRead(button) > resButtons[i][j]) && (analogRead(button) < upper)) { buttonPress(i, j); } } upper = resButtons[i][j]; } } } charlie.loop(); }</p><p>void buttonPress(int i, int j) { // Button pressed, light the green LED and note the square as taken Serial.print("Button press "); Serial.print(i); Serial.print(":"); Serial.println(j); charlie.setLed(green[i][j], true); gamePlay[i][j] = 1; // Note the square played squaresLeft -= 1; // Update number of squares left printGame(); // Print the game to serial monitor checkGame(); // Check for a winner arduinosTurn(); // Arduino's turn }</p><p>void arduinosTurn() { // Arduino takes a turn Serial.println("Arduino's turn"); checkPossiblities(); // Check to see if a winning move can be played if (played == 0) { checkBlockers(); // If no winning move played, check to see if we can block a win } if (played == 0) { randomPlay(); // Otherwise, pick a random square } squaresLeft -= 1; // Update number of squares left played = 0; // Reset if played or not printGame(); // Print the games to serial monitor checkGame(); // Check for a winner }</p><p>void checkPossiblities() { // Check all rows, then columns, then diagonals to see if there are two reds lit and a free square to make a line of three Serial.println("Checking possibilities to win..."); int poss = 0; // Used to count if possible - if it gets to 2 then its a possiblity int x = 0; // The X position to play int y = 0; // The Y position to play int space = 0; // Is there a free square or not to play // Check rows for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (gamePlay[i][j] == 2) { poss += 1; // Square is red. Increment the possiblity counter } if (gamePlay[i][j] == 0) { space = 1; // Square is empty. Note this and the position x = i; y = j; } if ((poss == 2) && (space == 1)) { // 2 red squares and a free square Serial.print("Found an obvious row! "); Serial.print(x); Serial.println(y); playPoss(x, y); } } poss = 0; x = 0; y = 0; space = 0; }</p><p> // Check columns - same as for rows but the "for" loops have been reversed to go to columns for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (gamePlay[j][i] == 2) { poss += 1; } if (gamePlay[j][i] == 0) { space = 1; x = j; y = i; } if ((poss == 2) && (space == 1) && (played == 0)) { // This time also check if we've already played Serial.print("Found an obvious column! "); Serial.print(x); Serial.println(y); playPoss(x, y); } } // Reset variables poss = 0; x = 0; y = 0; space = 0; }</p><p> // Check crosses - as for rows and columns but "for" loops changed // Check diagonal top left to bottom right for (int i = 0; i < 3; i++) { if (gamePlay[i][i] == 2) { poss += 1; } if (gamePlay[i][i] == 0) { space = 1; x = i; y = i; } if ((poss == 2) && (space == 1) && (played == 0)) { Serial.print("Found an obvious cross! "); Serial.print(x); Serial.println(y); playPoss(x, y); } } // Reset variables poss = 0; x = 0; y = 0; space = 0; // Check diagonal top right to bottom left int row = 0; // Used to count up the rows for (int i = 2; i >= 0; i--) { // We count DOWN the columns if (gamePlay[row][i] == 2) { poss += 1; } if (gamePlay[row][i] == 0) { space = 1; x = row; y = i; } if ((poss == 2) && (space == 1) && (played == 0)) { Serial.print("Found an obvious cross! "); Serial.print(x); Serial.println(y); playPoss(x, y); } row += 1; // Increment the row counter } // Reset variables poss = 0; x = 0; y = 0; space = 0; }</p><p>void checkBlockers() { // As for checkPossibilites() but this time checking the players squares for places to block a line of three Serial.println("Checking possibilities to block..."); int poss = 0; int x = 0; int y = 0; int space = 0; // Check rows for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (gamePlay[i][j] == 1) { poss += 1; } if (gamePlay[i][j] == 0) { space = 1; x = i; y = j; } if ((poss == 2) && (space == 1) && (played == 0)) { Serial.print("Found an blocker row! "); Serial.print(x); Serial.println(y); playPoss(x, y); } } poss = 0; x = 0; y = 0; space = 0; }</p><p> // Check columns for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (gamePlay[j][i] == 1) { poss += 1; } if (gamePlay[j][i] == 0) { space = 1; x = j; y = i; } if ((poss == 2) && (space == 1) && (played == 0)) { Serial.print("Found an blocker column! "); Serial.print(x); Serial.println(y); playPoss(x, y); } } poss = 0; x = 0; y = 0; space = 0; }</p><p> // Check crosses for (int i = 0; i < 3; i++) { if (gamePlay[i][i] == 1) { poss += 1; } if (gamePlay[i][i] == 0) { space = 1; x = i; y = i; } if ((poss == 2) && (space == 1) && (played == 0)) { Serial.print("Found an blocker cross! "); Serial.print(x); Serial.println(y); playPoss(x, y); } } poss = 0; x = 0; y = 0; space = 0; int row = 0; for (int i = 2; i >= 0; i--) { if (gamePlay[row][i] == 1) { poss += 1; } if (gamePlay[row][i] == 0) { space = 1; x = row; y = i; } if ((poss == 2) && (space == 1) && (played == 0)) { Serial.print("Found an blocker cross! "); Serial.print(x); Serial.println(y); playPoss(x, y); } row += 1; } poss = 0; x = 0; y = 0; space = 0; }</p><p>void randomPlay() { // No win or block to play... Let's just pick a square at random Serial.println("Choosing randomly..."); int choice = random(1, squaresLeft); // We pick a number from 0 to the number of squares left on the board Serial.print("Arduino chooses "); Serial.println(choice); int pos = 1; // Stores the free square we're currently on for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (gamePlay[i][j] == 0) { // Check to see if square empty if (pos == choice) { // Play the empty square that corresponds to the random number playPoss(i, j); } pos += 1; // Increment the free square counter } } } }</p><p>void playPoss(int x, int y) { // Simulate thought and then play the chosen square int delayStop = millis() + arduinoDelay; while (millis() < delayStop) { charlie.loop(); } charlie.setLed(red[x][y], true); gamePlay[x][y] = 2; // Update the game play array played = 1; // Note that we've played }</p><p>void checkGame() { // Check the game for a winner // Check if the player has won Serial.println("Checking for a winner"); int player = 1; int winner = 0; for (int i = 0; i < 8; i++) { // We cycle through all winning combinations in the 4D array and check if they correspond to the current game //Check game int winCheck = 0; for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { if ((win[i][j][k] == 1) && (gamePlay[j][k] == player)) { winCheck += 1; } } } if (winCheck == 3) { winner = 1; Serial.print("Player won game "); Serial.println(i); endGame(1); } } // Do the same for to check if the Arduino has won player = 2; winner = 0; for (int i = 0; i < 8; i++) { //Check game int winCheck = 0; for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { if ((win[i][j][k] == 1) && (gamePlay[j][k] == player)) { winCheck += 1; } } } if (winCheck == 3) { winner = 1; Serial.print("Arduino won game "); Serial.println(i); endGame(2); } } if (squaresLeft == -1) { endGame(0); } }</p><p>void printGame() { // Prints the game to the serial monitor for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { Serial.print(gamePlay[i][j]); Serial.print(" "); } Serial.println(""); } Serial.print(squaresLeft); Serial.println(" squares left"); }</p><p>void endGame(int winner) { // Is called when a winner is found switch (winner) { case 0: Serial.println("It's a draw"); charlie.setLed(greenWin, true); charlie.setLed(redWin, true); break; case 1: Serial.println("Player wins"); charlie.setLed(greenWin, true); break; case 2: Serial.println("Arduino wins"); charlie.setLed(redWin, true); break; } while (digitalRead(resetButton) == LOW) { charlie.loop(); } initialise(); }</p> 

Articles Liés

Arduino et Touchpad Tic Tac Toe

Arduino et Touchpad Tic Tac Toe

ou, un exercice en entrée et sortie de multiplexage et travail avec des morceaux.  Et une soumission pour le concours de l'Arduino.Il s'agit d'une implémentation d'un jeu de tic tac toe en utilisant un tableau de 3 x 3 de bicolore LED pour un afficha
Tic Tac Toe jeu Arduino à l’aide de

Tic Tac Toe jeu Arduino à l’aide de

Tic Tac Toe est un jeu populaire de papier de deux joueurs dans lequel premier joueur à remplir une ligne, colonne ou diagonale de « X » ou ' o ' de victoires. Si personne n'a pas pu réaliser l'exploit, le match est établi.Étape 1: Composants requis1
TIC-TAC-TOE Robot

TIC-TAC-TOE Robot

dans ce Instructable je vais vous montrer comment faire un bras robot qui joue Tic Tac Toe en utilisant une commande de robot magicien Micro, 4 servos et blocs de construction / matériaux de votre choix. Le câblage est super simple, il suffit de bran
Le bouclier de TIC-TAC-TOE...

Le bouclier de TIC-TAC-TOE...

Il s'agit d'une maison shield pour Arduino. Je peux jouer Tic-Tac-Toe avec le MCU avec ce bouclier attaché.Les gens me demandent, « Hé, que faites-vous tout le temps dans votre établi? », ou « Hey, nous montrer quelque chose vous avez fait. » Et pour
Tic Tac Toe de voyage

Tic Tac Toe de voyage

Morpion est un jeu de voyage génial qui peut garder des enfants et des adultes, occupées pendant les heures d'un voyage sur la route. Avec un peu de feutre et de créativité, j'ai pu faire un impressionnant voyage réutilisables tic tac plinthe qui se
Comment écrire un programme de TIC-TAC-TOE en Java

Comment écrire un programme de TIC-TAC-TOE en Java

Introduction :TIC-TAC-TOE est un jeu très commun qui est assez facile à jouer. Les règles du jeu sont simples et bien connus. À cause de ces choses, TIC-TAC-TOE est assez facile de code vers le haut. Dans ce tutoriel, nous allons regarder comment cod
TIC-TAC-TOE électronique avec LED RGB

TIC-TAC-TOE électronique avec LED RGB

jeu de LED RVB pour jouer TIC-TAC-TOE pour deux joueurs. Utilise 2 AVR microcontrôleurs : Mega16 et Mega8. LED RGB permettent à chaque utilisateur de choisir sa couleur pour représenter / écrou.
TIC-TAC-TOE upcycled

TIC-TAC-TOE upcycled

Ce mois-ci j'ai pris le défi « CENT-SATIONNELLE récupérable » la. Dans le défi, que je suis chargé de transformer un article commun trouvé à mon Habitat local pour restaurer l'humanité Centre en quelque chose de « cent-sationnelle. » Et je ne pouvais
Comment gagner Tic Tac Toe : Simple !

Comment gagner Tic Tac Toe : Simple !

Bonjour, je suis InstructableUltimate et j'ai aujourd'hui je vais vous montrer comment gagner de Tic Tac Toe en seulement quelques étapes, et c'est vraiment simple !Étape 1: Gagner va première Eh bien, c'est une victoire facile ! Au revoir et j'espèr
TIC-TAC-TOE stratégies gagnantes

TIC-TAC-TOE stratégies gagnantes

Tout le monde aime le simple jeu de TIC-TAC-TOE, mais on dirait un jeu aléatoire. En fait, ce n'est pas !Il semble que les nombreux différends peuvent être résolus par un simple jeu...Maintenant, vous pouvez gagner le prochain tic-tac-tac-toe-off en
Faire Tic Tac Toe en Java

Faire Tic Tac Toe en Java

ce Instructable vous guidera, étape par étape, en faisant Tic Tac Toe en Java ! Ce n'est pas prévu pour être une vue d'ensemble du langage Java, mais plus d'un exemple guidé. La première étape risque de dépasser certains concepts de base pour faire l
Connexion ESP8266-01 à Arduino UNO / MEGA et Billy

Connexion ESP8266-01 à Arduino UNO / MEGA et Billy

Il s'agit d'un tutoriel pour vous montrer comment flasher un firmware à ESP8266-01 et de se connecter à Billy à l'aide d'un ESP8266 - 01 comme un Arduino wifi bouclier.Matériel nécessaire :* Arduino Uno/Mega* Fils de raccordement* USB câble USB B A*
FPGA Tic Tac Toe

FPGA Tic Tac Toe

"Tic Tac Toe ? Qu'est-ce que c est? J'ai jamais entendu parler de cela. »-Personne ne jamaisPar Ryan Frawley et Derek NguyenCe guide vous montrera comment faire un travail de Tic Tac Toe jeu en VHDL sur une planche de Nexys 2 FPGA. Ce tutoriel a été
Drapeau américain Tic Tac Toe

Drapeau américain Tic Tac Toe

Divertir votre quart de juillet invités à la fête avec ce facile de faire le Conseil d'administration de drapeau américain tic tac toe !Étape 1: Recueillir vos matériaux 10 galetsMarqueur indélébile bleuMarqueur permanent rougeBoîte (double tant que