package checkers;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* The main purpose of this class is to display the board. It is composed by 64 black and white squares. Each of them is identified by two coordinate system: Col and Row or X and Y. The first couple is used all over the game to identify the position of a square; the second are just used in the SMR request.
*/
public class Grid
extends JPanel {
/**
* size of the board
*/
final static public int ROW_MIN = 0;
/**
* size of the board
*/
final static public int COL_MIN = 0;
/**
* size of the board
*/
final static public int ROW_MAX = 8;
/**
* size of the board
*/
final static public int COL_MAX = 8;
static private Square[][] square = new Square[ROW_MAX][COL_MAX];
static private Grid grid = new Grid();
static private Square outside = grid.new Square();
/**
*This Method initilize the grid and places the gound square on the board.
*
*/
private Grid() {
boolean rowFirstColor = Square.WHITE;
for (int x=ROW_MIN;x<ROW_MAX ;x++) {
rowFirstColor = !rowFirstColor;
boolean SQcolor = rowFirstColor;
for (int y=COL_MIN;y<COL_MAX ;y++) {
SQcolor = !SQcolor;
square[x][y] = new Square(x, y, SQcolor);
add(square[x][y]);
}
}
}
/**
* Panel size
* @return an instance of Dimension that represents the minimum size of this container.
*/
return new Dimension((ROW_MAX
+1) * Square.
edge,
(COL_MAX
+1)* Square.
edge);
}
/**
* Panel size
* @return an instance of Dimension that represents the preferred size of this container.
*/
return new Dimension((ROW_MAX
+1) * Square.
edge,
(COL_MAX
+1)* Square.
edge);
}
/**
* This method is used to check if a piece is locatet at the upperside side of board
* @return True if the piece is loacated at the upperside side of the board false otherwise.
* @param square Position to be tested
* @param color Color of the piece
*/
static public boolean backLine(Square square,boolean color){
boolean retValue = false;
retValue = (square.getRow() == ROW_MIN && color == Piece.WHITE);
retValue |= (square.getRow() == (ROW_MAX-1) && color == Piece.BLACK);
return retValue;
}
/**
* This method is used to check if information about a square and what it contains
* @return the square with information if it is located inside the board.
* @param col coordinate of the square
* @param row coordinate of the square
*/
static public Square getSquare(int row, int col) {
if (row >= ROW_MIN && row < ROW_MAX
&& col >= COL_MIN && col < COL_MAX)
return square[row][col];
else return outside;
}
/**
* This method is used to printout the information of the square in a string
* @return a string with square information converted to a text string.
*/
String retValue
= "The Grid \r\n";
for(int x=ROW_MIN;x<ROW_MAX;x++) {
for(int y=COL_MIN;y<COL_MAX;y++){
retValue += square[x][y];
}
retValue += "\r\n";
}
return retValue;
}
/**
* This method is used get Grid's object.
* @return the information about the grid.
*/
static public Grid getInstance() {
return grid;
}
/**
* This Class is used to initilize the squares and is a extendstion of Canvas. It contains all the information of the squares.
*/
public class Square
extends Canvas {
final static boolean WHITE = true;
final static boolean BLACK = false;
final static int edge = 30;
private boolean isFree = true;
private Piece piece;
private int col;
private int row;
private int x;
private int y;
private boolean SQcolor = WHITE;
private boolean outside = false;
/**
* Construtor of the Square the set all square outside the board.
*/
public Square(){
outside = true;
}
/**
*This Method initilize all the square with the information they need.
*
*/
private Square(final int row, final int col, boolean SQcolor){
this.row = row;
this.col = col;
this.SQcolor = SQcolor;
this.x = (row-7 + col);
this.y = (col-row);
setSize(edge,edge);
setBackground
(SQcolor
? Color.
white : Color.
black);
if ((e.getModifiers()) > 0)
System.
out.
println("Square " + row
+ " "+ col
);
}
});
}
/**
* Causes this container to lay out its components.
*/
public void doLayout() {
setBounds(row * edge , col * edge, (row + 1) * edge, (col + 1) * edge);
}
/**
* This Method is to tell the color of the square black or white
* @return the color of the square
*/
public boolean isBlack() {
return SQcolor;
}
/**
* This method is used to find out if a square is within the board and if it is black.
* @return true if the conditions are meet else false.
*/
public boolean isLegal(){
if ((Math.
abs(getX
())+Math.
abs(getY
()) <= 7) && SQcolor
== BLACK
&& !outside
)
return true;
else return false;
}
/**
* This Method finds out if there is a piece on the square
* @return true if there is no piece on the square else false if it is also outside the board.
*/
public boolean isFree(){
return (isFree && !outside);
}
/**
* This Method is used to set a square free of a piece or set a piece on it and it repaint the interface.
* @param isFree Value to be altered
*/
public void setFree(boolean isFree) {
this.isFree = isFree;
piece = Board.select(row, col);
repaint();
}
/**
* This method turns the coordinate of the square into a sting.
* @return the string containing the coordinats of the square.
*/
return "Square ("+row+", "+col+") ";
}
/**
* This method is used find out if the squares are equal
* @return true if they are else false.
* @param anObject object to be compared to
*/
public boolean equals
(Object anObject
) {
if (anObject != null && anObject instanceof Square) {
Square square = (Square) anObject;
return (square.getCol() == col && square.getRow() == row);
}
else
return false;
}
/**
* This method finds out if the piece on the square has the same colour of the parameter
* @return true if they are the same colour else false.
* @param color color to be tested
*/
public boolean PieceHasNotTheColor(boolean color) {
if (piece == null)
return false;
if (color != piece.getColor())
return true;
return false;
}
/**
* This method is used find out the information about the piece on the piece on the square.
* @return the information of the piece.
*/
public Piece getPiece() {
return piece;
}
/**
* This method is used to get the coloum of the square.
* @return the coloum.
*/
public int getCol() {
return col;
}
/**
* This method is used to get the row of the square.
* @return the row.
*/
public int getRow() {
return row;
}
/**
* This method is used to get the x- coordinat of the square (the SMR coordinatsystem.
* @return the x-coordinat.
*/
public int getX() {
return x;
}
/**
* This method is used to get the y- coordinat of the square (the SMR coordinatsystem.)
* @return the y-coordinat.
*/
public int getY() {
return y;
}
/**
* This method is used to get the colour of the square
* @return true if it white and false if it is black
*/
public boolean getColor() {
return SQcolor;
}
/**
* This method is used TO paint the graphical user interface by drawing it.
* @param g Graphic object
*/
g.clearRect(0,0,edge,edge);
if (!isFree) {
if (piece == null)
return;
g.
setColor(piece.
getColor() ? Color.
white : Color.
red);
g.fillOval(3,3,edge-7,edge-7);
if (piece.isKing()) {
g.
setColor(Color.
YELLOW);
g.fillOval(8,8,edge-15,edge-15);
}
g.setFont(f);
int number = piece.getNumber();
int w = fm.charWidth(number);
int h = fm.getHeight();
g.drawString(""+number,((edge-w)/2),((edge+h)/2));
}
}
}
}