From 1d88b45563a80a061398c6921184e9f8423c47e0 Mon Sep 17 00:00:00 2001 From: MuqDonalds Date: Thu, 3 Mar 2022 20:54:40 -0600 Subject: [PATCH 1/3] reeeeeeeeeeeeeeeee --- src/org/chess/ChessBoard.java | 61 +++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/src/org/chess/ChessBoard.java b/src/org/chess/ChessBoard.java index 75b1261..f2f143d 100644 --- a/src/org/chess/ChessBoard.java +++ b/src/org/chess/ChessBoard.java @@ -1,5 +1,6 @@ package org.chess; +import java.awt.Point; import java.io.IOException; /** @@ -27,9 +28,65 @@ public ChessBoard(AbstractChessBoard other) { */ @Override public boolean isPlayerInCheck(char c) { + Point kingPos = playerKingPos(c); + if (kingPos != null) + { + Point chessPiecePos = new Point(); + ChessPiece piece = null; + for (int x = 0; x<= 7; x++) + { + for (int y = 0; y <= 7; y++) + { + piece = get(x,y); + chessPiecePos.x = x; + chessPiecePos.y = y; + if (piece == null) + { + continue; + } + if (piece.getColor() == c) + { + continue; + } + if (canMove(piece, kingPos.x, kingPos.y) == null) + { + return true; + } + /* + if (canMoveWithCaptureRule(piece, kingPos.x, kingPos.y) == null) + { + return true; + }*/ + } + } + } return false; } + public Point playerKingPos(char c) { + Point chessPieceXY = new Point(); + ChessPiece piece = null; + for (int x = 0; x <= 7; x++) + { + for (int y = 0; y <= 7; y++) + { + piece = get(x,y); + if (piece == null) { + continue; + } + if (piece.getColor() != c) { + continue; + } + if (piece.isEssential()){ + chessPieceXY.x = x; + chessPieceXY.y = y; + return chessPieceXY; + } + } + } + return chessPieceXY; + } + /** * For a player 'W' or 'B', return whether that player has an essential piece * that's in checkmate. @@ -39,6 +96,10 @@ public boolean isPlayerInCheck(char c) { */ @Override public boolean isPlayerInCheckmate(char c) { + if (isPlayerInCheck(c)) + { + return true; + } return false; } } From 485791dc03bc2b841b5dd7eaa7a07488e61d66da Mon Sep 17 00:00:00 2001 From: MuqDonalds Date: Wed, 6 Jul 2022 19:51:00 -0500 Subject: [PATCH 2/3] klewdfkljadkgjkladjfg --- src/org/chess/ChessBoard.java | 160 ++++++++++++++++++++++++++++++++-- 1 file changed, 154 insertions(+), 6 deletions(-) diff --git a/src/org/chess/ChessBoard.java b/src/org/chess/ChessBoard.java index f2f143d..1396f1d 100644 --- a/src/org/chess/ChessBoard.java +++ b/src/org/chess/ChessBoard.java @@ -2,6 +2,8 @@ import java.awt.Point; import java.io.IOException; +import java.util.LinkedList; +import java.util.List; /** * This is the file you can edit. You will need to populate isPlayerInCheck @@ -52,11 +54,6 @@ public boolean isPlayerInCheck(char c) { { return true; } - /* - if (canMoveWithCaptureRule(piece, kingPos.x, kingPos.y) == null) - { - return true; - }*/ } } } @@ -87,6 +84,27 @@ public Point playerKingPos(char c) { return chessPieceXY; } + public ChessPiece playerKingPiece(char c) { + ChessPiece piece = null; + for (int x = 0; x <= 7; x++) + { + for (int y = 0; y <= 7; y++) + { + piece = get(x,y); + if (piece == null) { + continue; + } + if (piece.getColor() != c) { + continue; + } + if (piece.isEssential()){ + return piece; + } + } + } + return piece; + } + /** * For a player 'W' or 'B', return whether that player has an essential piece * that's in checkmate. @@ -96,10 +114,140 @@ public Point playerKingPos(char c) { */ @Override public boolean isPlayerInCheckmate(char c) { - if (isPlayerInCheck(c)) + if (!isPlayerInCheck(c)) + { + return false; + } + Point kingPos = playerKingPos(c); + ChessPiece kingPiece = playerKingPiece(c); + List enemyPieces = getEnemyPieces(c); + List alliedPieces = getAlliedPieces(c); + List kingMovableSpaces = getKingMovableSpaces(c); + List kingRuleset = getKingRuleset(c); + + if (kingPos != null && enemyPieces != null) { + for (Point enemyPiece : enemyPieces) + { + if (canMove(kingPiece, enemyPiece.x, enemyPiece.y) == null) + { + return false; + } + for (Point alliedPiece : alliedPieces) + { + for(Point rulesetSpace : kingRuleset) + { + if (canMove(get(alliedPiece.x,alliedPiece.y), rulesetSpace.x, rulesetSpace.y) == null) + { + return false; + } + } + } + } + } + if (kingMovableSpaces.isEmpty()) { return true; } + return false; } + + public List getEnemyPieces(char c) + { + List enemyPieces = new LinkedList(); + for (int x = 0; x<= 7; x++) + { + for (int y = 0; y <= 7; y++) + { + ChessPiece piece = get(x,y); + Point chessPiecePos = new Point(); + chessPiecePos.x = x; + chessPiecePos.y = y; + if (piece == null) + { + continue; + } + if (piece.getColor() != c) + { + enemyPieces.add(chessPiecePos); + } + } + } + return enemyPieces; + } + + public List getAlliedPieces(char c) + { + List alliedPieces = new LinkedList(); + ChessPiece kingPiece = playerKingPiece(c); + for (int x = 0; x<= 7; x++) + { + for (int y = 0; y <= 7; y++) + { + ChessPiece piece = get(x,y); + Point chessPiecePos = new Point(); + chessPiecePos.x = x; + chessPiecePos.y = y; + if (piece == null) + { + continue; + } + if (piece.getColor() == c && piece != kingPiece) + { + alliedPieces.add(chessPiecePos); + } + } + } + return alliedPieces; + } + + public List getKingMovableSpaces(char c) + { + Point kingPos = playerKingPos(c); + ChessPiece piece = playerKingPiece(c); + List movableSpaces = new LinkedList(); + if (kingPos != null && piece != null) + { + Point boardSpace = new Point(); + for (int x = 0; x<= 7; x++) + { + for (int y = 0; y <= 7; y++) + { + boardSpace.x = x; + boardSpace.y = y; + if (canMove(piece, x, y) == null) + { + movableSpaces.add(boardSpace); + } + } + } + } + return movableSpaces; + } + + public List getKingRuleset(char c) + { + Point kingPos = playerKingPos(c); + ChessPiece piece = playerKingPiece(c); + List ruleset = new LinkedList(); + if (kingPos != null && piece != null) + { + for (int i = kingPos.x - 1; i < kingPos.x + 1; ++i) { + for (int j = kingPos.y - 1; j < kingPos.y + 1; ++j) { + Point square = new Point(); + if (kingPos.x != i && kingPos.y != j) + { + if (i > -1 && j < 9) + { + square.x = i; + square.y = j; + ruleset.add(square); + } + } + } + } + } + return ruleset; + } + } From 0164969fc4cf16bac2c0fe7e1d890384fbe508f6 Mon Sep 17 00:00:00 2001 From: MuqDonalds Date: Thu, 7 Jul 2022 16:35:59 -0500 Subject: [PATCH 3/3] checkmate works --- src/org/chess/ChessBoard.java | 145 ++++++++++++++++------------------ 1 file changed, 68 insertions(+), 77 deletions(-) diff --git a/src/org/chess/ChessBoard.java b/src/org/chess/ChessBoard.java index 1396f1d..915cd28 100644 --- a/src/org/chess/ChessBoard.java +++ b/src/org/chess/ChessBoard.java @@ -11,7 +11,7 @@ * You are also allowed to add any other new methods you need. */ public class ChessBoard extends AbstractChessBoard { - + List attackingPieces = new LinkedList(); public ChessBoard(String fileName) throws IOException { super(fileName); @@ -52,6 +52,7 @@ public boolean isPlayerInCheck(char c) { } if (canMove(piece, kingPos.x, kingPos.y) == null) { + attackingPieces.add(piece); return true; } } @@ -94,12 +95,11 @@ public ChessPiece playerKingPiece(char c) { if (piece == null) { continue; } - if (piece.getColor() != c) { - continue; - } - if (piece.isEssential()){ + if (piece.getColor() == c && piece.isEssential()) + { return piece; } + } } return piece; @@ -118,83 +118,62 @@ public boolean isPlayerInCheckmate(char c) { { return false; } - Point kingPos = playerKingPos(c); ChessPiece kingPiece = playerKingPiece(c); - List enemyPieces = getEnemyPieces(c); - List alliedPieces = getAlliedPieces(c); + List alliedPieces = getAlliedPieces(c); List kingMovableSpaces = getKingMovableSpaces(c); - List kingRuleset = getKingRuleset(c); + List attackingSpaces = getAttackingSpaces(c); - if (kingPos != null && enemyPieces != null) { - for (Point enemyPiece : enemyPieces) + if (kingPiece != null && !attackingPieces.isEmpty()) + { + for (ChessPiece alliedPiece : alliedPieces) { - if (canMove(kingPiece, enemyPiece.x, enemyPiece.y) == null) - { - return false; - } - for (Point alliedPiece : alliedPieces) - { - for(Point rulesetSpace : kingRuleset) + for(Point attackingSpace : attackingSpaces) + { + if (canMove(alliedPiece, attackingSpace.x, attackingSpace.y) == null) { - if (canMove(get(alliedPiece.x,alliedPiece.y), rulesetSpace.x, rulesetSpace.y) == null) - { - return false; - } + return false; } - } + + for (ChessPiece attackingPiece : attackingPieces) + { + if(canMove(alliedPiece, attackingPiece.x, attackingPiece.y) == null) + { + return false; + } + if (canMove(kingPiece, attackingPiece.x, attackingPiece.y) == null) + { + return false; + } + } + } } - } - if (kingMovableSpaces.isEmpty()) - { - return true; - } - - return false; - } - - public List getEnemyPieces(char c) - { - List enemyPieces = new LinkedList(); - for (int x = 0; x<= 7; x++) - { - for (int y = 0; y <= 7; y++) + + if (!kingMovableSpaces.isEmpty()) { - ChessPiece piece = get(x,y); - Point chessPiecePos = new Point(); - chessPiecePos.x = x; - chessPiecePos.y = y; - if (piece == null) - { - continue; - } - if (piece.getColor() != c) - { - enemyPieces.add(chessPiecePos); - } + return false; } } - return enemyPieces; + + return true; } - public List getAlliedPieces(char c) + + public List getAlliedPieces(char c) { - List alliedPieces = new LinkedList(); + List alliedPieces = new LinkedList(); ChessPiece kingPiece = playerKingPiece(c); for (int x = 0; x<= 7; x++) { for (int y = 0; y <= 7; y++) { ChessPiece piece = get(x,y); - Point chessPiecePos = new Point(); - chessPiecePos.x = x; - chessPiecePos.y = y; if (piece == null) { continue; } if (piece.getColor() == c && piece != kingPiece) { - alliedPieces.add(chessPiecePos); + alliedPieces.add(piece); } } } @@ -225,29 +204,41 @@ public List getKingMovableSpaces(char c) return movableSpaces; } - public List getKingRuleset(char c) + public List getAttackingSpaces(char c) { - Point kingPos = playerKingPos(c); - ChessPiece piece = playerKingPiece(c); - List ruleset = new LinkedList(); - if (kingPos != null && piece != null) + List attackingSpaces = new LinkedList(); + for (ChessPiece attackingPiece : attackingPieces) { - for (int i = kingPos.x - 1; i < kingPos.x + 1; ++i) { - for (int j = kingPos.y - 1; j < kingPos.y + 1; ++j) { - Point square = new Point(); - if (kingPos.x != i && kingPos.y != j) - { - if (i > -1 && j < 9) - { - square.x = i; - square.y = j; - ruleset.add(square); - } - } - } - } + Point kingPos = playerKingPos(c); + int dx, dy, p, x, y; + dx = attackingPiece.x - kingPos.x; + dy = attackingPiece.y - kingPos.y; + x = kingPos.x; + y = kingPos.y; + p = 2 * dy - dx; + while(x < attackingPiece.x) + { + Point space = new Point(); + if(p >= 0) + { + space.x = x; + space.y = y; + attackingSpaces.add(space); + y = y + 1; + p = p + 2 * dy - 2 * dx; + } + else + { + + space.x = x; + space.y = y; + attackingSpaces.add(space); + p = p + 2 * dy;} + x = x + 1; + } } - return ruleset; + + return attackingSpaces; } }