Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <unistd.h>
- using namespace std;
- bool Complete = false;
- bool PlayerTurn = true;
- bool LegalMove = false;
- bool IsStillPossibleToWin = false;
- string PlayerCheck;
- string Answer;
- string PlayerOne = "X";
- string PlayerTwo = "O";
- string PlayerThree = "Y";
- int PlayerWhoWon;
- vector<string> TicTacToe {"1", "2", "3", "4", "5", "6", "7", "8", "9"};
- bool PlayAgain() {
- cout << "Want to play again?\n";
- cin >> Answer;
- if (Answer == "yes" || Answer == "Yes" || Answer == "Y" || Answer == "y") {
- return true;
- } else if (Answer == "No" || Answer == "no" || Answer == "N" || Answer == "n") {
- return false;
- } else {
- PlayAgain();
- }
- }
- void Track() {
- for (int Repeats = 30; Repeats >= 0; Repeats--) {
- cout << "\n";
- }
- cout << " " << TicTacToe[0] << " | " << TicTacToe[1] << " | " << TicTacToe[2] << "\n";
- cout << "----------\n";
- sleep(0.25);
- cout << " " << TicTacToe[3] << " | " << TicTacToe[4] << " | " << TicTacToe[5] << "\n";
- sleep(0.25);
- cout << "----------\n";
- sleep(0.25);
- cout << " " << TicTacToe[6] << " | " << TicTacToe[7] << " | " << TicTacToe[8] << "\n";
- sleep(0.25);
- cout << "\n\n";
- }
- void Turn() {
- do {
- if (PlayerTurn) {
- cout << "X ";
- } else {
- cout << "O ";
- }
- cin >> Answer;
- if (Answer == "1" && TicTacToe[0] == "1") {
- LegalMove = true;
- TicTacToe[0] = PlayerCheck;
- } else if (Answer == "2" && TicTacToe[1] == "2") {
- LegalMove = true;
- TicTacToe[1] = PlayerCheck;
- } else if (Answer == "3" && TicTacToe[2] == "3") {
- LegalMove = true;
- TicTacToe[2] = PlayerCheck;
- } else if (Answer == "4" && TicTacToe[3] == "4") {
- LegalMove = true;
- TicTacToe[3] = PlayerCheck;
- } else if (Answer == "5" && TicTacToe[4] == "5") {
- LegalMove = true;
- TicTacToe[4] = PlayerCheck;
- } else if (Answer == "6" && TicTacToe[5] == "6") {
- LegalMove = true;
- TicTacToe[5] = PlayerCheck;
- } else if (Answer == "7" && TicTacToe[6] == "7") {
- LegalMove = true;
- TicTacToe[6] = PlayerCheck;
- } else if (Answer == "8" && TicTacToe[7] == "8") {
- LegalMove = true;
- TicTacToe[7] = PlayerCheck;
- } else if (Answer == "9" && TicTacToe[8] == "9") {
- LegalMove = true;
- TicTacToe[8] = PlayerCheck;
- }
- } while (LegalMove != true);
- LegalMove = false;
- if (TicTacToe[0] == TicTacToe[4] && TicTacToe[4] == TicTacToe[8] || TicTacToe[0] == TicTacToe[3] && TicTacToe[3] == TicTacToe[6]) {
- Complete = true;
- if (PlayerTurn) {
- PlayerWhoWon = 1;
- } else {
- PlayerWhoWon = 2;
- }
- } else if (TicTacToe[0] == TicTacToe[1] && TicTacToe[1] == TicTacToe[2] || TicTacToe[1] == TicTacToe[4] && TicTacToe[4] == TicTacToe[7]) {
- Complete = true;
- if (PlayerTurn) {
- PlayerWhoWon = 1;
- } else {
- PlayerWhoWon = 2;
- }
- } else if (TicTacToe[2] == TicTacToe[4] && TicTacToe[4] == TicTacToe[6] || TicTacToe[3] == TicTacToe[4] && TicTacToe[4] == TicTacToe[5]) {
- Complete = true;
- if (PlayerTurn) {
- PlayerWhoWon = 1;
- } else {
- PlayerWhoWon = 2;
- }
- } else if (TicTacToe[2] == TicTacToe[5] && TicTacToe[5] == TicTacToe[8] || TicTacToe[6] == TicTacToe[7] && TicTacToe[7] == TicTacToe[8]) {
- Complete = true;
- if (PlayerTurn) {
- PlayerWhoWon = 1;
- } else {
- PlayerWhoWon = 2;
- }
- } else {
- if (TicTacToe[0] == "1" || TicTacToe[1] == "2" || TicTacToe[2] == "3" || TicTacToe[3] == "4" ||TicTacToe[4] == "5" || TicTacToe[5] == "6" ||
- TicTacToe[6] == "7" || TicTacToe[7] == "8" || TicTacToe[8] == "9") {
- IsStillPossibleToWin = true;
- } else {
- IsStillPossibleToWin = false;
- }
- if (IsStillPossibleToWin == false) {
- Complete = true;
- PlayerWhoWon = 3;
- }
- }
- if (PlayerTurn) {
- PlayerTurn = false;
- } else {
- PlayerTurn = true;
- }
- }
- int main() {
- Track();
- do {
- if (PlayerTurn) {
- cout << "Its player ones turn.\n";
- PlayerCheck = PlayerOne;
- } else {
- cout << "Its player twos turn.\n";
- PlayerCheck = PlayerTwo;
- }
- Turn();
- Track();
- } while (Complete != true);
- if (PlayerWhoWon == 1) {
- cout << "Player one won!\n";
- } else if (PlayerWhoWon == 2) {
- cout << "Player two won!\n";
- } else {
- cout << "It was a tie!\n";
- }
- if (PlayAgain() == true) {
- TicTacToe[0] = "1";
- TicTacToe[1] = "2";
- TicTacToe[2] = "3";
- TicTacToe[3] = "4";
- TicTacToe[4] = "5";
- TicTacToe[5] = "6";
- TicTacToe[6] = "7";
- TicTacToe[7] = "8";
- TicTacToe[8] = "9";
- Complete = false;
- PlayerTurn = true;
- main();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement