From d9f7fdc347434737717c4799fe310219f5f5f5b4 Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Thu, 28 Sep 2023 06:48:26 +0200 Subject: [PATCH] Implemented local games in the tictactoe.cpp --- tictactoe.cpp | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/tictactoe.cpp b/tictactoe.cpp index 143982a..28f0f3b 100644 --- a/tictactoe.cpp +++ b/tictactoe.cpp @@ -6,6 +6,8 @@ using namespace std; class TikTakToe { +public: bool botGame = true; + public: string rows[9] = {"-", "-", "-","-", "-", "-","-", "-", "-"}; @@ -53,10 +55,10 @@ public: } public: - void awaitInput() { + void awaitInput(string player) { printBoard(); - cout << "\n=====\nWo soll dein X hin? [1-9] "; + cout << "\n=====\nWo soll dein " << player << " hin? [1-9] "; int input; cin >> input; @@ -66,28 +68,38 @@ public: return; } - rows[input - 1] = "X"; - generateMove(); + rows[input - 1] = player; + + if (botGame) generateMove(); } }; int main() { TikTakToe board; + cout << "Möchtest du gegen einen Bot [1] oder einen lokalen Gegner [2] spielen?: "; + string in; + cin >> in; + + if (in == "2") board.botGame = false; + while (!(board.playerWon("X") || board.playerWon("O"))) { - board.awaitInput(); + board.awaitInput("X"); + if (!board.botGame && !board.playerWon("X")) board.awaitInput("O"); } cout << "\n===== ENDSTAND "; board.printBoard(); - cout << "\n===== ENDSTAND ====="; + cout << "\n===== ENDSTAND =====\n\n"; - if (board.playerWon("X")) { - cout << "\n\nDu hast gewonnen! Herzlichen Glückwunsch. :)"; - } else { + if (board.playerWon("X") && board.botGame) { + cout << "Du hast gewonnen! Herzlichen Glückwunsch. :)"; + } else if (board.botGame) { cout << "Du hast legit gegen einen random Bot verloren, würd mir mal Gedanken machen.."; + } else { + cout << (board.playerWon("X") ? "X" : "O") << " hat gewonnen. Glückwunsch!"; } cout << "\n\n";