Added the game state and started work on the win check. worked as described in #1
This commit is contained in:
38
main.cpp
38
main.cpp
@@ -1,7 +1,6 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <map>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
@@ -15,8 +14,13 @@ string spielfeld[3][3] = {
|
|||||||
|
|
||||||
struct userInput {
|
struct userInput {
|
||||||
bool ok;
|
bool ok;
|
||||||
int spalte;
|
|
||||||
int zeile;
|
int zeile;
|
||||||
|
int spalte;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct gameState {
|
||||||
|
bool running = true;
|
||||||
|
char winner;
|
||||||
};
|
};
|
||||||
|
|
||||||
void render() {
|
void render() {
|
||||||
@@ -41,30 +45,48 @@ userInput getUserInput() {
|
|||||||
cout << "Spalte: ";
|
cout << "Spalte: ";
|
||||||
cin >> inputSpalte;
|
cin >> inputSpalte;
|
||||||
cout << endl;
|
cout << endl;
|
||||||
if (spielfeld[inputSpalte-1][inputZeile-1] == " ") {
|
if (spielfeld[inputZeile-1][inputSpalte-1] == " ") {
|
||||||
return {true, inputSpalte-1, inputZeile-1};
|
return {true, inputZeile-1, inputSpalte-1};
|
||||||
} else {
|
} else {
|
||||||
cout << endl << "Input konnte nicht gelesen werden" << endl;
|
cout << endl << "Input konnte nicht gelesen werden" << endl;
|
||||||
return getUserInput();
|
return getUserInput();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void checkforWin() {
|
void checkforWin(gameState &state) {
|
||||||
// TODO: check for win or fail and return a game struct.
|
// 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() {
|
int main() {
|
||||||
SetConsoleOutputCP(CP_UTF8);
|
SetConsoleOutputCP(CP_UTF8);
|
||||||
cout << fixed << setprecision(2);
|
cout << fixed << setprecision(2);
|
||||||
|
|
||||||
|
gameState state;
|
||||||
|
|
||||||
int counter = 1;
|
int counter = 1;
|
||||||
while (true) {
|
while (state.running) {
|
||||||
render();
|
render();
|
||||||
string current_player = counter++ % 2 == 1 ? "X" : "O";
|
string current_player = counter++ % 2 == 1 ? "X" : "O";
|
||||||
cout << endl << "Am Zug ist Spieler: " << current_player << endl;
|
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;
|
spielfeld[zeile][spalte] = current_player;
|
||||||
}
|
}
|
||||||
|
checkforWin(state);
|
||||||
}
|
}
|
||||||
|
system("pause");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user