Hello. I was trying to make a Chess application and make every piece an object to fully understand the OOP's concept,
but there is an if statement that is never working and I have no idea.
My app was supposed to
1. instantiate every single piece and store them and their position in a 2-D list (Positions are stored with a custom object "PTuple")
2. iterate through the 2-D list and build a string by adding a space if there are no pieces and add the initial letter of the piece <- this is going wrong.
3. print a string for each column of the board
4. take user input to move the piece
5. store the new position
6. go back to 2
Until the game ends. but my If statement always adds a space. I even added a Println in the else block so it prints "no string to add" when the if statement evaluates to false. and it always does.
My code got pretty big, and now I'm searching through my code for errors but I can't find it.
Please help me.
Thanks.
The code:
package Chess;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.swing.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
class PTuple{
int left;
int right;
PTuple(int x, int y){
left = x;
right = y;
};
public int getValues(boolean Right){
if (Right){
return right;
} else {
return left;
}
}
boolean isEqualTo(PTuple T){
if (T == null){
return false;
};
if ((this.getValues(true) == T.getValues(true)) && (this.getValues(false) == T.getValues(false))){
return true;
} else {
return false;
}
};
int toInt(){
return ((this.getValues(false)*10) + this.getValues(true));
};
};
class PHolder{
PTuple Position[][];
PHolder(){
PTuple Position[][] = new PTuple[8][8];
for(int i = 0;i < Position.length; i++){
for(int x = 0;x < Position[0].length; x++){
Position[i][x] = new PTuple((i*20),(x*20));
};
};
};
};
abstract class Pieces{
String type;
boolean White = false;
boolean state = true;
int verMove;
int horiMove;
int yPos;
int xPos;
public Pieces(int x,int y,boolean white){
int xPos = x;
int yPos = y;
White = white;
};
boolean Move(Board board,int x,int y){
int xPos = x;
int yPos = y;
return true;
};
boolean doubleMoved(){
return false;
};
boolean didNotMove(){
return false;
};
int returnPos(){
PTuple Returnable = new PTuple(xPos , yPos);
return Returnable.toInt();
};
Pieces returnThis(){
return this;
};
abstract void Die();
String returnType(){
return type;
};
boolean isWhite(){
return White;
};
boolean isEqualTo(Pieces piece){
if (piece == null){
return false;
};
if (this == piece){
return true;
} else {
return false;
}
};
};
class Checker{
Checker(){
};
Pieces returnPiece(Board board,int xPos,int yPos){
PTuple f = new PTuple(xPos,yPos);
return board.findPiece(f.toInt());
};
int getPos(Pieces Piece){
return Piece.returnPos();
};
Pieces getPiece(Pieces Piece){
return Piece.returnThis();
};
};
//Stores the Pieces and their positions
class Board{
boolean game = true;
Map<Integer,Pieces> pieceMap = new HashMap<Integer,Pieces>();
PHolder Positions = new PHolder();
Pieces[] W_pawn = new Pieces[9];
Pieces[] B_pawn = new Pieces[9];
Checker Check = new Checker();
Board(){
};
Pieces findPiece(int i){
return pieceMap.get(i);
};
void setBoard(Board board){
for(int i=1 ; i<9 ; i++){
Pawn W_Pawn = new Pawn(i,2,true);
W_pawn[i] = W_Pawn;
board.recordPos(W_pawn[i]);
};
for(int i=1;i<9;i++){
Pawn B_Pawn = new Pawn(i,2,false);
B_pawn[i] = B_Pawn;
board.recordPos(B_pawn[i]);
};
Rook W_Rook1 = new Rook(1,1,true);
board.recordPos(W_Rook1);
Rook W_Rook2 = new Rook(1,8,true);
board.recordPos(W_Rook2);
Rook B_Rook1 = new Rook(8,1,false);
board.recordPos(B_Rook1);
Rook B_Rook2 = new Rook(8,8,false);
board.recordPos(B_Rook2);
Horse W_Horse1 = new Horse(1,2,true);
board.recordPos(W_Horse1);
Horse W_Horse2 = new Horse(1,7,true);
board.recordPos(W_Horse2);
Horse B_Horse1 = new Horse(8,2,false);
board.recordPos(B_Horse1);
Horse B_Horse2 = new Horse(8,7,false);
board.recordPos(B_Horse2);
Bishop W_Bishop1 = new Bishop(1,3,true);
board.recordPos(W_Bishop1);
Bishop W_Bishop2 = new Bishop(1,6,true);
board.recordPos(W_Bishop2);
Bishop B_Bishop1 = new Bishop(8,3,false);
board.recordPos(B_Bishop1);
Bishop B_Bishop2 = new Bishop(8,6,false);
board.recordPos(B_Bishop2);
Queen W_Queen = new Queen(1,4,true);
board.recordPos(W_Queen);
Queen B_Queen = new Queen(8,4,false);
board.recordPos(B_Queen);
King W_King = new King(1,5,true);
board.recordPos(W_King);
King B_King = new King(8,5,false);
board.recordPos(B_King);
};
void kill(Pieces Piece){
Piece.Die();
pieceMap.remove(Piece);
};
void recordPos(Pieces Piece){
pieceMap.put(Piece.returnPos() , Piece);
};
};
public class Chess{
public static void main(String arg[]){
Board board = new Board();
if ((arg.length > 0) && (arg[0]== "-graphics")){
//1. Create the frame.
JFrame frame = new JFrame("Chess Demo",null);
//2. Optional: What happens when the frame closes?
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//3. Create components and put them in the frame.
ImageIcon Board = new ImageIcon("Chessboard.png");
ImageIcon Rectangle = new ImageIcon("GrayRectangle.png");
JLabel Label1 = new JLabel(Board,JLabel.CENTER);
JLabel Label2 = new JLabel(Rectangle,JLabel.CENTER);
//4. Size the frame.
frame.pack();
//5. Show it.
frame.setVisible(true);
} else {
PHolder P = new PHolder();
Checker chk = new Checker();
Scanner scan = new Scanner(System.in);
board.setBoard(board);
boolean result;
boolean whiteIsPlaying;
int turn = 0;
int tempY = 1;
String printable;
while (board.game){
result = false;
turn++;
if (turn % 2 == 0){
whiteIsPlaying = false;
} else {
whiteIsPlaying = true;
};
for (int y = 1; y<8 ; y++){
StringBuilder sb = new StringBuilder();
for (int x = 1; x<8 ; x++){
PTuple position = new PTuple(x,y);
if (board.findPiece(position.toInt()) == null){
sb.append(" ");
System.out.println("No string to add.");
tempY = y;
} else {
String Type = board.findPiece(position.toInt()).returnType();
sb.append(Type.substring(0,1));
System.out.println("Sucessfully added string:" + " " + sb.toString());
tempY = y;
};
};
System.out.println(tempY + " " + sb.toString());
};
while (result == false){
System.out.println("Choose a piece by its position.");
String I_Piece = scan.next();
System.out.println("Now choose a position to move it.");
String I_Pos = scan.next();
Pieces selection = chk.returnPiece(board,Integer.parseInt(I_Piece.substring(0,1)),Integer.parseInt(I_Piece.substring(2,3)));
if (whiteIsPlaying && selection.isWhite()){
result = selection.Move(board,Integer.parseInt(I_Pos.substring(0,1)),Integer.parseInt(I_Pos.substring(2,3)));
} else {
if (!whiteIsPlaying && !selection.isWhite()){
result = selection.Move(board,Integer.parseInt(I_Pos.substring(0,1)),Integer.parseInt(I_Pos.substring(2,3)));
} else {
System.out.println("That move is invalid.");
};
};
};
};
};
};
};
class Pawn extends Pieces{
String type = "Pawn";
boolean doubleMove = false;
Checker checker = new Checker();
int xPos;
int yPos;
boolean isWhite;
public Pawn(int x,int y,boolean white){
super(x,y,white);
};
boolean doubleMoved(){
return doubleMove;
};
boolean Move(int x, int y,Board board){
if ((White && y - yPos == 1) || (!White && yPos - y == 1)){
yPos = y;
xPos = x;
return true;
} else {
if ((checker.returnPiece(board,x,(y + 1)).returnType() == "Pawn") && checker.returnPiece(board,x,(y + 1)).doubleMoved()){
en_Passant(x , y,board);
} else {
System.out.println("That move is invalid.");
return false;
};
};
return false;
};
boolean en_Passant(int x, int y,Board board){
if ((checker.returnPiece(board,xPos,(yPos + 1)).returnType() == "Pawn") && checker.returnPiece(board,xPos,(yPos + 1)).doubleMoved()){
board.kill((checker.returnPiece(board,xPos,(yPos + 1))));
xPos = x;
yPos = y;
return true;
};
return false;
};
int returnPos(){
PTuple Returnable = new PTuple(xPos , yPos);
return Returnable.toInt();
};
String returnType(){
return type;
};
void Die(){
state = false;
System.out.println("This method is not supported. This is a Placeholder for method \"Die\" ");
};
};
class Bishop extends Pieces{
public Bishop(int x,int y,boolean white){
super(x,y,white);
};
boolean Move(int x, int y){
if (White && (xPos - x == yPos - y) || (!White && (x - xPos == y - yPos))){
xPos = x;
yPos = y;
return true;
} else {
System.out.println("That move is invalid.");
};
return false;
};
int returnPos(){
PTuple Returnable = new PTuple(xPos , yPos);
return Returnable.toInt();
};
void Die(){
state = false;
System.out.println("This method is not supported. This is a Placeholder for method \"Die\" ");
};
};
class Rook extends Pieces{
Checker checker = new Checker();
boolean neverMoved = true;
public Rook(int x,int y,boolean white){
super(x,y,white);
};
boolean Move(Board board,int x, int y){
if (x != xPos){
for (int xP = xPos;xP<x;xP++){
Pieces result = checker.returnPiece(board,xP,yPos);
if (result != null){
System.out.println("There is a piece in the way.");
return false;
};
};
xPos = x;
return true;
} else {
for (int yP = yPos;yP<y;yP++){
Pieces result = checker.returnPiece(board,xPos,yP);
if (result != null){
System.out.println("There is a piece in the way.");
return false;
};
};
};
yPos = y;
return true;
};
boolean didNotMove(){
return neverMoved;
};
int returnPos(){
PTuple Returnable = new PTuple(xPos , yPos);
return Returnable.toInt();
};
void Die(){
state = false;
System.out.println("This method is not supported. This is a Placeholder for method \"Die\" ");
};
};
class King extends Pieces{
boolean neverMoved = true;
Checker checker = new Checker();
public King(int x,int y,boolean white){
super(x,y,white);
};
boolean Move(Board board,int x,int y){
if((Math.abs(xPos - x) <= 1) && (Math.abs(yPos - y) <= 1)){
xPos = x;
yPos = y;
return true;
} else {
boolean noPieces = true;
for(int pos = xPos;pos < x;pos++){
Pieces result = checker.returnPiece(board,yPos,pos);
if (result == null){
noPieces = true;
} else {
System.out.println("There is a piece in the way.");
return false;
};
};
if (noPieces && (checker.returnPiece(board,yPos, x + 1).returnType() == "Rook") && checker.returnPiece(board,yPos, x + 1).didNotMove()){
checker.returnPiece(board,yPos, x + 1).Move(board,y, x - 1);
xPos = x;
yPos = y;
return true;
};
};
return false;
};
int returnPos(){
PTuple Returnable = new PTuple(xPos , yPos);
return Returnable.toInt();
};
void Die(){
state = false;
System.out.println("This method is not supported. This is a Placeholder for method \"Die\" ");
};
};
class Queen extends Pieces{
public Queen(int x,int y,boolean white){
super(x,y,white);
};
boolean Move(int x, int y){
if((x == xPos) && (y != yPos) || (x != xPos && y == yPos)){
xPos = x;
yPos = y;
return true;
} else {
if(Math.abs(x - xPos) == Math.abs(y - yPos)){
xPos = x;
yPos = y;
return true;
} else {
System.out.println("That move is invalid.");
};
};
return false;
};
void Die(){
state = false;
System.out.println("This method is not supported. This is a Placeholder for method \"Die\" ");
};
int returnPos(){
PTuple Returnable = new PTuple(xPos , yPos);
return Returnable.toInt();
};
};
class Horse extends Pieces{
public Horse(int x,int y,boolean white){
super(x,y,white);
};
boolean Move(Board board,int x, int y){
if ((Math.abs(xPos - x) == 1 && Math.abs(yPos - y) == 2) || (Math.abs(xPos - x) == 2 && Math.abs(yPos - y) == 1)){
xPos = x;
yPos = y;
return true;
} else {
System.out.println("That move is invalid");
};
return false;
};
void Die(){
state = false;
System.out.println("This method is not supported. This is a Placeholder for method \"Die\" ");
};
int returnPos(){
PTuple Returnable = new PTuple(xPos , yPos);
return Returnable.toInt();
};
};