Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tictactoe #144

Merged
merged 13 commits into from
Oct 5, 2024
7 changes: 1 addition & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,8 @@ dir: files/*
@cp files/vector.cpp vorkurs/lektion15/. > /dev/null
@cp files/warnings.cpp vorkurs/lektion16/. > /dev/null
@cp files/warnprim.cpp vorkurs/lektion16/. > /dev/null
@cp files/tictactoe.cpp vorkurs/lektion17/. > /dev/null
@cp files/tictactoe.cpp vorkurs/lektion14/. > /dev/null
@cp files/assemble.cpp vorkurs/lektion18/. > /dev/null
@echo "TTT-Dateien kompilieren…"
@g++ -c -o vorkurs/lektion17/tictactoe.o files/ttt_closed/tictactoe.cpp > /dev/null
@g++ -c -o vorkurs/lektion19/frage_feld_nummer.o files/ttt_closed/frage_feld_nummer.cpp > /dev/null
@g++ -c -o vorkurs/lektion19/gewinnerin.o files/ttt_closed/gewinnerin.cpp > /dev/null
@g++ -c -o vorkurs/lektion19/gebe_feld_aus.o files/ttt_closed/gebe_feld_aus.cpp > /dev/null
@echo "Vorkurs-Verzeichnis erstellt."
@echo "Wenn du das Verzeichnis überall hin kopiert hast, nutze "make nodir", um sicherzustellen, dass das Skript nächstes Jahr wieder funktioniert"

Expand Down
124 changes: 124 additions & 0 deletions files/solutions/tictactoe.cpp
fschledorn marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#include <iostream>
#include <array>

static std::string fts(int feld) {
switch (feld) {
case 0:
return " ";
case 1:
return "X";
case 2:
return "O";
default:
return "?";
}
}

int frage_feld_nummer(std::array<int, 9> feld){
int in = -1;
while (true) {
std::cout << "Gib ein Feld ein (0-8): ";
std::cin >> in;
if (in < 0 || in > 8) {
std::cout << "Ungültige Feldnummer!" << std::endl;
continue;
}
if (feld[in] != 0) {
std::cout << "Feld ist besetzt!" << std::endl;
continue;
}
break;
}
return in;
}

void gebe_feld_aus(std::array<int, 9> feld){
std::cout << " | | " << std::endl;
std::cout << " " << fts(feld[0]) << " | " << fts(feld[1]) << " | " << fts(feld[2]) << " " << std::endl;
std::cout << " | | " << std::endl;
std::cout << "---+---+---" << std::endl;
std::cout << " | | " << std::endl;
std::cout << " " << fts(feld[3]) << " | " << fts(feld[4]) << " | " << fts(feld[5]) << " " << std::endl;
std::cout << " | | " << std::endl;
std::cout << "---+---+---" << std::endl;
std::cout << " | | " << std::endl;
std::cout << " " << fts(feld[6]) << " | " << fts(feld[7]) << " | " << fts(feld[8]) << " " << std::endl;
std::cout << " | | " << std::endl;
}

int gewinnerin(std::array<int, 9> feld){
// Prüfe Reihen und Spalten
for (int i = 0; i < 3; i++) {
int a = feld[3*i] & feld[3*i+1] & feld[3*i+2];
int b = feld[3*i] | feld[3*i+1] | feld[3*i+2];
if (a == b && a != 0) {
return feld[3*i];
}
a = feld[i] & feld[i+3] & feld[i+6];
b = feld[i] | feld[i+3] | feld[i+6];
if (a == b && a != 0) {
return feld[i];
}
}
// Prüfe Diagonale 1
int a = feld[0] & feld[4] & feld[8];
int b = feld[0] | feld[4] | feld[8];
if (a == b && a != 0) {
return feld[0];
}

// Prüfe Diagonale 2
a = feld[2] & feld[4] & feld[6];
b = feld[2] | feld[4] | feld[6];
if (a == b && a != 0) {
return feld[2];
}

for (int i = 0; i < 9; i++) {
if (feld[i] == 0) {
return 0;
}
}
return 3;
}



int main() {
// Setup
restart:
std::array<int, 9> spielfeld{0};
int current_player = 1;
int input_player;

while (true) {
// Input
input_player = frage_feld_nummer(spielfeld);

// Update
spielfeld[input_player] = current_player;
int gewinner = gewinnerin(spielfeld);
switch (gewinner) {
case 1:
std::cout << "Spieler 1 ist der Gewinner." << std::endl;
goto restart;

case 2:
std::cout << "Spieler 2 ist der Gewinner." << std::endl;
goto restart;

case 3:
std::cout << "Das Spiel endet unentschieden." << std::endl;
goto restart;

default:
current_player = (current_player == 2) ? 1 : 2;
break;
}


// Display
gebe_feld_aus(spielfeld);
}
return 0;
}
133 changes: 133 additions & 0 deletions files/solutions/tictactoe_easy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include <iostream>
#include <array>

std::string fts(int feld) {

if (feld == 0) {
return " ";
} else if (feld == 1) {
return "X";
} else if (feld == 2) {
return "O";
} else {
return "?";
}
}

int frage_feld_nummer(std::array<int, 9> feld){
int in = -1;
bool valid = false;
while (!valid) {
std::cout << "Gib ein Feld ein (0-8): ";
std::cin >> in;
if (in < 0 || in > 8) {
std::cout << "Ungültige Feldnummer!" << std::endl;
}
if (feld[in] != 0) {
std::cout << "Feld ist besetzt!" << std::endl;
}
else {
valid = true; // Markiert die Eingabe als gültig wenn keine Fehler aufgetauch sind
}
}
return in;
}

void gebe_feld_aus(std::array<int, 9> feld){
std::cout << " | | " << std::endl;
std::cout << " " << fts(feld[0]) << " | " << fts(feld[1]) << " | " << fts(feld[2]) << " " << std::endl;
std::cout << " | | " << std::endl;
std::cout << "---+---+---" << std::endl;
std::cout << " | | " << std::endl;
std::cout << " " << fts(feld[3]) << " | " << fts(feld[4]) << " | " << fts(feld[5]) << " " << std::endl;
std::cout << " | | " << std::endl;
std::cout << "---+---+---" << std::endl;
std::cout << " | | " << std::endl;
std::cout << " " << fts(feld[6]) << " | " << fts(feld[7]) << " | " << fts(feld[8]) << " " << std::endl;
std::cout << " | | " << std::endl;
}

int gewinnerin(std::array<int, 9> feld){
// Prüfe Reihen und Spalten
for (int i = 0; i < 3; i++) {
int a = feld[3*i] & feld[3*i+1] & feld[3*i+2];
int b = feld[3*i] | feld[3*i+1] | feld[3*i+2];
if (a == b && a != 0) {
return feld[3*i];
}
a = feld[i] & feld[i+3] & feld[i+6];
b = feld[i] | feld[i+3] | feld[i+6];
if (a == b && a != 0) {
return feld[i];
}
}
// Prüfe Diagonale 1
int a = feld[0] & feld[4] & feld[8];
int b = feld[0] | feld[4] | feld[8];
if (a == b && a != 0) {
return feld[0];
}

// Prüfe Diagonale 2
a = feld[2] & feld[4] & feld[6];
b = feld[2] | feld[4] | feld[6];
if (a == b && a != 0) {
return feld[2];
}

for (int i = 0; i < 9; i++) {
if (feld[i] == 0) {
return 0;
}
}
return 3;
}



int main() {
// Setup
std::array<int, 9> spielfeld{0};
Copy link
Collaborator Author

@fschledorn fschledorn Oct 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does currently not compile on Apple LLVM/Clang as they have not yet fully implemented the C++ 20 Standard. As normal LLVM-Clang has implemented aggregate Initialisation as far back as 2022 and g++ supports this as well, I have decided to ignore this issue.
A possible fix could be to just initialise it using a for-loop, but I’m lazy so instead of fixing this by writing 3 lines I will write 20 lines ranting here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd open an Issue for this and wait how far the homebrew clang got next year

int current_player = 1;
int input_player;

while (true) {
// Input
input_player = frage_feld_nummer(spielfeld);

// Update
spielfeld[input_player] = current_player;
int gewinner = gewinnerin(spielfeld);
bool reset = false;

if (gewinner == 1 || gewinner == 2 || gewinner == 3) { // Prüft ob das Spiel beendet ist
reset = true;
if (gewinner == 1) {
std::cout << "Spieler 1 ist der Gewinner." << std::endl;
} else if (gewinner == 2) {
std::cout << "Spieler 2 ist der Gewinner." << std::endl;
} else {
std::cout << "Das Spiel endet unentschieden." << std::endl;
}
}

if (current_player == 1) { // Wechselt den Spieler
current_player = 2;
} else {
current_player = 1;
}

// Display
gebe_feld_aus(spielfeld);

if (reset) // Setzt das Spiel zurück falls es geendet hat
{
current_player = 1;
for (int i = 0; i < 9; i++) {
spielfeld[i] = 0;
}
reset = false;
}
}
return 0;
}
16 changes: 13 additions & 3 deletions files/tictactoe.cpp
fschledorn marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
#include <iostream>
#include <vector>
#include <array>

int frage_feld_nummer(std::array<int ,9> feld){
// Implementiere hier die Abfrage nach dem gespielten Feld //
}

void gebe_feld_aus(std::array<int ,9>){
// Hier wird die Funktion implementiert
}

int gewinnerin(std::array<int ,9>){
//Hier wird die Funktion implementiert
}

extern int frage_feld_nummer(std::vector<int> feld);
// Fügt hier die anderen Funktionen ein

int main() {
// Setup
Expand Down
20 changes: 0 additions & 20 deletions files/ttt_closed/frage_feld_nummer.cpp

This file was deleted.

30 changes: 0 additions & 30 deletions files/ttt_closed/gebe_feld_aus.cpp

This file was deleted.

36 changes: 0 additions & 36 deletions files/ttt_closed/gewinnerin.cpp

This file was deleted.

5 changes: 0 additions & 5 deletions files/ttt_closed/tictactoe.cpp

This file was deleted.

Loading
Loading