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
|
#include <iostream>
using namespace std;
char square[3][3] = {'1', '2', '3', '4', '5' ,'6', '7', '8', '9'};
bool CheckWin(int a, int b);
void Board();
int Turn;
int move(int Turn, char Player);
int main()
{
int Turn;
int Valid = 0;
bool gamewon = false;
int charsplaced = 0;
char Player = 'O';
int a;
int b;
Board();
cout << "Above is how the various moves are identified" << endl;
system("pause");
for(int a = 0; a <= 3; a++)
{
for(int b = 0; b <= 3; b++)
square[a][b] = ' ';
}
Board();
while(!gamewon)
{
if (Valid != -1)
{
if(Player == 'O')
{
cout << "Your' move" << endl;
Player = 'X';
}
else
{
cout << "TEST";
Player = 'O';
}
}
while(true)
{
if(charsplaced == 9)
{
break;
}
cin >> Turn;
if(Turn <=9 && Turn >=1)
{
break;
}
else
{
cout << "\nPlease enter a value between 1 and 9: ";
if(cin.fail())
{
cin.clear();
char c;
cin >> c;
}
}
}
if(charsplaced == 9)
{
break;
}
Valid = move(Turn, Player);
if(Valid == -1)
cout << "That is an invalid move try again";
else
{
gamewon = CheckWin(a, b);
charsplaced ++;
system("cls");
Board();
}
}
system("cls");
if(charsplaced == 9)
{
cout << "Draw\n";
system("pause");
return 0;
}
if (Player == 'X')
{
cout << "You win";
}
else
{
cout << "I win";
}
system("pause");
return 0;
}
int move(int Turn, char Player)
{
if (Turn >=0 && Turn <= 9)
{
int a;
int b;
for (a = 0, b = 0; ((a*3)+(b+1)) != Turn; a++, b++)
if(square[a][b] == ' ')
{
square[a][b] = Player;
return a, b;
}
else
{
return -1;
}
}
}
bool CheckWin(int a, int b)
{
int vertical = 1;
int horizontal = 1;
int diagonal1 = 1;
int diagonal2 = 1;
char Player = square[a][b];
int i;
int ii;
for(i = a + 1; square[i][b] == Player && i <= 4; i++, vertical++);
for(i = a - 1; square[i][b] == Player && i >= 0; i--, vertical++);
if(vertical >= 3)
return true;
for(i = b - 1; square[a][i] == Player && i >= 0; i--, horizontal++);
for(i = b + 1; square[a][i] == Player && i <= 4; i++, horizontal++);
if(vertical >= 3)
return true;
for(i = a - 1, ii = b - 1; square[i][ii] == Player && i >= 0 && ii >=0; i--, ii--, diagonal1++);
for(i = a + 1, ii = b + 1; square[i][ii] == Player && i <= 4 && ii <=4; i++, ii++, diagonal1++);
if(diagonal1 >= 3)
return true;
for(i = a - 1, ii = b + 1; square[i][ii] == Player && i >= 0 && ii <=4; i--, ii++, diagonal2++);
for(i = a + 1, ii = b - 1; square[i][ii] == Player && i <= 4 && ii <=0; i++, ii--, diagonal2++);
if(diagonal2 >= 3)
return true;
return false;
}
void Board()
{
system("cls");
cout << "Player 1 (X) - Computer (O)" << endl << endl;
cout << endl;
cout << " | | " << endl;
cout << " " << square[0][0] << " | " << square[0][1] << " | " << square[0][2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << square[1][0] << " | " << square[1][1] << " | " << square[1][2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << square[1][0] << " | " << square[2][1] << " | " << square[2][2] << endl;
cout << " | | " << endl << endl;
}
|