-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
201 lines (186 loc) · 4.82 KB
/
main.cpp
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <SFML/Graphics.hpp>
#include <iostream>
#include "game.hpp"
//Size of the window
#define HIGH 600
#define WIDE 600
//Defines the positions of each row and collumn
#define POS1 28
#define POS2 228
#define POS3 428
int main()
{
Game myGame;
int mouseX, mouseY, moveX, moveY;
int turn = 0;
//Creates Window
sf::RenderWindow window(sf::VideoMode(WIDE,HIGH), "TicTacToe");
//Loads the texture for the board grid
sf::Texture boardTexture;
if(!boardTexture.loadFromFile("board.png"))
{
//Throws this error if texture load fails
std::cout << "Error loading textures" << std::endl;
return 0;
}
//Sprite for the board grid
sf::Sprite boardSprite;
boardSprite.setTexture(boardTexture);
boardSprite.setPosition(12,12);
//Loads the texture for the X and O
sf::Texture xandoTexture;
if(!xandoTexture.loadFromFile("xando.png"))
{
//Throws this error if the texture load fails
std::cout << "Error loading textures" << std::endl;
return 0;
}
//Sprite for the X
sf::Sprite xSprite;
xSprite.setTexture(xandoTexture);
xSprite.setTextureRect(sf::IntRect(0,0,160,160));
//Sprite for the O
sf::Sprite oSprite;
oSprite.setTexture(xandoTexture);
oSprite.setTextureRect(sf::IntRect(0,160,160,160));
//Loads the font for the victory message
sf::Font font;
if(!font.loadFromFile("raidercrusadershiftdown.ttf"))
{
//Throws this error if the font load fails
std::cout << "Error loading font!" << std::endl;
return 0;
}
//Text for the victory message
sf::Text text;
text.setFont(font);
text.setCharacterSize(48);
text.setColor(sf::Color::Black);
text.setPosition(WIDE/2,HIGH/2);
//Loops as long as window is open
while(window.isOpen())
{
//Loops through all events that have occurred since last loop
sf::Event event;
while(window.pollEvent(event))
{
if(event.type == sf::Event::Closed)
{
//Closes the window if a close event is received
window.close();
}
if(event.type == sf::Event::MouseButtonPressed)
{
//If mouse is clicked, checks if it was the left mouse button
if(event.mouseButton.button == sf::Mouse::Left)
{
//Loads in the x and y position of the mouse button at the time of the click
mouseX = event.mouseButton.x;
mouseY = event.mouseButton.y;
//figure out which collumn was clicked
if(POS1 <= mouseX && mouseX <= POS1+160)
{
moveX = 0;
}
if(POS2 <= mouseX && mouseX <= POS2+160)
{
moveX = 1;
}
if(POS3 <= mouseX && mouseX <= POS3+160)
{
moveX = 2;
}
//figure out which row was clicked
if(POS1 <= mouseY && mouseY <= POS1+160)
{
moveY = 0;
}
if(POS2 <= mouseY && mouseY <= POS2+160)
{
moveY = 1;
}
if(POS3 <= mouseY && mouseY <= POS3+160)
{
moveY = 2;
}
//If the move was legal, switch over the turn
if(myGame.makeMove(turn, moveY, moveX) == true)
{
turn++;
turn %= 2;
}
//Uncomment this line if you want it to print the board to the terminal (for debugging)
//myGame.printBoard();
}
}
}
//clears the window
window.clear(sf::Color::Blue);
//redraws all the X's and O's - loops through all 9 spaces
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
//reads character in space
char space = myGame.getSpace(i,j);
int xPos, yPos;
//If the space isn't blank, sets xPos and yPos
if(space != ' ')
{
if(i == 0)
{
xPos = POS1;
}
else if(i == 1)
{
xPos = POS2;
}
else if(i == 2)
{
xPos = POS3;
}
if(j == 0)
{
yPos = POS1;
}
else if(j == 1)
{
yPos = POS2;
}
else if(j == 2)
{
yPos = POS3;
}
}
//If the space is an X, print an X there
if(space == 'X')
{
xSprite.setPosition(yPos, xPos);
window.draw(xSprite);
}
//If the space is an O, print an O there
else if(space == 'O')
{
oSprite.setPosition(yPos, xPos);
window.draw(oSprite);
}
}
}
//Check for victory, prints a message
if(myGame.testForWin())
{
if(turn == 0)
{
text.setString("O Wins!");
}
else
{
text.setString("X Wins!");
}
window.draw(text);
}
//Draws the board, then updates the window
window.draw(boardSprite);
window.display();
}
}