Commit 3c6b4cfc authored by Omid Sayfun's avatar Omid Sayfun

Initital Commit

parents
*.iml
.idea
*.class
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.util.*;
import lab.game.*;
import java.io.IOException;
public class Main{
public static void main(String[] args){
Chess newChess = new Chess();
newChess.run();
}
}
class newJButton extends JButton{
private Piece piece;
private char X;
private int Y;
public newJButton(char X, int Y){
this.piece = null;
this.X = X;
this.Y = Y;
this.setIcon(null);
}
public newJButton(Piece p, char X, int Y){
this.piece = p;
this.X = X;
this.Y = Y;
Image img = null;
try {
img = ImageIO.read(Chess.class.getResource("resources/images/" + p.getName().toLowerCase() + ".png"));
} catch (IOException e1) {
e1.printStackTrace();
}
this.setIcon(new ImageIcon(img));
}
public Piece getPiece(){
return this.piece;
}
public char getNewX(){
return this.X;
}
public int getNewY(){
return this.Y;
}
}
class Chess implements MouseListener{
private Board board = null;
private String color = "B";
private boolean isSelected = false;
private newJButton inMove = null;
private ArrayList<newJButton> btns = new ArrayList<newJButton>();
public void run(){
JFrame frame = new JFrame("Chess Frame");
frame.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
// Biggest Panel
JPanel fullPanel = new JPanel(new BorderLayout());
// Board Panel
JPanel boardPanel = new JPanel(new GridLayout(8,8));
ArrayList<Piece> whitePieces = new ArrayList<Piece>();
ArrayList<Piece> blackPieces = new ArrayList<Piece>();
for(int i = 1; i < 9; i++){
for(char j = 'A'; j < 'I'; j++){
newJButton btn = null;
if( i == 8 ){
if( j == 'A' || j == 'H' ){
Rook newRook = new Rook(j, i, true, "WR");
whitePieces.add(newRook);
btn = new newJButton(newRook, j, i);
}else if( j == 'B' || j == 'G' ){
Knight newKnight = new Knight(j, i, true, "WN");
whitePieces.add(newKnight);
btn = new newJButton(newKnight, j, i);
}else if( j == 'C' || j == 'F' ){
Bishop newBishop = new Bishop(j, i, true, "WB");
whitePieces.add(newBishop);
btn = new newJButton(newBishop, j, i);
}else if( j == 'D' ){
Queen whiteQueen = new Queen(j, i, true,"WQ");
whitePieces.add(whiteQueen);
btn = new newJButton(whiteQueen, j, i);
}else if( j == 'E' ){
King whiteKing = new King(j, i, true, "WK");
whitePieces.add(whiteKing);
btn = new newJButton(whiteKing, j, i);
}
}else if( i == 7 ){
Pawn newPawn = new Pawn(j, i, true, "WP");
whitePieces.add(newPawn);
btn = new newJButton(newPawn, j, i);
}else if( i == 1 ){
if( j == 'A' || j == 'H' ){
Rook newRook = new Rook(j, i, false, "BR");
blackPieces.add(newRook);
btn = new newJButton(newRook, j, i);
}else if( j == 'B' || j == 'G' ){
Knight newKnight = new Knight(j, i, false, "BN");
blackPieces.add(newKnight);
btn = new newJButton(newKnight, j, i);
}else if( j == 'C' || j == 'F' ){
Bishop newBishop = new Bishop(j, i, false, "BB");
blackPieces.add(newBishop);
btn = new newJButton(newBishop, j, i);
}else if( j == 'D' ){
Queen blackQueen = new Queen(j, i, false,"BQ");
blackPieces.add(blackQueen);
btn = new newJButton(blackQueen, j, i);
}else if( j == 'E' ){
King blackKing = new King(j, i, false, "BQ");
blackPieces.add(blackKing);
btn = new newJButton(blackKing, j, i);
}
}else if( i == 2 ){
// Pawn newPawn = new Pawn(j, i, false, "BP");
// blackPieces.add(newPawn);
// btn = new newJButton(newPawn, j, i);
btn = new newJButton(j, i);
}else{
btn = new newJButton(j, i);
}
this.board = new Board(whitePieces, blackPieces);
this.btns.add(btn);
btn.addMouseListener(this);
btn.setFocusable(false);
if( (i % 2 == 1 && j % 2 == 1) || (i % 2 == 0 && j % 2 == 0) ){
btn.setBackground(new Color(219,217,164));
}else{
btn.setBackground(new Color(4,51,106));
}
boardPanel.add(btn);
}
}
// Left Divider Panel
JPanel leftDivider = new JPanel(new GridLayout(3, 1));
// Top Left
JPanel topLeft = new JPanel(new GridLayout(2, 8));
for(char i = 0; i < 2; i++){
for(int j = 0; j < 8; j++){
JButton btn = new JButton();
ImageIcon icon = new ImageIcon(
new BufferedImage(40, 40, BufferedImage.TYPE_INT_ARGB));
btn.setIcon(icon);
btn.setBackground(Color.WHITE);
topLeft.add(btn);
}
}
// Middle Left
JPanel middleLeft = new JPanel();
JLabel playing = new JLabel("White Player is in control");
middleLeft.add(playing);
leftDivider.add(middleLeft);
// Bottom Left
JPanel bottomLeft = new JPanel(new GridLayout(2, 8));
for(int i = 0; i < 2; i++){
for(int j = 0; j < 8; j++){
JButton btn = new JButton();
ImageIcon icon = new ImageIcon(
new BufferedImage(40, 40, BufferedImage.TYPE_INT_ARGB));
btn.setIcon(icon);
btn.setBackground(Color.BLACK);
bottomLeft.add(btn);
}
}
// Add Panels
leftDivider.add(topLeft);
leftDivider.add(middleLeft);
leftDivider.add(bottomLeft);
fullPanel.add(boardPanel, BorderLayout.EAST);
fullPanel.add(leftDivider, BorderLayout.WEST);
frame.add(fullPanel);
// Show Frame
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
@Override
public void mouseClicked(MouseEvent e) {
if( !isSelected ){
newJButton source = (newJButton)(e.getSource());
for(newJButton btn : this.btns){
if( source != btn && source.getPiece() != null && this.board.canGo(source.getPiece(), btn.getNewX(), btn.getNewY(), this.color) ){
if( this.board.canAttack(source.getPiece(), btn.getNewX(), btn.getNewY(), this.color) ){
btn.setBackground(new Color(255, 0, 0));
}else{
Image img = null;
try {
img = ImageIO.read(Chess.class.getResource("resources/images/dot.png"));
} catch (IOException e1) {
e1.printStackTrace();
}
btn.setIcon(new ImageIcon(img, "dot"));
}
this.isSelected = true;
this.inMove = source;
}
}
}else{
for(newJButton btn : this.btns) {
// if( ((ImageIcon)btn.getIcon()) != null ){
// System.out.println(((ImageIcon)btn.getIcon()).getDescription());
// }
if( btn.getPiece() != null ){
Image img = null;
try {
img = ImageIO.read(Chess.class.getResource("resources/images/" + btn.getPiece().getName().toLowerCase() + ".png"));
} catch (IOException e1) {
e1.printStackTrace();
}
btn.setIcon(new ImageIcon(img));
}else{
btn.setIcon(null);
}
if( (btn.getNewY() % 2 == 1 && btn.getNewX() % 2 == 1) || (btn.getNewY() % 2 == 0 && btn.getNewX() % 2 == 0) ){
btn.setBackground(new Color(219,217,164));
}else{
btn.setBackground(new Color(4,51,106));
}
this.isSelected = false;
this.inMove = null;
}
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
package lab.game;
import javax.xml.stream.events.StartDocument;
import java.util.*;
public class Bishop extends Piece{
public Bishop(char x, int y, boolean color, String name){
super(x, y, color, name);
}
public boolean canMove(char x, int y){ // Ignore the presence of other pieces
if( Math.abs(this.x - x) != 0 && Math.abs(this.y - y) != 0 && Math.abs((float)(this.x - x) / (this.y - y)) == 1 ){
return true;
}
return false;
}
public static boolean canMove(Piece p, char x, int y){ // Ignore the presence of other pieces
if( Math.abs(p.x - x) != 0 && Math.abs(p.y - y) != 0 && Math.abs((float)(p.x - x) / (p.y - y)) == 1 ){
return true;
}
return false;
}
public static boolean checkWay(Piece p, ArrayList<Piece> pieces, char x, int y){
if( p.x - x == 0 ){
return false;
}
if( p.y - y == 0 ){
return false;
}
int xShift = (x - p.x) / Math.abs(x - p.x);
int yShift = (y - p.y) / Math.abs(y - p.y);
int i = 1;
while( x != (char)(p.x + i * xShift) && y != p.y + i * yShift ){
if( checkTaken(pieces, (char)(p.x + i * xShift), p.y + i * yShift) ){
return false;
}
i++;
}
return true;
}
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
int xShift = (x - this.x) / Math.abs(x - this.x);
int yShift = (y - this.y) / Math.abs(y - this.y);
int i = 1;
while( x != (char)(this.x + i * xShift) && y != this.y + i * yShift ){
if( checkTaken(pieces, (char)(this.x + i * xShift), this.y + i * yShift) ){
return false;
}
i++;
}
return true;
}
}
This diff is collapsed.
package lab.game;
import java.util.*;
public class King extends Piece{
public King(char x, int y, boolean color, String name){
super(x, y, color, name);
}
public boolean canMove(char x, int y){ // Ignore the presence of other pieces
if( Math.abs(this.x - x) < 2 && Math.abs(this.y - y) < 2 ){
return true;
}
return false;
}
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
return true;
}
}
package lab.game;
import java.util.*;
public class Knight extends Piece{
public Knight(char x, int y, boolean color, String name){
super(x, y, color, name);
}
public boolean canMove(char x, int y){
int X[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int Y[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
for (int i = 0; i < 8; i++) {
if( y == this.y + Y[i] && x == (char)(this.x + X[i]) ){
return true;
}
}
return false;
}
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
return true;
}
}
package lab.game;
import java.util.*;
public class Pawn extends Piece{
private boolean firstMove;
public Pawn(char x, int y, boolean color, String name){
super(x, y, color, name);
this.firstMove = true;
}
public void setFirstMove(boolean flag){
this.firstMove = flag;
}
public boolean getFirstMove(){
return this.firstMove;
}
public boolean canMove(char x, int y){ // Ignore the presence of other pieces
if( this.x - x == 0 ){
if( Math.abs(this.y - y) == 1 || (this.firstMove && Math.abs(this.y - y) == 2) ){
if( (this.color && this.y - y > 0) || (!this.color && y - this.y > 0) ){ // Moving Backward
if( this.firstMove ){
this.firstMove = false;
}
return true;
}else{
return false;
}
}else{
return false;
}
}else if( Math.abs(this.x - x) == 1 && Math.abs(this.y - y) == 1 ){
return true;
}
return false;
}
@Override
public boolean crossMove(char x, int y){
if( Math.abs(this.x - x) == 1 && Math.abs(this.y - y) == 1 ){
return true;
}
return false;
}
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
if( Math.abs(this.x - x) == 1 && Math.abs(this.y - y) == 1 ){
return true;
}else if( Math.abs(this.y - y) == 1 ){
return true;
}else{
int yShift = 0;
if( this.y - y != 0 ){
yShift = (y - this.y) / Math.abs(y - this.y);
}
int i = 1;
while( y != this.y + i * yShift ){
if( checkTaken(pieces, this.x, this.y + i * yShift) ){
return false;
}
i++;
}
if( checkTaken(pieces, this.x, this.y + i * yShift) ){
return false;
}
return true;
}
}
}
package lab.game;
import java.util.*;
public abstract class Piece{
protected int y;
protected char x;
protected boolean color;
private String name;
public Piece(char x, int y, boolean color, String name){
setX(x);
setY(y);
setColor(color);
setName(name);
}
public void setY(int y){
this.y = y;
}
public void setX(char x){
this.x = x;
}
public void setName(String name){
this.name = name;
}
public void setColor(boolean color){
this.color = color;
}
public int getY(){
return this.y;
}
public char getX(){
return this.x;
}
public String getName(){
return this.name;
}
public boolean getColor(){
return this.color;
}
public static boolean checkTaken(ArrayList<Piece> pieces, char x, int y){
for(Piece p : pieces){
if( p.x == x && p.y == y ){
return true;
}
}
return false;
}
public boolean crossMove(char x, int y){
return false;
}
public abstract boolean canMove(char x, int y);
public abstract boolean checkWay(ArrayList<Piece> pieces, char x, int y);
}
package lab.game;
import java.util.*;
public class Queen extends Piece{
public Queen(char x, int y, boolean color, String name){
super(x, y, color, name);
}
public boolean canMove(char x, int y){ // Ignore the presence of other pieces
if( Rook.canMove(this, x, y) || Bishop.canMove(this, x, y) ){
return true;
}
return false;
}
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
if( Rook.checkWay(this, pieces, x, y) || Bishop.checkWay(this, pieces, x, y) ){
return true;
}
return false;
}
}
package lab.game;
import java.util.*;
public class Rook extends Piece{
public Rook(char x, int y, boolean color, String name){
super(x, y, color, name);
}
public boolean canMove(char x, int y){ // Ignore the presence of other pieces
if( this.x - x == 0 || this.y - y == 0 ){
return true;
}
return false;
}
public static boolean canMove(Piece p, char x, int y){ // Ignore the presence of other pieces
if( p.x - x == 0 || p.y - y == 0 ){
return true;
}
return false;
}
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
int xShift = 0;
int yShift = 0;
if( this.x - x != 0 ){
xShift = (x - this.x) / Math.abs(x - this.x);
}
if( this.y - y != 0 ){
yShift = (y - this.y) / Math.abs(y - this.y);
}
int i = 1;
while( x != (char)(this.x + i * xShift) || y != this.y + i * yShift ){
if( checkTaken(pieces, (char)(this.x + i * xShift), this.y + i * yShift) ){
return false;
}
i++;
}
return true;
}
public static boolean checkWay(Piece p, ArrayList<Piece> pieces, char x, int y){
int xShift = 0;
int yShift = 0;
if( p.x - x != 0 || p.y - y != 0 ){
if( p.x - x != 0 ){
xShift = (x - p.x) / Math.abs(x - p.x);
}
if( p.y - y != 0 ){
yShift = (y - p.y) / Math.abs(y - p.y);
}
int i = 1;
while( x != (char)(p.x + i * xShift) || y != p.y + i * yShift ){
if( checkTaken(pieces, (char)(p.x + i * xShift), p.y + i * yShift) ){
return false;
}
i++;
}
return true;
}else{
return false;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment