Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define PLAYER_MOVE 1
- #define COMPUTER_MOVE 2
- void setup()
- {
- Serial.begin(9600);
- Serial.print("Write you chooses using the format ");
- Serial.println("'line column' (in numbers) without \" ' \" ");
- }
- void loop()
- {
- while(Serial.available() > 0)
- {
- char ch = Serial.read();
- receiveChoose(ch);
- }
- }
- void receiveChoose(char ch)
- {
- static int line = 0;
- static int column = 0;
- static int param = 0;
- int n;
- if(ch == ' ')
- {
- return;
- }
- param ++;
- if(param == 3)
- {
- param = 1;
- }
- n = (int)(ch - '0');
- if(n < 1 || n > 3)
- {
- line = 0;
- column = 0;
- Serial.println("Index out of bounds!");
- return;
- }
- if(!line && param == 1)
- {
- line = n;
- }
- else if(line != 0 && param == 2)
- {
- column = n;
- playPlayer(line - 1, column - 1);
- line = 0;
- column = 0;
- }
- }
- void playPlayer(int line, int column)
- {
- static int board[3][3] = {
- {0, 0, 0},
- {0, 0, 0},
- {0, 0, 0}
- };
- if(board[line][column] != 0)
- {
- Serial.println("Already used!");
- return;
- }
- board[line][column] = PLAYER_MOVE;
- if(findWinner(board))
- {
- Serial.println("Player is the winner!");
- }
- printBoard(board);
- playComputer(board);
- }
- void playComputer(int board[3][3])
- {
- if(!chooseNextMove(board))
- {
- computerStrategicMove(board);
- }
- if(findWinner(board))
- {
- Serial.println("Computer is the winner!");
- }
- printBoard(board);
- }
- bool chooseNextMove(int board[3][3])
- {
- int x, y;
- for(x = 0; x < 3; x++)
- {
- for(y = 0; y < 3; y++)
- {
- if(!board[x][y])
- {
- board[x][y] = COMPUTER_MOVE;
- if(findWinner(board))
- {
- return true;
- }
- board[x][y] = PLAYER_MOVE;
- if(findWinner(board))
- {
- board[x][y] = COMPUTER_MOVE;
- return true;
- }
- board[x][y] = 0;
- }
- }
- }
- return false;
- }
- void computerStrategicMove(int board[3][3])
- {
- bool found;
- int x, y;
- //center
- if(!board[1][1])
- {
- board[1][1] = COMPUTER_MOVE;
- return;
- }
- //diagonal
- found = false;
- for(x = 0; x < 3; x+=2)
- {
- for(y = 0; y < 3; y+=2)
- {
- if(!board[x][y])
- {
- found = true;
- break;
- }
- }
- if(found)
- {
- break;
- }
- }
- if(found)
- {
- board[x][y] = COMPUTER_MOVE;
- return;
- }
- //mid lines
- for(y = 0; y < 3; y+=2)
- {
- if(!board[1][y])
- {
- x = 1;
- found = true;
- break;
- }
- }
- if(!found)
- {
- for(x = 0; x < 3; x+=2)
- {
- if(!board[x][1])
- {
- y = 1;
- found = true;
- break;
- }
- }
- }
- if(found)
- {
- board[x][y] = COMPUTER_MOVE;
- return;
- }
- }
- bool findWinner(const int board[3][3])
- {
- int a;
- for(a = 0; a < 3; a++)
- {
- if(!board[a][0])
- {
- continue;
- }
- if( board[a][0] == board[a][1] &&
- board[a][0] == board[a][2])
- {
- return true;
- }
- }
- //
- for(a = 0; a < 3; a++)
- {
- if(!board[0][a])
- {
- continue;
- }
- if( board[0][a] == board[1][a] &&
- board[0][a] == board[2][a])
- {
- return true;
- }
- }
- //
- if( board[0][0] == board[1][1] &&
- board[0][0] == board[2][2] && board[0][0] != 0)
- {
- return true;
- }
- if( board[0][2] == board[1][1] &&
- board[0][2] == board[2][0] && board[0][2] != 0)
- {
- return true;
- }
- return false;
- }
- void printBoard(const int board[3][3])
- {
- int x, y;
- Serial.println(" ");
- Serial.println(" ");
- for(x = 0; x < 3; x++)
- {
- for(y = 0; y < 3; y++)
- {
- Serial.print(board[x][y]);
- if(y < 2)
- {
- Serial.print(" | ");
- }
- }
- Serial.println(" ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement