Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- using namespace std;
- // constant varible for 3 x 3 size of the tic tact toe board
- const int SIZE = 3;
- //*********************
- // Function for printing the board
- // Arguments have the array and the array size of the board and the size = 3 x 3
- //*********************
- void printBoard(char arr[][SIZE])
- {
- // nested for loop for the printing of the board
- for (int i = 0; i < SIZE; i++){
- for (int j = 0; j < SIZE; j++)
- {
- cout << arr[i][j] << '*';
- }
- cout << endl;
- }
- }
- //***************************************************
- // Finding the winner function
- // using a boolean data type in this function to find the winner
- // arguments have the
- //***************************************************
- bool isWin(char arr[][SIZE], char ch)
- {
- int count;
- // nested for loop for [i] and [j] winner X
- for (int i = 0; i < SIZE; i++)
- {
- count = 0;
- for (int j = 0; j < SIZE; j++)
- {
- if (arr[i][j] == ch)
- count++;
- }
- if (count == SIZE)
- return true;
- }
- // nested for loop for [j] and [i] winner O
- for (int i = 0; i < SIZE; i++)
- {
- count = 0;
- for (int j = 0; j < SIZE; j++)
- {
- if (arr[j][i] == ch)
- count++;
- }
- if (count == SIZE)
- return true;
- }
- // nested for loop for determining if the user input is already taken
- count = 0;
- for (int i = 0; i < SIZE; i++)
- {
- if (arr[i][i] == ch)
- count++;
- }
- if (count == SIZE)
- return true;
- count = 0;
- // j-- is an important part to determine if the users input is already taken
- for (int i = 0, j = SIZE - 1; i < SIZE; i++, j--)
- {
- if (arr[i][j] == ch)
- count++;
- }
- // return statement if the user inputs an already taken spot
- if (count == SIZE)
- return true;
- return false;
- }
- //**************************
- // Determining if the game is a tie function with bool
- // arguments char array and the array size
- //**************************
- bool gameDraw(char arr[][SIZE])
- {
- for (int i = 0; i < SIZE; i++)
- {
- for (int j = 0; j < SIZE; j++)
- {
- if (arr[i][j] == '-')
- return false; // returning if its a tie
- }
- }
- return true;
- }
- //*****************
- // Main function
- // creats the board
- // figuring out whos turn it is
- // Finding the winner or if its a tie
- //****************
- int main()
- {
- cout << "Tic Tac Toe Game" << endl;
- char board[SIZE][SIZE];
- for (int i = 0; i < SIZE; i++){
- for (int j = 0; j < SIZE; j++){
- board[i][j] = '-';
- }
- }
- // varibles for finding if the game has ended
- bool gameEnd = false;
- int row, col, playerTurn = 1;
- char player;
- // Boolean nested while if loop for ask user for choices
- // also saves these choices
- while (!gameEnd)
- {
- //if statements for determining whos turn it is
- printBoard(board);
- if (playerTurn == 1)
- {
- player = 'X';
- playerTurn = 2;
- }
- else if (playerTurn == 2)
- {
- player = 'O';
- playerTurn = 1;
- }
- do
- {
- //getting the user inputs
- cout << "Player " << player << " Turn" << endl;
- cout << "Enter a row: "; cin >> row;
- cout << "Enter a colums: "; cin >> col;
- row--;
- col--;
- }
- // then has a while statement that enetrs the inputs into the board
- while (!(row >= 0 && row < SIZE && col >= 0 && col < SIZE && board[row][col] == '-'));
- board[row][col] = player;
- // using the cins and putting them in the right row and colums
- if (isWin(board, player))
- {
- // gameEnd
- printBoard(board);
- cout << "Player " << player << " wins" << endl;
- gameEnd = true;
- }
- // else if loop for tie game
- else if (gameDraw(board))
- {
- printBoard(board);
- cout << "Game is a tie" << endl;
- gameEnd = true;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement