Advertisement
Guest User

NaPewnoNieSzachy

a guest
Jan 16th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.13 KB | None | 0 0
  1. // Include Libraries
  2. #include <iostream>
  3.  
  4. // Import the SFML Graphics Library
  5. #include <SFML/Graphics.hpp>
  6.  
  7. // Change the Namespace in order to use the SFML Functions
  8. using namespace sf;
  9.  
  10. // Size of Each Piece
  11. int size = 100;
  12.  
  13. // State Constants
  14. const int OUT = 0;
  15. const int RED = 1;
  16. const int BLACK = 2;
  17. const int REDK = 3;
  18. const int BLACKK = 4;
  19. const int JUMPED = 5;
  20.  
  21. // Board
  22. int board[8][8];
  23.  
  24. // Array of Pieces
  25. Sprite pieces[24];
  26. int pieceState[24];
  27.  
  28. // Current Player moving
  29. int turn = RED;
  30.  
  31. // Generate Sprites and their position
  32. void loadPieces(Texture& red, Texture& black) {
  33.  
  34.     // Load Board
  35.     board[0][0] = board[0][2] = board[0][4] = board[0][6] = 2; // Row 1
  36.     board[0][1] = board[0][3] = board[0][5] = board[0][7] = 0;
  37.     board[1][0] = board[1][2] = board[1][4] = board[1][6] = 0; // Row 2
  38.     board[1][1] = board[1][3] = board[1][5] = board[1][7] = 2;
  39.     board[2][0] = board[2][2] = board[2][4] = board[2][6] = 2; // Row 3
  40.     board[2][1] = board[2][3] = board[2][5] = board[2][7] = 0;
  41.     board[3][0] = board[3][2] = board[3][4] = board[3][6] = 0; // Row 4
  42.     board[3][1] = board[3][3] = board[3][5] = board[3][7] = 0;
  43.     board[4][0] = board[4][2] = board[5][4] = board[4][6] = 0; // Row 5
  44.     board[4][1] = board[4][3] = board[5][5] = board[4][7] = 0;
  45.     board[5][0] = board[5][2] = board[5][4] = board[5][6] = 0; // Row 6
  46.     board[5][1] = board[5][3] = board[5][5] = board[5][7] = 1;
  47.     board[6][0] = board[6][2] = board[6][4] = board[6][6] = 1; // Row 7
  48.     board[6][1] = board[6][3] = board[6][5] = board[6][7] = 0;
  49.     board[7][0] = board[7][2] = board[7][4] = board[7][6] = 0; // Row 8
  50.     board[7][1] = board[7][3] = board[7][5] = board[7][7] = 1;
  51.  
  52.     // Set Textures and Positions
  53.     int k = 0;
  54.     for (int i = 0; i < 8; i++)
  55.         for (int j = 0; j < 8; j++){
  56.             int n = board[i][j];
  57.             if (n==0) continue;
  58.             if (n==1) {
  59.                 Sprite temp;
  60.                 temp.setTexture(red);
  61.                 pieces[k] = temp;
  62.                 pieces[k].setPosition(j * 100, i * 100);
  63.                 pieceState[k] = RED;
  64.             } else if (n==2){
  65.                 Sprite temp;
  66.                 temp.setTexture(black);
  67.                 pieces[k] = temp;
  68.                 pieces[k].setPosition(j * 100, i * 100);
  69.                 pieceState[k] = BLACK;
  70.             }
  71.             k++;
  72.         }
  73. }
  74.  
  75. // Get a sprite array position from a board position
  76. int getSpritePosFromBoard(int xPos, int yPos) {
  77.  
  78.     // Search Loop
  79.     for (int i = 0; i < 24; i++) {
  80.  
  81.         // Get a Sprite and its Coords
  82.         Sprite test = pieces[i];
  83.         int x = int((pieces[i].getPosition().x + 1)/size);
  84.         int y = int((pieces[i].getPosition().y + 1)/size);
  85.        
  86.         // Test and Return
  87.         if (x == xPos and y == yPos) return i;
  88.     }
  89.     return 24;
  90. }
  91.  
  92. // Validates move
  93. bool isMoveValid(int oriX, int oriY, int newX, int newY, int state){
  94.  
  95.     // Calculate Deltas
  96.     int delX = oriX - newX;
  97.     int delY = oriY - newY;
  98.    
  99.     // Log Move Attempt
  100.     std::cout << "OriX: " + std::to_string(oriX) + "\n";
  101.     std::cout << "OriY: " + std::to_string(oriY) + "\n";
  102.     std::cout << "NewX: " + std::to_string(newX) + "\n";
  103.     std::cout << "NewY: " + std::to_string(newY) + "\n";
  104.     std::cout << "Delta X: " + std::to_string(delX) + "\n";
  105.     std::cout << "Delta Y: " + std::to_string(delY) + "\n";
  106.  
  107.     // Check if it is the correct player's move
  108.     if ((state == BLACK || state == BLACKK) && turn == RED) return false;
  109.     if ((state == RED || state == REDK) && turn == BLACK) return false;
  110.  
  111.     // Check if space is empty
  112.     if (board[newY][newX] != 0) return false;
  113.  
  114.     // Determine if piece is moving to a tan square
  115.     if ((abs(delX) + abs(delY))%2 == 1) return false;
  116.  
  117.     // Determine if pice moves in the correct direction
  118.     if (state == RED && delY < 1) return false;
  119.     if (state == BLACK && delY > -1) return false;
  120.    
  121.     // Get the Piece on the Board
  122.     int piece = board[oriY][oriX];
  123.  
  124.     // If the Piece moves 1 Diagonal
  125.     if (abs(delX) + abs(delY) == 2) return true;
  126.  
  127.     // If the Piece jumps
  128.     if (abs(delX) + abs(delY) == 4){
  129.         int s = (piece == 1) ? 2 : 1;
  130.         if (board[oriY - delY/2][oriX - delX/2] == s) {
  131.             std::cout << std::to_string(getSpritePosFromBoard(oriX - delX/2, oriY - delY/2)) + "\n";
  132.             pieceState[getSpritePosFromBoard(oriX - delX/2, oriY - delY/2)] = JUMPED;
  133.             return true;
  134.         }
  135.         return false;
  136.     }
  137.  
  138.     return false;
  139. }
  140.  
  141. // Makes a move after checking if the move is valid.
  142. void makeMove(int i, int ox, int oy, int nx, int ny, Texture& rk, Texture& bk) {
  143.    
  144.     // Convert Coords
  145.     int oriX = int((ox + 1)/size);
  146.     int oriY = int((oy + 1)/size);
  147.     int newX = int((nx + 1)/size);
  148.     int newY = int((ny + 1)/size);
  149.  
  150.     // Make the Move if the piece is valid
  151.     if (isMoveValid(oriX, oriY, newX, newY, pieceState[i])) {
  152.  
  153.         // Move sprite and change the board
  154.         pieces[i].setPosition(nx, ny);
  155.         board[newY][newX] = board[oriY][oriX];
  156.         board[oriY][oriX] = 0;
  157.  
  158.         // Removed jumped pieces
  159.         // TO-DO add Multi-Jump Functionality
  160.         for (int i = 0; i < sizeof(pieceState); i++)
  161.             if (pieceState[i] == JUMPED){
  162.                 int x = int((pieces[i].getPosition().x + 1)/size);
  163.                 int y = int((pieces[i].getPosition().y + 1)/size);
  164.                 board[y][x] = 0;
  165.                 pieces[i].setPosition(800,800);
  166.                 pieceState[i] = OUT;
  167.             }
  168.  
  169.         // Promote Piece if Applicable
  170.         if (pieceState[i] == RED && newY == 0) {
  171.             pieceState[i] = REDK;
  172.             pieces[i].setTexture(rk);
  173.         } else if (pieceState[i] == BLACK && newY == 7) {
  174.             pieceState[i] = BLACKK;
  175.             pieces[i].setTexture(bk);
  176.         }
  177.  
  178.         // Change turn
  179.         turn = (turn == RED) ? BLACK : RED;
  180.  
  181.     }
  182.    
  183.     // Return the piece to its original position
  184.     else pieces[i].setPosition(ox, oy);
  185. }
  186.  
  187. // Main Game Function
  188. int main() {
  189.  
  190.     // Set Window
  191.     RenderWindow window(VideoMode(800,800), "Catesbeianus Checkers");
  192.  
  193.      // Set Gameboard Variable
  194.     Texture tGameboard;
  195.     tGameboard.loadFromFile("/Users/paulthompson/Documents/Catesbeianus/src/imgs/Gameboard.png");
  196.     Sprite Gameboard;
  197.     Gameboard.setTexture(tGameboard);
  198.  
  199.     // Get Piece Textures
  200.     Texture red;
  201.     red.loadFromFile("/Users/paulthompson/Documents/Catesbeianus/src/imgs/RedPiece.png");
  202.     Texture black;
  203.     black.loadFromFile("/Users/paulthompson/Documents/Catesbeianus/src/imgs/BlackPiece.png");
  204.     Texture redk;
  205.     redk.loadFromFile("/Users/paulthompson/Documents/Catesbeianus/src/imgs/RedKingPiece.png");
  206.     Texture blackk;
  207.     blackk.loadFromFile("/Users/paulthompson/Documents/Catesbeianus/src/imgs/BlackKingPiece.png");
  208.  
  209.  
  210.     // Load Pieces
  211.     loadPieces(red, black);
  212.  
  213.     // Movement Variables
  214.     bool isMoving = false;
  215.     int inMotion = 24;
  216.     float dx = 0, dy = 0;
  217.     float ox = 0, oy = 0;
  218.  
  219.     // Game Loop
  220.     while (window.isOpen()){
  221.  
  222.         // Get Mouse Position
  223.         Vector2i pos = Mouse::getPosition(window);
  224.  
  225.         // Event Detection
  226.         Event e;
  227.         while(window.pollEvent(e)){
  228.            
  229.             // Close Window Event
  230.             if (e.type == Event::Closed) window.close();
  231.  
  232.             // Drag Functionality
  233.             if (e.type == Event::MouseButtonPressed)
  234.                 if (e.mouseButton.button == Mouse::Left)
  235.                     for (int i = 0; i < 24; i++)
  236.                         if (pieces[i].getGlobalBounds().contains(pos.x,pos.y)){
  237.  
  238.                             // Set Old Position
  239.                             if (!isMoving) {
  240.                                 ox = pieces[i].getPosition().x;
  241.                                 oy = pieces[i].getPosition().y;
  242.                             }
  243.  
  244.                             // Calculate the delta of mouse position
  245.                             isMoving = true;
  246.                             inMotion = i;
  247.                             dx = pos.x - pieces[i].getPosition().x;
  248.                             dy = pos.y - pieces[i].getPosition().y;
  249.                         }
  250.                    
  251.  
  252.             // Drop Functionality
  253.             if (e.type == Event::MouseButtonReleased)
  254.                 if (e.mouseButton.button == Mouse::Left) {
  255.  
  256.                     // Correct the Location of the piece
  257.                     isMoving = false;
  258.  
  259.                     // Calculate New Location
  260.                     Vector2f pos = pieces[inMotion].getPosition() + Vector2f(size/2,size/2);
  261.                     Vector2f nps = Vector2f(size*int(pos.x/size),size*int(pos.y/size));
  262.  
  263.                     // Validate Move and Move Piece
  264.                     makeMove(inMotion, ox, oy, nps.x, nps.y, redk, blackk);
  265.                 }
  266.  
  267.         }
  268.  
  269.         // Move Red Piece
  270.         if (isMoving) pieces[inMotion].setPosition(pos.x - dx, pos.y - dy);
  271.  
  272.         // Drawing
  273.         window.clear();
  274.         window.draw(Gameboard);
  275.         for (int i = 0; i < 24; i++) window.draw(pieces[i]);
  276.         window.display();
  277.     }
  278.  
  279.     return 0;
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement