-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChess.cpp
176 lines (144 loc) · 4.24 KB
/
Chess.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
#include <SFML/Graphics.hpp>
#include <time.h>
#include "Connector.hpp"
using namespace sf;
int size = 56;
Vector2f offset(28,28);
Sprite f[32]; //figures
std::string position="";
int board[8][8] =
{-1,-2,-3,-4,-5,-3,-2,-1,
-6,-6,-6,-6,-6,-6,-6,-6,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
6, 6, 6, 6, 6, 6, 6, 6,
1, 2, 3, 4, 5, 3, 2, 1};
std::string toChessNote(Vector2f p)
{
std::string s = "";
s += char(p.x/size+97);
s += char(7-p.y/size+49);
return s;
}
Vector2f toCoord(char a,char b)
{
int x = int(a) - 97;
int y = 7-int(b)+49;
return Vector2f(x*size,y*size);
}
void move(std::string str)
{
Vector2f oldPos = toCoord(str[0],str[1]);
Vector2f newPos = toCoord(str[2],str[3]);
for(int i=0;i<32;i++)
if (f[i].getPosition()==newPos) f[i].setPosition(-100,-100);
for(int i=0;i<32;i++)
if (f[i].getPosition()==oldPos) f[i].setPosition(newPos);
//castling //if the king didn't move
if (str=="e1g1") if (position.find("e1")==-1) move("h1f1");
if (str=="e8g8") if (position.find("e8")==-1) move("h8f8");
if (str=="e1c1") if (position.find("e1")==-1) move("a1d1");
if (str=="e8c8") if (position.find("e8")==-1) move("a8d8");
}
void loadPosition()
{
int k=0;
for(int i=0;i<8;i++)
for(int j=0;j<8;j++)
{
int n = board[i][j];
if (!n) continue;
int x = abs(n)-1;
int y = n>0?1:0;
f[k].setTextureRect( IntRect(size*x,size*y,size,size) );
f[k].setPosition(size*j,size*i);
k++;
}
for(int i=0;i<position.length();i+=5)
move(position.substr(i,4));
}
int main()
{
RenderWindow window(VideoMode(504, 504), "The Chess! (press SPACE)");
ConnectToEngine("stockfish.exe");
Texture t1,t2;
t1.loadFromFile("images/figures.png");
t2.loadFromFile("images/board.png");
for(int i=0;i<32;i++) f[i].setTexture(t1);
Sprite sBoard(t2);
loadPosition();
bool isMove=false;
float dx=0, dy=0;
Vector2f oldPos,newPos;
std::string str;
int n=0;
while (window.isOpen())
{
Vector2i pos = Mouse::getPosition(window) - Vector2i(offset);
Event e;
while (window.pollEvent(e))
{
if (e.type == Event::Closed)
window.close();
////move back//////
if (e.type == Event::KeyPressed)
if (e.key.code == Keyboard::BackSpace)
{ if (position.length()>6) position.erase(position.length()-6,5); loadPosition();}
/////drag and drop///////
if (e.type == Event::MouseButtonPressed)
if (e.key.code == Mouse::Left)
for(int i=0;i<32;i++)
if (f[i].getGlobalBounds().contains(pos.x,pos.y))
{
isMove=true; n=i;
dx=pos.x - f[i].getPosition().x;
dy=pos.y - f[i].getPosition().y;
oldPos = f[i].getPosition();
}
if (e.type == Event::MouseButtonReleased)
if (e.key.code == Mouse::Left)
{
isMove=false;
Vector2f p = f[n].getPosition() + Vector2f(size/2,size/2);
newPos = Vector2f( size*int(p.x/size), size*int(p.y/size) );
str = toChessNote(oldPos)+toChessNote(newPos);
move(str);
if (oldPos!=newPos) position+=str+" ";
f[n].setPosition(newPos);
}
}
//comp move
if (Keyboard::isKeyPressed(Keyboard::Space))
{
str = getNextMove(position);
oldPos = toCoord(str[0],str[1]);
newPos = toCoord(str[2],str[3]);
for(int i=0;i<32;i++) if (f[i].getPosition()==oldPos) n=i;
/////animation///////
for(int k=0;k<50;k++)
{
Vector2f p = newPos - oldPos;
f[n].move(p.x/50, p.y/50);
window.draw(sBoard);
for(int i=0;i<32;i++) f[i].move(offset);
for(int i=0;i<32;i++) window.draw(f[i]); window.draw(f[n]);
for(int i=0;i<32;i++) f[i].move(-offset);
window.display();
}
move(str); position+=str+" ";
f[n].setPosition(newPos);
}
if (isMove) f[n].setPosition(pos.x-dx,pos.y-dy);
////// draw ///////
window.clear();
window.draw(sBoard);
for(int i=0;i<32;i++) f[i].move(offset);
for(int i=0;i<32;i++) window.draw(f[i]); window.draw(f[n]);
for(int i=0;i<32;i++) f[i].move(-offset);
window.display();
}
CloseConnection();
return 0;
}