//Hector Leal //Tic Tac Toe program //Programming Challenge # 8.6 #include #include #include using namespace std; const int COLUMN = 3;//creates 3 colums const int ROW = 3;//creates 3 rows char table[COLUMN][ROW] = {{'*','*','*'}, {'*','*','*'}, {'*','*','*'}}; //2D array with 3 rows and 3 columns and initializes each with * void showTable();//function to show rows and columns for tic tac toe void getX();//function to get location for X from player 1 void getO();//function to get location for O from player 2 void winnerX();//function to find if player one is winner void winnerO();//function to find if player two is winner bool repeat=false;//used to terminate loop if winner is found or tie game int main() { showTable(); cout << "Welcome to my Tic-Tac-Toe Game.\n"; cout << "You will be asked to enter the column and row for your letter.\n"; cout << endl; int loopcount = 0;//int to count loops for tie while (repeat==false)//repeats loop until tie or winner is found { loopcount++;//adds one to loop each time loop repeats getX();//get location for x from function showTable();//function to show user table winnerX();//checks to see if player wins if (repeat==true)//if x wins, break loop and terminate program break; if (loopcount ==5)//if tie, users have used all iterations, break loop { cout << "IT'S A TIE GAME!\n"; break; } getO();//function gets location for O showTable();//shows table to user winnerO();//checks to see if O is winner } cout << "Thank you for playing!\n"; return 0; } //******************************************************************************** //function to show rows and columns for tic tac toe table void showTable() { for (int row =0; row < ROW; row++) { for (int col =0; col < COLUMN; col++) { cout << setw(2) << table[col][row]; } cout << endl; } } //********************************************************************************* //function to get input from player one with location for X void getX() { int positionRow=0; int positionCol=0; bool test=true; while (test==true)//loops while test is true { cout << "Player One: Enter column for your X: "; cin >> positionCol; while (positionCol < 1 || positionCol > 3) { cout <<"Input is invalid, please enter number from 1-3: "; cin >> positionCol; } cout << "Player One: Enter row for your X: "; cin >> positionRow; while (positionRow < 1 || positionRow > 3) { cout <<"Input is invalid, please enter number from 1-3: "; cin >> positionRow; } if (table[positionCol-1][positionRow-1] == '*') { table[positionCol-1][positionRow-1] = 'X'; test = false;//terminates loop if entry is not repeating } else { cout <<"Input is invalid, space is already occupied, try again!\n"; test = true;//if entry is taken, function repeats } } } //********************************************************************************** //function to get input from player one with location for O void getO() { int positionRow=0; int positionCol=0; bool test=true; while (test==true) { cout << "Player Two: Enter column for your O: "; cin >> positionCol; while (positionCol < 1 || positionCol > 3) { cout <<"Input is invalid, please enter number from 1-3: "; cin >> positionCol; } cout << "Player Two: Enter row for your O: "; cin >> positionRow; while (positionRow < 1 || positionRow > 3) { cout <<"Input is invalid, please enter number from 1-3: "; cin >> positionRow; } if (table[positionCol-1][positionRow-1] == '*') { table[positionCol-1][positionRow-1] = 'O'; test = false; } else { cout <<"Input is invalid, space is already occupied, try again!\n"; test = true; } } } //***************************************************************************** //function to check if X is winner void winnerX() { if (table[0][0]=='X' && table [0][1] =='X' && table [0][2] =='X') { cout << "Player One is a Winner!\n"; repeat = true; } if (table[1][0]=='X' && table [1][1] =='X' && table [1][2] =='X') { cout << "Player One is a Winner!\n"; repeat = true; } if (table[2][0]=='X' && table [2][1] =='X' && table [2][2] =='X') { cout << "Player One is a Winner!\n"; repeat = true; } if (table[0][0]=='X' && table [1][0] =='X' && table [2][0] =='X') { cout << "Player One is a Winner!\n"; repeat = true; } if (table[0][1]=='X' && table [1][1] =='X' && table [2][1] =='X') { cout << "Player One is a Winner!\n"; repeat = true; } if (table[0][2]=='X' && table [1][2] =='X' && table [2][2] =='X') { cout << "Player One is a Winner!\n"; repeat = true; } if (table[0][0]=='X' && table [1][1] =='X' && table [2][2] =='X') { cout << "Player One is a Winner!\n"; repeat = true; } if (table[0][2]=='X' && table [1][1] =='X' && table [2][0] =='X') { cout << "Player One is a Winner!\n"; repeat = true; } } //******************************************************************************* //function to check if O is winner void winnerO() { if (table[0][0]=='O' && table [0][1] =='O' && table [0][2] =='O') { cout << "Player Two is a Winner!\n"; repeat = true; } if (table[1][0]=='O' && table [1][1] =='O' && table [1][2] =='O') { cout << "Player Two is a Winner!\n"; repeat = true; } if (table[2][0]=='O' && table [2][1] =='O' && table [2][2] =='O') { cout << "Player Two is a Winner!\n"; repeat = true; } if (table[0][0]=='O' && table [1][0] =='O' && table [2][0] =='O') { cout << "Player Two is a Winner!\n"; repeat = true; } if (table[0][1]=='O' && table [1][1] =='O' && table [2][1] =='O') { cout << "Player Two is a Winner!\n"; repeat = true; } if (table[0][2]=='O' && table [1][2] =='O' && table [2][2] =='O') { cout << "Player Two is a Winner!\n"; repeat = true; } if (table[0][0]=='O' && table [1][1] =='O' && table [2][2] =='O') { cout << "Player Two is a Winner!\n"; repeat = true; } if (table[0][2]=='O' && table [1][1] =='O' && table [2][0] =='O') { cout << "Player Two is a Winner!\n"; repeat = true; } }