diff --git a/CheckerBoard.java b/CheckerBoard.java index 868513e..ccd5014 100644 --- a/CheckerBoard.java +++ b/CheckerBoard.java @@ -1,22 +1,24 @@ -package Checkers; + import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.layout.Pane; +import javafx.scene.paint.Color; +import javafx.scene.shape.Rectangle; import javafx.stage.Stage; public class CheckerBoard extends Application { - public static final int TILE_SIZE = 100; //universal size determinant for the game. Change this number everything scales up or down - public static final int WIDTH = 8; //board width; 8 for standard checkers - public static final int HEIGHT = 8; //board height; 8 for standard checkers + public static final int TILE_SIZE = 100; + public static final int WIDTH = 8; + public static final int HEIGHT = 8; - private Tile[][] board = new Tile[WIDTH][HEIGHT]; //the game board as represented as a 2d array of tile objects + private Tile[][] board = new Tile[WIDTH][HEIGHT]; - private Group tileGroup = new Group(); //keeps track of rendered tiles i.e. the board - private Group pieceGroup = new Group(); //keeps track of the rendered game pieces in play + private Group tileGroup = new Group(); + private Group pieceGroup = new Group(); private Parent createContent() { @@ -25,37 +27,37 @@ private Parent createContent() root.getChildren().addAll(tileGroup, pieceGroup); for(int y=0; y=5 && (x+y)%2!=0){ - piece = makePiece(PieceType.BLACK, x, y); //populates the bottom two rows with black pieces + piece = makePiece(PieceType.BLACK, x, y); } if(piece!=null) { - tile.setPiece(piece); //changes the tile object to include the piece + tile.setPiece(piece); pieceGroup.getChildren().add(piece); } } } + return root; } private MoveResult tryMove(Piece piece, int newX, int newY){ - if(board[newX][newY].hasPiece() || (newX + newY) % 2 ==0) { //checks if the target tile is either a light tile or already has a piece - return new MoveResult(MoveType.NONE); //this results in no movement + if(board[newX][newY].hasPiece() || (newX + newY) % 2 ==0) { + return new MoveResult(MoveType.NONE); } int x0 = toBoard(piece.getOldX()); int y0 = toBoard(piece.getOldY()); - //get the original x and y coordinates so that the piece can return if the move is invalid if(Math.abs(newX-x0) == 1 && newY - y0 == piece.getType().moveDir){ return new MoveResult(MoveType.NORMAL); @@ -64,18 +66,18 @@ private MoveResult tryMove(Piece piece, int newX, int newY){ int y1 = y0 + (newY - y0) / 2; if(board[x1][y1].hasPiece() && board[x1][y1].getPiece().getType() != piece.getType()){ - return new MoveResult(MoveType.KILL, board[x1][y1].getPiece()); //these lines compare the original coordinates to the target - } //if the target is empty and there is an opposing piece in between the original and target it is a kill/jump move + return new MoveResult(MoveType.KILL, board[x1][y1].getPiece()); + } } - return new MoveResult(MoveType.NONE); //if the move doesn't satisfy any of the restraints, it returns to the original position + return new MoveResult(MoveType.NONE); } private int toBoard(double pixel){ return (int)(pixel + TILE_SIZE / 2) / TILE_SIZE; - } //converts pixel location to board coordinate location + } @Override - public void start(Stage primaryStage) { //javaFx start sequence + public void start(Stage primaryStage) { Scene scene = new Scene(createContent()); primaryStage.setTitle("Checkers"); primaryStage.setScene(scene); @@ -85,40 +87,85 @@ public void start(Stage primaryStage) { //javaFx start sequence private Piece makePiece(PieceType type, int x, int y){ Piece piece = new Piece(type, x, y); - piece.setOnMouseReleased(e -> { - int newX = toBoard(piece.getLayoutX()); //gets the pixel location that the piece was dragged to - int newY = toBoard(piece.getLayoutY()); //converts to the board location that the piece was dragged to - - MoveResult result = tryMove(piece, newX, newY); //using the coordinates and the piece type, check what kind of move can be made - - int x0 = toBoard(piece.getOldX()); - int y0 = toBoard(piece.getOldY()); - - switch (result.getType()){ - case NONE: - piece.abortMove(); //returns piece to original location - break; - case NORMAL: - piece.move(newX, newY); - board[x0][y0].setPiece(null); - board[newX][newY].setPiece(piece); //moves piece one tile in the desired diagonal direction - break; - case KILL: - piece.move(newX, newY); - board[x0][y0].setPiece(null); - board[newX][newY].setPiece(piece);//move the piece two spaces and remove the opposing jumped piece - - Piece otherPiece = result.getPiece(); - board[toBoard(otherPiece.getOldX())][toBoard(otherPiece.getOldY())].setPiece(null); - pieceGroup.getChildren().remove(otherPiece); - break; + if (type.equals(PieceType.BLACK)) { + piece.setOnMouseReleased(e -> { + int newX = toBoard(piece.getLayoutX()); + int newY = toBoard(piece.getLayoutY()); + + MoveResult result = tryMove(piece, newX, newY); + + int x0 = toBoard(piece.getOldX()); + int y0 = toBoard(piece.getOldY()); + + switch (result.getType()) { + case NONE: + piece.abortMove(); + break; + case NORMAL: + piece.move(newX, newY); + board[x0][y0].setPiece(null); + board[newX][newY].setPiece(piece); + break; + case KILL: + piece.move(newX, newY); + board[x0][y0].setPiece(null); + board[newX][newY].setPiece(piece); + + Piece otherPiece = result.getPiece(); + board[toBoard(otherPiece.getOldX())][toBoard(otherPiece.getOldY())].setPiece(null); + pieceGroup.getChildren().remove(otherPiece); + break; - } - }); + } + MinMaxStart(); + + }); + } + if(type.equals(PieceType.RED)){ + piece.setOnMouseReleased(e->{ + piece.abortMove(); + }); + + } return piece; } public static void main(String[] args) { launch(args); } + + public void MinMaxStart(){ + MinMax M=new MinMax(board); + M.Move(); + int newX=M.GetNewX(); + int newY=M.GetNewY(); + int x0=M.GetOldX(); + int y0=M.GetOldY(); + Piece piece=new Piece(PieceType.RED,x0,y0); + + MoveResult result = tryMove(piece, newX, newY); + + if(M.getType().equals(MoveType.KILL)){ + System.out.println("KILL"); + piece.move(newX, newY); + board[x0][y0].setPiece(null); + board[newX][newY].setPiece(piece); + + Piece otherPiece = result.getPiece(); + board[toBoard(otherPiece.getOldX())][toBoard(otherPiece.getOldY())].setPiece(null); + pieceGroup.getChildren().remove(otherPiece); + } + else if(M.getType().equals(MoveType.NORMAL)){ + System.out.println("NORMAL"); + piece.move(newX, newY); + board[x0][y0].setPiece(null); + board[newX][newY].setPiece(piece); + } + + + } + + + + } \ No newline at end of file diff --git a/MinMax.java b/MinMax.java new file mode 100644 index 0000000..28d148e --- /dev/null +++ b/MinMax.java @@ -0,0 +1,123 @@ +public class MinMax { + int[][] RedPieces=new int[8][8]; + private Tile[][] MoveBoard; + private MinMaxMove[] Moves=new MinMaxMove[100]; + private int MoveCount; + + private int NewX; + private int NewY; + public int GetNewX(){return NewX;} + public int GetNewY(){return NewY;} + + private int OldX; + private int OldY; + public int GetOldX(){return OldX;} + public int GetOldY(){return OldY;} + + private MoveType type; + public MoveType getType(){return type;} + + + public MinMax(Tile[][] board){ + MoveBoard = board; + } + + public void Move(){ + GetBoardState(); + + PossibleRedMoves(); + for(int i=0;i<8;i++){ + for(int j=0;j<8;j++){ + System.out.print(RedPieces[i][j]+" "); + } + System.out.println(); + } + System.out.println(); + + int Kill=-1; + + for(int j=0;j0 && j<7){ + if (RedPieces[i-1][j+1]==0){ + MinMaxMove M=new MinMaxMove(MoveType.NORMAL,i,j,i-1,j+1); + Moves[MoveCount]=M; + MoveCount++; + } + if (i>1 && j<6) { + if (RedPieces[i - 1][j + 1] == 2 && RedPieces[i - 2][j + 2] == 0) { + MinMaxMove M=new MinMaxMove(MoveType.KILL, i, j, i - 2, j + 2); + Moves[MoveCount]=M; + MoveCount++; + } + } + } + + if(i<7 && j<7){ + if (RedPieces[i+1][j+1]==0){ + MinMaxMove M=new MinMaxMove(MoveType.NORMAL,i,j,i+1,j+1); + Moves[MoveCount]=M; + MoveCount++; + } + if (i<6 && j<6) { + if (RedPieces[i + 1][j + 1] == 2 && RedPieces[i + 2][j + 2] == 0) { + MinMaxMove M=new MinMaxMove(MoveType.KILL, i, j, i + 2, j + 2); + Moves[MoveCount]=M; + MoveCount++; + } + } + } + + } + } + } + } + + +} diff --git a/MinMaxMove.java b/MinMaxMove.java new file mode 100644 index 0000000..c4c130d --- /dev/null +++ b/MinMaxMove.java @@ -0,0 +1,24 @@ +public class MinMaxMove { + private int NewX; + private int NewY; + public int GetNewX(){return NewX;} + public int GetNewY(){return NewY;} + + private int OldX; + private int OldY; + public int GetOldX(){return OldX;} + public int GetOldY(){return OldY;} + + private MoveType type; + public MoveType getType(){return type;} + + public MinMaxMove(MoveType type, int OldX,int OldY,int NewX,int NewY){ + this.NewX=NewX; + this.NewY=NewY; + this.OldX=OldX; + this.OldY=OldY; + this.type=type; + } + + +} diff --git a/MoveResult.java b/MoveResult.java index b93a397..a60ce40 100644 --- a/MoveResult.java +++ b/MoveResult.java @@ -1,4 +1,4 @@ -package Checkers; + public class MoveResult { @@ -22,4 +22,6 @@ public MoveResult(MoveType type, Piece piece){ this.type = type; this.piece = piece; } + + } diff --git a/MoveType.java b/MoveType.java index 9f26644..f69a138 100644 --- a/MoveType.java +++ b/MoveType.java @@ -1,5 +1,5 @@ -package Checkers; + public enum MoveType { - NONE, NORMAL, KILL //three possible types of movement none, normal, or kill/jump + NONE, NORMAL, KILL } diff --git a/Piece.java b/Piece.java index 2a84848..3dc7bfd 100644 --- a/Piece.java +++ b/Piece.java @@ -1,13 +1,13 @@ -package Checkers; import javafx.scene.layout.StackPane; import javafx.scene.paint.Color; import javafx.scene.shape.Ellipse; -import static Checkers.CheckerBoard.TILE_SIZE; public class Piece extends StackPane { + final int TILE_SIZE=100; + private PieceType type; private double mouseX, mouseY; @@ -26,7 +26,7 @@ public double getOldY() { } public Piece(PieceType type, int x, int y){ - this.type = type; //javaFx design of the game pieces; basically two ellipses on top of each other + this.type = type; move(x, y); @@ -67,4 +67,5 @@ public void move(int x, int y){ public void abortMove(){ relocate(oldX, oldY); } + } diff --git a/PieceType.java b/PieceType.java index a27dfb8..e64bf05 100644 --- a/PieceType.java +++ b/PieceType.java @@ -1,8 +1,8 @@ -package Checkers; + public enum PieceType { - RED(1), BLACK(-1); //red tiles can move down the board which is represented as moving in the positive direction in the array - //black tiles can move up the board which is represented as moving in the negative direction in the array + RED(1), BLACK(-1); + final int moveDir; PieceType(int moveDir){ diff --git a/Tile.java b/Tile.java index 0c7276f..3fa2af3 100644 --- a/Tile.java +++ b/Tile.java @@ -1,4 +1,4 @@ -package Checkers; + import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; @@ -20,11 +20,11 @@ public void setPiece(Piece piece) { } public Tile(boolean light, int x, int y){ - setWidth(CheckerBoard.TILE_SIZE); //javaFx design of the tiles that make up the board + setWidth(CheckerBoard.TILE_SIZE); setHeight(CheckerBoard.TILE_SIZE); relocate(x * CheckerBoard.TILE_SIZE, y * CheckerBoard.TILE_SIZE); - setFill(light ? Color.valueOf("#feb") : Color.valueOf("#582")); //arbitrary colors of the board + setFill(light ? Color.valueOf("#feb") : Color.valueOf("#582")); } }