Mert Us Matthew Hannay Logan Starr

Dependencies:   mbed 4DGL-uLCD-SE

Revision:
8:928d5d33258f
Parent:
7:9e01c9a334dc
Child:
9:1f33c0f299ae
diff -r 9e01c9a334dc -r 928d5d33258f main.cpp
--- a/main.cpp	Wed Nov 16 01:06:20 2022 +0000
+++ b/main.cpp	Thu Nov 17 18:18:21 2022 +0000
@@ -67,6 +67,7 @@
     std::vector<boardPos> getMoves(int row, int column)
     {
         std::vector<boardPos> moves;
+        std::vector<boardPos> lineMoves;
         Piece movingPiece = getPiece(row, column);
         bool isWhite;
         switch(movingPiece) {
@@ -86,118 +87,34 @@
             case wQ:
             case bQ:
                 isWhite = movingPiece == wQ;
-                for (int i = 1; ; i++) {
-                    if (validMove(isWhite, row + i, column + i)) {
-                        moves.push_back((boardPos) {
-                            row + i, column + i
-                        });
-                    } else {
-                        break;
-                    }
-                }
-                for (int i = 1; ; i++) {
-                    if (validMove(isWhite, row - i, column + i)) {
-                        moves.push_back((boardPos) {
-                            row - i, column + i
-                        });
-                    } else {
-                        break;
-                    }
-                }
-                for (int i = 1; ; i++) {
-                    if (validMove(isWhite, row + i, column - i)) {
-                        moves.push_back((boardPos) {
-                            row + i, column - i
-                        });
-                    } else {
-                        break;
-                    }
-                }
-                for (int i = 1; ; i++) {
-                    if (validMove(isWhite, row - i, column - i)) {
-                        moves.push_back((boardPos) {
-                            row - i, column - i
-                        });
-                    } else {
-                        break;
-                    }
-                }
-                for (int i = 1; ; i++) {
-                    if (validMove(isWhite, row + i, column)) {
-                        moves.push_back((boardPos) {
-                            row + i, column
-                        });
-                    } else {
-                        break;
-                    }
-                }
-                for (int i = 1; ; i++) {
-                    if (validMove(isWhite, row, column + i)) {
-                        moves.push_back((boardPos) {
-                            row, column + i
-                        });
-                    } else {
-                        break;
-                    }
-                }
-                for (int i = 1; ; i++) {
-                    if (validMove(isWhite, row, column - i)) {
-                        moves.push_back((boardPos) {
-                            row, column - i
-                        });
-                    } else {
-                        break;
-                    }
-                }
-                for (int i = 1; ; i++) {
-                    if (validMove(isWhite, row - i, column)) {
-                        moves.push_back((boardPos) {
-                            row - i, column
-                        });
-                    } else {
-                        break;
-                    }
-                }
+                lineMoves = movesInLine(isWhite, row, column, 1, 0);
+                moves.insert(moves.end(), lineMoves.begin(), moves.end());
+                lineMoves = movesInLine(isWhite, row, column, -1, 0);
+                moves.insert(moves.end(), lineMoves.begin(), moves.end());
+                lineMoves = movesInLine(isWhite, row, column, 0, 1);
+                moves.insert(moves.end(), lineMoves.begin(), moves.end());
+                lineMoves = movesInLine(isWhite, row, column, 0, -1);
+                moves.insert(moves.end(), lineMoves.begin(), moves.end());
+                lineMoves = movesInLine(isWhite, row, column, 1, 1);
+                moves.insert(moves.end(), lineMoves.begin(), moves.end());
+                lineMoves = movesInLine(isWhite, row, column, -1, 1);
+                moves.insert(moves.end(), lineMoves.begin(), moves.end());
+                lineMoves = movesInLine(isWhite, row, column, 1, -1);
+                moves.insert(moves.end(), lineMoves.begin(), moves.end());
+                lineMoves = movesInLine(isWhite, row, column, -1, -1);
+                moves.insert(moves.end(), lineMoves.begin(), moves.end());
                 break;
             case wB:
             case bB:
                 isWhite = movingPiece == wB;
-                for (int i = 1; ; i++) {
-                    if (validMove(isWhite, row + i, column + i)) {
-                        moves.push_back((boardPos) {
-                            row + i, column + i
-                        });
-                    } else {
-                        break;
-                    }
-                }
-                for (int i = 1; ; i++) {
-                    if (validMove(isWhite, row - i, column + i)) {
-                        moves.push_back((boardPos) {
-                            row - i, column + i
-                        });
-                    } else {
-                        break;
-                    }
-                }
-                for (int i = 1; ; i++) {
-                    if (validMove(isWhite, row + i, column - i)) {
-                        moves.push_back((boardPos) {
-                            row + i, column - i
-                        });
-                    } else {
-                        break;
-                    }
-                }
-                for (int i = 1; ; i++) {
-                    if (validMove(isWhite, row - i, column - i)) {
-                        moves.push_back((boardPos) {
-                            row - i, column - i
-                        });
-                    } else {
-                        break;
-                    }
-                }
+                lineMoves = movesInLine(isWhite, row, column, 1, 1);
+                moves.insert(moves.end(), lineMoves.begin(), moves.end());
+                lineMoves = movesInLine(isWhite, row, column, -1, 1);
+                moves.insert(moves.end(), lineMoves.begin(), moves.end());
+                lineMoves = movesInLine(isWhite, row, column, 1, -1);
+                moves.insert(moves.end(), lineMoves.begin(), moves.end());
+                lineMoves = movesInLine(isWhite, row, column, -1, -1);
+                moves.insert(moves.end(), lineMoves.begin(), moves.end());
                 break;
             case wN:
             case bN:
@@ -220,42 +137,14 @@
             case wR:
             case bR:
                 isWhite = movingPiece == wR;
-                for (int i = 1; ; i++) {
-                    if (validMove(isWhite, row + i, column)) {
-                        moves.push_back((boardPos) {
-                            row + i, column
-                        });
-                    } else {
-                        break;
-                    }
-                }
-                for (int i = 1; ; i++) {
-                    if (validMove(isWhite, row, column + i)) {
-                        moves.push_back((boardPos) {
-                            row, column + i
-                        });
-                    } else {
-                        break;
-                    }
-                }
-                for (int i = 1; ; i++) {
-                    if (validMove(isWhite, row, column - i)) {
-                        moves.push_back((boardPos) {
-                            row, column - i
-                        });
-                    } else {
-                        break;
-                    }
-                }
-                for (int i = 1; ; i++) {
-                    if (validMove(isWhite, row - i, column)) {
-                        moves.push_back((boardPos) {
-                            row - i, column
-                        });
-                    } else {
-                        break;
-                    }
-                }
+                lineMoves = movesInLine(isWhite, row, column, 1, 0);
+                moves.insert(moves.end(), lineMoves.begin(), moves.end());
+                lineMoves = movesInLine(isWhite, row, column, -1, 0);
+                moves.insert(moves.end(), lineMoves.begin(), moves.end());
+                lineMoves = movesInLine(isWhite, row, column, 0, 1);
+                moves.insert(moves.end(), lineMoves.begin(), moves.end());
+                lineMoves = movesInLine(isWhite, row, column, 0, -1);
+                moves.insert(moves.end(), lineMoves.begin(), moves.end());
                 break;
             case w:
             case b:
@@ -280,7 +169,31 @@
         }
         return moves;
     }
