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
|
#include <iostream>
#include <bitset>
#include <cstdlib>
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';
void al_move();
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);
}
void
make_move(size_t row, size_t col, board & b)
{
if (free(row, col))
b[pos(row, col)] = true;
}
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);
}
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) {
al_move(); // computer plays noughts
} 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;
} 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";
}
void
al_move() // Al move doesn`t take argument??
{
int x = rand() % 3;
int y = rand() % 3;
make_move(x, y);
}
int
main()
{
play();
return 0;
}
|