-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTickTackToe(GUI).cpp
More file actions
112 lines (90 loc) · 3.56 KB
/
TickTackToe(GUI).cpp
File metadata and controls
112 lines (90 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;
// Function to check if a player has won
bool checkWin(char board[3][3], char player) {
// Check rows and columns
for (int i = 0; i < 3; ++i) {
if ((board[i][0] == player && board[i][1] == player && board[i][2] == player) ||
(board[0][i] == player && board[1][i] == player && board[2][i] == player)) {
return true;
}
}
// Check diagonals
if ((board[0][0] == player && board[1][1] == player && board[2][2] == player) ||
(board[0][2] == player && board[1][1] == player && board[2][0] == player)) {
return true;
}
return false;
}
// Function to check if the board is full (draw)
bool checkDraw(char board[3][3]) {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (board[i][j] == ' ') {
return false; // Empty cell found
}
}
}
return true; // Board is full
}
int main() {
const int windowSize = 300;
const int cellSize = windowSize / 3;
char board[3][3] = {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
char currentPlayer = 'X';
sf::RenderWindow window(sf::VideoMode(windowSize, windowSize), "Tic Tac Toe");
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left) {
int x = event.mouseButton.x / cellSize;
int y = event.mouseButton.y / cellSize;
if (board[y][x] == ' ' && !checkWin(board, 'X') && !checkWin(board, 'O') && !checkDraw(board)) {
board[y][x] = currentPlayer;
if (checkWin(board, currentPlayer)) {
cout << "Player " << currentPlayer << " wins!" << endl;
} else if (checkDraw(board)) {
cout << "The game is a draw!" << endl;
}
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
}
}
}
window.clear();
// Draw the Tic Tac Toe grid
for (int i = 1; i < 3; ++i) {
sf::Vertex line[] = {
sf::Vertex(sf::Vector2f(i * cellSize, 0)),
sf::Vertex(sf::Vector2f(i * cellSize, windowSize))
};
window.draw(line, 2, sf::Lines);
line[0] = sf::Vertex(sf::Vector2f(0, i * cellSize));
line[1] = sf::Vertex(sf::Vector2f(windowSize, i * cellSize));
window.draw(line, 2, sf::Lines);
}
// Draw X and O on the board
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (board[i][j] == 'X') {
sf::RectangleShape line(sf::Vector2f(cellSize - 20, 10));
line.setPosition(j * cellSize + 10, i * cellSize + 10);
line.rotate(45);
window.draw(line);
line.setRotation(-45);
line.setPosition(j * cellSize + 10, (i + 1) * cellSize - 10);
window.draw(line);
} else if (board[i][j] == 'O') {
sf::CircleShape circle(cellSize / 2 - 10);
circle.setPosition(j * cellSize + 10, i * cellSize + 10);
window.draw(circle);
}
}
}
window.display();
}
return 0;
}