Add win/lose screen

main
Elnu 2 years ago
parent f16448bdf0
commit 884fc44ed4

@ -30,12 +30,17 @@ public class Board {
ArrayList<BoardCoordinate> legalMoves = null;
Stack<Move> moveHistory;
public boolean isGameOver;
public boolean victor;
Image youWin;
Image youLose;
public Board() {
moveHistory = new Stack<>();
// Initialize DrawingPanel
panel = new DrawingPanel(DIMENSION, DIMENSION);
youWin = panel.loadImage("you-win.png");
youLose = panel.loadImage("you-lose.png");
graphics = panel.getGraphics();
// Connect up event handlers
@ -46,6 +51,11 @@ public class Board {
});
panel.onMouseUp(this::handleMouseUp);
// Initialize board
setup();
}
public void setup() {
// Initialize board
board = new Piece[BOARD_SIZE][BOARD_SIZE];
@ -131,6 +141,11 @@ public class Board {
// Mouse down event handler
// This sets the currently dragging piece
void handleMouseDown(int x, int y) {
if (isGameOver) {
isGameOver = false;
setup();
return;
}
// Get board coordinate of mouse click
BoardCoordinate coordinate = new ScreenCoordinate(x, y).toBoard();
// If there's no piece there, return
@ -154,23 +169,11 @@ public class Board {
for (BoardCoordinate legalMove : legalMoves) {
if (newCoordinate.equals(legalMove)) {
move(dragging, newCoordinate);
// QUICK TESTING CODE
Piece movedPiece = get(newCoordinate);
King oppositeKing = movedPiece.black ? whiteKing : blackKing;
BoardCoordinate oppositeKingPosition = null;
boolean inCheck = false;
for (BoardCoordinate move : movedPiece.getLegalMoves(newCoordinate, this)) {
if (get(move) == oppositeKing) {
oppositeKingPosition = move;
inCheck = true;
break;
}
checkForCheckmate();
if (!isGameOver) {
move(ChessAI.findBestMove(this));
checkForCheckmate();
}
if (inCheck && oppositeKing.getLegalMoves(oppositeKingPosition, this).size() == 0)
isGameOver = true;
move(ChessAI.findBestMove(this));
break;
}
}
@ -181,6 +184,23 @@ public class Board {
draw();
}
public void checkForCheckmate() {
BoardCoordinate movedCoordinate = moveHistory.peek().to;
Piece movedPiece = get(movedCoordinate);
King oppositeKing = movedPiece.black ? whiteKing : blackKing;
BoardCoordinate oppositeKingPosition = null;
boolean inCheck = false;
for (BoardCoordinate move : movedPiece.getLegalMoves(movedCoordinate, this)) {
if (get(move) == oppositeKing) {
oppositeKingPosition = move;
inCheck = true;
break;
}
}
isGameOver = inCheck && oppositeKing.getLegalMoves(oppositeKingPosition, this).size() == 0;
victor = movedPiece.black;
}
public void draw() {
draw(null);
}
@ -224,6 +244,11 @@ public class Board {
// Otherwise, render it at the center of the board tile
piece.draw(graphics, panel, boardCoordinate.equals(dragging) ? mousePosition : boardCoordinate.toScreen());
});
// Draw game over text
if (isGameOver) {
graphics.drawImage(victor ? youLose : youWin, 0, 0, panel);
}
}
// Functional interfaces for forEachPiece

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

@ -0,0 +1,117 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
width="512mm"
height="512mm"
viewBox="0 0 512 512"
version="1.1"
id="svg5"
sodipodi:docname="you-lose.svg"
inkscape:export-filename="you-win.png"
inkscape:export-xdpi="25.4"
inkscape:export-ydpi="25.4"
inkscape:version="1.2.2 (b0a8486541, 2022-12-01)"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg">
<sodipodi:namedview
id="namedview7"
pagecolor="#ffffff"
bordercolor="#000000"
borderopacity="0.25"
inkscape:showpageshadow="2"
inkscape:pageopacity="0.0"
inkscape:pagecheckerboard="0"
inkscape:deskcolor="#d1d1d1"
inkscape:document-units="mm"
showgrid="false"
inkscape:zoom="0.3901936"
inkscape:cx="1232.7214"
inkscape:cy="1014.8808"
inkscape:window-width="1920"
inkscape:window-height="1048"
inkscape:window-x="1920"
inkscape:window-y="32"
inkscape:window-maximized="1"
inkscape:current-layer="layer1" />
<defs
id="defs2">
<rect
x="380.73235"
y="551.6332"
width="1925.2562"
height="945.95678"
id="rect952" />
<filter
style="color-interpolation-filters:sRGB;"
inkscape:label="Drop Shadow"
id="filter1272"
x="-0.020339003"
y="0"
width="1.020339"
height="1.1324139">
<feFlood
flood-opacity="0.498039"
flood-color="rgb(0,0,0)"
result="flood"
id="feFlood1262" />
<feComposite
in="flood"
in2="SourceGraphic"
operator="in"
result="composite1"
id="feComposite1264" />
<feGaussianBlur
in="composite1"
stdDeviation="0"
result="blur"
id="feGaussianBlur1266" />
<feOffset
dx="-32"
dy="32"
result="offset"
id="feOffset1268" />
<feComposite
in="SourceGraphic"
in2="offset"
operator="over"
result="composite2"
id="feComposite1270" />
</filter>
</defs>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1">
<rect
style="fill:#333333;stroke-width:0.264583;fill-opacity:0.5"
id="rect111"
width="512"
height="512"
x="0"
y="0" />
<text
xml:space="preserve"
transform="matrix(0.26458333,0,0,0.26458333,-72.389152,55.55428)"
id="text950"
style="font-size:341.333px;white-space:pre;shape-inside:url(#rect952);display:inline;fill:#3d3d3d;fill-opacity:1;filter:url(#filter1272)"><tspan
x="380.73242"
y="859.09003"
id="tspan1803"><tspan
style="font-weight:bold;font-family:Arimo;-inkscape-font-specification:'Arimo Bold';fill:#ffffff"
id="tspan1801">YOU LOSE</tspan></tspan></text>
<text
xml:space="preserve"
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:33.8667px;font-family:Arimo;-inkscape-font-specification:'Arimo Bold';fill:#ffffff;fill-opacity:0.75;stroke-width:0.264583"
x="110.52037"
y="328.68213"
id="text1451"><tspan
sodipodi:role="line"
id="tspan1449"
style="font-size:33.8667px;fill:#ffffff;fill-opacity:0.75;stroke-width:0.264583"
x="110.52037"
y="328.68213">click to play again</tspan></text>
</g>
</svg>

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

@ -0,0 +1,117 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
width="512mm"
height="512mm"
viewBox="0 0 512 512"
version="1.1"
id="svg5"
sodipodi:docname="you-win.svg"
inkscape:export-filename="you-win.png"
inkscape:export-xdpi="25.4"
inkscape:export-ydpi="25.4"
inkscape:version="1.2.2 (b0a8486541, 2022-12-01)"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg">
<sodipodi:namedview
id="namedview7"
pagecolor="#ffffff"
bordercolor="#000000"
borderopacity="0.25"
inkscape:showpageshadow="2"
inkscape:pageopacity="0.0"
inkscape:pagecheckerboard="0"
inkscape:deskcolor="#d1d1d1"
inkscape:document-units="mm"
showgrid="false"
inkscape:zoom="0.3901936"
inkscape:cx="1232.7214"
inkscape:cy="1014.8808"
inkscape:window-width="1920"
inkscape:window-height="1048"
inkscape:window-x="1920"
inkscape:window-y="32"
inkscape:window-maximized="1"
inkscape:current-layer="layer1" />
<defs
id="defs2">
<rect
x="380.73235"
y="551.6332"
width="1925.2562"
height="945.95678"
id="rect952" />
<filter
style="color-interpolation-filters:sRGB;"
inkscape:label="Drop Shadow"
id="filter1272"
x="-0.020339003"
y="0"
width="1.020339"
height="1.1324139">
<feFlood
flood-opacity="0.498039"
flood-color="rgb(0,0,0)"
result="flood"
id="feFlood1262" />
<feComposite
in="flood"
in2="SourceGraphic"
operator="in"
result="composite1"
id="feComposite1264" />
<feGaussianBlur
in="composite1"
stdDeviation="0"
result="blur"
id="feGaussianBlur1266" />
<feOffset
dx="-32"
dy="32"
result="offset"
id="feOffset1268" />
<feComposite
in="SourceGraphic"
in2="offset"
operator="over"
result="composite2"
id="feComposite1270" />
</filter>
</defs>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1">
<rect
style="fill:#333333;stroke-width:0.264583;fill-opacity:0.5"
id="rect111"
width="512"
height="512"
x="0"
y="0" />
<text
xml:space="preserve"
transform="matrix(0.26458333,0,0,0.26458333,-50.184205,55.55428)"
id="text950"
style="font-size:341.333px;white-space:pre;shape-inside:url(#rect952);display:inline;fill:#3d3d3d;fill-opacity:1;filter:url(#filter1272)"><tspan
x="380.73242"
y="859.09003"
id="tspan1767"><tspan
style="font-weight:bold;font-family:Arimo;-inkscape-font-specification:'Arimo Bold';fill:#ffffff"
id="tspan1765">YOU WIN!</tspan></tspan></text>
<text
xml:space="preserve"
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:33.8667px;font-family:Arimo;-inkscape-font-specification:'Arimo Bold';fill:#ffffff;fill-opacity:0.75;stroke-width:0.264583"
x="110.52037"
y="328.68213"
id="text1451"><tspan
sodipodi:role="line"
id="tspan1449"
style="font-size:33.8667px;fill:#ffffff;fill-opacity:0.75;stroke-width:0.264583"
x="110.52037"
y="328.68213">click to play again</tspan></text>
</g>
</svg>

After

Width:  |  Height:  |  Size: 3.5 KiB