Chess program

An overview of my chess program written in my first year of programming in java

Overview:

This was one of the first larger programs that I wrote that involved multiple java classes. The project is very simple implementing only the very basic elements of chess.

Design choices:

As part of the assignment, I needed a program that demonstrated the use of Arrays, Class Structures, GUI elements and Inheritance.

Takeaways:

This program taught me how difficult taking on a big project can be but also how satisfying putting in hard work to make a good product can feel. While other projects were simple text boxes and buttons, I loved how my program stood out and I’ve been working hard since.

Chess.java:
package Game;
import javax.swing.*;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.security.SecureRandom;
import javax.imageio.ImageIO;
/**
 ****Chess of Champions****
 * @Author Ian Sodersjerna
 * @Contributors Alexander Venezia assistance with GUI elements, Andrew Thompson of StackOverflow for chess piece images and Create Images Method.
 * @Date 4/18/2019
 * @Project Chess of Champions
 * @Modified 4/18/2019
 * @Modified 4/20/2019
 * @Modified 4/21/2019
 * @Modified 4/23/2019
 * @Modified 4/25/2019
 * @Modified 4/27/2019
 * @Modified 4/28/2019
 * @Modified 4/30/2019
 * @Modified 5/01/2019
 * @Modified 5/02/2019
 * @Modified 5/03/2019
 * @Modified 5/04/2019
 * @Modified 5/05/2019
 * @Modified 5/07/2019
 * @Modified 5/09/2019
 * @Modified 5/10/2019
 ****Early Access Notice****
 **Features under development:
 * Check will occasionally break game comment out MouseActions lines 44 and 67 to disable
 * Pawn promotion will only ever result in queen
 **Working Features:
 * movement
 * Highlighting Movements
 * taking pieces
 * pieces have accurate images at all times
 */
public class Chess extends JFrame {
    //attributes
    private static final int scale = 75;
    private static final int GAME_WIDTH = 8* scale;
    private static final int GAME_HEIGHT = (8 * scale)+(scale/3);
    public static final Image[][] chessPieceImages = new Image[2][6];
    private static Board display;
    private final MouseActions mouseActions;
    private static Point selected;
    private static boolean check;
    private static boolean checkmate = false;
    public static final String w = "white";
    public static final String b = "black";
    private static Piece[][] pieces;
    private static String turn = w;
    private static SecureRandom random = new SecureRandom();
    private static Point[] moves;
    private static Piece p1;
    private static Point theMove;
    
