From 3b69bc8b794918f773ac5a094a245fd96fa9996f Mon Sep 17 00:00:00 2001 From: Muchtar Salimov Date: Sun, 14 Oct 2018 19:49:42 -0400 Subject: [PATCH] Remove hard-coded symbols and magic numbers --- TicTacToe/backend/TicTacToe.h | 2 +- TicTacToe/backend/TicTacToeBoard.cpp | 28 +++++++++---------- TicTacToe/backend/TicTacToeBoard.h | 5 ++++ .../backend/TicTacToeBoardDescriptor.cpp | 4 +-- TicTacToe/backend/TicTacToeBoardDescriptor.h | 3 ++ TicTacToe/engine/TicTacToePlayer.cpp | 8 +++--- TicTacToe/engine/TicTacToePlayer.h | 2 +- 7 files changed, 30 insertions(+), 22 deletions(-) diff --git a/TicTacToe/backend/TicTacToe.h b/TicTacToe/backend/TicTacToe.h index e34d0e1..ad2f46d 100644 --- a/TicTacToe/backend/TicTacToe.h +++ b/TicTacToe/backend/TicTacToe.h @@ -21,6 +21,6 @@ class TicTacToe { std::unique_ptr board; int turn; Player players[2] = {HUMAN, COMPUTER}; - static constexpr char symbols[2] = {'x', 'o'}; + static constexpr char symbols[2] = {P1_SQUARE, P2_SQUARE}; }; #endif //GAMES_TICTACTOE_H diff --git a/TicTacToe/backend/TicTacToeBoard.cpp b/TicTacToe/backend/TicTacToeBoard.cpp index a07f20c..0df46db 100644 --- a/TicTacToe/backend/TicTacToeBoard.cpp +++ b/TicTacToe/backend/TicTacToeBoard.cpp @@ -11,7 +11,7 @@ const TicTacToeBoard TicTacToeBoard::makeMove(const TicTacToeMove& m) const { inline bool TicTacToeBoard::isLegal(const TicTacToeMove &m) const { - if (d[m.i] != '.') + if (d[m.i] != EMPTY_SQUARE) return false; return true; } @@ -20,7 +20,7 @@ GameState TicTacToeBoard::getGameState() const { auto str = d.getDescription(); for (auto i = 0; i < BOARD_SIZE / 3; ++i) { auto state = checkLine(str[3 * i], str[3 * i + 1], str[3 * i + 2]); - if (state == 0) { + if (state == CONTINUE_STATE) { if (i == 0) { state = checkLine(str[0], str[4], str[8]); } else { @@ -29,19 +29,19 @@ GameState TicTacToeBoard::getGameState() const { } } } - if (state == 1) + if (state == P1_WIN_STATE) return Win1; - else if (state == -1) + else if (state == P2_WIN_STATE) return Win2; } for (auto i = 0; i < BOARD_SIZE / 3; ++i) { auto state = checkLine(str[i], str[i + 3], str[i + 6]); - if (state == 1) + if (state == P1_WIN_STATE) return Win1; - else if (state == -1) + else if (state == P2_WIN_STATE) return Win2; } - if (numOccupied() < 9) + if (numOccupied() < BOARD_SIZE) return Continue; return Draw; } @@ -49,17 +49,17 @@ GameState TicTacToeBoard::getGameState() const { int TicTacToeBoard::numOccupied() const { auto occ = 0; for (auto c: d.getDescription()) - occ += (c != '.') ? 1 : 0; + occ += (c != EMPTY_SQUARE) ? 1 : 0; return occ; } int TicTacToeBoard::checkLine(char x, char y, char z) const { - if (x == y && y == z && x == 'x') - return 1; + if (x == P1_SQUARE && x == y && y == z) + return P1_WIN_STATE; else{ - if (x == y && y == z && x == 'o') - return -1; + if (x == P2_SQUARE && x == y && y == z) + return P2_WIN_STATE; else - return 0; + return CONTINUE_STATE; } -} \ No newline at end of file +} diff --git a/TicTacToe/backend/TicTacToeBoard.h b/TicTacToe/backend/TicTacToeBoard.h index 225c225..c22827c 100644 --- a/TicTacToe/backend/TicTacToeBoard.h +++ b/TicTacToe/backend/TicTacToeBoard.h @@ -5,6 +5,11 @@ #include #include +const int P1_WIN_STATE = 1; +const int P2_WIN_STATE = -1; +const int CONTINUE_STATE = 0; + + class TicTacToeMove { public: TicTacToeMove(int row, int col, char symbol) :r(row), c(col), s(symbol), i(3 * r + c) { diff --git a/TicTacToe/backend/TicTacToeBoardDescriptor.cpp b/TicTacToe/backend/TicTacToeBoardDescriptor.cpp index 76cbadd..59b2c99 100644 --- a/TicTacToe/backend/TicTacToeBoardDescriptor.cpp +++ b/TicTacToe/backend/TicTacToeBoardDescriptor.cpp @@ -7,7 +7,7 @@ TicTacToeBoardDescriptor::TicTacToeBoardDescriptor(const std::string d) :descrip throw std::invalid_argument("Length of descriptor " + d + " is invalid!"); for (auto c: d){ - if (c != '.' && c != 'o' && c != 'x') + if (c != EMPTY_SQUARE && c != P2_SQUARE && c != P1_SQUARE) throw std::invalid_argument("Found invalid character in descriptor!"); } } @@ -16,4 +16,4 @@ const TicTacToeBoardDescriptor TicTacToeBoardDescriptor::editDescription(int pos std::string newDescription(descriptor); newDescription[pos] = val; return TicTacToeBoardDescriptor(newDescription); -} \ No newline at end of file +} diff --git a/TicTacToe/backend/TicTacToeBoardDescriptor.h b/TicTacToe/backend/TicTacToeBoardDescriptor.h index 19aab2f..bb54694 100644 --- a/TicTacToe/backend/TicTacToeBoardDescriptor.h +++ b/TicTacToe/backend/TicTacToeBoardDescriptor.h @@ -4,6 +4,9 @@ #include const int BOARD_SIZE = 9; +const char P1_SQUARE = 'x'; +const char P2_SQUARE = 'o'; +const char EMPTY_SQUARE = '.'; class TicTacToeBoardDescriptor { public: diff --git a/TicTacToe/engine/TicTacToePlayer.cpp b/TicTacToe/engine/TicTacToePlayer.cpp index dce845a..a21ca33 100644 --- a/TicTacToe/engine/TicTacToePlayer.cpp +++ b/TicTacToe/engine/TicTacToePlayer.cpp @@ -6,7 +6,7 @@ TicTacToeMove TicTacToePlayer::generateMove(const TicTacToeBoardDescriptor& board, char next_sym, std::vector& generated){ auto last = generated.size() > 0 ? generated.back() : -1; for (auto i = ++last; i < BOARD_SIZE; ++i){ - if (board[i] == '.'){ + if (board[i] == EMPTY_SQUARE){ generated.push_back(i); return TicTacToeMove(i, next_sym); } @@ -59,7 +59,7 @@ int findMoveInTree(Tree& t) { TicTacToeMove TicTacToePlayer::play(const TicTacToeBoard & board) { Tree playTree(board.getDescriptor()); - makeTree(playTree, assigned_simbol == 'x'); + makeTree(playTree, assigned_simbol == P1_SQUARE); bool is_first = board.numOccupied() == 0; auto move = treeSearch(playTree, assigned_simbol, is_first); return move; @@ -83,7 +83,7 @@ Tree& TicTacToePlayer::makeTree(Tree & t, bool x) { } while(1){ try { - auto move = generateMove(b.getDescriptor(), x ? 'x' : 'o', generatedMoves); + auto move = generateMove(b.getDescriptor(), x ? P1_SQUARE : P2_SQUARE, generatedMoves); t.addChildren(new Tree(d.editDescription(move.i, move.s))); } catch (int e){ // Moves are finished break; @@ -94,4 +94,4 @@ Tree& TicTacToePlayer::makeTree(Tree & t, bool x) { makeTree(t.getChildren(i), !x); } return t; -} \ No newline at end of file +} diff --git a/TicTacToe/engine/TicTacToePlayer.h b/TicTacToe/engine/TicTacToePlayer.h index 8eb4ee9..37a06e4 100644 --- a/TicTacToe/engine/TicTacToePlayer.h +++ b/TicTacToe/engine/TicTacToePlayer.h @@ -32,7 +32,7 @@ class Tree { class TicTacToePlayer { public: TicTacToePlayer(char symbol) :assigned_simbol(symbol) { - if (symbol != 'x' && symbol != 'o') + if (symbol != P1_SQUARE && symbol != P2_SQUARE) throw std::invalid_argument("Not a valid symbol!"); }; TicTacToeMove play(const TicTacToeBoard&);