Skip to content
Snippets Groups Projects
Commit 35f14239 authored by dsjohns2's avatar dsjohns2
Browse files

WIP

parent 9fda7947
No related branches found
No related tags found
No related merge requests found
package com.company;
import java.util.List;
import java.util.Random;
import java.util.Vector;
import java.util.concurrent.TimeUnit;
/**
* The class that contains the entire chess game
*/
public class Game {
public int x_dim = 8;
public int y_dim = 8;
public Square[][] board = new Square[x_dim][y_dim];
public boolean white_turn;
public boolean white_turn = true;
/**
* This method sets up the game board at the beginning of the game.
*/
public void initialize_board(){
for(int i=0; i<x_dim; i++){
for(int j=0; j<y_dim; j++){
......@@ -16,24 +24,7 @@ public class Game {
if(i==0 || i==1 | i==6 | i==7){
new_square.occupied = true;
Piece new_piece = new Piece();
if(i==1 || i==6) {
new_piece.name = "P";
}
else if(j==0 || j==7) {
new_piece.name = "R";
}
else if(j==1 || j==6){
new_piece.name = "N";
}
else if(j==2 || j==5){
new_piece.name = "B";
}
else if(j==3){
new_piece.name = "Q";
}
else{
new_piece.name = "K";
}
new_piece.name = get_piece_name(i, j);
if(i<2){
new_piece.white = false;
}
......@@ -54,6 +45,37 @@ public class Game {
}
}
/**
* A method to determine the piece name during initialization
* @param i the x location
* @param j the y location
* @return the name of the piece
*/
public String get_piece_name(int i, int j){
if(i==1 || i==6) {
return "P";
}
else if(j==0 || j==7) {
return "R";
}
else if(j==1 || j==6){
return "N";
}
else if(j==2 || j==5){
return "B";
}
else if(j==3){
return "Q";
}
else{
return "K";
}
}
/**
* A method to print out the current state of the game
* @param board the current state of the game
*/
public void print_board(Square[][] board){
/* color vars from https://stackoverflow.com/questions/5762491/how-to-print-color-in-console-using-system-out-println */
String ANSI_RESET = "\u001B[0m";
......@@ -82,15 +104,53 @@ public class Game {
System.out.println();
}
public int make_move(){
return 0;
/**
* A method to change board states through a legal move
* @return -1 if the game is not over
* 0 if the game is a stalemate
* 1 if white has won
* 2 if black has won *
* @throws InterruptedException
*/
public int make_move() throws InterruptedException {
Square[][][] legal_moves = get_legal_moves(white_turn);
/* Check for end of game */
if(legal_moves.length == 0){
if(white_turn){
return 1;
}
else{
return 2;
}
}
/* Game continues */
this.board = pick_move(legal_moves);
white_turn = !white_turn;
return -1;
}
/**
* A method to pick one of the valid moves to make
* @param legal_moves the array of valid moves to choose from
* @return a legal move
*/
public Square[][] pick_move(Square[][][] legal_moves){
Random rand = new Random();
int n = rand.nextInt(legal_moves.length-1);
return(legal_moves[n]);
}
/**
* A method to get all of the legal moves available to the player about to move
* @param color the player about to move
* @return an array of legal moves/board states
*/
public Square[][][] get_legal_moves(boolean color){
List<Square[][]> legal_moves = new Vector<Square[][]>();
for(int i=0; i<x_dim; i++) {
for (int j = 0; j < y_dim; j++) {
if(this.board[i][j].occupied) {
if(this.board[i][j].occupied && this.board[i][j].piece.white == color) {
int[][] squares = this.board[i][j].piece.squares_that_can_be_taken(this.board);
for(int k=0; k<squares.length; k++){
/* copy board */
......@@ -100,14 +160,11 @@ public class Game {
new_board[row][col] = new Square(this.board[row][col]);
}
}
/* remove old piece */
new_board[i][j].occupied = false;
/* add new piece */
new_board[squares[k][0]][squares[k][1]].occupied = true;
new_board[squares[k][0]][squares[k][1]].piece = new Piece(board[i][j].piece);
/* add new board state to list */
legal_moves.add(new_board);
}
......
......@@ -2,15 +2,28 @@ package com.company;
import java.util.concurrent.TimeUnit;
/**
* The class that runs the program
*/
public class Main {
public static void main(String[] args) throws InterruptedException {
Game my_game = new Game();
my_game.initialize_board();
my_game.print_board(my_game.board);
Square[][][] legal_moves = my_game.get_legal_moves(true);
for(int i=0; i<legal_moves.length; i++){
int end_of_game = -1;
while(end_of_game == -1){
end_of_game = my_game.make_move();
TimeUnit.SECONDS.sleep(1);
my_game.print_board(legal_moves[i]);
my_game.print_board(my_game.board);
}
if(end_of_game == 0){
System.out.println("Stalemate");
}
if(end_of_game == 1){
System.out.println("White Wins");
}
else{
System.out.println("Black Wins");
}
}
}
......@@ -4,6 +4,9 @@ import java.util.Arrays;
import java.util.List;
import java.util.Vector;
/**
* The piece class represents a movable piece in the game.
*/
public class Piece {
/* King = K
* Queen = Q
......@@ -16,18 +19,30 @@ public class Piece {
public boolean white;
public int[] location;
/**
* This is the default constructor that initializes a piece.
*/
public Piece(){
this.name = "";
this.white = true;
this.location = new int[2];
}
/**
* Copy Constructor
* @param other an different Piece
*/
public Piece(Piece other){
this.name = other.name;
this.white = other.white;
this.location = other.location;
}
/**
* A method to return the possible locations the current piece can reach given its current location
* @param board the current state of the board
* @return an array of tuples representing x and y, the locations that can be reached
*/
public int[][] squares_that_can_be_taken(Square[][] board){
if(this.name == "P" && this.white){
return white_pawn_squares(board);
......@@ -47,15 +62,16 @@ public class Piece {
else if(this.name == "Q"){
return queen_squares(board);
}
else if(this.name == "K"){
return king_squares(board);
}
else{
int[][] empty = new int[0][0];
return empty;
return king_squares(board);
}
}
/**
* A helper method returning the possible locations of a white pawn
* @param board the current state of the board
* @return an array of tuples representing x and y, the locations that can be reached
*/
public int[][] white_pawn_squares(Square[][] board){
List<int[]> squares = new Vector<int[]>();
int x = this.location[0];
......@@ -63,35 +79,37 @@ public class Piece {
/* move forward one */
if(x-1>=0 && !board[x-1][y].occupied){
int[] square = new int[2];
square[0] = x-1;
square[1] = y;
squares.add(square);
squares.addAll(legal_square_addition(x-1, y));
}
/* move forward two */
if(x==6 && !board[x-1][y].occupied && !board[x-2][y].occupied){
int[] square = new int[2];
square[0] = x-2;
square[1] = y;
squares.add(square);
squares.addAll(legal_square_addition(x-2, y));
}
/* take piece left */
if(x-1 >= 0 && y-1 >= 0 && board[x-1][y-1].occupied && !board[x-1][y-1].piece.white){
int[] square = new int[2];
square[0] = x-1;
square[1] = y-1;
squares.add(square);
squares.addAll(legal_square_addition(x-1, y-1));
}
/* take piece right */
if(x-1 >= 0 && y+1 <= 7 && board[x-1][y+1].occupied && !board[x-1][y+1].piece.white){
int[] square = new int[2];
square[0] = x-1;
square[1] = y+1;
squares.add(square);
squares.addAll(legal_square_addition(x-1, y+1));
}
return squares.toArray(new int[squares.size()][2]);
}
public List<int[]> legal_square_addition(int new_x, int new_y){
List<int[]> squares = new Vector<int[]>();
int[] square = new int[2];
square[0] = new_x;
square[1] = new_y;
squares.add(square);
return squares;
}
/**
* A helper method returning the possible locations of a black pawn
* @param board the current state of the board
* @return an array of tuples representing x and y, the locations that can be reached
*/
public int[][] black_pawn_squares(Square[][] board){
List<int[]> squares = new Vector<int[]>();
int x = this.location[0];
......@@ -99,35 +117,30 @@ public class Piece {
/* move forward one */
if(x+1<=7 && !board[x+1][y].occupied){
int[] square = new int[2];
square[0] = x+1;
square[1] = y;
squares.add(square);
squares.addAll(legal_square_addition(x+1, y));
}
/* move forward two */
if(x==1 && !board[x+1][y].occupied && !board[x+2][y].occupied){
int[] square = new int[2];
square[0] = x+2;
square[1] = y;
squares.add(square);
squares.addAll(legal_square_addition(x+2, y));
}
/* take piece left */
if(x+1 <= 7 && y-1 >= 0 && board[x+1][y-1].occupied && board[x+1][y-1].piece.white){
int[] square = new int[2];
square[0] = x+1;
square[1] = y-1;
squares.add(square);
squares.addAll(legal_square_addition(x+1, y-1));
}
/* take piece right */
if(x+1 <= 7 && y+1 <= 7 && board[x+1][y+1].occupied && board[x+1][y+1].piece.white){
int[] square = new int[2];
square[0] = x+1;
square[1] = y+1;
squares.add(square);
squares.addAll(legal_square_addition(x+1, y+1));
}
return squares.toArray(new int[squares.size()][2]);
}
/**
* A method to determine the possible square locations in a straight line from x, y
* @param board the current state of the board
* @param x_inc the x direction of the line
* @param y_inc the y direction of the line
* @return a list of tuples representing x and y, the locations that can be reached
*/
public List<int[]> line(Square[][] board, int x_inc, int y_inc){
List<int[]> squares = new Vector<int[]>();
int x = this.location[0];
......@@ -150,13 +163,18 @@ public class Piece {
square[0] = new_x;
square[1] = new_y;
squares.add(square);
x = x+x_inc;
y = y+y_inc;
new_x = new_x+x_inc;
new_y = new_y+y_inc;
}
}
return squares;
}
/**
* A helper method returning the possible locations of a rook
* @param board the current state of the board
* @return an array of tuples representing x and y, the locations that can be reached
*/
public int[][] rook_squares(Square[][] board){
List<int[]> squares = new Vector<int[]>();
......@@ -167,10 +185,15 @@ public class Piece {
/* Left */
squares.addAll(line(board, 0, -1));
/* Right */
squares.addAll(line(board, 1, 1));
squares.addAll(line(board, 0, 1));
return squares.toArray(new int[squares.size()][2]);
}
/**
* A helper method returning the possible locations of a bishop
* @param board the current state of the board
* @return an array of tuples representing x and y, the locations that can be reached
*/
public int[][] bishop_squares(Square[][] board) {
List<int[]> squares = new Vector<int[]>();
......@@ -185,31 +208,53 @@ public class Piece {
return squares.toArray(new int[squares.size()][2]);
}
public List<int[]> square_checker(Square[][] board, List<int[]> cur_list, int x, int y, int new_x, int new_y){
if(new_x >= 0 && new_x <= 7 && new_y >= 0 && new_y <= 7 && (board[x][y].piece.white != board[new_x][new_y].piece.white)){
int[] square = new int[2];
square[0] = new_x;
square[1] = new_y;
cur_list.add(square);
/**
* A method to check if the current piece can reach new_x, new_y from x, y
* @param board the current state of the board
* @param x current x location
* @param y current y location
* @param new_x target x location
* @param new_y target y location
* @return A list of tuples representing x and y, the locations that can be reached
*/
public List<int[]> square_checker(Square[][] board, int x, int y, int new_x, int new_y){
List<int[]> squares = new Vector<int[]>();
if(new_x >= 0 && new_x <= 7 && new_y >= 0 && new_y <= 7){
if(!board[new_x][new_y].occupied || board[x][y].piece.white != board[new_x][new_y].piece.white) {
int[] square = new int[2];
square[0] = new_x;
square[1] = new_y;
squares.add(square);
}
}
return cur_list;
return squares;
}
/**
* A helper method returning the possible locations of a knight
* @param board the current state of the board
* @return an array of tuples representing x and y, the locations that can be reached
*/
public int[][] knight_squares(Square[][] board) {
List<int[]> squares = new Vector<int[]>();
int x = this.location[0];
int y = this.location[1];
squares = square_checker(board, squares, x, y, x-1, y-2);
squares = square_checker(board, squares, x, y, x-1, y+2);
squares = square_checker(board, squares, x, y, x-2, y-1);
squares = square_checker(board, squares, x, y, x-2, y+1);
squares = square_checker(board, squares, x, y, x+1, y-2);
squares = square_checker(board, squares, x, y, x+1, y+2);
squares = square_checker(board, squares, x, y, x+2, y-1);
squares = square_checker(board, squares, x, y, x+2, y+1);
squares.addAll(square_checker(board, x, y, x-1, y-2));
squares.addAll(square_checker(board, x, y, x-1, y+2));
squares.addAll(square_checker(board, x, y, x-2, y-1));
squares.addAll(square_checker(board, x, y, x-2, y+1));
squares.addAll(square_checker(board, x, y, x+1, y-2));
squares.addAll(square_checker(board, x, y, x+1, y+2));
squares.addAll(square_checker(board, x, y, x+2, y-1));
squares.addAll(square_checker(board, x, y, x+2, y+1));
return squares.toArray(new int[squares.size()][2]);
}
/**
* A helper method returning the possible locations of a queen
* @param board the current state of the board
* @return an array of tuples representing x and y, the locations that can be reached
*/
public int[][] queen_squares(Square[][] board) {
List<int[]> squares = new Vector<int[]>();
squares.addAll(Arrays.asList(bishop_squares(board)));
......@@ -217,18 +262,23 @@ public class Piece {
return squares.toArray(new int[squares.size()][2]);
}
/**
* A helper method returning the possible locations of a king
* @param board the current state of the board
* @return an array of tuples representing x and y, the locations that can be reached
*/
public int[][] king_squares(Square[][] board) {
List<int[]> squares = new Vector<int[]>();
int x = this.location[0];
int y = this.location[1];
squares = square_checker(board, squares, x, y, x-1, y-1);
squares = square_checker(board, squares, x, y, x-1, y+1);
squares = square_checker(board, squares, x, y, x, y-1);
squares = square_checker(board, squares, x, y, x, y+1);
squares = square_checker(board, squares, x, y, x+1, y-1);
squares = square_checker(board, squares, x, y, x+1, y+1);
squares = square_checker(board, squares, x, y, x-1, y);
squares = square_checker(board, squares, x, y, x+1, y);
squares.addAll(square_checker(board, x, y, x-1, y-1));
squares.addAll(square_checker(board, x, y, x-1, y+1));
squares.addAll(square_checker(board, x, y, x, y-1));
squares.addAll(square_checker(board, x, y, x, y+1));
squares.addAll(square_checker(board, x, y, x+1, y-1));
squares.addAll(square_checker(board, x, y, x+1, y+1));
squares.addAll(square_checker(board, x, y, x-1, y));
squares.addAll(square_checker(board, x, y, x+1, y));
return squares.toArray(new int[squares.size()][2]);
}
}
package com.company;
/**
* This class is what makes up the game board.
*/
public class Square {
public boolean occupied;
public Piece piece;
/**
* A default constructor
*/
public Square(){
this.occupied = false;
this.piece = new Piece();
}
/**
* A copy constructor
* @param other the other square to copy
*/
public Square(Square other){
this.occupied = other.occupied;
this.piece = new Piece(other.piece);
......
package com.company;
/**
* This is the testing class
*/
public class Test {
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment