Created
May 27, 2017 20:31
-
-
Save shlomif/9a150fcef1e37d6546beae1b461aa7f9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
using namespace std; | |
enum _tile_state | |
{ | |
empty = 0, | |
X, | |
Y | |
}; | |
class board; | |
struct _tile | |
{ | |
public: | |
_tile_state tile_state; | |
_tile_state state() | |
{ | |
switch (tile_state) | |
{ | |
case empty: | |
return empty; | |
case X: | |
return X; | |
case Y: | |
return Y; | |
} | |
} | |
}; | |
typedef vector<vector<_tile>> vi; | |
class _graphics | |
{ | |
public: | |
void render(_tile *tile) | |
{ | |
// Render single tile | |
} | |
void render(vi board) | |
{ | |
// Render entire 2D board | |
int newlineFlag = 0; | |
for (vi::iterator oIt = board.begin(); oIt != board.end(); ++oIt) | |
{ | |
for (vector<_tile>::iterator iIt = oIt->begin(); iIt != oIt->end(); | |
++iIt) | |
{ | |
++newlineFlag; | |
switch ((*iIt).state()) | |
{ | |
case X: | |
{ | |
cout << "|X|"; | |
} | |
break; | |
case Y: | |
{ | |
cout << "|O|"; | |
} | |
break; | |
default: | |
cout << "| |"; | |
} | |
if (newlineFlag >= 3) | |
{ | |
newlineFlag = 0; | |
cout << "\n"; | |
} | |
} | |
} | |
} | |
}; | |
class _board | |
{ | |
vector<vector<_tile>> board; | |
_graphics graphics; | |
public: | |
void render() { graphics.render(board); } | |
void set_area(const int area = 3) | |
{ | |
for (int n = 0; n < area; ++n) | |
{ | |
board.push_back(vector<_tile>()); | |
for (int r = 0; r < area; ++r) | |
{ | |
board.back().push_back(_tile()); | |
} | |
} | |
} | |
void set_tile_state(const int x, const int y, const _tile_state new_state) | |
{ | |
board[x][y].tile_state = new_state; | |
} | |
_tile_state get_tile_state(const int x, const int y) | |
{ | |
return (board[x][y].tile_state); | |
} | |
}; | |
class _game | |
{ | |
_board board; | |
public: | |
_game() { board.set_area(); } | |
void new_game(int area = 3) | |
{ | |
cout << "Started new game of " << area << " × " << area | |
<< " Tic-Tac-Toe - OO C++\n"; | |
board.render(); | |
} | |
void move(const int x, const int y, const _tile_state entity) | |
{ | |
board.set_tile_state(x, y, entity); | |
} | |
}; | |
int main() | |
{ | |
_game game; | |
game.move(1, 1, X); | |
game.new_game(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment