Aufgabe 7
Schwierigkeitsgrad: Mittel
Themen: Programmein- und ausgabe Kontollstrukturen Funktionen Arrays
- Aufgabenstellung
- Lösung
Erstellen Sie ein TicTacToe-Spiel. Initialisieren Sie dafür ein zweidimensionales Array und lesen Sie ein, wie viele Spieler (1 oder 2) spielen wollen. Im Einzelspieler soll das Programm ein zufälliges Feld auswählen und besetzen.
Nutzen sie dafür rand() und % (z.B. int a = rand() % 3 für eine zufällige Zahl zwischen 0 und 2). Lesen Sie die einzelnen Züge aus der Konsole ein und schreiben Sie jeweils eine Funktion um das Array auszugeben und eine Funktion um zu überprüfen, ob ein Spieler gewonnen hat.
#include <iostream>
using std::cout, std::endl, std::cin;
void computer_turn(int field[3][3], int turn)
{
while (true)
{
int move = rand() % 9;
if (field[move / 3][move % 3] == 0)
{
field[move / 3][move % 3] = turn + 1;
cout << "The Computer chose " << (move + 1) << "!" << endl;
return;
}
}
}
void print_field(int field[3][3])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (field[i][j] == 1)
{
cout << "x ";
}
else if (field[i][j] == 2)
{
cout << "o ";
}
else
{
cout << "- ";
}
}
cout << endl;
}
}
bool check_win(int field[3][3])
{
// Check rows and columns
for (int i = 0; i < 3; i++)
{
if ((field[i][0] == field[i][1] && field[i][1] == field[i][2] && field[i][0] != 0) ||
(field[0][i] == field[1][i] && field[1][i] == field[2][i] && field[0][i] != 0))
{
return true;
}
}
// Check diagonals
if ((field[0][0] == field[1][1] && field[1][1] == field[2][2] && field[0][0] != 0) ||
(field[0][2] == field[1][1] && field[1][1] == field[2][0] && field[0][2] != 0))
{
return true;
}
return false;
}
int main()
{
srand(time(0));
cout << "Welcome to Tic Tac Toe!" << endl;
cout << "Please enter how many players are playing (1 or 2): ";
int players;
cin >> players;
if (players < 1 || players > 2)
{
cout << "Invalid number of players. Exiting." << endl;
return 1;
}
int field[3][3] = {};
int turn = rand() % 2;
int count = 0;
if (players == 1)
{
cout << "You are playing against the computer." << endl;
if (turn == 0)
{
cout << "You start!" << endl;
}
else
{
cout << "The Computer starts!" << endl;
computer_turn(field, turn);
print_field(field);
turn = (turn + 1) % 2;
count++;
}
}
else
{
cout << "You are playing against another player." << endl;
cout << "Player " << (turn + 1) << " starts!" << endl;
}
while (count < 9)
{
int move;
cout << "Choose a field from 1 to 9: ";
cin >> move;
move--;
if (field[move / 3][move % 3] == 0)
{
field[move / 3][move % 3] = turn + 1;
}
else
{
cout << "Error not a number between 1 and 9 !" << endl;
continue;
}
if (check_win(field))
{
print_field(field);
cout << "Player " << (turn + 1) << " wins!" << endl;
return 0;
}
turn = (turn + 1) % 2;
count++;
print_field(field);
if (players == 1)
{
computer_turn(field, turn);
if (check_win(field))
{
print_field(field);
cout << "The Computer wins!" << endl;
return 0;
}
turn = (turn + 1) % 2;
count++;
print_field(field);
}
}
cout << "It's a draw!" << endl;
return 0;
}