Migration to Codeberg
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
# terminal-tictactoe
|
||||
|
||||
## Overview
|
||||
This Tic Tac Toe game is implemented in C++ and allows two players to take turns marking spaces on a 3x3 grid. The game determines a winner if a player successfully aligns three of their symbols horizontally, vertically, or diagonally.
|
||||
|
||||
## Prerequisites
|
||||
To compile and run the game, ensure you have the following installed on your system:
|
||||
|
||||
- **g++ (GNU Compiler Collection)**: A C++ compiler to build the project.
|
||||
|
||||
## Compilation and Execution
|
||||
Follow these steps to compile and run the program:
|
||||
|
||||
1. Open a terminal and navigate to the directory containing `tictactoe.cpp`.
|
||||
2. Compile the code with the following command:
|
||||
```bash
|
||||
g++ -std=c++11 tictactoe.cpp -o tictactoe
|
||||
```
|
||||
This will create an executable file named `tictactoe`.
|
||||
|
||||
3. Run the program using:
|
||||
```bash
|
||||
./tictactoe
|
||||
```
|
||||
|
||||
## Gameplay Instructions
|
||||
1. At the start, the game displays an empty 3x3 board and a guide for the input keys.
|
||||
2. Players take turns entering their moves. Input keys correspond to the grid positions:
|
||||
- `L1`, `M1`, `R1` for the top row.
|
||||
- `L2`, `M2`, `R2` for the middle row.
|
||||
- `L3`, `M3`, `R3` for the bottom row.
|
||||
|
||||
3. Example of valid input:
|
||||
- `M2` will place the current player's marker in the middle cell of the board.
|
||||
|
||||
4. The game checks for a win or draw after each move. If a player wins, the game announces the winner and exits.
|
||||
|
||||
5. If a player attempts to mark an already occupied cell, they will be prompted to choose another position.
|
||||
|
||||
6. Invalid input (e.g., `X1`, `middle`) will prompt the player to enter a valid move.
|
||||
|
||||
## Troubleshooting
|
||||
- If you encounter issues running the game, ensure `g++` is installed and the file paths are correct.
|
||||
- If the program fails to compile, ensure you are using at least C++11 by specifying `-std=c++11` during compilation.
|
||||
|
||||
## License
|
||||
This project is distributed under the MIT License.
|
||||
|
||||
## Contributions
|
||||
Contributions are welcome! Feel free to fork the repository and submit pull requests.
|
||||
|
||||
## Credits
|
||||
- **Developer:** Joaquin Gottlebe
|
||||
- **Built With:** C++
|
||||
@@ -0,0 +1,122 @@
|
||||
// clear && g++ -std=c++11 tictactoe.cpp -o tictactoe && ./tictactoe
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
bool checkWin(std::vector<std::vector<char>>& board, char player);
|
||||
void printBoard(const std::vector<std::vector<char>>& board);
|
||||
|
||||
int main() {
|
||||
|
||||
std::vector<std::vector<char>> board = {{' ', ' ', ' '},
|
||||
{' ', ' ', ' '},
|
||||
{' ', ' ', ' '}};
|
||||
|
||||
struct position {
|
||||
int row;
|
||||
int col;
|
||||
};
|
||||
|
||||
std::map<std::string, position> input = {
|
||||
{"L1", {0, 0}},
|
||||
{"M1", {0, 1}},
|
||||
{"R1", {0, 2}},
|
||||
{"L2", {1, 0}},
|
||||
{"M2", {1, 1}},
|
||||
{"R2", {1, 2}},
|
||||
{"L3", {2, 0}},
|
||||
{"M3", {2, 1}},
|
||||
{"R3", {2, 2}},
|
||||
};
|
||||
|
||||
char Player1 = 'X';
|
||||
char Player2 = 'O';
|
||||
char currentPlayer = Player1;
|
||||
std::string move;
|
||||
char winner = ' ';
|
||||
|
||||
while (winner == ' ') {
|
||||
std::cout << "This is the Tic Tac Toe the Board: \n";
|
||||
std::cout << "\n";
|
||||
printBoard(board);
|
||||
std::cout << "Here are the inputs for the Board: \n";
|
||||
std::cout << "\n";
|
||||
std::cout << "|L1|M1|R1| \n";
|
||||
std::cout << "|L2|M2|R2| \n";
|
||||
std::cout << "|L3|M3|R3| \n";
|
||||
std::cout << "\n";
|
||||
std::cout << "Spieler " << currentPlayer << ", du bist am Zug: ";
|
||||
|
||||
std::cin >> move;
|
||||
std::cout << "\n\n";
|
||||
|
||||
if (input.find(move) != input.end()) {
|
||||
position pos = input[move];
|
||||
|
||||
if (board[pos.row][pos.col] == ' ') {
|
||||
board[pos.row][pos.col] = currentPlayer;
|
||||
|
||||
if (checkWin(board, currentPlayer)) {
|
||||
winner = currentPlayer;
|
||||
break;
|
||||
}
|
||||
|
||||
if (currentPlayer == Player1) {
|
||||
currentPlayer = Player2;
|
||||
}
|
||||
else {
|
||||
currentPlayer = Player1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
std::cout << "Das Feld ist bereits besetzt. Bitte waehle ein anderes Feld.\n";
|
||||
}
|
||||
}
|
||||
else {
|
||||
std::cout << "Ungueltige Eingabe. Bitte gebe einen gueltigen Zug ein (z.B. 'L1', 'M2', ...).\n";
|
||||
}
|
||||
}
|
||||
|
||||
printBoard(board);
|
||||
std::cout << "!!! Der Gewinner ist " << winner << " 🥳🥳🥳 !!!" <<std::endl;
|
||||
std::cout << "\n";
|
||||
|
||||
}
|
||||
|
||||
bool checkWin(std::vector<std::vector<char>>& board, char player) {
|
||||
// Überprüfe die Zeilen
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
if (board[i][0] == player && board[i][1] == player && board[i][2] == player) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Überprüfe die Spalten
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
if (board[0][i] == player && board[1][i] == player && board[2][i] == player) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Überprüfe die Diagonalen
|
||||
if (board[0][0] == player && board[1][1] == player && board[2][2] == player) {
|
||||
return true;
|
||||
}
|
||||
if (board[0][2] == player && board[1][1] == player && board[2][0] == player) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void printBoard(const std::vector<std::vector<char>>& board) {
|
||||
for (const auto &row : board) {
|
||||
for (const auto &col : row) {
|
||||
std::cout << '|' << col << '|';
|
||||
}
|
||||
std::cout << '\n';
|
||||
}
|
||||
std::cout << '\n';
|
||||
}
|
||||
Reference in New Issue
Block a user