This commit is contained in:
DarthMooMancer
2025-11-07 00:49:52 -05:00
parent 1ef6f37a49
commit a40813294e
4 changed files with 45 additions and 16 deletions
+40
View File
@@ -0,0 +1,40 @@
# Tic-Tac-Toe
A simple terminal-based Tic-Tac-Toe game.
## Build
To build the game, run the following commands:
```bash
cmake -S . -B build && cmake --build build
```
## Run
To run the game, execute the following command from the `build` directory:
```bash
./build/tic_tac_toe
```
## How to Play
- Use the numbers 1-9 on your keyboard to select a cell on the board.
- The numbers correspond to the cells as follows:
```
1 | 2 | 3
--+---+--
4 | 5 | 6
--+---+--
7 | 8 | 9
```
- Press 'q' to quit the game at any time.
## About
- This is a simple implementation of the classic Tic-Tac-Toe game.
- It is written in C++23.
- It uses the `ncurses` library to render the game in the terminal.
+3 -3
View File
@@ -3,12 +3,13 @@
#include <memory> #include <memory>
#include <array> #include <array>
#include <globals.hpp>
constexpr int ROW = 5;
constexpr int COL = 5;
struct Window { struct Window {
Window(); Window();
std::array<std::array<char, COL>, ROW> view; std::array<std::array<char, COL>, ROW> view;
void clear_display();
void draw_display(); void draw_display();
}; };
@@ -40,7 +41,6 @@ private:
Player *temp; Player *temp;
GameState() : player1('x'), player2('o'), temp { &player1 } {}; GameState() : player1('x'), player2('o'), temp { &player1 } {};
} state; } state;
void set_player();
bool process_input(); bool process_input();
bool exit_code(); bool exit_code();
bool check_line(char id); bool check_line(char id);
-7
View File
@@ -1,7 +0,0 @@
#ifndef GLOBALS_HPP
#define GLOBALS_HPP
constexpr int ROW = 5;
constexpr int COL = 5;
#endif
+2 -6
View File
@@ -88,18 +88,14 @@ bool Engine::process_input() {
index = ch - '1'; index = ch - '1';
if(win->view[map[index].y][map[index].x] == ' ') { if(win->view[map[index].y][map[index].x] == ' ') {
win->view[map[index].y][map[index].x] = state.temp->id; win->view[map[index].y][map[index].x] = state.temp->id;
set_player(); if(state.temp->id == 'x') { state.temp = &state.player2; }
else if(state.temp->id == 'o') { state.temp = &state.player1; }
} }
} }
} }
return true; return true;
} }
void Engine::set_player() {
if(state.temp->id == 'x') { state.temp = &state.player2; }
else if(state.temp->id == 'o') { state.temp = &state.player1; }
}
bool Engine::check_line(char id) { bool Engine::check_line(char id) {
for(int i : std::ranges::views::iota(0, COL)) { for(int i : std::ranges::views::iota(0, COL)) {
if(ROW % 2 != 0) continue; if(ROW % 2 != 0) continue;