belrey10

Tic Tac Toe

Nov 4th, 2024 (edited)
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. String board[] = {"","","","","","","","",""};
  2.  
  3. void setup() {
  4.   // put your setup code here, to run once:
  5.   Serial.begin(9600);
  6.  
  7. }
  8.  
  9. void loop() {
  10.   // put your main code here, to run repeatedly:
  11.   if(checkIfOver()) {
  12.     resetBoard();
  13.   }
  14.   else {
  15.     playerPlaceX();
  16.   }
  17.   if(checkIfOver()) {
  18.     resetBoard();
  19.   }
  20.   else {
  21.     HEROPlaceO();
  22.   }
  23. }
  24.  
  25. boolean isAvailable(int square) {
  26.   if(board[square]==""){
  27.     return true;
  28.   }
  29.   else {
  30.     return false;
  31.   }
  32. }
  33.  
  34. void resetBoard() {
  35.   Serial.println("Resetting board...");
  36.   delay(5000);
  37.   for(int i = 0; i < 8; i++){
  38.     board[i] = "";
  39.   }
  40. }
  41. void playerPlaceX() {
  42.   Serial.println("Where would you like to place an X? Enter 0-8");
  43.   while (Serial.available() == 0) { // stops program from continuing until Serial.available is 0, or the user inputs something.
  44.      //wait for user to input data
  45.   }
  46.   int playerChoice = Serial.readString().toInt();
  47.  
  48.   if (isAvailable(playerChoice)) {
  49.     board[playerChoice] = 'X';
  50.   }
  51.   else {
  52.     Serial.println("Spot already contains: " + board[playerChoice]);
  53.     delay(2000);
  54.     playerPlaceX();
  55.   }
  56. }
  57.  
  58. void HEROPlaceO() {
  59.   randomSeed(analogRead(0));
  60.   int AIChoice = random(9);
  61.   if(isAvailable(AIChoice)){
  62.     board[AIChoice] = 'O';
  63.     Serial.println("HERO Placed 0 in board spot " + String(AIChoice));
  64.   }
  65.   else {
  66.     HEROPlaceO();
  67.   }
  68. }
  69.  
  70. boolean crossCheck(String a, String b, String c){
  71.   if(a.equals(b) && b.equals(c)){
  72.     if(c.equals("")){
  73.       return false;
  74.     }
  75.     return true;
  76.   }
  77.   else {
  78.     return false;
  79.   }
  80. }
  81.  
  82. boolean boardFull() {
  83.   for(int i = 0; i < 8; i++){
  84.     if(board[i].equals("")) {
  85.       return false;
  86.     }
  87.   }
  88.   return true;
  89. }
  90.  
  91. boolean checkIfOver() {
  92.   if(crossCheck(board[0], board[3], board[6])
  93.   || crossCheck(board[0], board[1], board[2])
  94.   || crossCheck(board[0], board[4], board[8])) { //case 1-3
  95.     Serial.println(board[0] + " wins!");
  96.     return true;
  97.   }
  98.   else if(crossCheck(board[4], board[3], board[5])
  99.   || crossCheck(board[4], board[1], board[7])
  100.   || crossCheck(board[4], board[2], board[6])) { //case 4-6
  101.     Serial.println(board[4] + " wins!");
  102.     return true;
  103.   }
  104.   else if(crossCheck(board[8], board[6], board[7])
  105.   || crossCheck(board[8], board[2], board[5])) { //case 7-8
  106.     Serial.println(board[8] + " wins!");
  107.     return true;
  108.   }
  109.   else if(boardFull()) {
  110.     Serial.println("A draw has occured.");
  111.     return true;
  112.   }
  113.   else {
  114.     return false;
  115.   }
  116. }
  117.  
Tags: cogsworth
Add Comment
Please, Sign In to add comment