Added the game state and started work on the win check. worked as described in #1

This commit is contained in:
2026-04-22 16:21:49 +02:00
parent 7c15d4ccc2
commit 4d12622a41

View File

@@ -1,7 +1,6 @@
#include <iostream>
#include <windows.h>
#include <iomanip>
#include <map>
#include <vector>
using namespace std;
@@ -15,8 +14,13 @@ string spielfeld[3][3] = {
struct userInput {
bool ok;
int spalte;
int zeile;
int spalte;
};
struct gameState {
bool running = true;
char winner;
};
void render() {
@@ -41,30 +45,48 @@ userInput getUserInput() {
cout << "Spalte: ";
cin >> inputSpalte;
cout << endl;
if (spielfeld[inputSpalte-1][inputZeile-1] == " ") {
return {true, inputSpalte-1, inputZeile-1};
if (spielfeld[inputZeile-1][inputSpalte-1] == " ") {
return {true, inputZeile-1, inputSpalte-1};
} else {
cout << endl << "Input konnte nicht gelesen werden" << endl;
return getUserInput();
}
}
void checkforWin() {
// TODO: check for win or fail and return a game struct.
void checkforWin(gameState &state) {
// TODO: implement a proper efficent check that doesnt end when every tile is filled
// we simulate for debugging purposes that once every field in the spielfeld array is filled, the game ends
int counter = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (spielfeld[i][j] != " ") {
counter++;
}
}
}
if (counter == 9) {
state.running = false;
render();
return;
}
}
int main() {
SetConsoleOutputCP(CP_UTF8);
cout << fixed << setprecision(2);
gameState state;
int counter = 1;
while (true) {
while (state.running) {
render();
string current_player = counter++ % 2 == 1 ? "X" : "O";
cout << endl << "Am Zug ist Spieler: " << current_player << endl;
if (auto [ok, spalte, zeile] = getUserInput(); ok) {
if (auto [ok, zeile, spalte] = getUserInput(); ok) {
spielfeld[zeile][spalte] = current_player;
}
checkforWin(state);
}
system("pause");
return 0;
}