Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion TicTacToe/backend/TicTacToe.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ class TicTacToe {
std::unique_ptr<TicTacToeBoard> 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
28 changes: 14 additions & 14 deletions TicTacToe/backend/TicTacToeBoard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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 {
Expand All @@ -29,37 +29,37 @@ 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;
}

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;
}
}
}
5 changes: 5 additions & 0 deletions TicTacToe/backend/TicTacToeBoard.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
#include <stdexcept>
#include <string>

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) {
Expand Down
4 changes: 2 additions & 2 deletions TicTacToe/backend/TicTacToeBoardDescriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}
}
Expand All @@ -16,4 +16,4 @@ const TicTacToeBoardDescriptor TicTacToeBoardDescriptor::editDescription(int pos
std::string newDescription(descriptor);
newDescription[pos] = val;
return TicTacToeBoardDescriptor(newDescription);
}
}
3 changes: 3 additions & 0 deletions TicTacToe/backend/TicTacToeBoardDescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include <string>

const int BOARD_SIZE = 9;
const char P1_SQUARE = 'x';
const char P2_SQUARE = 'o';
const char EMPTY_SQUARE = '.';

class TicTacToeBoardDescriptor {
public:
Expand Down
8 changes: 4 additions & 4 deletions TicTacToe/engine/TicTacToePlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
TicTacToeMove TicTacToePlayer::generateMove(const TicTacToeBoardDescriptor& board, char next_sym, std::vector<int>& 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);
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -94,4 +94,4 @@ Tree& TicTacToePlayer::makeTree(Tree & t, bool x) {
makeTree(t.getChildren(i), !x);
}
return t;
}
}
2 changes: 1 addition & 1 deletion TicTacToe/engine/TicTacToePlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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&);
Expand Down