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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
#include <iostream>
#include <bitset>
#include <cstdlib>
#include <algorithm>
#define NOUGHTS 1
#define CROSSES -1
using std::cin;
using std::cout;
using std::ostream;
using std::endl;
using std::bitset;
const size_t N = 3;
typedef bitset <N*N> board;
const board wins[] = {
board(0b111000000), board(0b000111000), board(0b000000111), // rows
board(0b100100100), board(0b010010010), board(0b001001001), // cols
board(0b100010001), board(0b001010100) // diagonals
};
board noughts; // bit board for first player
board crosses; // bit board for second player
const char NOUGHT = 'O';
const char CROSS = 'X';
int Search (int depth, board b);
int ComputerThink (board b, unsigned int depth);
board combined();
ostream & print_board(ostream & stm = cout, board b = combined());
// This indicates the current player. Computer plays noughts.
bool curr_move_noughts = true;
board
combined()
{
return noughts | crosses;
} // combined bit board for both players
bool
valid(size_t row, size_t col)
{
return row < N && col < N;
}
size_t
pos(size_t row, size_t col)
{
return row * N + col;
} // map row, col to bit position
bool
occupied(size_t row, size_t col)
{
return valid(row, col) && combined()[pos(row, col)];
}
bool
free(size_t row, size_t col)
{
return valid(row, col) && !occupied(row, col);
}
size_t last_move_row = -1, last_move_col = -1 ;
void make_move( size_t row, size_t col, board & b )
{
if (free(row, col))
{
b[pos(row, col)] = true;
last_move_row = row ;
last_move_col = col ;
}
}
void unMake(board & b )
{
if (valid(last_move_row, last_move_col)) // if valid last_move_row, last_move_col
{
int row= -1;
int col= -1;
b[pos(row, col)] = true;
last_move_row = -1 ;
last_move_col = -1 ;
}
}
void
make_move(size_t row, size_t col)
{
if (curr_move_noughts)
make_move(row, col, noughts);
else
make_move(row, col, crosses);
}
bool
is_win(board b)
{
for (int i = 0; i < sizeof(wins) / sizeof(wins[0]); ++i) {
if ((wins[i] & b) == wins[i])
return true;
}
return false;
}
bool
is_win()
{
return is_win(curr_move_noughts ? noughts : crosses);
}
// is the board full?
bool is_full(board b)
{
static board fullboard(0b111111111);
return b == fullboard;
}
bool is_full()
{
return is_full(curr_move_noughts ? noughts : crosses);
}
static int eval(board b)
{
if (is_win (NOUGHTS))
{
return 1;
}
else if (is_win (CROSSES))
{
return -1;
}
else
{
return 0;
}
}
void
play()
{
int row = -1; // For player input
int col = -1;
for (int turns = 0; turns < 9; ++turns) {
curr_move_noughts = !curr_move_noughts;
if (curr_move_noughts) {
board b = combined();
int z;
z = ComputerThink(b,1);
}
else {
cout << "Choose a move..." << endl;
cout << "Enter row and col : ";
cin >> row >> col;
// dmh - note that valid rows/col are 0-2, not 1-3. If you
// want the user to enter 1-3 then decrement the value
// they enter right after they enter it
if (!valid(row,col)) {
cout << "Illegal move! Try again...\n" << endl;
} else {
make_move(row, col);
}
}
print_board();
if (is_win()) {
cout << "Player " << (curr_move_noughts ? NOUGHT : CROSS) << " has won!" << endl; exit(0);
} else if (is_full()) {
cout << "Game ended in a tie!" << endl;
}
}
}
ostream & print_board(ostream & stm, board b)
{
stm << "------\n";
for (std::size_t i = 0; i < N; ++i) {
for (std::size_t j = 0; j < N; ++j) {
const size_t
k = pos(i, j);
if (b[k])
stm << (noughts[k] ? NOUGHT : CROSS) << ' ';
else
stm << ". ";
}
stm << '\n';
}
return stm << "------\n";
}
int negamax( board b, unsigned int depth, unsigned int side)
{
if (depth==0)
{
int score=eval(b);
return side * score;
}
int bestScore = -1000;
for (int x = 0; x < 9; ++x) {
for (int y = 0; y < 9; ++y) {
while (!occupied(x,y));
make_move(x,y);
int val = -negamax(b, depth -1, -side);
unMake(b);
bestScore = std::max(bestScore, val);
}
}
return bestScore;
}
int ComputerThink (board b, unsigned int depth)
{
int g;
// side = b->turn ? 1 : -1; ??
// Search now
for (int x = 0; x < 9; ++x) {
for (int y = 0; y < 9; ++y) {
while (!occupied(x,y));
{
g= negamax (b,1,1);
make_move(x, y);
}
}
}
return g;
}
int
main()
{
play();
return 0;
}
|