Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 97 additions & 50 deletions CheckerBoard.java
Original file line number Diff line number Diff line change
@@ -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()
{
Expand All @@ -25,37 +27,37 @@ private Parent createContent()
root.getChildren().addAll(tileGroup, pieceGroup);
for(int y=0; y<HEIGHT ; y++){
for(int x = 0; x < WIDTH; x++){
Tile tile = new Tile((x+y)%2==0, x, y); //the first argument checks whether the tile is light or dark
board[x][y] = tile; //populates the board with tiles
Tile tile = new Tile((x+y)%2==0, x, y);
board[x][y] = tile;

tileGroup.getChildren().add(tile);

Piece piece = null; //piece is null by default
Piece piece = null;

if(y<=2 && (x+y)%2!=0){
piece = makePiece(PieceType.RED, x, y); //populates the top two rows with red pieces
piece = makePiece(PieceType.RED, x, y);
}

if(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);
Expand All @@ -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);
Expand All @@ -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);
}


}




}
123 changes: 123 additions & 0 deletions MinMax.java
Original file line number Diff line number Diff line change
@@ -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;j<MoveCount;j++){
// System.out.println(Moves[j].getType()+" "+Moves[j].GetOldX()+" "+Moves[j].GetOldY()+" "+Moves[j].GetNewX()+" "+Moves[j].GetNewY());
if(Moves[j].getType().equals(MoveType.KILL)){
Kill=j;
}
}

if (Kill!=-1){
NewX=Moves[Kill].GetNewX();
NewY=Moves[Kill].GetNewY();
OldX=Moves[Kill].GetOldX();
OldY=Moves[Kill].GetOldY();
type=MoveType.KILL;
}
else {
NewX=Moves[0].GetNewX();
NewY=Moves[0].GetNewY();
OldX=Moves[0].GetOldX();
OldY=Moves[0].GetOldY();
type=MoveType.NORMAL;
}





}

public void GetBoardState(){
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
if(MoveBoard[i][j].hasPiece()) {
Piece P = MoveBoard[i][j].getPiece();
if (P.getType().equals(PieceType.RED)) {
RedPieces[i][j] = 1;
}
else if(P.getType().equals(PieceType.BLACK)){
RedPieces[i][j]=2;
}
}
}
}
}

public void PossibleRedMoves(){
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
if(RedPieces[i][j]==1) {
if(i>0 && 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++;
}
}
}

}
}
}
}


}
24 changes: 24 additions & 0 deletions MinMaxMove.java
Original file line number Diff line number Diff line change
@@ -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;
}


}
4 changes: 3 additions & 1 deletion MoveResult.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Checkers;


public class MoveResult {

Expand All @@ -22,4 +22,6 @@ public MoveResult(MoveType type, Piece piece){
this.type = type;
this.piece = piece;
}


}
4 changes: 2 additions & 2 deletions MoveType.java
Original file line number Diff line number Diff line change
@@ -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
}
Loading