-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicTacToe.cpp
175 lines (158 loc) · 5.79 KB
/
TicTacToe.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
////////////////////////////////
/**Author - Keshav Sahu
* Version - 1.0
* This is made using SFML2 library
*//////////////////////////////
#include <SFML\Graphics.hpp>
/// Pixel size of Image ///
#define PixelSize 150
/// Define player type ///
#define PNONE 0
#define PlayerO 1
#define PlayerX 2
/// To check if match is won by any player or not ///
int getWinner(unsigned short int data[3][3])
{
for(int k = 0; k < 3; k++)
{
/// Check row wise ///
if(data[0][k] != PNONE and (data[0][k] == data[1][k] and data[1][k] == data[2][k]))
return data[0][k];
/// Check column wise ///
if(data[k][0] != PNONE and (data[k][0] == data[k][1] and data[k][1] == data[k][2]))
return data[k][0];
}
if(data[1][1] != PNONE)
{
/// Check top-left to bottom-right ///
if(data[0][0] == data[1][1] and data[1][1] == data[2][2])
return data[0][0];
/// Check top-right to bottom-left ///
else if(data[0][2] == data[1][1] and data[1][1] == data[2][0])
return data[0][2];
}
/// If no player won then return none ///
return PNONE;
}
int main()
{
/// Image Texture and Sprite ///
sf::Texture texture;
sf::Sprite sprite;
/// Load image frome png file ///
if(!texture.loadFromFile("res/texture.png"))
return 0;
sprite.setTexture(texture);
/// Texts and Fonts ///
sf::Font font;
sf::Text text;
sf::String textS;
/// Load font from ttf file ///
if(!font.loadFromFile("res/sansation.ttf"))
return 0;
text.setFont(font);
text.setPosition(30,200);
text.setFillColor(sf::Color::Red);
text.setOutlineColor(sf::Color::Green);
text.setOutlineThickness(3);
/// Variable to manage activity ///
short int i,j;
unsigned short int data[3][3] = {PNONE};
unsigned short int player = PlayerO;
unsigned short int count = 0;
bool isGameFinished = false;
/// Window and EventHandler ///
sf::RenderWindow app;
sf::Event e;
sf::Vector2i MousePosition;
/// Create window here ///
app.create(sf::VideoMode(450,450),"Tic Tac Toe");
/// window's main loop ///
while(app.isOpen())
{
/// Handle user events ///
while(app.pollEvent(e))
{
/// If user clicked close button ///
if(e.type == sf::Event::Closed)
app.close();
/// If user pressed any key ///
else if(e.type == sf::Event::KeyPressed)
{
/// Key press event only run if game is finished(either won by anyone or it's a draw)
if(isGameFinished)
{
/// If key is pressed (after game finished) it will reset game
isGameFinished = false;
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
{
data[i][j] = PNONE;
}
text.setString("");
count = 0;
}
}
/// If player clicked (mouse) on board ///
else if(e.type == sf::Event::MouseButtonPressed)
{
/// If click button is left click and game is not finished ///
if(sf::Mouse::isButtonPressed(sf::Mouse::Left) && !isGameFinished)
{
/// Get mouse position relative to app ///
MousePosition = sf::Mouse::getPosition(app);
i = MousePosition.y/150;
j = MousePosition.x/150;
if(i > 2 || j > 2)
continue;
/// check if it is already not used, then use ///
if(data[i][j] == PNONE)
{
data[i][j] = player;
player = (player == PlayerO)?PlayerX:PlayerO;
count++;
}
}
}
}//End of event
/// Clear previous screen ///
app.clear();
/// Main drawing starts here ///
/// Draw sprites here ///
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
{
sprite.setTextureRect(sf::IntRect(PixelSize*data[i][j],0,PixelSize,PixelSize));
sprite.setPosition(float(PixelSize*j),float(PixelSize*i));
app.draw(sprite);
}
/// If count >= 5 (2 + 3) possibility of winning game ///
if(!isGameFinished && count >= 5)
{
int winner = getWinner(data);
/// If winner is none then any player has won the game ///
if(winner != PNONE)
{
textS.clear();
textS = (winner == PlayerO)?("PlayerO"):("PlayerX");
textS += " Has Won The Game\nPress Any Key To Play Again";
text.setString(textS);
isGameFinished = true;
}
/// If count = 9 and winner is none then it's a draw ///
else if(count == 9)
{
textS.clear();
textS = "\t\t\tGame Is Draw\nPress Any Key To Play Again";
text.setString(textS);
isGameFinished = true;
}
}
/// Draw text here ///
app.draw(text);
/// Display all drawing ///
app.display();
}//End of loop
/// return exit success ///
return 0;
}