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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
#include <iostream>
#include <time.h>
#include <stdlib.h> /* srand, rand */
using namespace std;
void showboard(int takenplace[], char board[]); // to show the board and taken places to the player
int checkend(char board[]); // to check whether anybody has won
int checkdanger(char board[]); // for the AI to know when there is a potentially dangerous moment( ex. the opponent having two-in-a-row
// plz input 1 for the first player move to see the problem with this code
int main()
{
char board[10] = " O X "; // The board is made this way because this is the board that causes problems
int takenplace[9] = {1,2,3,4,0,6,7,0,9}; // Same here
int whowon = 0; // variable to show who won(1 = player, 2 = AI, 0 = draw)
int turns = 0; // Turns remembered in case of draw
cout << "Welcome to a game of TIC TAC TOE" << endl;
cout << "make a line of three 0s to win!" << endl;
cout << "Only type in numbers if you do not want the game to crash!" << endl;
int playermove = 0; // variable to store player's move
int compmove = 0; // variable to store AI's move
showboard(takenplace, board);
while(1)
{
//First player move
cout << "please pick a vacancy(number) to circle other than zero" << endl;
retryone: // where the code goes back to if player picks a wrong choice
cin >> playermove; // input player's move
if(takenplace[playermove-1] == 0)
{
cout << "please pick a place other than 0" << endl;;
goto retryone; // repick
}
else
{
cout << "please pick a number between 1 and 9 that is not zero";
goto retryone;
}
//Filling out the board
takenplace[playermove-1] = 0; // the taken place is replaced with a 0
board[playermove-1] = 'O'; // the board now has a 'O' circled
turns++;
showboard(takenplace, board);
if(checkend(board)) // checking to see if there is a 3-in-a-row
{
whowon = 1;
break;
}
else if(turns == 9) // checking to see if it is a draw
{
break;
}
//computer player move
compmove = checkdanger(board); // AI checking for imminent danger
// debugging
cout << board[0] << board[4] << board[8] << endl; // This is the part I don't understand
if(board[0] == board[4] && board[8] != ' ' && board[0] != ' ') // This is the code checking for a right-diagonal 'imminent danger' btw it's inside the checkdanger function
compmove = 9;
cout << compmove << endl; // Yet the AI move is still 0 instead of 9
system("pause"); // I have absolutely no clue why this happens... the others seem to work fine
//debugging
if(compmove == 0)
{
retrytwo:
srand (time(NULL)); // random seed initialize
compmove = rand() % 9 + 1; // pick a random number between 1 and 9
if(takenplace[compmove-1] == 0)
{
goto retrytwo; // retry if the move is taken
}
}
//Filling out the board
takenplace[compmove-1] = 0; // same as player
board[compmove-1] = 'X';
showboard(takenplace, board);
turns++;
if(checkend(board))
{
whowon = 2;
break;
}
}
if(whowon == 0)
{
cout << "It's a draw!" << endl;
}
else
{
cout << "Player " << whowon << " Has won!" << endl;
}
cin >> whowon;
}
void showboard(int takenplace[], char board[])
{
//drawing the board
cout << "This is the current tictactoe board" << endl << endl;
cout << "-------------" << endl;
cout << "| " << board[0] << " | " << board[1] << " | " << board[2] << " |" << endl;
cout << "-------------" << endl;
cout << "| " << board[3] << " | " << board[4] << " | " << board[5] << " |" << endl;
cout << "-------------" << endl;
cout << "| " << board[6] << " | " << board[7] << " | " << board[8] << " |" << endl;
cout << "-------------" << endl << endl;
cout << takenplace[0] << " " << takenplace[1] << " " << takenplace[2] << endl;
cout << takenplace[3] << " " << takenplace[4] << " " << takenplace[5] << endl;
cout << takenplace[6] << " " << takenplace[7] << " " << takenplace[8] << endl;
}
/*
0 1 2
3 4 5
6 7 8
*/
int checkend(char board[])
{
//horizontal
if(board[0] == board[1] && board[0] == board[2] && board[0] != ' ')
return 1;
else if(board[3] == board[4] && board[3] == board[5] && board[3] != ' ')
return 1;
else if(board[6] == board[7] && board[6] == board[8] && board[6] != ' ')
return 1;
//diagonal
else if(board[0] == board[4] && board[0] == board[8] && board[0] != ' ')
return 1;
else if(board[6] == board[4] && board[6] == board[2] && board[6] != ' ')
return 1;
//vertical
else if(board[0] == board[3] && board[0] == board[6] && board[0] != ' ')
return 1;
else if(board[1] == board[4] && board[1] == board[7] && board[1] != ' ')
return 1;
else if(board[2] == board[5] && board[2] == board[8] && board[2] != ' ')
return 1;
else
return 0;
}
int checkdanger(char board[])
{
//horizontal
if(board[0] == board[1] && board[2] == ' ' && board[0] != ' ')
return 3;
else if(board[0] == board[2] && board[1] == ' ' && board[0] != ' ')
return 2;
else if(board[1] == board[2] && board[0] == ' ' && board[1] != ' ')
return 1;
else if(board[3] == board[4] && board[5] == ' ' && board[3] != ' ')
return 6;
else if(board[3] == board[5] && board[4] == ' ' && board[3] != ' ')
return 5;
else if(board[4] == board[5] && board[3] == ' ' && board[4] != ' ')
return 4;
else if(board[6] == board[7] && board[8] == ' ' && board[6] != ' ')
return 9;
else if(board[6] == board[8] && board[7] == ' ' && board[6] != ' ')
return 8;
else if(board[7] == board[8] && board[6] == ' ' && board[7] != ' ')
return 7;
//diagonal
else if(board[0] == board[4] && board[8] != ' ' && board[0] != ' ') // WHY U NO WORK?!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
return 9;
else if(board[0] == board[8] && board[4] != ' ' && board[0] != ' ')
return 5;
else if(board[8] == board[4] && board[0] != ' ' && board[8] != ' ')
return 1;
else if(board[6] == board[4] && board[2] == ' ' && board[6] != ' ')
return 3;
else if(board[2] == board[4] && board[6] == ' ' && board[2] != ' ')
return 7;
else if(board[6] == board[2] && board[4] == ' ' && board[6] != ' ')
return 5;
//vertical
else if(board[0] == board[3] && board[6] == ' ' && board[0] != ' ')
return 7;
else if(board[6] == board[3] && board[0] == ' ' && board[6] != ' ')
return 1;
else if(board[0] == board[6] && board[3] == ' ' && board[0] != ' ')
return 4;
else if(board[1] == board[4] && board[7] == ' ' && board[1] != ' ')
return 8;
else if(board[7] == board[4] && board[1] == ' ' && board[7] != ' ')
return 2;
else if(board[1] == board[7] && board[4] == ' ' && board[1] != ' ')
return 5;
else if(board[2] == board[5] && board[8] == ' ' && board[2] != ' ')
return 9;
else if(board[2] == board[8] && board[5] == ' ' && board[2] != ' ')
return 6;
else if(board[8] == board[5] && board[2] == ' ' && board[8] != ' ')
return 3;
else
return 0;
}
|