OOP A3 Board Games 35
a Board Game Project Made by Students at Cairo FCAI
Loading...
Searching...
No Matches
wordTic_tac_toe.h
Go to the documentation of this file.
1#ifndef WORDTICTACTOE_H
2#define WORDTICTACTOE_H
3
4#include "BoardGame_Classes.h"
5#include <fstream>
6#include <set>
7#include <algorithm>
8#include <iostream>
9#include <cstdlib>
10#include <ctime>
11using namespace std;
12
13// Word Tic Tac Toe Board
14class word_Board : public Board<char> {
15private:
16 set<string> dictionary;
17
18 void load_dictionary() {
19 ifstream file("dic.txt");
20 string word;
21 while (file >> word) {
22 transform(word.begin(), word.end(), word.begin(), ::toupper);
23 dictionary.insert(word);
24 }
25 file.close();
26 }
27
28 string get_word(int r1, int c1, int r2, int c2, int r3, int c3) {
29 return string(1, board[r1][c1]) +
30 string(1, board[r2][c2]) +
31 string(1, board[r3][c3]);
32 }
33
34public:
35 word_Board() : Board(3, 3) {
36 load_dictionary();
37 for (int i = 0; i < 3; i++) {
38 for (int j = 0; j < 3; j++) {
39 board[i][j] = ' ';
40 }
41 }
42 }
43
44 bool update_board(Move<char>* move) override {
45 int x = move->get_x();
46 int y = move->get_y();
47 char symbol = move->get_symbol();
48
49 if (x < 0 || x >= 3 || y < 0 || y >= 3) {
50 cout << "Invalid position! Use 0-2.\n";
51 return false;
52 }
53
54 if (board[x][y] != ' ') {
55 cout << "Cell already occupied!\n";
56 return false;
57 }
58
59 if (!isalpha(symbol)) {
60 cout << "Invalid symbol! Use letters only.\n";
61 return false;
62 }
63
64 board[x][y] = toupper(symbol);
65 n_moves++;
66 return true;
67 }
68
69 bool is_win(Player<char>* player) override {
70 // Check all rows, columns and diagonals
71 string lines[8][3] = {
72 {"00", "01", "02"}, {"10", "11", "12"}, {"20", "21", "22"}, // Rows
73 {"00", "10", "20"}, {"01", "11", "21"}, {"02", "12", "22"}, // Columns
74 {"00", "11", "22"}, {"02", "11", "20"} // Diagonals
75 };
76
77 for (int i = 0; i < 8; i++) {
78 int r1 = lines[i][0][0] - '0', c1 = lines[i][0][1] - '0';
79 int r2 = lines[i][1][0] - '0', c2 = lines[i][1][1] - '0';
80 int r3 = lines[i][2][0] - '0', c3 = lines[i][2][1] - '0';
81
82 string word = get_word(r1, c1, r2, c2, r3, c3);
83 string rev_word = string(word.rbegin(), word.rend());
84
85 if (dictionary.find(word) != dictionary.end() ||
86 dictionary.find(rev_word) != dictionary.end()) {
87 return true;
88 }
89 }
90
91 return false;
92 }
93
94 bool is_lose(Player<char>* player) override {
95 return false;
96 }
97
98 bool is_draw(Player<char>* player) override {
99 return (n_moves == 9 && !is_win(player));
100 }
101
102 bool game_is_over(Player<char>* player) override {
103 return is_win(player) || is_draw(player);
104 }
105};
106
107// Word Player
108class word_Player : public Player<char> {
109public:
111};
112
113// Word Random Player
114class word_Random_Player : public Player<char> {
115public:
117 srand(time(0));
118 }
119};
120
121// Word UI
122class word_UI : public UI<char> {
123public:
124 word_UI() : UI("Welcome to Word Tic-Tac-Toe!", 3) {}
125
126 Move<char>* get_move(Player<char>* player) override {
127 if (player->get_type() == PlayerType::HUMAN) {
128 int x, y;
129 char letter;
130 cout << player->get_name() << " (" << player->get_symbol()
131 << "), enter your move (row, column, and letter): ";
132 cin >> x >> y >> letter;
133 letter = toupper(letter);
134 return new Move<char>(x, y, letter);
135 }
136 else {
137 int x = rand() % 3;
138 int y = rand() % 3;
139 char letter = 'A' + rand() % 26;
140 cout << player->get_name() << " chooses position (" << x << ", " << y
141 << ") with letter " << letter << "\n";
142 return new Move<char>(x, y, letter);
143 }
144 }
145};
146
147#endif // WORDTICTACTOE_H
148
PlayerType
Represents the type of player in the game.
Definition BoardGame_Classes.h:24
@ HUMAN
A human player.
Definition BoardGame_Classes.h:25
@ COMPUTER
A computer-controlled player.
Definition BoardGame_Classes.h:26
int n_moves
Definition BoardGame_Classes.h:45
Board(int rows, int columns)
Definition BoardGame_Classes.h:51
vector< vector< char > > board
Definition BoardGame_Classes.h:44
Represents a single move in a board game.
Definition BoardGame_Classes.h:100
T get_symbol() const
Get the move symbol.
Definition BoardGame_Classes.h:116
int get_y() const
Get column index.
Definition BoardGame_Classes.h:113
int get_x() const
Get row index.
Definition BoardGame_Classes.h:110
Base template for all players (human or AI).
Definition BoardGame_Classes.h:126
char symbol
Definition BoardGame_Classes.h:130
PlayerType get_type() const
Get player type (e.g., 'H' or 'C').
Definition BoardGame_Classes.h:147
Player(string n, char s, PlayerType t)
Definition BoardGame_Classes.h:137
string get_name() const
Get the player's name.
Definition BoardGame_Classes.h:144
string name
Definition BoardGame_Classes.h:128
T get_symbol() const
Get the player's symbol.
Definition BoardGame_Classes.h:150
UI(string message, int cell_display_width)
Definition BoardGame_Classes.h:196
bool is_draw(Player< char > *player) override
Check if the game ended in a draw.
Definition wordTic_tac_toe.h:98
bool is_win(Player< char > *player) override
Check if a player has won.
Definition wordTic_tac_toe.h:69
bool game_is_over(Player< char > *player) override
Check if the game is over.
Definition wordTic_tac_toe.h:102
bool update_board(Move< char > *move) override
Update the board with a new move.
Definition wordTic_tac_toe.h:44
bool is_lose(Player< char > *player) override
Check if a player has lost.
Definition wordTic_tac_toe.h:94
word_Board()
Definition wordTic_tac_toe.h:35
word_Player(string name, char symbol)
Definition wordTic_tac_toe.h:110
word_Random_Player(char symbol)
Definition wordTic_tac_toe.h:116
word_UI()
Definition wordTic_tac_toe.h:124
Move< char > * get_move(Player< char > *player) override
Ask the user (or AI) to make a move.
Definition wordTic_tac_toe.h:126