//Project15_3_AC.cpp //Andy Cobaugh //10-3-02 //Input: The user's next move and whether they want to be X or O //Output: Progress of the game #include #include //clear screen #include"XSleep.h" //XSleep delay #include"matrix.h" //matrix class #include //required for srand #include //getch() function void print_directions();//function to print directions int main() { print_directions(); matrix board(3, 3); //3 x 3 matrix to store the tic-tac-toe board int i = 0; //loop variable int j; //user selection variable int r; //computer's numerical selection variable (randomized) int x = 0; //used to tell computer that the computer/user has made a valid //decision on which space to choose char user_xo; //user's character char computer_xo; //computer's character char user_select; //user's space selection variable char win; //the winner's character is stored here //ask user if he/she wants to be X's or O's cout<<"Do you wish to be 1)X or 2)O ?"<>j; //if user wants to be X's if (j == 1) { user_xo = 'X'; computer_xo = 'O'; } //otherwise user will be O's (also if something other than 1 or 2 is entered) else { user_xo = 'O'; computer_xo = 'X'; } //initialize the playing board with position markers board[0][0] = 'a'; board[1][0] = 'b'; board[2][0] = 'c'; board[0][1] = 'd'; board[1][1] = 'e'; board[2][1] = 'f'; board[0][2] = 'g'; board[1][2] = 'h'; board[2][2] = 'i'; //loop while there is not a winner do { system("CLS"); //loop to display the playing board for (i = 0; i <= 2; i++) { cout<<' '<>user_select; if (user_select == 'a' && ((board[0][0] != 'X') && (board[0][0] != 'O'))) { board[0][0] = user_xo; break; } if (user_select == 'b' && ((board[1][0] != 'X') && (board[1][0] != 'O'))) { board[1][0] = user_xo; break; } if (user_select == 'c' && ((board[2][0] != 'X') && (board[2][0] != 'O'))) { board[2][0] = user_xo; break; } if (user_select == 'd' && ((board[0][1] != 'X') && (board[0][1] != 'O'))) { board[0][1] = user_xo; break; } if (user_select == 'e' && ((board[1][1] != 'X') && (board[1][1] != 'O'))) { board[1][1] = user_xo; break; } if (user_select == 'f' && ((board[2][1] != 'X') && (board[2][1] != 'O'))) { board[2][1] = user_xo; break; } if (user_select == 'g' && ((board[0][2] != 'X') && (board[0][2] != 'O'))) { board[0][2] = user_xo; break; } if (user_select == 'h' && ((board[1][2] != 'X') && (board[1][2] != 'O'))) { board[1][2] = user_xo; break; } if (user_select == 'i' && ((board[2][2] != 'X') && (board[2][2] != 'O'))) { board[2][2] = user_xo; break; } }while (1); //look to see if X wins if (board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X') { win = 'X'; break; } if (board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X') { win = 'X'; break; } if (board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X') { win = 'X'; break; } if (board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X') { win = 'X'; break; } if (board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X') { win = 'X'; break; } if (board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X') { win = 'X'; break; } if (board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X') { win = 'X'; break; } if (board[2][0] == 'X' && board[1][1] == 'X' && board[0][2] == 'X') { win = 'X'; break; } //look to see if O wins if (board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O') { win = 'O'; break; } if (board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O') { win = 'O'; break; } if (board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O') { win = 'O'; break; } if (board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O') { win = 'O'; break; } if (board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O') { win = 'O'; break; } if (board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O') { win = 'O'; break; } if (board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O') { win = 'O'; break; } if (board[2][0] == 'O' && board[1][1] == 'O' && board[0][2] == 'O') { win = 'O'; break; } system("CLS"); //display the new board while computer "makes a decision" for (i = 0; i <= 2; i++) { cout<<' '<