Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // tictactoe.txt - Purpose: To be able to play tic tac toe against a computer opponent.
- // Scott Hancock
- // CISP 360
- // November 6, 2015
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- int freeSpaces = 9;
- int turn;
- char board[3][3] =
- {
- {'1','2','3'},
- {'4','5','6'},
- {'7','8','9'}
- };
- //////////////////
- //Display Board//
- /////////////////
- void displayBoard()
- {
- cout << " _________________" << endl;
- cout << "| | | |" << endl;
- cout << "| " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] <<" |" << endl;
- cout << "|_____|_____|_____|" << endl;
- cout << "| | | |" << endl;
- cout << "| " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] <<" |" << endl;
- cout << "|_____|_____|_____|" << endl;
- cout << "| | | |" << endl;
- cout << "| " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] <<" |" << endl;
- cout << "|_____|_____|_____|" << endl;
- }
- ///////////////////
- //Makes game move//
- ///////////////////
- void gameMove()
- {
- if (turn == 1)
- {
- char move;
- cout << endl << "Where would you like to make your next move?" << endl;
- cin >> move;
- //Check space and set array to the move space
- for (int i= 0; i < 3; i++)
- for (int j = 0; j < 3; j++)
- {
- if (board[i][j] == move)
- {
- board[i][j] = 'X';
- cout << "Move is okay." << endl;
- turn = turn + 1;
- freeSpaces = freeSpaces - 1;
- displayBoard();
- }
- }
- }
- //////////////////
- //Computers turn//
- //////////////////
- else
- {
- int compMove;
- char newMove;
- srand( time(NULL) );
- compMove = rand() % 9 + 1;
- newMove = compMove + '0';
- //Check space and set array to the move space
- for (int i = 0; i < 3; i++)
- for (int j = 0; j < 3; j++)
- {
- if (board[i][j] == newMove)
- {
- board[i][j] = 'O';
- turn = turn - 1;
- freeSpaces = freeSpaces - 1;
- cout << endl << "The computer is making its move." << endl;
- displayBoard();
- }
- }
- }
- }
- /////////////
- //Start Up//
- void startUp()
- {
- cout << "This is a Tic Tac Toe Game. Choose the number for the space you want to take." << endl;
- //Choose Starting player
- cout << "Randomizing starting player." << endl;
- srand( time(NULL) );
- turn = rand() % 2 + 1;
- if(turn == 1)
- {
- cout << "Player " << turn << " goes first. (Human Player or X)" << endl;
- }
- else
- {
- cout << "Player " << turn << " goes first. (Computer or O)" << endl;
- }
- }
- ///////////////////////////
- //Check for winner or tie//
- ///////////////////////////
- bool checkWin()
- {
- char winner = 'N';
- for(int x = 0; x < 3; x++)
- {
- winner = board[x][0];
- for(int y = 0; y < 3; y++)
- {
- if(board[x][y] != winner){
- winner = 'N';
- break;
- }
- }
- if(winner != 'N')
- {
- cout << "Winner is " << winner << endl;
- return true;
- }
- }
- for(int x = 0; x < 3; x++)
- {
- winner = board[0][x];
- for(int y = 0; y < 3; y++)
- {
- if(board[y][x] != winner)
- {
- winner = 'N';
- break;
- }
- }
- if(winner != 'N')
- {
- cout << "Winner is " << winner << endl;
- return true;
- }
- }
- //Check Diagonals
- if((board[0][0] == board[1][1] && board[1][1] == board[2][2]) || (board[0][2] == board[1][1] && board[1][1] == board[2][0]))
- {
- cout << "Winner is " << board[1][1] << endl;
- return true;
- }
- while (freeSpaces !=0)
- {
- gameMove();
- return false;
- }
- if (freeSpaces == 0)
- {
- cout << "It is a tie!" << endl;
- return true;
- }
- return false;
- }
- int main()
- {
- bool gameOver = false;
- startUp();
- displayBoard();
- while(!gameOver)
- {
- gameOver = checkWin();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement