1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
#include <iostream>
#include <cstdlib>
using namespace std;
void board();
int CheckGame();
int PlayAgain();
int ClearBoard();
bool draw = false;
bool RunningGame = true;
char square[9] = {'1','2','3','4','5','6','7','8','9'};
int mark;
char answer;
int main()
{
ClearBoard();
do{
while(RunningGame == true)
{
board();
cout<<"Player 1 choose a number: ";
cin>>mark;
while(mark!=1 && mark!=2 && mark!=3 && mark!=4 && mark!=5 && mark!=6 && mark!=7 && mark!=8 && mark!=9)//Works for numbers, but not for letters
{
cout<<"Wrong input. Try again: ";
cin>>mark;
}
square[--mark]='X';
system("CLS");
CheckGame();
if(RunningGame == false && draw == false)
{
system("CLS");
cout<<"Player 1 won!"<<endl;
break;
}
board();
cout<<"Player 2 choose a number: ";
cin>>mark;
while(mark!=1 && mark!=2 && mark!=3 && mark!=4 && mark!=5 && mark!=6 && mark!=7 && mark!=8 && mark!=9)//Works for numbers, but not for letters
{
cout<<"Wrong input. Try again: ";
cin>>mark;
}
square[--mark]='O';
system("CLS");
CheckGame();
if(RunningGame == false && draw == false)
{
system("CLS");
cout<<"Player 2 won!"<<endl;
break;
}
CheckGame();
if(RunningGame == false && draw == true)//Doesn't work
{
system("CLS");
cout<<"The game ended in a draw!"<<endl;
break;
}
}
PlayAgain();
}while(RunningGame == true);
return 0;
}
void board()
{
system("cls");
cout << "\n\n\t\t Tic Tac Toe\n\n";
cout << "\t Player 1 (X) - Player 2 (O)" << endl << endl;
cout << endl;
cout << "\t\t | | " << endl;
cout << "\t\t " << square[0] << " | " << square[1] << " | " << square[2] << endl;
cout << "\t\t_____|_____|_____" << endl;
cout << "\t\t | | " << endl;
cout << "\t\t " << square[3] << " | " << square[4] << " | " << square[5] << endl;
cout << "\t\t_____|_____|_____" << endl;
cout << "\t\t | | " << endl;
cout << "\t\t " << square[6] << " | " << square[7] << " | " << square[8] << endl;
cout << "\t\t | | " << endl << endl;
}
int CheckGame()
{
if (square[0] == square[1] && square[1] == square[2])
RunningGame = false;
else if (square[3] == square[4] && square[4] == square[5])
RunningGame = false;
else if (square[6] == square[7] && square[7] == square[8])
RunningGame = false;
else if (square[0] == square[3] && square[3] == square[6])
RunningGame = false;
else if (square[3] == square[4] && square[4] == square[7])
RunningGame = false;
else if (square[2] == square[5] && square[5] == square[8])
RunningGame = false;
else if (square[0] == square[4] && square[4] == square[8])
RunningGame = false;
else if (square[2] == square[4] && square[4] == square[6])
RunningGame = false;
else if (square[0] != '1' && square[1] != '2' && square[2] != '3' && square[3] != '4' && square[4] != '5' && square[5] != '6'
&& square[6] != '7' && square[7] != '8' && square[8] != '9')
{
draw = true;
RunningGame = false;
}
}
int PlayAgain()
{
cout<<"Would you like to play again?(Y/N)"<<endl;
cin>>answer;
while(answer != 'Y' && answer != 'N')
{
system("Cls");
cout<<"Wrong input. Please type \"Y\" or \"N\"."<<endl;
cin>>answer;
if(answer=='Y')
{
system("CLS");
ClearBoard();
RunningGame = true;
}
else if(answer=='N')
{
break;
system("Cls");
}
}
if(answer=='Y')
{
system("CLS");
ClearBoard();
RunningGame = true;
}
else if(answer=='N')
{
system("Cls");
return 0;
}
}
int ClearBoard()
{
char square[9] = {'1','2','3','4','5','6','7','8','9'};//This doesnt do anything, i thought it would give back the original elements to the array
}
|