From f8ef01b479ffac6e4432970ca4c471635dc2b120 Mon Sep 17 00:00:00 2001 From: Joursoir Date: Fri, 19 Feb 2021 17:58:36 +0000 Subject: add copy constructor and helpful getters --- GameField.cpp | 38 +++++++++++++++++++++++++++++++++----- 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); -- cgit v1.2.3-18-g5258