#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] = 
      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) );

     for(int i=0;i<position.length();i+=5)

int main()
    RenderWindow window(VideoMode(504, 504), "Chess The Filipino Battle (press SPACE)");


    Texture t1,t2;

    for(int i=0;i<32;i++) f[i].setTexture(t1);
    Sprite sBoard(t2); 


    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)

            ////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)
                  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);
                  if (oldPos!=newPos) position+=str+" ";

       //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;

         for(int k=0;k<50;k++)
            Vector2f p = newPos - oldPos;
            f[n].move(p.x/50, p.y/50); 
            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);

        move(str);  position+=str+" ";

        if (isMove) f[n].setPosition(pos.x-dx,pos.y-dy);

    ////// draw  ///////
    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);


    return 0;

Edited by Hansly: i cant add check mate in this code..

2 Months
Discussion Span
Last Post by John_191

sorry... im just new here... I do not know that there are rules here.. i just want how to add check mate for the king. because my prof said he wants to see how to check mate a king. but i don't know how to add a codes..


You 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.

https://rosettacode.org/wiki/Chess_player and other works are far beyond 175 lines of code. A thought here. If you want to add a specific checkmate test, create a function that does this test. It's your code so you have a deep understanding of your design. Your code doesn't convey design. It's just code. So tackle this by breaking out your new test to a function on its own.

Edited by rproffitt: Grammar fix.


I do not know that there are rules here.

Were you not presented with the rules/TOS or a link to them when you signed up/posted? And did you decide to not read them?


Just a note about the rules. There's been a rash of posts that are just poor in quality.

The reason the question about reading the rules is not directed just at you but if you can help feed back if this isn't showing up we can report it up the chain to who fixes this area.

Be honest. If you just blew past that just tell that. The number of posts that dump code and run is too high.


It's somewhat involved to test for checkmate.

Testing for check is pretty easy. Can you write a function that tests if the king is in check?

To test for checkmate you need to check if there is a way out of check:

  1. can the king move to another square?
  2. can the attacking piece be captured?
  3. can a piece be interposed between the attacker and the king?

The check-testing code can be used to solve 1 and 2. To solve 1, just run it from the alternate positions. To solve 2, run it from the attackers position (are any of your pieces attacking the attacker?).

3 might be the hardest one. I suppose you could determine where the interposing squares are and run the check-testing code on them. Are any of your pieces "attacking" those squares? Then they can move there (the pawn is an exception since it doesn't attack the same way it moves).

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.