Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Include Libraries
- #include <iostream>
- // Import the SFML Graphics Library
- #include <SFML/Graphics.hpp>
- // Change the Namespace in order to use the SFML Functions
- using namespace sf;
- // Size of Each Piece
- int size = 100;
- // State Constants
- const int OUT = 0;
- const int RED = 1;
- const int BLACK = 2;
- const int REDK = 3;
- const int BLACKK = 4;
- const int JUMPED = 5;
- // Board
- int board[8][8];
- // Array of Pieces
- Sprite pieces[24];
- int pieceState[24];
- // Current Player moving
- int turn = RED;
- // Generate Sprites and their position
- void loadPieces(Texture& red, Texture& black) {
- // Load Board
- board[0][0] = board[0][2] = board[0][4] = board[0][6] = 2; // Row 1
- board[0][1] = board[0][3] = board[0][5] = board[0][7] = 0;
- board[1][0] = board[1][2] = board[1][4] = board[1][6] = 0; // Row 2
- board[1][1] = board[1][3] = board[1][5] = board[1][7] = 2;
- board[2][0] = board[2][2] = board[2][4] = board[2][6] = 2; // Row 3
- board[2][1] = board[2][3] = board[2][5] = board[2][7] = 0;
- board[3][0] = board[3][2] = board[3][4] = board[3][6] = 0; // Row 4
- board[3][1] = board[3][3] = board[3][5] = board[3][7] = 0;
- board[4][0] = board[4][2] = board[5][4] = board[4][6] = 0; // Row 5
- board[4][1] = board[4][3] = board[5][5] = board[4][7] = 0;
- board[5][0] = board[5][2] = board[5][4] = board[5][6] = 0; // Row 6
- board[5][1] = board[5][3] = board[5][5] = board[5][7] = 1;
- board[6][0] = board[6][2] = board[6][4] = board[6][6] = 1; // Row 7
- board[6][1] = board[6][3] = board[6][5] = board[6][7] = 0;
- board[7][0] = board[7][2] = board[7][4] = board[7][6] = 0; // Row 8
- board[7][1] = board[7][3] = board[7][5] = board[7][7] = 1;
- // Set Textures and Positions
- int k = 0;
- for (int i = 0; i < 8; i++)
- for (int j = 0; j < 8; j++){
- int n = board[i][j];
- if (n==0) continue;
- if (n==1) {
- Sprite temp;
- temp.setTexture(red);
- pieces[k] = temp;
- pieces[k].setPosition(j * 100, i * 100);
- pieceState[k] = RED;
- } else if (n==2){
- Sprite temp;
- temp.setTexture(black);
- pieces[k] = temp;
- pieces[k].setPosition(j * 100, i * 100);
- pieceState[k] = BLACK;
- }
- k++;
- }
- }
- // Get a sprite array position from a board position
- int getSpritePosFromBoard(int xPos, int yPos) {
- // Search Loop
- for (int i = 0; i < 24; i++) {
- // Get a Sprite and its Coords
- Sprite test = pieces[i];
- int x = int((pieces[i].getPosition().x + 1)/size);
- int y = int((pieces[i].getPosition().y + 1)/size);
- // Test and Return
- if (x == xPos and y == yPos) return i;
- }
- return 24;
- }
- // Validates move
- bool isMoveValid(int oriX, int oriY, int newX, int newY, int state){
- // Calculate Deltas
- int delX = oriX - newX;
- int delY = oriY - newY;
- // Log Move Attempt
- std::cout << "OriX: " + std::to_string(oriX) + "\n";
- std::cout << "OriY: " + std::to_string(oriY) + "\n";
- std::cout << "NewX: " + std::to_string(newX) + "\n";
- std::cout << "NewY: " + std::to_string(newY) + "\n";
- std::cout << "Delta X: " + std::to_string(delX) + "\n";
- std::cout << "Delta Y: " + std::to_string(delY) + "\n";
- // Check if it is the correct player's move
- if ((state == BLACK || state == BLACKK) && turn == RED) return false;
- if ((state == RED || state == REDK) && turn == BLACK) return false;
- // Check if space is empty
- if (board[newY][newX] != 0) return false;
- // Determine if piece is moving to a tan square
- if ((abs(delX) + abs(delY))%2 == 1) return false;
- // Determine if pice moves in the correct direction
- if (state == RED && delY < 1) return false;
- if (state == BLACK && delY > -1) return false;
- // Get the Piece on the Board
- int piece = board[oriY][oriX];
- // If the Piece moves 1 Diagonal
- if (abs(delX) + abs(delY) == 2) return true;
- // If the Piece jumps
- if (abs(delX) + abs(delY) == 4){
- int s = (piece == 1) ? 2 : 1;
- if (board[oriY - delY/2][oriX - delX/2] == s) {
- std::cout << std::to_string(getSpritePosFromBoard(oriX - delX/2, oriY - delY/2)) + "\n";
- pieceState[getSpritePosFromBoard(oriX - delX/2, oriY - delY/2)] = JUMPED;
- return true;
- }
- return false;
- }
- return false;
- }
- // Makes a move after checking if the move is valid.
- void makeMove(int i, int ox, int oy, int nx, int ny, Texture& rk, Texture& bk) {
- // Convert Coords
- int oriX = int((ox + 1)/size);
- int oriY = int((oy + 1)/size);
- int newX = int((nx + 1)/size);
- int newY = int((ny + 1)/size);
- // Make the Move if the piece is valid
- if (isMoveValid(oriX, oriY, newX, newY, pieceState[i])) {
- // Move sprite and change the board
- pieces[i].setPosition(nx, ny);
- board[newY][newX] = board[oriY][oriX];
- board[oriY][oriX] = 0;
- // Removed jumped pieces
- // TO-DO add Multi-Jump Functionality
- for (int i = 0; i < sizeof(pieceState); i++)
- if (pieceState[i] == JUMPED){
- int x = int((pieces[i].getPosition().x + 1)/size);
- int y = int((pieces[i].getPosition().y + 1)/size);
- board[y][x] = 0;
- pieces[i].setPosition(800,800);
- pieceState[i] = OUT;
- }
- // Promote Piece if Applicable
- if (pieceState[i] == RED && newY == 0) {
- pieceState[i] = REDK;
- pieces[i].setTexture(rk);
- } else if (pieceState[i] == BLACK && newY == 7) {
- pieceState[i] = BLACKK;
- pieces[i].setTexture(bk);
- }
- // Change turn
- turn = (turn == RED) ? BLACK : RED;
- }
- // Return the piece to its original position
- else pieces[i].setPosition(ox, oy);
- }
- // Main Game Function
- int main() {
- // Set Window
- RenderWindow window(VideoMode(800,800), "Catesbeianus Checkers");
- // Set Gameboard Variable
- Texture tGameboard;
- tGameboard.loadFromFile("/Users/paulthompson/Documents/Catesbeianus/src/imgs/Gameboard.png");
- Sprite Gameboard;
- Gameboard.setTexture(tGameboard);
- // Get Piece Textures
- Texture red;
- red.loadFromFile("/Users/paulthompson/Documents/Catesbeianus/src/imgs/RedPiece.png");
- Texture black;
- black.loadFromFile("/Users/paulthompson/Documents/Catesbeianus/src/imgs/BlackPiece.png");
- Texture redk;
- redk.loadFromFile("/Users/paulthompson/Documents/Catesbeianus/src/imgs/RedKingPiece.png");
- Texture blackk;
- blackk.loadFromFile("/Users/paulthompson/Documents/Catesbeianus/src/imgs/BlackKingPiece.png");
- // Load Pieces
- loadPieces(red, black);
- // Movement Variables
- bool isMoving = false;
- int inMotion = 24;
- float dx = 0, dy = 0;
- float ox = 0, oy = 0;
- // Game Loop
- while (window.isOpen()){
- // Get Mouse Position
- Vector2i pos = Mouse::getPosition(window);
- // Event Detection
- Event e;
- while(window.pollEvent(e)){
- // Close Window Event
- if (e.type == Event::Closed) window.close();
- // Drag Functionality
- if (e.type == Event::MouseButtonPressed)
- if (e.mouseButton.button == Mouse::Left)
- for (int i = 0; i < 24; i++)
- if (pieces[i].getGlobalBounds().contains(pos.x,pos.y)){
- // Set Old Position
- if (!isMoving) {
- ox = pieces[i].getPosition().x;
- oy = pieces[i].getPosition().y;
- }
- // Calculate the delta of mouse position
- isMoving = true;
- inMotion = i;
- dx = pos.x - pieces[i].getPosition().x;
- dy = pos.y - pieces[i].getPosition().y;
- }
- // Drop Functionality
- if (e.type == Event::MouseButtonReleased)
- if (e.mouseButton.button == Mouse::Left) {
- // Correct the Location of the piece
- isMoving = false;
- // Calculate New Location
- Vector2f pos = pieces[inMotion].getPosition() + Vector2f(size/2,size/2);
- Vector2f nps = Vector2f(size*int(pos.x/size),size*int(pos.y/size));
- // Validate Move and Move Piece
- makeMove(inMotion, ox, oy, nps.x, nps.y, redk, blackk);
- }
- }
- // Move Red Piece
- if (isMoving) pieces[inMotion].setPosition(pos.x - dx, pos.y - dy);
- // Drawing
- window.clear();
- window.draw(Gameboard);
- for (int i = 0; i < 24; i++) window.draw(pieces[i]);
- window.display();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement