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 177 178 179 180 181 182 183 184 185 186 187 188
|
#include <iostream>
#include <limits>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
int main ()
{
int random_numb;
char array [9] = {'1','2','3','4','5','6','7','8','9'}; // location for X and 0's
bool check [9] = {0,0,0,0,0,0,0,0,0}; // to show which squares have been used
int n=0; // position in array corresponding to position in grid
int turn_num = 0; // to keep track of number of turns
bool win = 0; // for if game has been one changes to 1
srand((unsigned)time(0));
random_numb=rand() % 9;
string line = "-----+-----+-----"; //
string space = " | | "; // The constant building blocks of the grid as strings
string bit = " | "; //
cout << string(50, '\n'); //Gives some clear space
while (win ==0) // main while loop ends when game is won
{
//Printing the grid with array entries
//Have the grid this way round to reflect number pad
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n" << "\t\t\t\t" << space << '\n';
cout << "\t\t\t\t" << " " << array[6] << bit << array[7] << bit << array[8] << '\n';
cout << "\t\t\t\t" << space << '\n' << "\t\t\t\t" << line << '\n' << "\t\t\t\t" << space << '\n';
cout << "\t\t\t\t" << " " << array[3] << bit << array[4] << bit << array[5] << '\n';
cout << "\t\t\t\t" << space << '\n' << "\t\t\t\t" << line << '\n' << "\t\t\t\t" << space << '\n';
cout << "\t\t\t\t" << " " << array[0] << bit << array[1] << bit << array[2] << '\n';
cout << "\t\t\t\t" << space;
// check for win situation
if ((array[6]==array[7] && array[7]==array[8]) || (array[6]==array[3] && array[3]==array[0]))
{
win =1;
break;
}
else if ((array[0]==array[1] && array[1]==array[2]) || (array[2]==array[5] && array[5]==array[8]))
{
win =1;
break;
}
else if ((array[6]==array[4] && array[4]==array[2]) || (array[0]==array[4] && array[4]==array[8]))
{
win =1;
break;
}
else if ((array[7]==array[4] && array[4]==array[1]) || (array[3]==array[4] && array[4]==array[5]))
{
win =1;
break;
}
while ((cout << "\n\n\nWhere will you place your 'X'? Please select a number:\n")
&& (!(cin >> n)
|| n < 1 || n > 9
|| check[n-1]==1)) //checks for already used spaces
{
cout << "\nThat's NOT an acceptable number....try again.....HARDER!\n";
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
n--;
array [n] = 'X'; //placing an 'X' into the array
check [n] = 1; //showing that this square has been used
// move turn counter on
turn_num++;
// check for win situation
if ((array[6]==array[7] && array[7]==array[8]) || (array[6]==array[3] && array[3]==array[0]))
{
win =1;
break;
}
else if ((array[0]==array[1] && array[1]==array[2]) || (array[2]==array[5] && array[5]==array[8]))
{
win =1;
break;
}
else if ((array[6]==array[4] && array[4]==array[2]) || (array[0]==array[4] && array[4]==array[8]))
{
win =1;
break;
}
else if ((array[7]==array[4] && array[4]==array[1]) || (array[3]==array[4] && array[4]==array[5]))
{
win =1;
break;
}
//computers move
if (array[6]==array[7] || array[0]==array[4] || array[2]==array[5] && (check[8] == 0))
{
array[8]= 'O';
check[8]= 1;
}
else if (array[6]==array[3] || array[8]==array[4] || array[2]==array[1]&& (check[0] == 0))
{
array[0]= 'O';
check[0]=1;
}
else if (array[8]==array[7] || array[2]==array[4] || array[0]==array[3] && (check[6] == 0))
{
array[6]= 'O';
check[6]= 1;
}
else if (array[8]==array[5] || array[6]==array[4] || array[0]==array[1] && (check[2]==0))
{
array[2]= 'O';
check[2]= 1;
}
else if (array[8]==array[0] || array[2]==array[6] || array[7]==array[1] || array[3]==array[5] && (check[4]==0))
{
check[4]= 1;
array[4]= 'O';
}
else if (array[6]==array[0] || array[5]==array[4] && (check[3]==0))
{
array[3]='O';
check[3]= 1;
}
else if (array[0]==array[2] || array[7]==array[4] && (check[1]==0))
{
array[1]='O';
check[1]= 1;
}
else if (array[8]==array[2] || array[4]==array[3] && (check[5]==0))
{
array[5]='O';
check[5]= 1;
}
else if (array[8]==array[6] || array[4]==array[1] && (check[7]== 0))
{
array[7]='O';
check[7]= 1;
}
else
{
while (check[random_numb]==1) //if random number was already taken generates another and checks6
{
random_numb =rand()% 9;
}
array[random_numb]= 'O';
check[random_numb]=1;
}
// Move turn counter on again
turn_num++;
} //end of game loop finish
if (turn_num ==9 && win==0)
{
cout << "\n\nThis game is a draw.";
}
if (win==1 && (turn_num==5 || turn_num==7 || turn_num==9))
{
cout << "\n\n";
cout << "You win, well done, ... i suppose.\n\n";
}
if (win==1 && (turn_num==6 || turn_num==8))
{
cout << "\n\n";
cout << "I win, WA HA HAAAAA!!!!!\n";
}
return 0;
}
|