Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //TIC TAC TOEE
- #include <iostream>
- #include <math.h>
- #include <stdio.h>
- using namespace std;
- //includes
- void printTable (string[3][3]); //Initialize the function to print the table
- int
- main ()
- {
- int v = 0, i = 0, x = 0, w = 0, column, row; //Initialize the variables needed
- string table[3][3] = { " ", " ", " ", " ", " ", " ", " ", " ", " " }; //initialize the array 2D and each element
- printTable (table); //call the function
- for (int turn = 0; turn < 9; turn++)
- { //for statement that repeats 9 times
- cout << "TURN: "; //shows the user who's turn it is
- if (turn % 2 == 0)
- { //checks who's turn it is
- cout << "Player 1" << endl; //displays that it is player one's turn
- }
- else
- {
- cout << "Player 2" << endl; //displays that it is player two's turn
- }
- while (x == 0)
- { //while statement checks for valid information
- while (i == 0)
- { //cheks for valid selection of row
- cout << "Enter the row of your selection" << endl; //displays message
- cin >> row; //gets nuber of row from player
- if (0 < row and row < 4)
- { //if selected number meets the requirements
- row--; //subtract one to match the array position
- i = 1; //breaks while statement
- }
- else
- { //if selected number doesnt meet the requirements
- cout << "Invalid!" << endl; //display text and repet while statement
- }
- }
- while (v == 0)
- { //checks for valid selection of column
- cout << "Enter the column of your selection" << endl; //displays message
- cin >> column; //gets nuber of column from player
- if (0 < column and column < 4)
- { //if selected number meets the requirements
- column--; //subtract one to match the array position
- v = 1; //breaks while statement
- }
- else
- { //if selected number doesn't meet the requirements
- cout << "Invalid!" << endl; //display text and repeat while statement
- }
- }
- if (table[row][column] == " ")
- { //Checks that the position selected is valid
- if (turn % 2 == 0)
- { //checks for who's turn it is
- table[row][column] = " X "; //player one's turn saves an X in the position to store information
- x = 1; //breaks validation while statement
- }
- else
- {
- table[row][column] = " O "; //player two turn saves an O in the position
- x = 1; //breaks validation while statement
- }
- }
- else
- { //if the position is not avalible
- cout << "INVALID. This box has already been selected." << endl; //display text
- cout << "Please enter a valid location NOW." << endl; //display text
- printTable (table); //function that displays the table
- v = 0; //resets variables for the while statement
- i = 0; //resets variables for the while statement
- x = 0; //resets variables for the while statement
- }
- }
- printTable (table); //function that displays the table
- if (table[row][column] == table[row][0] and table[row][column] ==
- table[row][1] and table[row][column] == table[row][2])
- {
- //cheks for winner in the row that was just selected
- cout << "WiNnEr!" << endl; //displays text
- if (turn % 2 == 0)
- { //cheks for whos turn is it
- cout << "Congratulations player 1, you are decent!" << endl; //display text for player one
- w = 1; //declares that there is a winner
- break; //breaks all the statements and ends the code
- }
- else
- {
- cout << "Congratulations player 2, you are amazing!" << endl; //displays text for player two
- w = 1; //declares that there is a winner
- break; //breaks all the statements and ends the code
- }
- }
- if (table[row][column] == table[0][column] and table[row][column] ==
- table[1][column] and table[row][column] == table[2][column])
- {
- //cheks for winner in the column that was just selected
- cout << "Weiner!" << endl; //displays text
- if (turn % 2 == 0)
- { //cheks for whos turn is it
- cout << "Congratulations player 1, you are the ultimate warrior!" << endl; //display text for player one
- w = 1; //declares that there is a winner
- break; //breaks all the statements and ends the code
- }
- else
- {
- cout << "Congratulations player 2, you are a sasquatch!" << endl; //displays text for player two
- w = 1; //declares that there is a winner
- break; //breaks all the statements and ends the code
- }
- }
- if (turn > 3)
- {
- if (table[0][0] == table[1][1] and table[1][1] == table[2][2])
- { //cheks for winner in an / patern
- cout << "WINNER!" << endl; //displays text
- if (turn % 2 == 0)
- { //cheks for whos turn is it
- cout << "Congratulations player 1, you are an actual baller!" << endl; //display text for player one
- w = 1; //declares that there is a winner
- break; //breaks all the statements and ends the code
- }
- else
- {
- cout << "Congratulations player 2, you are the GOAT!" << endl; //displays text for player two
- w = 1; //declares that there is a winner
- break; //breaks all the statements and ends the code
- }
- }
- if (table[0][2] == table[1][1] and table[1][1] == table[2][0])
- { //cheks for winner in an \ patern
- cout << "WIN, You are the ultimate champion!" << endl; //displays text
- if (turn % 2 == 0)
- { //cheks for whos turn is it
- cout << "Congratulations player 1, you are a fantastic player!" << endl; //display text for player one
- w = 1; //declares that there is a winner
- break; //breaks all the statements and ends the code
- }
- else
- {
- cout << "Congratulations player 2, you are mediocre!" << endl; //displays text for player two
- w = 1; //declares that there is a winner
- break; //breaks all the statements and ends the code
- }
- }
- }
- v = 0; //resets the variables for the while statement
- i = 0; //resets the variables for the while statement
- x = 0; //resets the variables for the while statement
- }
- if (w == 0)
- { //if there is no winner after all 9 turns
- cout << "Nobody wins, you all are losers." << endl; //display text
- }
- return 0;
- }
- void
- printTable (string table[][3])
- { //void function that displays the table
- cout << table[0][0] << "|" << table[0][1] << "|" << table[0][2] << endl; //displays the fist row of the array
- cout << "---|---|---" << endl; //displays lines that separate the rows
- cout << table[1][0] << "|" << table[1][1] << "|" << table[1][2] << endl; //displays the second row of the table
- cout << "---|---|---" << endl; //displays lines that separate the rows
- cout << table[2][0] << "|" << table[2][1] << "|" << table[2][2] << endl; //displays the third row of the table
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement