diff options
author | Joursoir <chat@joursoir.net> | 2021-02-19 17:58:36 +0000 |
---|---|---|
committer | Joursoir <chat@joursoir.net> | 2021-02-19 17:58:36 +0000 |
commit | f8ef01b479ffac6e4432970ca4c471635dc2b120 (patch) | |
tree | 85443ff53d3e4e299bfd0733230f2d095a8722a6 | |
parent | b3228ceab88cd2a37b7aca127237403e010b060b (diff) | |
download | lp-gomoku-f8ef01b479ffac6e4432970ca4c471635dc2b120.tar.gz lp-gomoku-f8ef01b479ffac6e4432970ca4c471635dc2b120.tar.bz2 lp-gomoku-f8ef01b479ffac6e4432970ca4c471635dc2b120.zip |
add copy constructor and helpful getters
-rw-r--r-- | GameField.cpp | 38 | ||||
-rw-r--r-- | GameField.hpp | 4 |
2 files changed, 37 insertions, 5 deletions
diff --git a/GameField.cpp b/GameField.cpp index 75485b4..18a3470 100644 --- a/GameField.cpp +++ b/GameField.cpp @@ -15,6 +15,25 @@ GameField::GameField(int a_cols, int a_rows, int a_lwin) free_fields = cols * rows; } +GameField::GameField(const GameField& a) +{ + int c, r; + cols = a.cols; + rows = a.rows; + free_fields = a.free_fields; + win_length = a.win_length; + state = a.state; + who_move = a.who_move; + + field = new int*[cols]; + for(c = 0; c < cols; c++) { + field[c] = new int[rows]; + for(r = 0; r < rows; r++) { + field[c][r] = a.field[c][r]; + } + } +} + GameField::~GameField() { if(field) { @@ -42,19 +61,28 @@ void GameField::Move(int y, int x) UpdateState(y, x); } -void GameField::UpdateState(int y, int x) +void GameField::UndoMove(int y, int x) { - if(!free_fields) { - state = G_DRAW; - return; - } + who_move = field[y][x]; + field[y][x] = G_EMPTY; + free_fields++; + UpdateState(y, x); +} + +void GameField::UpdateState(int y, int x) +{ if((state = ScanRowsAround(y, x)) != G_NONE) return; if((state = ScanColsAround(y, x)) != G_NONE) return; if((state = ScanDiagsAround(y, x)) != G_NONE) return; + + if(!free_fields) { + state = G_DRAW; + return; + } } int GameField::ScanRowsAround(int y, int x) diff --git a/GameField.hpp b/GameField.hpp index 4c6d61d..de5ccd1 100644 --- a/GameField.hpp +++ b/GameField.hpp @@ -21,11 +21,15 @@ class GameField { int who_move; public: GameField(int a_cols, int a_rows, int a_lwin); + GameField(const GameField &a); ~GameField(); int GetState() { return state; } + int GetCols() { return cols; } + int GetRows() { return rows; } bool CanMove(int y, int x); void Move(int y, int x); + void UndoMove(int y, int x); private: void UpdateState(int y, int x); |