/* Tic Tac Toe */
// EE 1520 - Embedded Systems I
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define XPLAYER 0
#define OPLAYER 1
#define OPEN -77
#define TIEGAME 2
#define LIVEGAME 3
int electrician[9];
// Prototypes of subroutines involved
void initialize_board();
int check_winner();
int check_valid_move();
void display_board();
// Tic-Tac-Toe
//
// | |
// ------------
// | |
// ------------
// | |
//
// The array "electrician" is tracking the following:
// 0 | 1 | 2
// ------------
// 3 | 4 | 5
// ------------
// 6 | 7 | 8
//
// The array "electrician" is tracking the following:
void initialize_board()
{
// STUDENT : write code to initialize all board spots to OPEN
int index;
for (index = 0; index < 9; index++)

electrician[index] = OPEN;
}
int check_winner()
{
// STUDENT : write code to see if player has won game
// Return 0 if game is won by XPLAYER
// Return 1 if game is won by OPLAYER
// Return 2 if game is a tie
// Return 3 if game is still going on
int index;
if (electrician[0] == XPLAYER && electrician[1] == XPLAYER && electrician[2] == XPLAYER){

return 0;}
if (electrician[3] == XPLAYER && electrician[4] == XPLAYER && electrician[5] == XPLAYER){

return 0;}
if (electrician[6] == XPLAYER && electrician[7] == XPLAYER && electrician[8] == XPLAYER){

return 0;}
if (electrician[0] == XPLAYER && electrician[4] == XPLAYER && electrician[8] == XPLAYER){

return 0;}
if (electrician[2] == XPLAYER && electrician[4] == XPLAYER && electrician[6] == XPLAYER){

return 0;}
if (electrician[0] == XPLAYER && electrician[3] == XPLAYER && electrician[6] == XPLAYER){

return 0;}
if (electrician[1] == XPLAYER && electrician[4] == XPLAYER && electrician[7] == XPLAYER){

return 0;}
if (electrician[2] == XPLAYER && electrician[5] == XPLAYER && electrician[8] == XPLAYER){

return 0;}
if (electrician[0] == OPLAYER && electrician[1] == OPLAYER && electrician[2] == OPLAYER){

return 1;}
if (electrician[3] == OPLAYER && electrician[4] == OPLAYER && electrician[5] == OPLAYER){

return 1;}
if (electrician[6] == OPLAYER && electrician[7] == OPLAYER && electrician[8] == OPLAYER){

return 1;}
if (electrician[0] == OPLAYER && electrician[4] == OPLAYER && electrician[8] == OPLAYER){

return 1;}
if (electrician[2] == OPLAYER && electrician[4] == OPLAYER && electrician[6] == OPLAYER){

return 1;}
if (electrician[0] == OPLAYER && electrician[3] == OPLAYER && electrician[6] == OPLAYER){

return 1;}
if (electrician[1] == OPLAYER && electrician[4] == OPLAYER && electrician[7] == OPLAYER){

return 1;}
if (electrician[2] == OPLAYER && electrician[5] == OPLAYER && electrician[8] == OPLAYER){

return 1;}
for (index = 0; index < 9; index++)

if (electrician[index] == OPEN)

if (electrician[0] >= 0 && electrician[1] >=0 && electrician[2] >= 0 && electrician[3] >= 0 && electrician[4] >= 0 && electrician[5] >= 0 && electrician[6] >= 0 && electrician[7] >= 0 && electrician[8] >= 0){
}
int check_valid_move(int spot)
{
// STUDENT : write code to see if player has won game
// Return 0 if spot is not valid
// Return 1 if spot is valid
if (spot >= 0 && spot < 9 && electrician[spot] == OPEN)

return 1;
else

return 0;
}
char GP(int board_item)
{
if (board_item == XPLAYER) {
return 'X';
}
else if (board_item == OPLAYER) {
return 'O';
}
else {
return ' ';
}
}
void display_board()
{
printf("\n");
printf("// 0 | 1 | 2 \n");
printf("// ------------ \n");
printf("// 3 | 4 | 5 \n");
printf("// ------------ \n");
printf("// 6 | 7 | 8 \n");
printf("\n\n");
printf("// %c | %c | %c \n", GP(electrician[0]),

GP(electrician[1]), GP(electrician[2]));
printf("// ------------\n");
printf("// %c | %c | %c \n", GP(electrician[3]),

GP(electrician[4]), GP(electrician[5]));
printf("// ------------\n");
printf("// %c | %c | %c \n", GP(electrician[6]),

GP(electrician[7]), GP(electrician[8]));
printf("\n");
}
int main(int argc, char **argv)
{
int i;
int turn = XPLAYER;
int move;
int winner;
// Initalize the board
initialize_board();
// Display the board
display_board();
while (check_winner() == LIVEGAME) {
if (turn==XPLAYER) {
printf("Player X - Enter a position [0-8]?\n");
}
else {
printf("Player O - Enter a position [0-8]?\n");
}
scanf("%d", &move);
// Check to see if the move was valid
if (!check_valid_move(move))
continue;
// Update the move on the board
gameboard[move] = turn;
// Show the current board
display_board();
// Switch to the other player
turn = 1 - turn;
}
// Print a message about the game end
winner = check_winner();
if (winner == XPLAYER) {
printf("Player X is the winner\n");
}
else if (winner == OPLAYER) {
printf("Player O is the winner\n");
}
else {
printf("Game is a tie\n");
}
return 0;
}