#include <SFML/Graphics.hpp>
#include <time.h>
#include "Connector.hpp"
using namespace sf;
int size = 56;
Vector2f offset(28,28);
Sprite f[32];
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), "Chess The Filipino Battle (press SPACE)");
ConnectToEngine("stockfish.exe");
Texture t1,t2;
t1.loadFromFile("images/figures1.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;
}
Hansly
0
Newbie Poster
Recommended Answers
Jump to PostOver 100 lines of code for someone to read through and guess where to add or such?
Try again. But first read how to make a great discussion. Read https://www.daniweb.com/programming/threads/435023/read-this-before-posting-a-question
Jump to PostYou may need to reduce the question to exactly where in your code you want to test for a checkmate condition. Note: Notice the lack of space in the word checkmate.
All 6 Replies
rproffitt
2,580
"Nothing to see here."
Moderator
Hansly
0
Newbie Poster
rproffitt
2,580
"Nothing to see here."
Moderator
Reverend Jim
4,780
Hi, I'm Jim, one of DaniWeb's moderators.
Moderator
Featured Poster
rproffitt
2,580
"Nothing to see here."
Moderator
John_191
15
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.