From 35183683878f792d2162ab29258fc44d0589cf91 Mon Sep 17 00:00:00 2001 From: Michael Kowalczyk Date: Wed, 20 Nov 2024 12:03:51 -0500 Subject: [PATCH] Day 49 --- .../Chess.class | Bin 6502 -> 6762 bytes .../Chess.java | 13 +++--- .../GameState.class | Bin 2433 -> 3455 bytes .../GameState.java | 43 ++++++++++++++++-- .../Move.class | Bin 277 -> 1168 bytes .../Move.java | 28 +++++++++++- .../control structure notes.txt | 18 ++++++++ 7 files changed, 91 insertions(+), 11 deletions(-) create mode 100644 Day 43, 45, 46 (chess application design)/control structure notes.txt diff --git a/Day 43, 45, 46 (chess application design)/Chess.class b/Day 43, 45, 46 (chess application design)/Chess.class index 00d69e0886ca7c5372d3f971e003e31e8554cc95..a37f7e6c040787ce3954c13b7cb31322ba49b4ff 100644 GIT binary patch delta 2192 zcmai#cYIVu5XXPNOYZjNu`w~BLj(~diO4CSh**NCsB~jQ><2!=VUmN~B_UW25y9S` zK?D^G7Az=6gT43OdoS2~R}^P9fFI!xFPHE9X5Z}Y?9Sw6&7d!aWS{)|&U*n2BqZZf#pM-EmRtnv}XEgo8Z+=|;2Z3e4j70oh5KEC|iuDByl=G|#@V^xd%GK)k6F-ZFUG$KdwF-Z6OB$Ig1x z!FVaFxxS}(U-5zB!y=jgnI7x&B2ZNszA8clpB2f*;x^&iSjGJN-7b13-zmOV{Gj+z zA?LUiKjqMqk(BFVCy$-I-t6MBtH*90yL;^6 zv8Trpk6U@{WzcHayEQ*DFOs{pVV^L}?bErWbj*xIV`AFuc+eQsW#S{IW|FD8@cG=K z;cdD7LK7X}wP2CuHdqo4j}Ge5!oEawOF6M>AnLMKP7fSO$_IF2@y?(^zdeokNLO<*bH8ePEg742uw_Ssu$8iwi{I z48@s>vlM45PF0+)I7e}=;ylIq0@mt%*0FQSaHi!f)?22TF>J7GWK-*Rra783o6TWO ze$SjH&I!-RukO*r8z)|qsH=`wHm5T|Es>LI z$Pk@eOU09^_=NgoU8XWqA0%pr6UEg*W<*W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w?XR8_I6k!LTDc8FpqD!>&Buup9FYyDNGy-ya|9)*`{44tp8)<_U&< zc%tD+JlWwXhNtp0e{HCJQi9q3&QNDJ!2-j+?B|D_%=m1~krob!Op6YSMPgBdi!sOG zQpFXB8(fY!5$;!B9;=L1L}m<$#!Aa4R5lE;FwFi9gFHQq<%VZ)fZ;$6l7TbjJWI~O z21~V3Lo~^uJlpUbRp+WY55B`;hUYV0WOxAu#k|n4L^8u&xZ7|zM;MM|Cn7X#=B!9X z)Nm9>8{CR(499S+!9r2EPI0~B2E~nvs}O!|7 zm^6V=e?wY!Qj8P*9ch!&Vw@zqlBJ5t)g=vvGTUL9;S`n|+%E;=0mVIv6<&;L(`Im?>IIJTBo&As%A(mo;9oBS*sp55+>z~Khx#`u3dqpXro{vAlo@@HQG)<$;IP<1)nU0!k|PU41jKc) ztOkP}6y??)d50kXp!_9LHk01d?~|DmKbr7-8I#4+EAoJ5a%yV-t80M-$nw?~wn%2* zi~H&ei!YT3**x{dt&jlOUiF2ogo~AR>$6=x$$Bi@goz~?lh&iO8l$bAZ1tE;h|7gC z>kPLSrdU0~>Zw+bw0fG=qpY58^;oORt&Uhd!|HKX&$N2H)fHA3TU}{&N!s#P t;6ceTfQL9;tOs%`XKi7a*V)uV)U!D4@PJNBt{{qU7h5Y~k diff --git a/Day 43, 45, 46 (chess application design)/Chess.java b/Day 43, 45, 46 (chess application design)/Chess.java index a5f6622..c1bd178 100644 --- a/Day 43, 45, 46 (chess application design)/Chess.java +++ b/Day 43, 45, 46 (chess application design)/Chess.java @@ -91,9 +91,9 @@ public class Chess extends Application { //Set up mouse/click events board.setOnMousePressed(this::mouseClick); - undo.setOnMousePressed(this::undo); - reset.setOnMousePressed(this::reset); - aiMove.setOnMousePressed(this::moveAI); + undo.setOnAction(this::undo); + reset.setOnAction(this::reset); + aiMove.setOnAction(this::moveAI); //Refresh the board paintBoard(); @@ -133,7 +133,8 @@ public class Chess extends Application { } //Reset to the intial game position. - public void reset(MouseEvent e) { + public void reset(ActionEvent e) { + System.out.println(board().getAllPossibleMoves() ); //Set up the initial board //Clear out clickRo and clickCol //Clera out all the histories. @@ -141,7 +142,7 @@ public class Chess extends Application { } //AI makes a move - public void moveAI(MouseEvent e) { + public void moveAI(ActionEvent e) { //Ask the gameState for a list of all legal moves. //Get one of those in the list uniformly at random. // makeMove(...); @@ -162,7 +163,7 @@ public class Chess extends Application { } //Undo button for the most recent move - public void undo(MouseEvent e) { + public void undo(ActionEvent e) { //Pop those 3 history object things and update the GUI. } diff --git a/Day 43, 45, 46 (chess application design)/GameState.class b/Day 43, 45, 46 (chess application design)/GameState.class index a694af17c581895c1682479184d6dd3ef8d345ad..d1724e960460fb998ae39d91d0077a6e993ec0d9 100644 GIT binary patch delta 1396 zcmb7DO-~zF6g|&l&l?ZpFo~HEW1K*N#Kt6UoL~~$kWv~_TBoi=QZ!*xCpgrpg;QgY zBBin_e0sfb|3=|dW@De5~G zu43}Z2coy`nVsTAbSn0dfG^6Dxyt!s?Z$)ql&(c%yw(eIN}WI``k&{UsFcgaJGD|} zArrerQfYDKlTxio;+er3K{b*EoPB1wurQy<-@Zp8S?KL&-;T1^9)46qgi=e?Ay{cA z@x=P7cqrO!-Mb0jv}>Q-g610QXSE%TMy+YAHjOFyx#$g%ehgk>8!-Tmi@t|mhU!9C z1$^?t=&36&l>`_db|a#0c+f2mRC;tzw_khd9157McS|+KxUX{hClIYCQ@{=p* zJMeqGyq1%11zDw{QHX6)g?&T?>r}x)O2(f`#;*+-KR0Cj)ROVYBi457KgQMjdK&MW zgvEUV29g|t3+%;3c4ZhtxP%fe;~S*$92xwAtVkd!hH(WwWSoHiZ^rI*fBm% z`BfhsziMR4RQoWu7B`-+*-a!Jv(a6|9P=qUTF(0(!H{Z^E^mmEx;X}NSyU1agcMo~$q30QJk~?5X*g|4}pHvH{ mc$d#2gyFnG`>U4z)125d460`Syg%stJIgm|i!q=FgZ}^%00^Z3 delta 383 zcmaKm%SyvQ7=_Pda+yifBwDYjwn?jyfNpf*8wdrvDpsm17hQ`5T`A%tNZ!G1g@A&B z58xxYu%1CdL2!{T{QrFC%*>M8#@5@%^DBToZ2C}WYIs=lumWzaU8)I;W>b9Y#2 zofjtu{jG=o1Oo2h{Ni*n4p2weU?YHrUmjW6uATRap@5V3j*B5O_9@{GigCQAJO?f3EWk10gjN;Y{rJZw^vH8ON( zXtQdLdlr^`KfN)Q%L(X`4J?=wP6_|!IhJ@J1%AAO(;}9Bc_#hW+orLMmH)kv(H$zQ HrUJ<)6(%Qo diff --git a/Day 43, 45, 46 (chess application design)/GameState.java b/Day 43, 45, 46 (chess application design)/GameState.java index 0dcdd84..10fa681 100644 --- a/Day 43, 45, 46 (chess application design)/GameState.java +++ b/Day 43, 45, 46 (chess application design)/GameState.java @@ -1,4 +1,6 @@ +import java.util.*; + // We are making this an *immutable* class. The state that an object has when constructed is its state permanently. // Instead of making changes to objects we create new ones (with attemptMove() and the constructor). @@ -14,7 +16,7 @@ public class GameState { boardSquares = new Square[] { new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), - new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.ROOK, true), new Square(Square.KNIGHT, true), new Square(Square.BISHOP, true), new Square(Square.QUEEN, true), new Square(Square.KING, true), new Square(Square.BISHOP, true), new Square(Square.KNIGHT, true), new Square(Square.ROOK, true), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), //white + new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.ROOK, true), new Square(Square.KNIGHT, true), new Square(Square.BISHOP, true), new Square(Square.KING, true), new Square(Square.QUEEN, true), new Square(Square.BISHOP, true), new Square(Square.KNIGHT, true), new Square(Square.ROOK, true), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), //white new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.PAWN, true), new Square(Square.PAWN, true), new Square(Square.PAWN, true), new Square(Square.PAWN, true), new Square(Square.PAWN, true), new Square(Square.PAWN, true), new Square(Square.PAWN, true), new Square(Square.PAWN, true), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), //white pawns new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.EMPTY), new Square(Square.EMPTY), new Square(Square.EMPTY), new Square(Square.EMPTY), new Square(Square.EMPTY), new Square(Square.EMPTY), new Square(Square.EMPTY), new Square(Square.EMPTY), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), @@ -23,7 +25,7 @@ public class GameState { new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.EMPTY), new Square(Square.EMPTY), new Square(Square.EMPTY), new Square(Square.EMPTY), new Square(Square.EMPTY), new Square(Square.EMPTY), new Square(Square.EMPTY), new Square(Square.EMPTY), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.PAWN, false), new Square(Square.PAWN, false), new Square(Square.PAWN, false), new Square(Square.PAWN, false), new Square(Square.PAWN, false), new Square(Square.PAWN, false), new Square(Square.PAWN, false), new Square(Square.PAWN, false), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), //white pawns - new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.ROOK, false), new Square(Square.KNIGHT, false), new Square(Square.BISHOP, false), new Square(Square.QUEEN, false), new Square(Square.KING, false), new Square(Square.BISHOP, false), new Square(Square.KNIGHT, false), new Square(Square.ROOK, false), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), //black + new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.ROOK, false), new Square(Square.KNIGHT, false), new Square(Square.BISHOP, false), new Square(Square.KING, false), new Square(Square.QUEEN, false), new Square(Square.BISHOP, false), new Square(Square.KNIGHT, false), new Square(Square.ROOK, false), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), //black new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), }; @@ -41,8 +43,41 @@ public class GameState { return null; } - public Move[] getAllPossibleMoves() { - return null; + public ArrayList getAllPossibleMoves() { + ArrayList moves = new ArrayList(); + + //Loop through all squares + for(int index = 0; index < boardSquares.length; index++) { + int type = boardSquares[index].getType(); + if (type == Square.OUT_OF_BOUNDS) continue; + if (type == Square.EMPTY) continue; + + //For each square, find all possible places that piece can move to. + if (type == Square.KING) moves.addAll(findAllMovesFor(index, new int[] {1,-1,12,-12,13,-13,11,-11}, false)); + if (type == Square.QUEEN) moves.addAll(findAllMovesFor(index, new int[] {1,-1,12,-12,13,-13,11,-11}, true)); + if (type == Square.BISHOP) moves.addAll(findAllMovesFor(index, new int[] {13,-13,11,-11}, true)); + if (type == Square.ROOK) moves.addAll(findAllMovesFor(index, new int[] {1,-1,12,-12}, true)); + if (type == Square.KNIGHT) moves.addAll(findAllMovesFor(index, new int[] {10,14,23,25,-10,-14,-23,-25}, false)); + } + + return moves; + } + + private ArrayList findAllMovesFor(int index, int[] directions, boolean canMoveMultipleSquares) { + ArrayList moves = new ArrayList(); + //loop through the directions, see if they work - return the resulting arraylist. + for(int dir : directions) { + int destIndex = index + dir; + int type = boardSquares[destIndex].getType(); + if (boardSquares[index].isWhite() != isWhitesTurn) continue; //don't move other person's pieces! + + if (type == Square.OUT_OF_BOUNDS) continue; + if (type == Square.EMPTY || boardSquares[destIndex].isWhite() != isWhitesTurn) { + moves.add(new Move(index, destIndex)); + } + + } + return moves; } // returns something like "checkmate", "stalemate", etc. diff --git a/Day 43, 45, 46 (chess application design)/Move.class b/Day 43, 45, 46 (chess application design)/Move.class index 42b46f1952a9dafa63d564d27e6cadc8f38a287d..f4ea5be09400313368f21f0d8de1a42addb14efc 100644 GIT binary patch literal 1168 zcmaJ=T~8BH5Iwi0-F8_hA_4-+hoEic!*5J#c(8`p1T+zX4?ZpJ4J<5so81e_Kk-NS zpowV0gFnC@WjuFxNh>ry%-NmUGiUD1o&Eji{1<>#Jhx$Es)!`1$zZUim)NkB9NiY*yk`1u!J;ORAiT)EMx>Ez z;kLW$Gh&gDP@Fxfx72ail<(9>fo!=#(N3_gd=@3=70bko3n%_k&}vJEUa%)HQm$+q zxF_z4?*@A-Nwmh?M10*dWs95YL@pbagF#dr^uuv5VBk7N9NaK4YG4c_0z)sA(vc3` z_NLVPsuh*;s1|VF!6KH}olzsJQ}ylcfo$ryiya%wSP>X3i$zhHSz4QkUj)XE+(TJc zLDSXmJ-yF2jx^Abz<4s0t6f-lU`jp_nD5l`f)jNp(?%1$b+@TicxvI9Kz>81!{at* z*$4s|)_pgMB#V45cj>1ZNq4W^pykVGK1r-~(b8ddMgB?LK5fe$^bcM9WH7bv8%ibW zuJ3PoTGlMA3M^dduh-cGZvUT$SuU>4z0J^g$3&QGCR*T~phO&^_n3?2`dragNXN_k|?wi4<%+voMI+3 z`OX~tvr1p@`vX7^aR3`t4>gTQqb_j!d6Dmi0^J_-I&*w&KB9q z9G?T%K|_F3Dy;$uEN+#jAaLMPHB<6UfYZG}t}Fx|H6X^4FD2+R|L`WAo3LVa`NJgw Kgw&9(Q279*r58H@ diff --git a/Day 43, 45, 46 (chess application design)/Move.java b/Day 43, 45, 46 (chess application design)/Move.java index cd00174..9cd2fe9 100644 --- a/Day 43, 45, 46 (chess application design)/Move.java +++ b/Day 43, 45, 46 (chess application design)/Move.java @@ -1,14 +1,40 @@ public class Move { //source and dest coordinates + int startIndex; + int destIndex; + + + public Move(int startIndex, int destIndex) { + this.startIndex = startIndex; + this.destIndex = destIndex; + } public Move(int sourceRow, int sourceColumn, int destinationRow, int destinationColumn) { + //TODO + } + + public int getSourceRow() { + return startIndex / 12 - 2; + } + + public int getSourceCol() { + return startIndex % 12 - 2; + } + + public int getDestRow() { + return destIndex / 12 - 2; + + } + + public int getDestCol() { + return destIndex % 12 - 2; } //returns something like e2-e4 or Qc8++ public String toString() { - return ""; + return "("+getSourceRow()+","+getSourceCol() + ") -> " + "("+getDestRow()+","+getDestCol() + ")"; } } diff --git a/Day 43, 45, 46 (chess application design)/control structure notes.txt b/Day 43, 45, 46 (chess application design)/control structure notes.txt new file mode 100644 index 0000000..eb2e77e --- /dev/null +++ b/Day 43, 45, 46 (chess application design)/control structure notes.txt @@ -0,0 +1,18 @@ + +When should you use break or continue in a loop? + When it makes your code easier to read/maintain/understand as opposed to working it into the boolean condition for the loop. + The danger of using break or continue in a large loop is that it becomes 'hidden' and people might not realize that something inside the loop affects its control flow. + +What are the advantages/disadvantages of switch...case? + + The expression is only checked once in one place, and would only need to changed in one place + - You have to keep putting 'break;' at the end of each case if you don't want the case to "fall through". If you forget, it still compiles. + + You can exploit the fall-through behavior, as follows: + for(int days = 1; days <= 12; days++) { + switch (days) { + case 12: ...three french hens... + ... + case 3: ...three french hens... + case 2: ...two turtle doves... + case 1: ...partridge in a pear tree... + } + }