    //constructor 
    public Chess(){
            super("Chess of Champions");
            display = new Board(0,0,GAME_WIDTH, GAME_HEIGHT, scale);
            mouseActions = new MouseActions();
            super.add(display);
            display.addMouseListener(mouseActions);
            super.pack();
        }
    //main method for program
    public static void main(String[] args) {
         //array of all pieces to be used in game
         pieces = new Piece[2][16];
         //populate piece array with inital pieces in inital locations
         pieces[0][0]= new King(b,3,0);
         pieces[1][0]= new King(w,3,7);         
         pieces[0][1]= new Queen(b,4,0);
         pieces[1][1]= new Queen(w,4,7);        
         pieces[0][2]= new Bishop(b,2,0);
         pieces[0][3]= new Bishop(b,5,0);
         pieces[1][2]= new Bishop(w,2,7);
         pieces[1][3]= new Bishop(w,5,7);         
         pieces[0][4]= new Knight(b,1,0);
         pieces[0][5]= new Knight(b,6,0);
         pieces[1][4]= new Knight(w,1,7);
         pieces[1][5]= new Knight(w,6,7);         
         pieces[0][6]= new Rook(b,0,0);
         pieces[0][7]= new Rook(b,7,0);
         pieces[1][6]= new Rook(w,0,7);
         pieces[1][7]= new Rook(w,7,7);
         for(int i = 0; i<8; i++){
             pieces[0][(i+8)] = new Pawn(b,i,1);
             pieces[1][(i+8)] = new Pawn(w,i,6);
         }
         //creates the frame containing the board
        Chess frame = new Chess();
        //creates image array
        createImages();
        //initialize selected point and sets equil to 9,9
        selected = new Point();
        clearSelected();
        //frame requirements
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setResizable(true);
        //update frame untill window is closed
        while (!checkmate){
            //randomRun();
            frame.update();
                    }
    }
    //update method for frame
    private void update(){
        display.update();
    }
    //returns game width
    public static int getGameWidth(){
            return GAME_WIDTH;
        }
    //returns game height
    public static int getGameHeight(){
            return GAME_HEIGHT;
        }
    //returns scale
    public static int getScale(){
        return scale;
    }
    //if any piece is in location p true if not false
    public static boolean occupied(Point p){
        for(int i =0; i<2;i++){
            for( int j =0;j<16;j++){
                if(pieces[i][j].getLocation().x==p.x&&pieces[i][j].getLocation().y==p.y){
                    return true;
                }
            }
        }
        return false;
    }
    //returns name at point p
    public static String getName(Point p){
        for(int i =0; i<2;i++){
            for( int j =0;j<16;j++){
                if(pieces[i][j].getLocation().x==p.x&&pieces[i][j].getLocation().y==p.y){
                return pieces[i][j].getName();
                }
            }
        }
        return "";
    }
    //returns color at x,y
    public static String getColor(int x, int y){
        for(int i =0; i<2;i++){
            for( int j =0;j<16;j++){
                if(pieces[i][j].getLocation().x==x&&pieces[i][j].getLocation().y==y){
                return pieces[i][j].getColor();
                }
            }
        }
        return "";
    }
    //returns color of point p
    public static String getColor(Point p){
        for(int i =0; i<2;i++){
            for( int j =0;j<16;j++){
                if(pieces[i][j].getLocation().x==p.x&&pieces[i][j].getLocation().y==p.y){
                return pieces[i][j].getColor();
                }
            }
        }
        return "";
    }
    //selects p if another square is not highlighted
    public static void select(Point p){
        // set the inital button push
        if(selected.x>8&&selected.y>8){
            selected = p;
        }
        //for if the same button is hit
        else if(selected.x == p.x&&selected.y==p.y){
            clearSelected();
        }
        
    }
    //returns selected
    public static Point getSelected(){    
        return selected;
    }
    //clears selected by setting selected to 9,9
    public static void clearSelected(){
        selected.x = 9;
        selected.y = 9;
    }
    //creats chess image array
    //code and image taken from https://stackoverflow.com/questions/21142686/making-a-robust-resizable-swing-chess-gui
    private static final void createImages() {
        try {
            BufferedImage bi = ImageIO.read(Chess.class.getResourceAsStream("ChessPieces.png"));
            for (int i = 0; i < 2; i++) {
                for (int j = 0; j < 6; j++) {
                    chessPieceImages[i][j] = bi.getSubimage(j * 64, i * 64, 64, 64);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
    //returns image at x,y
    public static Image getImage(int x, int y){
        return chessPieceImages[x][y];
    }
    //returns pieces array
    public static Piece[][] getpieces(){
        return pieces;
    }
    //returns the piece at point p
    public static Piece findPiece(Point p){
        for(int i = 0; i<2; i++){
            for(int j = 0;j<16; j++){
                if(pieces[i][j].getLocation().x==p.x&&pieces[i][j].getLocation().y==p.y)
                    return pieces[i][j];
            }
        }
        return null;
    }
    //turns piece at point p to a queen
    public static void transformPiece(Point p){
        for(int i = 0; i<2; i++){
            for(int j = 0;j<16; j++){
                if(pieces[i][j].getLocation().x==p.x&&pieces[i][j].getLocation().y==p.y)
                    if(pieces[i][j].getColor().contains(b)){
                        pieces[i][j] = new Queen(b,p.x,p.y);
                    }
                    else{
                        pieces[i][j] = new Queen(w,p.x,p.y);
                    }
            }
        }
    }
    //returns true if selected piece can move to location p
    public static boolean includedMove(Point s,Point p){
        Point[] moves = findPiece(s).moves(s);
        if(moves == null){
            return false;
        }
        for(int i =0; i< moves.length;i++){
            if(moves[i].x == p.x && moves[i].y == p.y){
                return true;
            }
        }
        return false;
    }
    //moves piece at selected to p, takes piece if present
    public static void move(Point s,Point p){
        Point kill = new Point(9,9);
        if(occupied(p)){
            for(int i = 0; i<2; i++){
                for(int j = 0;j<16; j++){
                    if(pieces[i][j].getLocation().x==p.x&&pieces[i][j].getLocation().y==p.y)
                         pieces[i][j].setLocation(kill);
                }
            }
            for(int i = 0; i<2; i++){
                for(int j = 0;j<16; j++){
                    if(pieces[i][j].getLocation().x==s.x&&pieces[i][j].getLocation().y==s.y)
                         pieces[i][j].setLocation(p);
                }
            }
        }
        else{
            for(int i = 0; i<2; i++){
                for(int j = 0;j<16; j++){
                    if(pieces[i][j].getLocation().x==s.x&&pieces[i][j].getLocation().y==s.y)
                         pieces[i][j].setLocation(p);
                }
            }
        }
        clearSelected();
    }
    //changes the turn from white to black or black to white
    public static void changeTurn(){
        if(turn.contains(w)){
            turn =b;
            if(checkCheck()){
                //checkCheckMate();
            }
        }
        else if(turn.contains(b)){
            turn = w;
            if(checkCheck()){
                //checkCheckMate();
            }
        }
        else{
            throw new IllegalArgumentException("Error: flipTurn");
        }        
    }
    //returns turn
    public static String getTurn(){
        return turn;
    }
    //returns check status
    public static boolean getCheck(){
        return check;
    }
    // sets check if nessary
    public static boolean checkCheck(){
        Point king;
        Point temp;
        //if its blacks turn
        if(turn.contains(b)){
            //get the black kings location
            king = pieces[0][0].getLocation();
            //for each black piece
            for(int i=0; i<16; i++){
                temp = pieces[1][i].getLocation();
                if(includedMove(temp,king)){
                    check = true;
                    return true;
                }
            }
            check = false;
            return false;
        }
        else if(turn.contains(w)){
            king = pieces[1][0].getLocation();
            for(int i=0; i<16; i++){
                temp = pieces[0][i].getLocation();
                if(includedMove(temp,king)){
                    check = true;
                    return true;
                }
            }
            check = false;
            return false;
        }
        else{
            throw new IllegalArgumentException("bad turn input checkCheck");
        }
    }
//    public static boolean checkCheckMate(){
//        Point king;
//        Point piece;
//        Point[] tempMoves;
//        //if its blacks turn
//        if(turn.contains(b)){
//            //get the black kings location
//            king = pieces[0][0].getLocation();
//            for(int i=0; i<16; i++){
//                piece = pieces[1][i].getLocation();
//                tempMoves = pieces[1][i].moves(piece);
//                for(int j=0;i<tempMoves.length;j++){
//                    if(tempMoves[j])
//                }
//              
//            }
//            
//        }
//    }
    public static boolean assumeMove(Point p){
        Piece tempPiece;
        Point temp;
        tempPiece = findPiece(selected);
        temp = tempPiece.getLocation();
        tempPiece.setLocation(p);
        if(checkCheck()){
           tempPiece.setLocation(temp);
           select(selected);
           return false;
        }
        else{
           tempPiece.setLocation(temp);
           return true;
        }
    }
    public static void randomRun(){
        try{
            if(turn==w){
              p1 = pieces[1][random.nextInt(16)];
              if(p1.moves(p1.getLocation())!=null|| (p1.getLocation().x==9 && p1.getLocation().y==9)){
                  moves = p1.moves(p1.getLocation());
                  if(moves.length==0){
                      return;
                  }
                  theMove = moves[random.nextInt(moves.length)];
                  move(p1.getLocation(),theMove);
                  changeTurn();
              }
            }
            else if(turn==b){
            p1 = pieces[0][random.nextInt(16)];
              if(p1.moves(p1.getLocation())!=null|| (p1.getLocation().x==9 && p1.getLocation().y==9)){
                  moves = p1.moves(p1.getLocation());
                  if(moves.length==0){
                      return;
                  }
                  System.out.println(moves.length);
                  theMove = moves[random.nextInt(moves.length)];
                  move(p1.getLocation(),theMove);
                  changeTurn();
              }
            }
            else{
                return;
            }
        }catch(NullPointerException e){
            return;
        }
    }
}
board.java:
package Game;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import javax.swing.*;
/**
 ****Chess of Champions****
 * @Author Ian Sodersjerna
 * @Contributors Andrew Thompson of StackOverflow for chess piece images , Alexander Venezia early assistance with GUI elements
 * @Date 4/18/2019
 * @Project Chess of Champions
 * @Modified 4/18/2019
 * @Modified 4/20/2019
 * @Modified 4/21/2019
 * @Modified 4/23/2019
 * @Modified 4/25/2019
 * @Modified 4/27/2019
 * @Modified 4/28/2019
 * @Modified 4/30/2019
 * @Modified 5/01/2019
 * @Modified 5/02/2019
 * @Modified 5/03/2019
 * @Modified 5/04/2019
 * @Modified 5/05/2019
 * @Modified 5/07/2019
 * @Modified 5/09/2019
 * @Modified 5/10/2019
 ****Early Access Notice****
 **Features under development:
 * Check will occasionally break game comment out MouseActions lines 44 and 67 to disable
 * Pawn promotion will only ever result in queen
 **Working Features:
 * movement
 * Highlighting Movements
 * taking pieces
 * pieces have accurate images at all times
 */
public class Board extends JPanel{
    private static Piece[][] pieces = new Piece[2][16]; 
    private final int scale;
    private final int x;
    private final int y; 
    private final int DISPLAY_WIDTH;
    private final int DISPLAY_HEIGHT; 
    //constructor
    Board(int x, int y, int width, int height, int scale){       
        this.scale = scale;
        this.x = x;
        this.y = y;
        this.DISPLAY_WIDTH = width;
        this.DISPLAY_HEIGHT = height;
        super.setBackground(Color.black);
    }
    //updates Panel
    public void update(){
        paintImmediately(x, y, DISPLAY_WIDTH, DISPLAY_HEIGHT); //Redraw graphics 
    }
    //Creates chessboard pattern
    private void render(Graphics g){
        super.paintComponent(g);
        g.setColor(Color.gray);
        for(int i =0; i<8; i+=2){
            for(int j =0; j<8; j++){
               if(j==1||j==3||j==5||j==7){
                  g.fillRect(i*scale, j*scale, scale, scale);
               }
               else{
                  g.fillRect((i+1)*scale, j*scale, 1*scale, 1*scale); 
               }
            }
        }
    }
    //draws each piece in its location or off screen if piece is taken and highlights selected piece and moves
    private void renderUI(Graphics g){
        //highlights the selected piece
        Point p = Chess.getSelected();
        if(p.x<9&&p.y<9){
            highlightMoves(g,p);
        }
        g.setColor(Color.yellow);        
        g.fillRect(p.x*scale, p.y*scale, 1*scale, 1*scale);
        //draws pieces on board
        drawPieces(g);
        //adds borders to squares
        addBoarders(g);
        //writes who'turn it is and writes if a king is in check
        writeText(g);
    }
    //highlights possible moves of piece at Point p
    public void highlightMoves(Graphics g, Point p){
        Piece selected = Chess.findPiece(p);
        try{
            Point[] moves = selected.moves(p);

            g.setColor(Color.green);
            moves = Chess.findPiece(p).moves(p);
            try{
                for(int i = 0;i<moves.length;i++){
                 g.fillRect(moves[i].x*scale, moves[i].y*scale, scale, scale);
                } 
            }catch(ArrayIndexOutOfBoundsException e){
                    System.out.println("HighlightMoves array out of bound exception in class: Board");
            }
        }catch(NullPointerException e){
        }
    } 
    //overrides JPanel paintComponrnt
    @Override
    public void paintComponent(Graphics g){      
        render(g);
        renderUI(g);
    }
    //overrides JPanel getPreferredSize
    @Override
    public Dimension getPreferredSize()
    {
        return new Dimension(DISPLAY_WIDTH, DISPLAY_HEIGHT);
    }
    //adds border to each square
    public void addBoarders(Graphics g){
        g.setColor(Color.black);
        for(int i =0; i<8; i++){
            for(int j =0; j<8; j++){
                g.drawRect(i*scale, j*scale, scale, scale);
            }
        }
    }
    public void drawPieces(Graphics g){
        pieces = Chess.getpieces();
        for(int i=0;i<2;i++){
            for(int j=0;j<16;j++){
                if(pieces[i][j].getColor().contains("white")){
                  if(pieces[i][j].getName().contains("king")){
                      g.drawImage(Chess.getImage(1,0), pieces[i][j].getLocation().x*scale+(scale/2)-32, pieces[i][j].getLocation().y*scale+(scale/2)-32, this);
                  }
                  else if(pieces[i][j].getName().contains("queen")){
                      g.drawImage(Chess.getImage(1,1), pieces[i][j].getLocation().x*scale+(scale/2)-32, pieces[i][j].getLocation().y*scale+(scale/2)-32, this);
                  }
                  else if(pieces[i][j].getName().contains("bishop")){
                      g.drawImage(Chess.getImage(1,4), pieces[i][j].getLocation().x*scale+(scale/2)-32, pieces[i][j].getLocation().y*scale+(scale/2)-32, this);
                  }
                  else if(pieces[i][j].getName().contains("rook")){
                      g.drawImage(Chess.getImage(1,2), pieces[i][j].getLocation().x*scale+(scale/2)-32, pieces[i][j].getLocation().y*scale+(scale/2)-32, this);
                  }
                  else if(pieces[i][j].getName().contains("knight")){
                      g.drawImage(Chess.getImage(1,3), pieces[i][j].getLocation().x*scale+(scale/2)-32, pieces[i][j].getLocation().y*scale+(scale/2)-32, this);
                  }
                  else if(pieces[i][j].getName().contains("pawn")){
                      g.drawImage(Chess.getImage(1,5), pieces[i][j].getLocation().x*scale+(scale/2)-32, pieces[i][j].getLocation().y*scale+(scale/2)-32, this);
                  }
                  else{
                      throw new IllegalArgumentException("bad piece name renderUI");
                  }
                }
                else if(pieces[i][j].getColor().contains("black")){
                  if(pieces[i][j].getName().contains("king")){
                      g.drawImage(Chess.getImage(0,0), pieces[i][j].getLocation().x*scale+(scale/2)-32, pieces[i][j].getLocation().y*scale+(scale/2)-32, this);
                  }
                  else if(pieces[i][j].getName().contains("queen")){
                      g.drawImage(Chess.getImage(0,1), pieces[i][j].getLocation().x*scale+(scale/2)-32, pieces[i][j].getLocation().y*scale+(scale/2)-32, this);
                  }
                  else if(pieces[i][j].getName().contains("bishop")){
                      g.drawImage(Chess.getImage(0,4), pieces[i][j].getLocation().x*scale+(scale/2)-32, pieces[i][j].getLocation().y*scale+(scale/2)-32, this);
                  }
                  else if(pieces[i][j].getName().contains("rook")){
                      g.drawImage(Chess.getImage(0,2), pieces[i][j].getLocation().x*scale+(scale/2)-32, pieces[i][j].getLocation().y*scale+(scale/2)-32, this);
                  }
                  else if(pieces[i][j].getName().contains("knight")){
                      g.drawImage(Chess.getImage(0,3), pieces[i][j].getLocation().x*scale+(scale/2)-32, pieces[i][j].getLocation().y*scale+(scale/2)-32, this);
                  }
                  else if(pieces[i][j].getName().contains("pawn")){
                      g.drawImage(Chess.getImage(0,5), pieces[i][j].getLocation().x*scale+(scale/2)-32, pieces[i][j].getLocation().y*scale+(scale/2)-32, this);
                  } 
                  else{
                      throw new IllegalArgumentException("bad piece name renderUI");
                  }
                }
                else{
                    throw new IllegalArgumentException("bad color input renderUI");
                }
            }
        }
    }
    public void writeText(Graphics g){
         g.setColor(Color.white);
        g.drawString("it is "+Chess.getTurn()+"'s turn", 0*scale, (8*scale)+(scale/4));
        if(Chess.getCheck()){
            g.drawString("you are in check", 2*scale, (8*scale)+(scale/4));
        }
    }
}
mouseactions.java:
package Game;
import java.awt.Point;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
/**
 ****Chess of Champions****
 * @Author Ian Sodersjerna
 * @Contributors Andrew Thompson of StackOverflow for chess piece images , Alexander Venezia early assistance with GUI elements
 * @Date 4/18/2019
 * @Project Chess of Champions
 * @Modified 4/18/2019
 * @Modified 4/20/2019
 * @Modified 4/21/2019
 * @Modified 4/23/2019
 * @Modified 4/25/2019
 * @Modified 4/27/2019
 * @Modified 4/28/2019
 * @Modified 4/30/2019
 * @Modified 5/01/2019
 * @Modified 5/02/2019
 * @Modified 5/03/2019
 * @Modified 5/04/2019
 * @Modified 5/05/2019
 * @Modified 5/07/2019
 * @Modified 5/09/2019
 * @Modified 5/10/2019
 ****Early Access Notice****
 **Features under development:
 * Check will occasionally break game comment out MouseActions lines 44 and 67 to disable
 * Pawn promotion will only ever result in queen
 **Working Features:
 * movement
 * Highlighting Movements
 * taking pieces
 * pieces have accurate images at all times
 */
public class MouseActions extends JPanel implements MouseListener {
    //actions upon pressing mouse button
    public void mousePressed(MouseEvent e) {
        //Chess.randomRun();
        Point p = new Point();
        String move = Chess.getTurn();
        Point selected = Chess.getSelected();
        p.x = e.getPoint().x/Chess.getScale();
        p.y = e.getPoint().y/Chess.getScale();
        
        //if no piece is selected
        if(selected.x>8&&selected.y>8){
            //check if square at point p is occupied
            if(Chess.occupied(p)){
                //checks if it is that colors turn
                if(Chess.getColor(p).contains(move)){
                    //checks if piece has moves
                    if(Chess.findPiece(p).moves(p)!=null){
                        //selects piece if all conditions are met
                        Chess.select(p);
                    }
                    else{
                        return;
                    }
                }
                else{
                    return;
                }
            }   
            else{
                return;
            }
        }
        //checks if the piece at selected can move to p
        else if(Chess.includedMove(selected,p)){
            if(Chess.assumeMove(p)){
                //if the piece is a pawn - used for pawn promotion
                if(Chess.getName(selected).contains("pawn")){
                    if(Chess.getColor(selected).contains("black")){
                        if(p.y==7){
                            Chess.transformPiece(selected);
                            Chess.move(selected, p);
                            Chess.changeTurn();
                            return;   
                        }
                    }
                    else if(Chess.getColor(selected).contains("white")){
                        if(p.y==0){
                            Chess.transformPiece(selected);
                            Chess.move(selected, p);
                            Chess.changeTurn();
                            return; 
                        }    
                    }
                }
                //if the piece is not a pawn
                Chess.move(selected,p);
                Chess.changeTurn();
            }
        }
        //if the selected piece is selected again
        else if(selected.x==p.x&&selected.x==p.x){
            //piece is deselected
            Chess.select(p);
        }
    }
    //actions upon release of mouse button
    public void mouseReleased(MouseEvent e) {
    }
    //actions upon entering frame
    public void mouseEntered(MouseEvent e) {
       
    }
    //actions upon leaving frame
    public void mouseExited(MouseEvent e) {
       
    }
    //actions upon clicking mouse button
    public void mouseClicked(MouseEvent e) {
        
    }
}
package Game;
import java.awt.Point;
/**
 ****Chess of Champions****
 * @Author Ian Sodersjerna
 * @Contributors Andrew Thompson of StackOverflow for chess piece images , Alexander Venezia early assistance with GUI elements
 * @Date 4/18/2019
 * @Project Chess of Champions
 * @Modified 4/18/2019
 * @Modified 4/20/2019
 * @Modified 4/21/2019
 * @Modified 4/23/2019
 * @Modified 4/25/2019
 * @Modified 4/27/2019
 * @Modified 4/28/2019
 * @Modified 4/30/2019
 * @Modified 5/01/2019
 * @Modified 5/02/2019
 * @Modified 5/03/2019
 * @Modified 5/04/2019
 * @Modified 5/05/2019
 * @Modified 5/07/2019
 * @Modified 5/09/2019
 * @Modified 5/10/2019
 ****Early Access Notice****
 **Features under development:
 * Check will occasionally break game comment out MouseActions lines 44 and 67 to disable
 * Pawn promotion will only ever result in queen
 **Working Features:
 * movement
 * Highlighting Movements
 * taking pieces
 * pieces have accurate images at all times
 */
public class Piece {
    //attributes
      private final String name;
      private final String color;
      private Point location;
    //constructor
     public Piece(){
         location = new Point(9,9);
         name = "blank";
         color = null;
     }
     //returns name
    public String getName(){
        return this.name;
    }
    //sets Location
    public void setLocation(Point p){
        this.location = p;
    }
    //returns location
    public Point getLocation(){
        return this.location;
    }
    //returns color
    public String getColor(){
        return this.color;
    } 
    //returns empty array
    public Point[] moves(Point p){
        Point[] moves = new Point[0];
        return moves;
    }  
    //concatinates point array a and b
    public static Point[] combineArray(Point[] a, Point[] b){
        //combines the length of both arrays
        int length = a.length + b.length;
        // creates a new array the length of both arrays
        Point[] combined = new Point[length];
        //coppies from array a, starting position 0, to combined, starting position 0, repeadly coppies for array a.length times
        System.arraycopy(a, 0, combined, 0, a.length);
        //coppies from array b, starting position 0, to combined, starting position a.length because thats where it stoped writing previously, repeadly coppies for array b.length times
        System.arraycopy(b, 0, combined, a.length, b.length);
        //returns the combined array
        return combined;
    }
    //concatinates point array a, b and c
    //uses additional System.arraycopy statements same as previous combineArray method, detailed comments omited
    public static Point[] combineArray(Point[] a, Point[] b,Point[] c){
        int length = a.length + b.length+c.length;
        Point[] combined = new Point[length];
        System.arraycopy(a, 0, combined, 0, a.length);
        System.arraycopy(b, 0, combined, a.length, b.length);
        System.arraycopy(c, 0, combined, (a.length+b.length), c.length);
        return combined;
    }
    //concatinates point array a, b, c and d
    //uses additional System.arraycopy statements same as previous combineArray method, detailed comments omited
    public static Point[] combineArray(Point[] a, Point[] b,Point[] c, Point[] d){
        int length = a.length + b.length+c.length+d.length;
        Point[] combined = new Point[length];
        System.arraycopy(a, 0, combined, 0, a.length);
        System.arraycopy(b, 0, combined, a.length, b.length);
        System.arraycopy(c, 0, combined, (a.length+b.length), c.length);
        System.arraycopy(d, 0, combined, (a.length+b.length+c.length), d.length);
        return combined;
    }
    //checks if either point array a or b are empty and utilises combineArray to concatinate
    public static Point[] checkCombine(Point[] a, Point[] b){
        // if array a contains at least one point
        if(a.length>0){
            //array a contains at least one point checks array b
           if(b.length>0){
               //combine the arrays if both contain points
               return combineArray(a,b);
           }
           //array a contains at least 1 long but b does not
           else{
               //returns only a
               return a;
           }
       }
       else{
            //array a contains no points checks b
           if(b.length>0){
               //array b contains points
               return b;
           }
           else{
               //neither contain any points
               return null;
           }
       }
    }
     //checks if either point array a, b, c or d are empty and utilises combineArray to concatinate
     //uses additional nested if statements same as previous checkCombine method, detailed comments omited
    public static Point[] checkCombine(Point[] a, Point[] b,Point[] c, Point[] d){
        if(a.length>0){
            if(b.length>0){
                if(c.length>0){
                    if(d.length>0){
                        return combineArray(a,b,c,d);
                    }
                    else{
                        return combineArray(a,b,c);
                    }
                }
                else {
                    if(d.length>0){
                        return combineArray(a,b,d);
                    }
                    else{
                        return combineArray(a,b);
                    }
                }
            }
            else{
                if(c.length>0){
                    if(d.length>0){
                        return combineArray(a,c,d);
                    }
                    else{
                        return combineArray(a,c);
                    }
                }
                else {
                    if(d.length>0){
                        return combineArray(a,d);
                    }
                    else{
                        return a;
                    }
                }
                
            }
        }
        else{
           if(b.length>0){
                if(c.length>0){
                    if(d.length>0){
                        return combineArray(b,c,d);
                    }
                    else{
                        return combineArray(b,c);
                    }
                }
                else {
                    if(d.length>0){
                        return combineArray(b,d);
                    }
                    else{
                        return b;
                    }
                }
            }
            else{
                if(c.length>0){
                    if(d.length>0){
                        return combineArray(c,d);
                    }
                    else{
                        return c;
                    }
                }
                else {
                    if(d.length>0){
                        return d;
                    }
                } 
            } 
        }
        return null;
    }
    //returns color of piece at point p in array pieces
    public static String findColor(Point p, Piece[][] pieces){
     for(int i =0; i<2;i++){
            for( int j =0;j<16;j++){
                if(pieces[i][j].getLocation().x==p.x&&pieces[i][j].getLocation().y==p.y){
                return pieces[i][j].getColor();
                }
            }
        }
        return null;
    }
}