diff --git a/TikTakToe/TikTakToeGameModel.cpp b/TikTakToe/TikTakToeGameModel.cpp index c57de10..bdb76c8 100644 --- a/TikTakToe/TikTakToeGameModel.cpp +++ b/TikTakToe/TikTakToeGameModel.cpp @@ -26,36 +26,104 @@ TikTakToeGameModel TikTakToeGameModel::gameModel; void TikTakToeGameModel::playersMove(const std::string& player, int cellID) { if (player == players[whosNext]) { int cellValue = 0; + if(board[cellID] == 0){ + if (player == "red") { + cellValue = 1; + } else if (player == "blue") { + cellValue = -1; + } + board[cellID] = cellValue; + + checkIfGameCompleted(); - if (player == "red") { - cellValue = 1; - } else if (player == "blue") { - cellValue = -1; - } - - board[cellID] = cellValue; - - if (whosNext >= 1) { - whosNext = 0; - } else { - whosNext += 1; + if (whosNext >= 1) { + whosNext = 0; + } else { + whosNext += 1; + } } } } void TikTakToeGameModel::resetBoard() { whosNext = 0; + gameFinished= false; for (int i = 0; i < 9; i++) { board[i] = 0; } } +void TikTakToeGameModel::checkIfGameCompleted() { + //Check for a winner + if(checkForWinner()){ + gameFinished = true; + winner = players[whosNext]; + } + + //Check for a draw + if(checkIfBoardFull() && !gameFinished) { + gameFinished = true; + draw = true; + } +} + + +bool TikTakToeGameModel::checkForWinner() { + //Vertical + for(int i = 0; i < 3; i++) { + if(board[i] != 0 && board[0 + i] == board [3 + i] && board[3 + i] == board[6 + i]){ + return true; + } + } + + //Horizontal + for(int i = 0; i < 3; i++) { + if(board[i * 3] != 0 && board[i * 3] == board [i * 3 + 1] && board[i * 3 + 1] == board[i * 3 + 2]){ + return true; + } + } + + //Diagonal left to right + if(board[0] != 0 && board[0] == board[4] && board[4] == board[8]) { + return true; + } + + //Diagonal right to left + if(board[2] != 0 && board[2] == board[4] && board[4] == board[6]) { + return true; + } + + return false; +} + + +bool TikTakToeGameModel::checkIfBoardFull() { + for(int i = 0; i < 9; i++) { + if(board[i] == 0) { + return false; + } + } + return true; +} + + nlohmann::json TikTakToeGameModel::updateClientState() { nlohmann::json message; message["type"] = "update"; message["whosTurn"] = players[whosNext]; message["board"] = board; + + if(gameFinished) { + if(draw) { + message["gameOver"] = "Nobody won, it's a draw!"; + } else { + message["gameOver"] = "Congratulations, player " + winner + " won!"; + } + } + else { + message["gameOver"] = false; + } return message; } diff --git a/TikTakToe/TikTakToeGameModel.h b/TikTakToe/TikTakToeGameModel.h index 7abda23..c974181 100644 --- a/TikTakToe/TikTakToeGameModel.h +++ b/TikTakToe/TikTakToeGameModel.h @@ -29,6 +29,9 @@ class TikTakToeGameModel { public: void playersMove(const std::string& player, int cellID); void resetBoard(); + void checkIfGameCompleted(); + bool checkForWinner(); + bool checkIfBoardFull(); nlohmann::json updateClientState(); static TikTakToeGameModel& getGameModel(); @@ -38,6 +41,9 @@ class TikTakToeGameModel { int whosNext = 0; int numPlayers = 0; int board[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; + bool gameFinished = false; //if one player won, or all fields have been filled + bool draw = false; //all fields are filled, but no player won + std::string winner = ""; friend class TikTakToeSubProtocol; diff --git a/TikTakToe/TikTakToeSubProtocol.cpp b/TikTakToe/TikTakToeSubProtocol.cpp index c681069..60dd6d4 100644 --- a/TikTakToe/TikTakToeSubProtocol.cpp +++ b/TikTakToe/TikTakToeSubProtocol.cpp @@ -88,17 +88,14 @@ void TikTakToeSubProtocol::onMessageEnd() { if (action["type"] == "move") { gameModel.playersMove(action["playerID"], action["cellID"]); - nlohmann::json message = gameModel.updateClientState(); - - /* // also possible - forEachClient([&message](SubProtocol* client) { - client->sendMessage(message.dump()); - }); - */ + } - sendBroadcast(message.dump()); - VLOG(0) << "SendMessage Dump: " << message.dump(); + if (action["type"] == "reset-game") { + gameModel.resetBoard(); } + nlohmann::json message = gameModel.updateClientState(); + sendBroadcast(message.dump()); + VLOG(0) << "SendMessage Dump: " << message.dump(); data.clear(); } diff --git a/TikTakToe/public/index.html b/TikTakToe/public/index.html index 4551366..50997c4 100644 --- a/TikTakToe/public/index.html +++ b/TikTakToe/public/index.html @@ -29,6 +29,8 @@