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
|
#include <iostream>
using namespace std;
// row and col are 0-based
void set(unsigned& board, int row, int col, char sym)
{
unsigned val = 3u;
switch (sym) {
case ' ': val = 0u; break;
case 'O': val = 1u; break;
case 'X': val = 2u; break;
}
board |= val << ((row * 3 + col) * 2);
}
char get(unsigned board, int row, int col)
{
return " OX?"[(board >> ((row * 3 + col) * 2)) & 3u];
}
void draw_board(unsigned board)
{
for (int row = 0; row < 3; ++row)
{
cout << ' ';
for (int col = 0; col < 2; ++col)
cout << get(board, row, col) << " | ";
cout << get(board, row, 2) << '\n';
if (row < 2) cout << "-----------\n";
}
}
int main()
{
unsigned board = 0;
set(board, 1, 2, 'O');
set(board, 2, 0, 'X');
set(board, 1, 1, 'O');
draw_board(board);
}
|