+    
+    // returns a vector of board positions that a piece can move in a line
+    std::vector<boardPos> movesInLine(bool isMovingPieceWhite, int row, int column, int rowChange, int columnChange)
+    {
+        std::vector<boardPos> moves;
+        for (int i = 1; ; i++) {
+            // check if piece can move to location
+            if (validMove(isMovingPieceWhite, row + i * rowChange, column + i * columnChange)) {
+                moves.push_back((boardPos) {
+                    row + i * rowChange, column + i * columnChange
+                });
+                // if piece is capturable, stop moving beyond it
+                if (getPiece(row + i * rowChange, column + i * columnChange) != e)
+                {
+                    break;
+                }
+            // if unable to move, break out
+            } else {
+                break;
+            }
+        }
+        return moves;
+    }
 
+    // returns if a piece can move to a given location
     bool validMove(bool isMovingPieceWhite, int row, int column)
     {
         if (row < 0 || row > 7 || column < 0 || column > 7) {
@@ -632,6 +545,17 @@
         pixelCoord tl = getTopLeftOfSquare(row, column);
         uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, SELECTED_COLOR);
     }
+    
+    // draws the movement border around square
+    void moveSquare(boardPos pos)
+    {
+        moveSquare(pos.row, pos.column);
+    }
+    void moveSquare(int row, int column)
+    {
+        pixelCoord tl = getTopLeftOfSquare(row, column);
+        uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, MOVE_COLOR);
+    }
 };
 
 // game variables
@@ -641,16 +565,12 @@
 {
     3, 4
 };
-boardPos selectedPos = (boardPos)
-{
-    3, 4
-};
+boardPos selectedPos;
 
 // callbacks
 void joyStickUp()
 {
-    if (cursorPos.row < 7)
-    {
+    if (cursorPos.row < 7) {
         boardPos nextPos;
         nextPos.row = cursorPos.row + 1;
         nextPos.column = cursorPos.column;
@@ -662,8 +582,7 @@
 
 void joyStickDown()
 {
-    if (cursorPos.row > 0)
-    {
+    if (cursorPos.row > 0) {
         boardPos nextPos;
         nextPos.row = cursorPos.row - 1;
         nextPos.column = cursorPos.column;
@@ -675,8 +594,7 @@
 
 void joyStickLeft()
 {
-    if (cursorPos.column > 0)
-    {
+    if (cursorPos.column > 0) {
         boardPos nextPos;
         nextPos.row = cursorPos.row;
         nextPos.column = cursorPos.column - 1;
@@ -688,8 +606,7 @@
 
 void joyStickRight()
 {
-    if (cursorPos.column < 7)
-    {
+    if (cursorPos.column < 7) {
         boardPos nextPos;
         nextPos.row = cursorPos.row;
         nextPos.column = cursorPos.column + 1;
@@ -701,7 +618,17 @@
 
 void joyStickPressed()
 {
-    // state machine goes here
+    switch(state) {
+        case whiteSelecting:
+            selectedPos = cursorPos;
+            break;
+        case whitePickedUp:
+            break;
+        case blackSelecting:
+            break;
+        case blackPickedUp:
+            break;
+    }
 }
 
 int main()