Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- string player1 = "Player 1";
- string player2 = "Player 2";
- int player = 1;
- void gameboard();
- char space[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
- bool winCheck();
- void takeTurns(bool, int);
- void move(int);
- bool legalMove(int);
- int main()
- {
- cout << "Welcome to Devyn's Super Awesome Tic Tac Toe Game." << endl
- << "Please enter name for Player 1." << endl;
- cin >> player1;
- cout << "Please enter name for Player 2." << endl;
- cin >> player2;
- cout << endl;
- gameboard();
- while (!winCheck())
- {
- cout << "Player = " << player << endl;
- takeTurns(winCheck(), player);
- cout << "End of loop" << endl;
- cout << player << endl;
- }
- char end;
- cout << "Type any key, and press enter." << endl;
- cin >> end;
- return 0;
- }
- void gameboard()
- {
- cout << " " << space[0] << " | " << space[1] << " | " << space[2] << endl
- << " " << "--------" << endl
- << " " << space[3] << " | " << space[4] << " | " << space[5] << endl
- << " " << "--------" << endl
- << " " << space[6] << " | " << space[7] << " | " << space[8] << endl << endl;
- }
- bool winCheck()
- {
- if (space[0] == space[1] && space[2] == space[0])
- return true;
- if (space[3] == space[4] && space[5] == space[3])
- return true;
- if (space[6] == space[7] && space[8] == space[6])
- return true;
- if (space[0] == space[3] && space[6] == space[0])
- return true;
- if (space[1] == space[4] && space[7] == space[1])
- return true;
- if (space[2] == space[5] && space[8] == space[2])
- return true;
- if (space[0] == space[4] && space[8] == space[0])
- return true;
- if (space[6] == space[4] && space[2] == space[6])
- return true;
- else
- return false;
- }
- void takeTurns(bool winCheck, int player)
- {
- if (!winCheck)
- {
- if (player == 1)
- player = 0;
- else
- player = 1;
- }
- move(player);
- }
- void move(int player)
- {
- int box;
- if (player == 1)
- cout << "Enter move " << player1 << endl;
- else
- cout << "Enter move " << player2 << endl;
- cin >> box;
- if (legalMove(box))
- {
- if (player == 0)
- space[box-1] = 'X';
- else
- space[box-1] = 'O';
- }
- gameboard();
- }
- bool legalMove(int box)
- {
- if (space[box-1] == 'X' || space[box-1] == 'O')
- {
- cout << "You cannot place move on an already taken space." << endl;
- return false;
- }
- if (box > 9)
- {
- cout << "You must enter a number on the board." << endl;
- return false;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment