├── Chess ├── chess ├── images │ ├── board.png │ ├── gameOver.png │ ├── blackBishop.png │ ├── blackKing.png │ ├── blackKnight.png │ ├── blackPawn.png │ ├── blackQueen.png │ ├── blackRook.png │ ├── play-button.png │ ├── rsz_1images.png │ ├── save-disk.png │ ├── whiteBishop.png │ ├── whiteKing.png │ ├── whiteKnight.png │ ├── whitePawn.png │ ├── whiteQueen.png │ ├── whiteRook.png │ ├── refresh-button.png │ ├── save-icon-54519.png │ ├── gamepad-controller.png │ └── Crystal_Project_Player_play.png ├── boardpositions.txt ├── main.cpp ├── spritepositions.txt ├── piece.h ├── save.h ├── play.h ├── king.h ├── bishop.h ├── queen.h ├── utilityBox.h ├── knight.h ├── pawn.h ├── rook.h ├── graphs.h └── ChessBoard.h └── README.md /Chess/chess: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soothscier/SFML-CHESS-GAME/HEAD/Chess/chess -------------------------------------------------------------------------------- /Chess/images/board.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soothscier/SFML-CHESS-GAME/HEAD/Chess/images/board.png -------------------------------------------------------------------------------- /Chess/images/gameOver.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soothscier/SFML-CHESS-GAME/HEAD/Chess/images/gameOver.png -------------------------------------------------------------------------------- /Chess/images/blackBishop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soothscier/SFML-CHESS-GAME/HEAD/Chess/images/blackBishop.png -------------------------------------------------------------------------------- /Chess/images/blackKing.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soothscier/SFML-CHESS-GAME/HEAD/Chess/images/blackKing.png -------------------------------------------------------------------------------- /Chess/images/blackKnight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soothscier/SFML-CHESS-GAME/HEAD/Chess/images/blackKnight.png -------------------------------------------------------------------------------- /Chess/images/blackPawn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soothscier/SFML-CHESS-GAME/HEAD/Chess/images/blackPawn.png -------------------------------------------------------------------------------- /Chess/images/blackQueen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soothscier/SFML-CHESS-GAME/HEAD/Chess/images/blackQueen.png -------------------------------------------------------------------------------- /Chess/images/blackRook.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soothscier/SFML-CHESS-GAME/HEAD/Chess/images/blackRook.png -------------------------------------------------------------------------------- /Chess/images/play-button.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soothscier/SFML-CHESS-GAME/HEAD/Chess/images/play-button.png -------------------------------------------------------------------------------- /Chess/images/rsz_1images.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soothscier/SFML-CHESS-GAME/HEAD/Chess/images/rsz_1images.png -------------------------------------------------------------------------------- /Chess/images/save-disk.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soothscier/SFML-CHESS-GAME/HEAD/Chess/images/save-disk.png -------------------------------------------------------------------------------- /Chess/images/whiteBishop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soothscier/SFML-CHESS-GAME/HEAD/Chess/images/whiteBishop.png -------------------------------------------------------------------------------- /Chess/images/whiteKing.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soothscier/SFML-CHESS-GAME/HEAD/Chess/images/whiteKing.png -------------------------------------------------------------------------------- /Chess/images/whiteKnight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soothscier/SFML-CHESS-GAME/HEAD/Chess/images/whiteKnight.png -------------------------------------------------------------------------------- /Chess/images/whitePawn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soothscier/SFML-CHESS-GAME/HEAD/Chess/images/whitePawn.png -------------------------------------------------------------------------------- /Chess/images/whiteQueen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soothscier/SFML-CHESS-GAME/HEAD/Chess/images/whiteQueen.png -------------------------------------------------------------------------------- /Chess/images/whiteRook.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soothscier/SFML-CHESS-GAME/HEAD/Chess/images/whiteRook.png -------------------------------------------------------------------------------- /Chess/images/refresh-button.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soothscier/SFML-CHESS-GAME/HEAD/Chess/images/refresh-button.png -------------------------------------------------------------------------------- /Chess/images/save-icon-54519.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soothscier/SFML-CHESS-GAME/HEAD/Chess/images/save-icon-54519.png -------------------------------------------------------------------------------- /Chess/images/gamepad-controller.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soothscier/SFML-CHESS-GAME/HEAD/Chess/images/gamepad-controller.png -------------------------------------------------------------------------------- /Chess/images/Crystal_Project_Player_play.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soothscier/SFML-CHESS-GAME/HEAD/Chess/images/Crystal_Project_Player_play.png -------------------------------------------------------------------------------- /Chess/boardpositions.txt: -------------------------------------------------------------------------------- 1 | -1,-2,-3,4,-5,-3,-2,-1,-6,-6,-6,0,-6,0,-6,-6,0,0,0,0,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-6,0,0,0,0,0,0,0,0,0,0,6,6,6,0,0,6,6,6,1,2,3,0,5,3,2,1, -------------------------------------------------------------------------------- /Chess/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "play.h" 5 | using namespace sf; 6 | int main(){ 7 | play p; 8 | return 0; 9 | } -------------------------------------------------------------------------------- /Chess/spritepositions.txt: -------------------------------------------------------------------------------- 1 | 0,1,2,64,59,5,6,7,8,9,10,64,12,64,14,15,64,64,64,64,51,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,13,64,64,64,64,64,64,64,64,64,64,48,49,50,64,64,53,54,55,56,57,58,64,60,61,62,63, -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SFML-CHESS-GAME 2 | 3 | A two player chess game made in c++. 4 | 5 | Steps to compile and run CHESS game on your ubuntu system: 6 | 7 | 1~ First you need to install SFML in ubuntu. Open the terminal and write this command: 8 | "sudo apt-get install libsfml-dev" 9 | 10 | 2~ Second you need to cd(move) to the directory where you have saved the files from the terminal. 11 | 12 | 3~ Then for compiling main.cpp, run this command: 13 | "g++ -std=c++14 main.cpp -o chess -lsfml-graphics -lsfml-window -lsfml-system" ## We have used c++14 syntax in major cases 14 | 15 | 4~ Lastly write "./chess" in the terminal and the game will run. 16 | 17 | NOTE: While playing game, you need to select the piece with RIGHT-MOUSE-KEY, and select the position on which you want to move that piece with LEFT-MOUSE-KEY. 18 | 19 | You can see the demo gameplay here: https://youtu.be/ROHttONknq4 20 | -------------------------------------------------------------------------------- /Chess/piece.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | class Piece 6 | { 7 | private: 8 | int arrayOfChess[64]; 9 | 10 | public: 11 | Piece(int Chess[]); 12 | 13 | bool isTileOccupied(int tile); 14 | 15 | std::string pieceAlliance(int tile, std::string alliance); 16 | 17 | }; 18 | 19 | //----------------------------DEFINITIONS 20 | 21 | Piece::Piece(int *Chess) { 22 | 23 | for(unsigned i = 0; i < 64; ++i) { 24 | arrayOfChess[i] = Chess[i]; 25 | } 26 | } 27 | 28 | bool Piece::isTileOccupied(int tile) { 29 | if(arrayOfChess[tile] == 0) 30 | return false; 31 | else 32 | return true; 33 | } 34 | 35 | std::string Piece::pieceAlliance(int tile, std::string alliance) { 36 | if(alliance == "white") 37 | { 38 | if(arrayOfChess[tile] < 0) 39 | { 40 | return "black"; 41 | } 42 | else 43 | { 44 | return "white"; 45 | } 46 | } 47 | 48 | else if(alliance == "black") 49 | { 50 | if (arrayOfChess[tile] > 0) 51 | { 52 | return "white"; 53 | } 54 | else 55 | return "black"; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Chess/save.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace sf; 5 | class save{ 6 | private: 7 | float W = 650; 8 | float H = 650; 9 | public: 10 | save(){ 11 | 12 | } 13 | bool smain() 14 | { 15 | sf::RenderWindow window(sf::VideoMode(W, H), "Chess The Game Of Kings!"); 16 | sf::Texture texture[2]; 17 | sf::Sprite sprite[2]; 18 | texture[0].loadFromFile("images/save-disk.png"); 19 | texture[1].loadFromFile("images/gameOver.png"); 20 | 21 | 22 | sprite[0].setTexture(texture[0]); 23 | sprite[0].setScale(2.0f,2.0f); 24 | sprite[0].setPosition(250,20); 25 | sprite[1].setTexture(texture[1]); 26 | sprite[1].setScale(0.73f,0.73f); 27 | sprite[1].setPosition(0,0); 28 | 29 | while (window.isOpen()) 30 | { Vector2i pos= Mouse::getPosition(window); 31 | sf::Event event; 32 | while (window.pollEvent(event)) 33 | { 34 | if (event.type == sf::Event::Closed) 35 | window.close(); 36 | if (sf::Mouse::isButtonPressed(sf::Mouse::Right) || sf::Mouse::isButtonPressed(sf::Mouse::Left) ){ 37 | if (sprite[0].getGlobalBounds().contains(pos.x,pos.y)){ 38 | return true; 39 | window.close(); 40 | 41 | } 42 | } 43 | 44 | } 45 | 46 | window.clear(); 47 | window.draw(sprite[1]); 48 | window.draw(sprite[0]); 49 | window.display(); 50 | } 51 | 52 | 53 | return 0; 54 | } 55 | }; 56 | -------------------------------------------------------------------------------- /Chess/play.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "ChessBoard.h" 5 | using namespace sf; 6 | const float w = 650; 7 | const float h = 650; 8 | class play{ 9 | private: 10 | sf::Texture texture[3]; 11 | sf::Sprite sprite[3]; 12 | sf::RectangleShape rectangle; 13 | public: 14 | play() 15 | { 16 | sf::RenderWindow window(sf::VideoMode(w, h), "Chess The Game Of Kings!"); 17 | 18 | texture[0].loadFromFile("images/play-button.png"); 19 | texture[1].loadFromFile("images/refresh-button.png"); 20 | texture[2].loadFromFile("images/board.png"); 21 | 22 | rectangle.setSize(sf::Vector2f(w, h)); 23 | sprite[0].setTexture(texture[0]); 24 | sprite[1].setTexture(texture[1]); 25 | sprite[2].setTexture(texture[2]); 26 | sprite[0].setScale(2.0f,2.0f); 27 | sprite[1].setScale(2.0f,2.0f); 28 | sprite[2].setScale(0.73f,0.73f); 29 | sprite[0].setPosition(120,20); 30 | sprite[1].setPosition(400,20); 31 | sprite[2].setPosition(0,0); 32 | rectangle.setFillColor(sf::Color::White); 33 | 34 | 35 | while (window.isOpen()) 36 | { Vector2i pos= Mouse::getPosition(window); 37 | sf::Event event; 38 | while (window.pollEvent(event)) 39 | { 40 | if (event.type == sf::Event::Closed){ 41 | window.close(); 42 | } 43 | if (sf::Mouse::isButtonPressed(sf::Mouse::Right) || sf::Mouse::isButtonPressed(sf::Mouse::Left) ){ 44 | if (sprite[0].getGlobalBounds().contains(pos.x,pos.y)){ 45 | window.close(); 46 | ChessBoard graphics; 47 | graphics.MainFunctions(1); 48 | } 49 | if (sprite[1].getGlobalBounds().contains(pos.x,pos.y)){ 50 | window.close(); 51 | ifstream in,in2; 52 | in.open("spritepositions.txt"); 53 | in2.open("boardpositions.txt"); 54 | for(int j=0;j<64;j++){ 55 | int a,b; 56 | char c,d; 57 | in>>a>>c; 58 | spritepositions[j]=a; 59 | in2>>b>>d; 60 | board[j]=b;} 61 | ChessBoard graphics; 62 | graphics.MainFunctions(0); 63 | } 64 | } 65 | } 66 | 67 | window.clear(); 68 | window.draw(rectangle); 69 | window.draw(sprite[2]); 70 | window.draw(sprite[0]); 71 | window.draw(sprite[1]); 72 | window.display(); 73 | } 74 | 75 | } 76 | }; -------------------------------------------------------------------------------- /Chess/king.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include "piece.h" 5 | 6 | class king 7 | { 8 | private: 9 | const int CANDIDATE_MOVE_COORDINATES[8] = {-9, -8, -7, -1, 1, 8, 7, 9}; 10 | int pieceTile; 11 | std::string alliance; 12 | int arrOfChess[64]; 13 | 14 | public: 15 | king(int pieceTile, std::string alliance, const int chess[]); 16 | 17 | bool isValidTileCoordinate(int currentCandidate); 18 | 19 | //-------------------------------EXCLUSIONS 20 | 21 | bool firstColum(int pieceTile, int offset); 22 | 23 | bool eighthColum(int pieceTile, int offset); 24 | 25 | //--------------------EXCLUSIONS 26 | 27 | std::vector getLegalMoves(); 28 | }; 29 | 30 | 31 | //----------------------------DEFINITIONS 32 | 33 | king::king(int pieceTile, std::string alliance, const int chess[]) { 34 | for( int i = 0; i < 64; ++i) { 35 | arrOfChess[i] = chess[i]; 36 | } 37 | this->pieceTile = pieceTile; 38 | this->alliance = alliance; 39 | } 40 | 41 | bool king::isValidTileCoordinate(int currentCandidate) { 42 | return currentCandidate >= 0 && currentCandidate < 64; 43 | } 44 | 45 | bool king::firstColum(int pieceTile, int offset) { 46 | int firstColumArr[] = {0,8,16,24,32,40,43,56}; 47 | bool temp = false; 48 | 49 | for(int i: firstColumArr) 50 | { 51 | if(pieceTile == i) 52 | { 53 | temp = true; 54 | break; 55 | }else 56 | temp = false; 57 | 58 | } 59 | 60 | return temp && (offset == -9 || offset == -1 || offset == 7); 61 | } 62 | 63 | bool king::eighthColum(int pieceTile, int offset) { 64 | int eighthColumArr[] = {7,15,23,31,39,47,55,63}; 65 | bool temp = false; 66 | 67 | for(int i: eighthColumArr) 68 | { 69 | if(pieceTile == i) 70 | { 71 | temp = true; 72 | break; 73 | } 74 | else 75 | temp = false; 76 | } 77 | 78 | return temp && (offset == -7 || offset == 1 || offset == 9); 79 | } 80 | 81 | std::vector king::getLegalMoves() { 82 | 83 | Piece destinationTile(arrOfChess); 84 | int possibleDestinationTile = this->pieceTile; 85 | std::vector legalMoves; 86 | // int=new x; 87 | 88 | for(int offset:CANDIDATE_MOVE_COORDINATES) 89 | { 90 | 91 | if (firstColum(this->pieceTile, offset) || eighthColum(this->pieceTile, offset)) 92 | { 93 | continue; 94 | } 95 | possibleDestinationTile = this->pieceTile + offset; 96 | if (isValidTileCoordinate(possibleDestinationTile)) 97 | { 98 | if (destinationTile.isTileOccupied(possibleDestinationTile)) { 99 | if (this->alliance != destinationTile.pieceAlliance(possibleDestinationTile, alliance)) { 100 | legalMoves.push_back(possibleDestinationTile); 101 | } 102 | } else { 103 | legalMoves.push_back(possibleDestinationTile); 104 | } 105 | 106 | } 107 | 108 | } 109 | return legalMoves; 110 | } 111 | -------------------------------------------------------------------------------- /Chess/bishop.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include "piece.h" 5 | 6 | class bishop 7 | { 8 | private: 9 | const int CANDIDATE_MOVE_COORDINATES[4] = {-9, -7, 9, 7}; 10 | int pieceTile; 11 | std::string alliance; 12 | int arrOfChess[64]; 13 | 14 | public: 15 | bishop(int pieceTile, std::string alliance, int chess[]); 16 | 17 | bool isValidTileCoordinate(int currentCandidate); 18 | 19 | //----------------------------Exclusions 20 | 21 | bool firstColum(int pieceTile, int offset); 22 | 23 | bool eighthColum(int pieceTile, int offset); 24 | 25 | //----------------------------CALCULATIONS 26 | 27 | std::vector getLegalMoves(); 28 | }; 29 | 30 | //----------------------------DEFINITIONS 31 | 32 | bishop::bishop(int pieceTile, std::string alliance, int chess[]) { 33 | for( int i = 0; i < 64; ++i) { 34 | arrOfChess[i] = chess[i]; 35 | } 36 | this->pieceTile = pieceTile; 37 | this->alliance = alliance; 38 | } 39 | 40 | bool bishop::isValidTileCoordinate(int currentCandidate) { 41 | return currentCandidate >= 0 && currentCandidate < 64; 42 | } 43 | 44 | bool bishop::firstColum(int pieceTile, int offset) { 45 | int firstColumArr[] = {0,8,16,24,32,40,43,56}; 46 | bool temp = false; 47 | 48 | for(int i: firstColumArr) 49 | { 50 | if(pieceTile == i) 51 | { 52 | temp = true; 53 | break; 54 | }else 55 | temp = false; 56 | } 57 | 58 | return temp && (offset == 7 || offset == -9); 59 | } 60 | 61 | bool bishop::eighthColum(int pieceTile, int offset) { 62 | int eighthColumArr[] = {7,15,23,31,39,47,55,63}; 63 | bool temp = false; 64 | 65 | for(int i: eighthColumArr) 66 | { 67 | if(pieceTile == i) 68 | { 69 | temp = true; 70 | break; 71 | }else 72 | temp = false; 73 | } 74 | 75 | return temp && (offset == -7 || offset == 9); 76 | } 77 | 78 | std::vector bishop::getLegalMoves() { 79 | 80 | Piece destinationTile(arrOfChess); 81 | int possibleDestinationTile; 82 | std::vector legalMoves; 83 | for(int offset:CANDIDATE_MOVE_COORDINATES) 84 | { 85 | possibleDestinationTile = this->pieceTile; 86 | 87 | if (firstColum(possibleDestinationTile, offset) || eighthColum(possibleDestinationTile, offset)) 88 | { 89 | continue; 90 | } 91 | while(isValidTileCoordinate(possibleDestinationTile)) { 92 | int temp = possibleDestinationTile; 93 | possibleDestinationTile += offset; 94 | if (firstColum(temp, offset) || eighthColum(temp, offset)) 95 | { 96 | continue; 97 | } 98 | if (isValidTileCoordinate(possibleDestinationTile)) 99 | { 100 | if (!destinationTile.isTileOccupied(possibleDestinationTile)) 101 | { 102 | legalMoves.push_back(possibleDestinationTile); 103 | } 104 | else 105 | { 106 | if(!(this->alliance == destinationTile.pieceAlliance(possibleDestinationTile, this->alliance))) 107 | { 108 | legalMoves.push_back(possibleDestinationTile); 109 | } 110 | break; 111 | } 112 | } 113 | } 114 | } 115 | 116 | return legalMoves; 117 | } 118 | 119 | -------------------------------------------------------------------------------- /Chess/queen.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include "piece.h" 6 | 7 | class queen 8 | { 9 | private: 10 | const int CANDIDATE_MOVE_COORDINATES[8] = {-8, -1, 1, 8, -9, -7, 9, 7}; 11 | int pieceTile; 12 | std::string alliance; 13 | int arrOfChess[64]{}; 14 | 15 | public: 16 | queen(const int Tile, std::string alliance, int chess[]); 17 | 18 | bool isValidTileCoordinate(int currentCandidate); 19 | 20 | //---------------------------EXCLUSIONS 21 | 22 | bool firstColum(int tile, int offset); 23 | 24 | bool eighthColum(int tile, int offset); 25 | 26 | //---------------------------CALCULATIONS 27 | 28 | std::vector getLegalMoves(); 29 | 30 | }; 31 | 32 | //----------------------------DEFINITIONS 33 | 34 | queen::queen(const int Tile, std::string alliance, int chess[]) { 35 | for( int i = 0; i < 64; ++i) { 36 | arrOfChess[i] = chess[i]; 37 | } 38 | this->pieceTile = Tile; 39 | this->alliance = std::move(alliance); 40 | } 41 | 42 | bool queen::isValidTileCoordinate(int currentCandidate) { 43 | return currentCandidate >= 0 && currentCandidate < 64; 44 | } 45 | 46 | bool queen::firstColum(int tile, int offset) { 47 | int firstColumArr[] = {0,8,16,24,32,40,43,56}; 48 | bool temp; 49 | 50 | for(int i: firstColumArr) 51 | { 52 | if(tile == i) 53 | { 54 | temp = true; 55 | break; 56 | } 57 | else{ 58 | temp=false; 59 | } 60 | } 61 | 62 | return temp && (offset == -1 || offset == 7 || offset == -9); 63 | } 64 | 65 | bool queen::eighthColum(int tile, int offset) { 66 | int eighthColumArr[] = {7,15,23,31,39,47,55,63}; 67 | bool temp; 68 | 69 | for(int i: eighthColumArr) 70 | { 71 | if(tile == i) 72 | { 73 | temp = true; 74 | break; 75 | } 76 | else{ 77 | temp=false; 78 | } 79 | } 80 | 81 | return temp && (offset == 1 || offset == -7 || offset == 9); 82 | } 83 | 84 | std::vector queen::getLegalMoves() { 85 | 86 | Piece destinationTile(arrOfChess); 87 | int possibleDestinationTile; 88 | std::vector legalMoves; 89 | for(int offset:CANDIDATE_MOVE_COORDINATES) 90 | { 91 | possibleDestinationTile = this->pieceTile; 92 | 93 | /* if (firstColum(possibleDestinationTile, offset) || eighthColum(possibleDestinationTile, offset)) 94 | { 95 | continue; 96 | }*/ 97 | while(isValidTileCoordinate(possibleDestinationTile)) { 98 | 99 | int temp = possibleDestinationTile; 100 | possibleDestinationTile += offset; 101 | if (firstColum(temp, offset) || eighthColum(temp, offset)) 102 | { 103 | continue; 104 | } 105 | 106 | if (isValidTileCoordinate(possibleDestinationTile)) 107 | { 108 | if (!destinationTile.isTileOccupied(possibleDestinationTile)) 109 | { 110 | legalMoves.push_back(possibleDestinationTile); 111 | } 112 | else 113 | { 114 | if(!(this->alliance == destinationTile.pieceAlliance(possibleDestinationTile, alliance))) 115 | { 116 | legalMoves.push_back(possibleDestinationTile); 117 | } 118 | break; 119 | } 120 | } 121 | } 122 | } 123 | 124 | return legalMoves; 125 | } 126 | -------------------------------------------------------------------------------- /Chess/utilityBox.h: -------------------------------------------------------------------------------- 1 | #include "king.h" 2 | #include "queen.h" 3 | #include "pawn.h" 4 | //#include "piece.h" 5 | #include "bishop.h" 6 | #include "rook.h" 7 | #include "knight.h" 8 | class Identity 9 | { 10 | public: 11 | bool chessKing(int initialTile, int finalTile, std::string alliance, int *arrOfChess) { 12 | king king1(initialTile, alliance, arrOfChess); 13 | 14 | std::vector v1; 15 | v1 = king1.getLegalMoves(); 16 | 17 | 18 | for(std::vector::iterator it1 =v1.begin(); it1 !=v1.end(); ++it1) { 19 | if (*it1 == finalTile) 20 | return true; 21 | } 22 | 23 | return false; 24 | } 25 | 26 | bool chessQueen(int initialTile, int finalTile, std::string alliance, int *arrOfChess) { 27 | 28 | queen queen1(initialTile, alliance, arrOfChess); 29 | 30 | std::vectorv2; 31 | v2 = queen1.getLegalMoves(); 32 | 33 | 34 | for(std::vector::iterator it2 =v2.begin(); it2 !=v2.end(); ++it2) { 35 | if (*it2 == finalTile) 36 | return true; 37 | } 38 | 39 | return false; 40 | } 41 | 42 | bool chessPawn(int initialTile, int finalTile, std::string alliance, int *arrOfChess) { 43 | pawn pawn1(initialTile, alliance, arrOfChess); 44 | 45 | std::vectorv3; 46 | v3 = pawn1.getLegalMoves(); 47 | 48 | 49 | for(std::vector::iterator it3 =v3.begin(); it3 !=v3.end(); ++it3) { 50 | if (*it3 == finalTile) 51 | return true; 52 | } 53 | 54 | return false; 55 | } 56 | 57 | bool chessRook(int initialTile, int finalTile, std::string alliance, int *arrOfChess) { 58 | rook rook1(initialTile, alliance, arrOfChess); 59 | 60 | std::vector v4; 61 | v4 = rook1.getLegalMoves(); 62 | 63 | 64 | for(std::vector::iterator it4 = v4.begin(); it4 != v4.end(); ++it4) { 65 | if (*it4 == finalTile) 66 | return true; 67 | } 68 | 69 | return false; 70 | } 71 | 72 | bool chessBishop(int initialTile, int finalTile, std::string alliance, int *arrOfChess) { 73 | bishop bishop1(initialTile, alliance, arrOfChess); 74 | 75 | std::vector v5; 76 | v5 = bishop1.getLegalMoves(); 77 | 78 | 79 | for(std::vector::iterator it5 = v5.begin(); it5 != v5.end(); ++it5) { 80 | if (*it5 == finalTile) 81 | return true; 82 | } 83 | 84 | return false; 85 | } 86 | 87 | bool chessKnight(int initialTile, int finalTile, std::string alliance, int *arrOfChess) { 88 | knight knight1(initialTile, alliance, arrOfChess); 89 | 90 | std::vector v6; 91 | v6 = knight1.getLegalMoves(); 92 | 93 | 94 | for(std::vector::iterator it6 = v6.begin(); it6 != v6.end(); ++it6) { 95 | if (*it6 == finalTile) 96 | return true; 97 | } 98 | 99 | return false; 100 | } 101 | 102 | std::string checkAlliance(int piece) { 103 | if (piece > 0) 104 | return "white"; 105 | else if(piece < 0) 106 | return "black"; 107 | } 108 | 109 | bool identifier(int initialTile, int finalTile,int piece, int *arrOfChess) 110 | { 111 | std::string alliance = checkAlliance(piece); 112 | 113 | if(piece == -1 || piece == 1) 114 | return chessRook( initialTile, finalTile, alliance, arrOfChess); 115 | 116 | else if(piece == -2 || piece == 2) 117 | return chessKnight( initialTile, finalTile, alliance, arrOfChess); 118 | 119 | else if(piece == -3 || piece == 3) 120 | return chessBishop( initialTile, finalTile, alliance, arrOfChess); 121 | 122 | else if(piece == -4 || piece == 4) 123 | return chessQueen( initialTile, finalTile, alliance, arrOfChess); 124 | 125 | else if(piece == -5 || piece == 5) 126 | return chessKing( initialTile, finalTile, alliance, arrOfChess); 127 | 128 | else if(piece == -6 || piece == 6) 129 | return chessPawn( initialTile, finalTile, alliance, arrOfChess); 130 | 131 | } 132 | 133 | }; 134 | -------------------------------------------------------------------------------- /Chess/knight.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | #include 4 | #include 5 | #include "piece.h" 6 | 7 | class knight 8 | { 9 | private: 10 | const int CANDIDATE_MOVE_COORDINATES[8] = {-17, -15, -10, -6, 6, 10, 15, 17}; 11 | int pieceTile; 12 | std::string alliance; 13 | int arrOfChess[64]; 14 | 15 | public: 16 | knight(int pieceTile, std::string alliance, int chess[]); 17 | 18 | bool isValidTileCoordinate(int currentCandidate); 19 | 20 | //-------------------------------EXCLUSIONS 21 | 22 | bool firstColum(int pieceTile, int offset); 23 | 24 | bool secondColum(int pieceTile, int offset); 25 | 26 | bool seventhColum(int pieceTile, int offset); 27 | 28 | bool eighthColum(int pieceTile, int offset); 29 | 30 | //--------------------EXCLUSIONS 31 | 32 | std::vector getLegalMoves(); 33 | }; 34 | 35 | 36 | //----------------------------DEFINITIONS 37 | 38 | knight::knight(int pieceTile, std::string alliance, int chess[]) { 39 | for( int i = 0; i < 64; ++i) { 40 | arrOfChess[i] = chess[i]; 41 | } 42 | this->pieceTile = pieceTile; 43 | this->alliance = alliance; 44 | } 45 | 46 | bool knight::isValidTileCoordinate(int currentCandidate) { 47 | return currentCandidate >= 0 && currentCandidate < 64; 48 | } 49 | 50 | bool knight::firstColum(int pieceTile, int offset) { 51 | int firstColumArr[] = {0,8,16,24,32,40,43,56}; 52 | bool temp = false; 53 | 54 | for(int i: firstColumArr) 55 | { 56 | if(pieceTile == i) 57 | { 58 | temp = true; 59 | break; 60 | }else 61 | temp = false; 62 | 63 | } 64 | 65 | return temp && ((offset == -17) || (offset == -10) || (offset == 6) || (offset == 15)); 66 | } 67 | 68 | bool knight::secondColum(int pieceTile, int offset) { 69 | int secondColumArr[] = {1,9,17,25,33,41,49,55}; 70 | bool temp = false; 71 | 72 | for(int i: secondColumArr) 73 | { 74 | if(pieceTile == i) 75 | { 76 | temp = true; 77 | break; 78 | }else 79 | temp = false; 80 | 81 | } 82 | 83 | return temp && ((offset == -10) || (offset == 6)); 84 | } 85 | 86 | bool knight::seventhColum(int pieceTile, int offset) { 87 | int seventhColumArr[] = {6,14,22,30,38,46,54,62}; 88 | bool temp = false; 89 | 90 | for(int i: seventhColumArr) 91 | { 92 | if(pieceTile == i) 93 | { 94 | temp = true; 95 | break; 96 | }else 97 | temp = false; 98 | 99 | } 100 | 101 | return temp && ((offset == -6) || (offset == 10)); 102 | } 103 | 104 | bool knight::eighthColum(int pieceTile, int offset) { 105 | int eighthColumArr[] = {7,15,23,31,39,47,55,63}; 106 | bool temp = false; 107 | 108 | for(int i: eighthColumArr) 109 | { 110 | if(pieceTile == i) 111 | { 112 | temp = true; 113 | break; 114 | } 115 | else 116 | temp = false; 117 | } 118 | 119 | return temp && ((offset == -15) || (offset == -6) || (offset == 10) || (offset == 17)); 120 | } 121 | 122 | std::vector knight::getLegalMoves() { 123 | 124 | Piece destinationTile(arrOfChess); 125 | int possibleDestinationTile; 126 | std::vector legalMoves; 127 | 128 | for(int offset:CANDIDATE_MOVE_COORDINATES) 129 | { 130 | 131 | if (firstColum(this->pieceTile, offset) || secondColum(this->pieceTile, offset) || 132 | seventhColum(this->pieceTile, offset) || eighthColum(this->pieceTile, offset)) 133 | { 134 | continue; 135 | } 136 | possibleDestinationTile = this->pieceTile + offset; 137 | if (isValidTileCoordinate(possibleDestinationTile)) 138 | { 139 | if (destinationTile.isTileOccupied(possibleDestinationTile)) { 140 | if (this->alliance != destinationTile.pieceAlliance(possibleDestinationTile, this->alliance)) { 141 | legalMoves.push_back(possibleDestinationTile); 142 | } 143 | } else { 144 | legalMoves.push_back(possibleDestinationTile); 145 | } 146 | 147 | } 148 | 149 | } 150 | return legalMoves; 151 | } 152 | -------------------------------------------------------------------------------- /Chess/pawn.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include "piece.h" 5 | 6 | class pawn 7 | { 8 | private: 9 | const int CANDIDATE_MOVE_COORDINATES[4] = {7, 8, 9, 16}; 10 | int pieceTile; 11 | int arrOfChess[64]; 12 | std::string alliance; 13 | signed int direction; 14 | 15 | public: 16 | pawn(int Tile, std::string alliance, int chess[]); 17 | 18 | bool isValidTileCoordinate(int currentCandidate); 19 | 20 | //------------------------------------------------------------------EXCLUSIONS 21 | //---------------------------------COLUMN 22 | 23 | bool eighthColum(int tile); 24 | 25 | bool firstColum(int tile); 26 | 27 | bool isFirstMove(int tile); 28 | 29 | //-----------------CALCULATIONS 30 | 31 | std::vector getLegalMoves(); 32 | 33 | }; 34 | 35 | //----------------------------DEFINITIONS 36 | 37 | pawn::pawn(int Tile, std::string alliance, int *chess) { 38 | for( int i = 0; i < 64; ++i) { 39 | arrOfChess[i] = chess[i]; 40 | } 41 | 42 | this->pieceTile = Tile; 43 | this->alliance = alliance; 44 | 45 | if (this->alliance == "white") 46 | { 47 | direction = -1; 48 | } 49 | else if (this->alliance == "black") 50 | { 51 | direction = 1; 52 | } 53 | } 54 | 55 | bool pawn::isValidTileCoordinate(int currentCandidate) { 56 | return currentCandidate >= 0 && currentCandidate < 64; 57 | } 58 | 59 | bool pawn::eighthColum(int tile) { 60 | int eighthColumArr[] = {7,15,23,31,39,47,55,63}; 61 | bool temp = false; 62 | 63 | for(int i: eighthColumArr) 64 | { 65 | if(tile == i) 66 | { 67 | temp = true; 68 | break; 69 | }else 70 | temp = false; 71 | } 72 | 73 | return temp; 74 | } 75 | 76 | bool pawn::firstColum(int tile) { 77 | int firstColumArr[] = {0,8,16,24,32,40,43,56}; 78 | bool temp = false; 79 | 80 | for(int i: firstColumArr) 81 | { 82 | if(tile == i) 83 | { 84 | temp = true; 85 | break; 86 | }else 87 | temp = false; 88 | } 89 | 90 | return temp; 91 | } 92 | 93 | bool pawn::isFirstMove(int tile) { 94 | int secondRowArr[] = {8,9,10,11,12,13,14,15}; 95 | int seventhRowArr[] = {48,49,50,51,52,53,54,55}; 96 | 97 | if(alliance == "black") 98 | { 99 | for (int i : secondRowArr) { 100 | if (tile == i) 101 | return true; 102 | } 103 | } 104 | else if (alliance == "white") 105 | { 106 | for (int i : seventhRowArr) { 107 | if (tile == i) 108 | return true; 109 | } 110 | } 111 | 112 | return false; 113 | } 114 | 115 | std::vector pawn::getLegalMoves() { 116 | Piece destinationTile(arrOfChess); 117 | int possibleDestinationTile; 118 | std::vector legalMoves; 119 | 120 | for(int offset:CANDIDATE_MOVE_COORDINATES) 121 | { 122 | possibleDestinationTile = this->pieceTile + (direction * offset); 123 | 124 | if (!isValidTileCoordinate(possibleDestinationTile)) 125 | { 126 | continue; 127 | } 128 | 129 | if (offset == 8 && !destinationTile.isTileOccupied(possibleDestinationTile)) 130 | { 131 | legalMoves.push_back(possibleDestinationTile); 132 | } 133 | 134 | //TWO STEPS 135 | if (offset == 16 && isFirstMove(this->pieceTile)) 136 | { 137 | int behindCandidate = this->pieceTile + (this->direction * 8); 138 | 139 | if (!destinationTile.isTileOccupied(behindCandidate) && !destinationTile.isTileOccupied(possibleDestinationTile)) 140 | { 141 | legalMoves.push_back(possibleDestinationTile); 142 | } 143 | } 144 | 145 | 146 | if (offset == 7 && !((eighthColum(this->pieceTile) && this->alliance == "white") || (firstColum(this->pieceTile) && this->alliance == "black"))) 147 | { 148 | if (destinationTile.isTileOccupied(possibleDestinationTile)) 149 | { 150 | if(this->alliance != destinationTile.pieceAlliance(possibleDestinationTile, alliance)) 151 | { 152 | legalMoves.push_back(possibleDestinationTile); 153 | } 154 | } 155 | } 156 | if (offset == 9 && !((firstColum(this->pieceTile)&& this->alliance == "white") || (eighthColum(this->pieceTile) && this->alliance == "black"))) 157 | { 158 | if (destinationTile.isTileOccupied(possibleDestinationTile)) 159 | { 160 | if(this->alliance != destinationTile.pieceAlliance(possibleDestinationTile, alliance)) 161 | { 162 | legalMoves.push_back(possibleDestinationTile); 163 | } 164 | } 165 | } 166 | 167 | } 168 | 169 | return legalMoves; 170 | } -------------------------------------------------------------------------------- /Chess/rook.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include "piece.h" 5 | 6 | class rook 7 | { 8 | private: 9 | const int CANDIDATE_MOVE_COORDINATES[4] = {-8, -1, 1, 8}; 10 | int pieceTile; 11 | std::string alliance; 12 | int arrOfChess[64]; 13 | 14 | public: 15 | rook(const int Tile, std::string alliance, int chess[]); 16 | 17 | bool isValidTileCoordinate(int currentCandidate); 18 | 19 | //---------------------------EXCLUSIONS 20 | 21 | bool firstColum(int tile, int offset); 22 | 23 | bool eighthColum(int tile, int offset); 24 | 25 | //---------------------------CALCULATIONS 26 | 27 | std::vector getLegalMoves(); 28 | 29 | }; 30 | 31 | //----------------------------DEFINITIONS 32 | 33 | rook::rook(const int Tile, std::string alliance, int chess[]) { 34 | for( int i = 0; i < 64; ++i) { 35 | arrOfChess[i] = chess[i]; 36 | } 37 | this->pieceTile = Tile; 38 | this->alliance = alliance; 39 | } 40 | 41 | bool rook::isValidTileCoordinate(int currentCandidate) { 42 | return currentCandidate >= 0 && currentCandidate < 64; 43 | } 44 | 45 | bool rook::firstColum(int tile, int offset) { 46 | int firstColumArr[] = {0,8,16,24,32,40,43,56}; 47 | bool temp; 48 | 49 | for(int i: firstColumArr) 50 | { 51 | if(tile == i) 52 | { 53 | temp = true; 54 | break; 55 | } 56 | else{ 57 | temp=false; 58 | } 59 | } 60 | 61 | return temp && (offset == -1); 62 | } 63 | 64 | bool rook::eighthColum(int tile, int offset) { 65 | int eighthColumArr[] = {7,15,23,31,39,47,55,63}; 66 | bool temp; 67 | 68 | for(int i: eighthColumArr) 69 | { 70 | if(tile == i) 71 | { 72 | temp = true; 73 | break; 74 | } 75 | else{ 76 | temp=false; 77 | } 78 | } 79 | 80 | return temp && (offset == 1); 81 | } 82 | 83 | //std::vector rook::getLegalMoves() { 84 | // 85 | // Piece destinationTile(arrOfChess); 86 | // int possibleDestinationTile; 87 | // std::vector legalMoves; 88 | // for(int offset:CANDIDATE_MOVE_COORDINATES) 89 | // { 90 | // possibleDestinationTile = this->pieceTile; 91 | // 92 | // if (firstColum(possibleDestinationTile, offset) || eighthColum(possibleDestinationTile, offset)) 93 | // { 94 | // continue; 95 | // } 96 | // 97 | // while(isValidTileCoordinate(possibleDestinationTile)) { 98 | // if (firstColum(possibleDestinationTile, offset) || eighthColum(possibleDestinationTile, offset)) 99 | // { 100 | // break; 101 | // } 102 | // 103 | // possibleDestinationTile += offset; 104 | // if (isValidTileCoordinate(possibleDestinationTile)) 105 | // { 106 | // if (!destinationTile.isTileOccupied(possibleDestinationTile)) 107 | // { 108 | // legalMoves.push_back(possibleDestinationTile); 109 | // } 110 | // else 111 | // { 112 | // if(!(this->alliance == destinationTile.pieceAlliance(possibleDestinationTile, alliance))) 113 | // { 114 | // legalMoves.push_back(possibleDestinationTile); 115 | // } 116 | // break; 117 | // } 118 | // } 119 | // } 120 | // } 121 | // 122 | // return legalMoves; 123 | //} 124 | 125 | std::vector rook::getLegalMoves() { 126 | 127 | Piece destinationTile(arrOfChess); 128 | int possibleDestinationTile; 129 | std::vector legalMoves; 130 | for(int offset:CANDIDATE_MOVE_COORDINATES) 131 | { 132 | possibleDestinationTile = this->pieceTile; 133 | 134 | if (firstColum(possibleDestinationTile, offset) || eighthColum(possibleDestinationTile, offset)) 135 | { 136 | continue; 137 | } 138 | while(isValidTileCoordinate(possibleDestinationTile)) { 139 | int temp = possibleDestinationTile; 140 | possibleDestinationTile += offset; 141 | if (firstColum(temp, offset) || eighthColum(temp, offset)) 142 | { 143 | continue; 144 | } 145 | if (isValidTileCoordinate(possibleDestinationTile)) 146 | { 147 | if (!destinationTile.isTileOccupied(possibleDestinationTile)) 148 | { 149 | legalMoves.push_back(possibleDestinationTile); 150 | } 151 | else 152 | { 153 | if(!(this->alliance == destinationTile.pieceAlliance(possibleDestinationTile, this->alliance))) 154 | { 155 | legalMoves.push_back(possibleDestinationTile); 156 | } 157 | break; 158 | } 159 | } 160 | } 161 | } 162 | 163 | return legalMoves; 164 | } 165 | 166 | -------------------------------------------------------------------------------- /Chess/graphs.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "utilityBox.h" 4 | #include "save.h" 5 | #include 6 | using namespace sf; 7 | using namespace std; 8 | int spritepositions[64]={ 9 | 0,1,2,3,4,5,6,7, 10 | 8,9,10,11,12,13,14,15, 11 | 64,64,64,64,64,64,64,64, 12 | 64,64,64,64,64,64,64,64, 13 | 64,64,64,64,64,64,64,64, 14 | 64,64,64,64,64,64,64,64, 15 | 48,49,50,51,52,53,54,55, 16 | 56,57,58,59,60,61,62,63}; 17 | int board[64]={ 18 | -1,-2,-3,-4,-5,-3,-2,-1, 19 | -6,-6,-6,-6,-6,-6,-6,-6, 20 | 0, 0, 0, 0,0, 0, 0, 0, 21 | 0, 0, 0, 0, 0, 0, 0, 0, 22 | 0, 0, 0, 0, 0, 0, 0, 0, 23 | 0, 0, 0, 0, 0, 0, 0, 0, 24 | 6, 6, 6, 6, 6, 6, 6, 6, 25 | 1, 2, 3, 4, 5, 3, 2, 1}; 26 | class ChessBoard{ 27 | private: 28 | const float WIDTH = 650; 29 | const float HEIGTH = 650; 30 | public: 31 | void loadtextures(Texture texture[64]){ 32 | for(int i=0;i<64;i++){ 33 | if(spritepositions[i]==0 || spritepositions[i]==7) 34 | texture[i].loadFromFile("images/blackRook.png"); 35 | if(spritepositions[i]==1 || spritepositions[i]==6) 36 | texture[i].loadFromFile("images/blackKnight.png"); 37 | if(spritepositions[i]==2 || spritepositions[i]==5) 38 | texture[i].loadFromFile("images/blackBishop.png"); 39 | if(spritepositions[i]==3) 40 | texture[i].loadFromFile("images/blackQueen.png"); 41 | if(spritepositions[i]==4) 42 | texture[i].loadFromFile("images/blackKing.png"); 43 | if(spritepositions[i]>=8 and spritepositions[i]<=15) 44 | texture[i].loadFromFile("images/blackPawn.png"); 45 | if(spritepositions[i]==63 || spritepositions[i]==56) 46 | texture[i].loadFromFile("images/whiteRook.png"); 47 | if(spritepositions[i]==62 || spritepositions[i]==57) 48 | texture[i].loadFromFile("images/whiteKnight.png"); 49 | if(spritepositions[i]==61 || spritepositions[i]==58) 50 | texture[i].loadFromFile("images/whiteBishop.png"); 51 | if(spritepositions[i]==59) 52 | texture[i].loadFromFile("images/whiteQueen.png"); 53 | if(spritepositions[i]==60) 54 | texture[i].loadFromFile("images/whiteKing.png"); 55 | if(spritepositions[i]>=48 and spritepositions[i]<=55) 56 | texture[i].loadFromFile("images/whitePawn.png"); 57 | } 58 | } 59 | void loadboard(Texture texture[64],RectangleShape rectangle[64],Sprite sprite[64]){ 60 | for(int j=0;j<64;j++){ 61 | sprite[j].setTexture(texture[j]); 62 | sprite[j].setScale(1.7f,1.7f); 63 | } 64 | for (int j = 0; j < 64; ++j) 65 | { 66 | rectangle[j].setSize(sf::Vector2f(WIDTH/8.0f, HEIGTH/8.0f)); 67 | } 68 | int counter=0; 69 | for (int i = 0; i < 8; ++i) 70 | { 71 | for (int j = 0; j < 8; ++j) 72 | { 73 | rectangle[counter].setPosition(j * rectangle[counter].getSize().y, i * rectangle[counter].getSize().x); ///x,y 74 | sprite[counter].setPosition(j * rectangle[counter].getSize().y, i * rectangle[counter].getSize().x); 75 | if((i+j) % 2 == 0) 76 | rectangle[counter].setFillColor(sf::Color::White); 77 | else 78 | rectangle[counter].setFillColor(sf::Color::Blue); 79 | counter++; 80 | } 81 | } 82 | } 83 | void MainFunctions() 84 | { 85 | sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGTH), "Chess The Game Of Kings!"); 86 | sf::RectangleShape rectangle[64]; 87 | sf::Texture texture[65]; 88 | sf::Sprite sprite[65]; 89 | loadtextures(texture); 90 | loadboard(texture,rectangle,sprite); 91 | Identity box; 92 | bool isMove; 93 | float dx=0,dy=0; 94 | Vector2f oldPos,newPos; 95 | int n; 96 | int cc; 97 | int alience,position; 98 | Vector2f firstpos,secondpos; 99 | int v;int q[64]; 100 | static int cap=0; 101 | for (int j = 0; j < 64; ++j) 102 | q[j]=64; 103 | while (window.isOpen()) 104 | { Vector2i pos= Mouse::getPosition(window); 105 | sf::Event event; 106 | while (window.pollEvent(event)) 107 | { 108 | if (event.type == sf::Event::Closed){ 109 | save s; 110 | window.close(); 111 | if(s.smain()){ 112 | ofstream out,out2; 113 | out.open("spritepositions.txt"); 114 | out2.open("boardpositions.txt") 115 | for(int i=0;i<64;i++){ 116 | out<=56 and board[n]==-6){ 156 | board[j]=-4; 157 | } 158 | else if(j>=0 and j<=7 and board[n]==6){ 159 | board[j]=4; 160 | } 161 | else{ 162 | board[j]=board[n]; 163 | board[n]=0;} 164 | n=j;} 165 | } 166 | int counter=0; 167 | for (int i = 0; i < 8; ++i){ 168 | for (int j = 0; j < 8; ++j){ 169 | if((i+j) % 2 == 0) 170 | rectangle[counter].setFillColor(sf::Color::White); 171 | else 172 | rectangle[counter].setFillColor(sf::Color::Blue); 173 | counter++; 174 | 175 | } 176 | 177 | } 178 | 179 | } 180 | } 181 | cap=0; 182 | 183 | } 184 | } 185 | 186 | window.clear(); 187 | for (int j = 0; j < 64; ++j) 188 | window.draw(rectangle[j]); 189 | for (int j = 0; j < 65; j++){ 190 | if(q[j]==64) 191 | window.draw(sprite[j]); 192 | } 193 | 194 | window.display(); 195 | } 196 | } 197 | }; 198 | -------------------------------------------------------------------------------- /Chess/ChessBoard.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "utilityBox.h" 4 | #include "save.h" 5 | #include 6 | using namespace sf; 7 | using namespace std; 8 | int spritepositions[64]={ 9 | 0,1,2,3,4,5,6,7, 10 | 8,9,10,11,12,13,14,15, 11 | 64,64,64,64,64,64,64,64, 12 | 64,64,64,64,64,64,64,64, 13 | 64,64,64,64,64,64,64,64, 14 | 64,64,64,64,64,64,64,64, 15 | 48,49,50,51,52,53,54,55, 16 | 56,57,58,59,60,61,62,63}; 17 | int board[64]={ 18 | -1,-2,-3,-4,-5,-3,-2,-1, 19 | -6,-6,-6,-6,-6,-6,-6,-6, 20 | 0, 0, 0, 0,0, 0, 0, 0, 21 | 0, 0, 0, 0, 0, 0, 0, 0, 22 | 0, 0, 0, 0, 0, 0, 0, 0, 23 | 0, 0, 0, 0, 0, 0, 0, 0, 24 | 6, 6, 6, 6, 6, 6, 6, 6, 25 | 1, 2, 3, 4, 5, 3, 2, 1}; 26 | class ChessBoard{ 27 | private: 28 | const float WIDTH = 650; 29 | const float HEIGTH = 650; 30 | public: 31 | void loadtextures(Texture texture[64]); 32 | void loadboard(Texture texture[64],RectangleShape rectangle[64],Sprite sprite[64]); 33 | void MainFunctions(int u); 34 | 35 | }; 36 | 37 | void ChessBoard::loadtextures(Texture texture[64]){ 38 | for(int i=0;i<64;i++){ 39 | if(spritepositions[i]==0 || spritepositions[i]==7) 40 | texture[i].loadFromFile("images/blackRook.png"); 41 | if(spritepositions[i]==1 || spritepositions[i]==6) 42 | texture[i].loadFromFile("images/blackKnight.png"); 43 | if(spritepositions[i]==2 || spritepositions[i]==5) 44 | texture[i].loadFromFile("images/blackBishop.png"); 45 | if(spritepositions[i]==3) 46 | texture[i].loadFromFile("images/blackQueen.png"); 47 | if(spritepositions[i]==4) 48 | texture[i].loadFromFile("images/blackKing.png"); 49 | if(spritepositions[i]>=8 and spritepositions[i]<=15) 50 | texture[i].loadFromFile("images/blackPawn.png"); 51 | if(spritepositions[i]==63 || spritepositions[i]==56) 52 | texture[i].loadFromFile("images/whiteRook.png"); 53 | if(spritepositions[i]==62 || spritepositions[i]==57) 54 | texture[i].loadFromFile("images/whiteKnight.png"); 55 | if(spritepositions[i]==61 || spritepositions[i]==58) 56 | texture[i].loadFromFile("images/whiteBishop.png"); 57 | if(spritepositions[i]==59) 58 | texture[i].loadFromFile("images/whiteQueen.png"); 59 | if(spritepositions[i]==60) 60 | texture[i].loadFromFile("images/whiteKing.png"); 61 | if(spritepositions[i]>=48 and spritepositions[i]<=55) 62 | texture[i].loadFromFile("images/whitePawn.png"); 63 | } 64 | } 65 | void ChessBoard::loadboard(Texture texture[64],RectangleShape rectangle[64],Sprite sprite[64]){ 66 | for(int j=0;j<64;j++){ 67 | sprite[j].setTexture(texture[j]); 68 | sprite[j].setScale(1.7f,1.7f); 69 | } 70 | for (int j = 0; j < 64; ++j) 71 | { 72 | rectangle[j].setSize(sf::Vector2f(WIDTH/8.0f, HEIGTH/8.0f)); 73 | } 74 | int counter=0; 75 | for (int i = 0; i < 8; ++i) 76 | { 77 | for (int j = 0; j < 8; ++j) 78 | { 79 | rectangle[counter].setPosition(j * rectangle[counter].getSize().y, i * rectangle[counter].getSize().x); ///x,y 80 | sprite[counter].setPosition(j * rectangle[counter].getSize().y, i * rectangle[counter].getSize().x); 81 | if((i+j) % 2 == 0) 82 | rectangle[counter].setFillColor(sf::Color::White); 83 | else 84 | rectangle[counter].setFillColor(sf::Color::Blue); 85 | counter++; 86 | } 87 | } 88 | } 89 | void ChessBoard::MainFunctions(int u) 90 | { 91 | sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGTH), "Chess The Game Of Kings!"); 92 | sf::RectangleShape rectangle[64]; 93 | sf::Texture texture[65]; 94 | sf::Sprite sprite[65]; 95 | loadtextures(texture); 96 | loadboard(texture,rectangle,sprite); 97 | Identity box; 98 | bool isMove; 99 | float dx=0,dy=0; 100 | Vector2f oldPos,newPos; 101 | int n; 102 | int cc,turn=1; 103 | int alience,position; 104 | Vector2f firstpos,secondpos; 105 | int v;int q[64]; 106 | static int cap=0; 107 | for (int j = 0; j < 64; ++j) 108 | q[j]=64; 109 | while (window.isOpen()) 110 | { Vector2i pos= Mouse::getPosition(window); 111 | sf::Event event; 112 | while (window.pollEvent(event)) 113 | { 114 | if (event.type == sf::Event::Closed){ 115 | save s; 116 | window.close(); 117 | if(s.smain()){ 118 | ofstream out,out2; 119 | out.open("spritepositions.txt"); 120 | out2.open("boardpositions.txt"); 121 | for(int i=0;i<64;i++){ 122 | out<0){ 146 | if (rectangle[j].getGlobalBounds().contains(pos.x,pos.y)) 147 | { 148 | n=j; 149 | firstpos=rectangle[j].getPosition(); 150 | v=spritepositions[j]; 151 | rectangle[n].setFillColor(sf::Color::Red); 152 | if(spritepositions[n]!=64) 153 | cap++; 154 | } 155 | } 156 | } 157 | 158 | } 159 | if(cap!=0) 160 | if (sf::Mouse::isButtonPressed(sf::Mouse::Left)){ 161 | for (int j = 0; j < 64; ++j){ 162 | //cout<=56 and board[n]==-6){ 193 | board[j]=-4; 194 | } 195 | else if(j>=0 and j<=7 and board[n]==6){ 196 | board[j]=4; 197 | } 198 | else{ 199 | board[j]=board[n]; 200 | board[n]=0;} 201 | n=j;} 202 | } 203 | int counter=0; 204 | for (int i = 0; i < 8; ++i){ 205 | for (int j = 0; j < 8; ++j){ 206 | if((i+j) % 2 == 0) 207 | rectangle[counter].setFillColor(sf::Color::White); 208 | else 209 | rectangle[counter].setFillColor(sf::Color::Blue); 210 | counter++; 211 | 212 | } 213 | 214 | } 215 | 216 | } 217 | } 218 | cap=0; 219 | 220 | } 221 | } 222 | } 223 | 224 | window.clear(); 225 | for (int j = 0; j < 64; ++j) 226 | window.draw(rectangle[j]); 227 | for (int j = 0; j < 65; j++){ 228 | if(q[j]==64) 229 | window.draw(sprite[j]); 230 | } 231 | 232 | window.display(); 233 | } 234 | } 235 | --------------------------------------------------------------------------------