Commit b2453bc2 authored by hamed's avatar hamed

firstCommit

parents
Pipeline #593 failed with stages
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
package Main;
import Pieces.*;
public class Board {
private Piece board[][] = new Piece[8][8];
public Board(){
board[0][0] = new Rook(0,0,false,this);
board[0][7] = new Rook(0,7,false,this);
board[0][1] = new Knight(0,1,false,this);
board[0][6] = new Knight(0,6,false,this);
board[0][2] = new Bishop(0,2,false,this);
board[0][5] = new Bishop(0,5,false,this);
board[0][3] = new King(0,3,false,this);
board[0][4] = new Queen(0,4,false,this);
for (int i = 0 ; i < 8 ; i++){
board[1][i] = new Pawn(1,i,false,this);
board[6][i] = new Pawn(6,i,true,this);
}
board[7][0] = new Rook(7,0,true,this);
board[7][7] = new Rook(7,7,true,this);
board[7][1] = new Knight(7,1,true,this);
board[7][6] = new Knight(7,6,true,this);
board[7][2] = new Bishop(7,2,true,this);
board[7][5] = new Bishop(7,5,true,this);
board[7][3] = new Queen(7,3,true,this);
board[7][4] = new King(7,4,true,this);
}
public boolean getMove(int x , int y , int destX , int destY){
if(board[x][y]==null) return false;
if (board[x][y].isValidMove(destX,destY)){
board[x][y].setX(destX);
board[x][y].setY(destY);
board[destX][destY] = board[x][y];
board[x][y] = null;
return true;
}
else return false;
}
public Piece[][] getBoard() {
return board;
}
public boolean isEmpty(int x , int y){
if (board[x][y] ==null) return true;
return false;
}
public int getColor(int x , int y){
if(isEmpty(x, y)) return 0;
if (board[x][y].isWhite()) return 1;
if (board[x][y].isBlack()) return 2;
return 0;
}
@Override
public String toString() {
String str = "";
for (int i = 0 ; i < 8 ; i++){
for (int j = 0 ; j < 7 ; j++){
if(board[i][j]!=null) {
str = str + board[i][j] + " ";
}
else
str = str + "0 ";
}
if(board[i][7]!=null) {
str = str + board[i][7] + "\n";
}
else
str = str + "0\n";
}
return str;
}
}
package Main;
import java.util.Scanner;
public class Main {
public static void main(String args[]){
Scanner in = new Scanner(System.in);
Board b = new Board();
System.out.println(b);
}
}
package Main;
enum PiecesName {
KING,
KNIGHT,
PAWN,
BISHOP,
QUEEN,
ROOK
}
\ No newline at end of file
package Pieces;
import Main.Board;
import java.util.ArrayList;
public class Bishop extends Piece {
public Bishop(int x, int y, boolean isWhite, Board board) {
super(x, y, isWhite, board);
}
@Override
public boolean isCanMove(int dx, int dy) {
if(isOutside(dx, dy)) return false;
int deltaX = Math.abs(dx-getX());
int deltaY = Math.abs(dy-getY());
if( deltaX==deltaY) return true;
return false;
}
@Override
public boolean isValidMove(int dx, int dy) {
if(!isCanMove(dx, dy)) return false;
for(int i = Math.min(dx,getX());i< Math.max(dx,getX());i++){
for(int j =Math.min(dy,getY());i < Math.max(dy,getY());j++){
if(getBoard().getBoard()[i][j]!=null) return false;
}
}
return true;
}
@Override
public ArrayList<int[]> moves() {
ArrayList<int[]> moves = new ArrayList<>();
moves.clear();
int tempx=getX()+1,tempy=getY()-1;
while(tempx<8&&tempy>=0)
{
if(getBoard().isEmpty(tempx,tempy))
{
moves.add(new int[]{tempx,tempy});
}
else if((getBoard().getColor(tempx,tempy)==1 && this.isWhite()) || (getBoard().getColor(tempx,tempy)==2 && this.isBlack()))
break;
else
{
moves.add(new int[]{tempx,tempy});
break;
}
tempx++;
tempy--;
}
tempx=getX()-1;tempy=getY()+1;
while(tempx>=0&&tempy<8)
{
if(getBoard().isEmpty(tempx,tempy))
{
moves.add(new int[]{tempx,tempy});
}
else if((getBoard().getColor(tempx,tempy)==1 && this.isWhite()) || (getBoard().getColor(tempx,tempy)==2 && this.isBlack()))
break;
else
{
moves.add(new int[]{tempx,tempy});
break;
}
tempx--;
tempy++;
}
tempx=getX()-1;tempy=getY()-1;
while(tempx>=0&&tempy>=0)
{
if(getBoard().isEmpty(tempx,tempy))
{
moves.add(new int[]{tempx,tempy});
}
else if((getBoard().getColor(tempx,tempy)==1 && this.isWhite()) || (getBoard().getColor(tempx,tempy)==2 && this.isBlack()))
break;
else
{
moves.add(new int[]{tempx,tempy});
break;
}
tempx--;
tempy--;
}
tempx=getX()+1;tempy=getY()+1;
while(tempx<8&&tempy<8)
{
if(getBoard().isEmpty(tempx,tempy))
{
moves.add(new int[]{tempx,tempy});
}
else if((getBoard().getColor(tempx,tempy)==1 && this.isWhite()) || (getBoard().getColor(tempx,tempy)==2 && this.isBlack()))
break;
else
{
moves.add(new int[]{tempx,tempy});
break;
}
tempx++;
tempy++;
}
return moves;
}
@Override
public String toString() {
return "B";
}
}
package Pieces;
import Main.Board;
import java.util.ArrayList;
public class King extends Piece {
public King(int x, int y, boolean isWhite, Board board) {
super(x, y, isWhite, board);
}
@Override
public boolean isCanMove(int dx, int dy) {
if (isOutside(dx, dy)) return false;
int deltaX = Math.abs(dx-getX());
int deltaY = Math.abs(dy-getY());
if( deltaX==deltaY && deltaX ==1) return true;
if(deltaX ==1 && deltaY ==0) return true;
if(deltaX==0 && deltaY ==1) return true;
return false;
}
@Override
public boolean isValidMove(int dx, int dy) {
if(!isCanMove(dx, dy)) return false;
for(int i = Math.min(dx,getX());i< Math.max(dx,getX());i++){
for(int j =Math.min(dy,getY());i < Math.max(dy,getY());j++){
if(getBoard().getBoard()[i][j]!=null) return false;
}
}
return true;
}
@Override
public ArrayList<int[]> moves() {
ArrayList<int[]> move = new ArrayList<>();
int x = getX();
int y = getY();
int posx[]={x,x,x+1,x+1,x+1,x-1,x-1,x-1};
int posy[]={y-1,y+1,y-1,y,y+1,y-1,y,y+1};
for(int i=0;i<8;i++){
if((posx[i]>=0&&posx[i]<8&&posy[i]>=0&&posy[i]<8)){
if(getBoard().isEmpty(posx[i],posy[i]) || (getBoard().getColor(posx[i],posy[i])==2 && this.isWhite()) ||(getBoard().getColor(posx[i],posy[i])==1 && isBlack()) )
{
move.add(new int[]{posx[i], posy[i]});
}
}
}
return move;
}
@Override
public String toString() {
return "K";
}
}
package Pieces;
import Main.Board;
import java.util.ArrayList;
public class Knight extends Piece {
public Knight(int x, int y, boolean isWhite, Board board) {
super(x, y, isWhite, board);
}
@Override
public boolean isCanMove(int dx, int dy) {
if(isOutside(dx,dy)) return false;
if(dx != getX() - 1 && dx != getX() + 1 && dx != getX() + 2 && dx != getX() - 2)
return false;
if(dy != getY() - 2 && dy != getY() + 2 && dy != getY() - 1 && dy != getY() + 1)
return false;
return true;
}
@Override
public boolean isValidMove(int dx, int dy) {
return isCanMove(dx, dy);
}
@Override
public ArrayList<int[]> moves() {
ArrayList<int[]> move = new ArrayList<>();
int x = getX();
int y = getY();
int posx[]={x+1,x+1,x+2,x+2,x-1,x-1,x-2,x-2};
int posy[]={y-2,y+2,y-1,y+1,y-2,y+2,y-1,y+1};
for(int i=0;i<8;i++) {
if ((posx[i] >= 0 && posx[i] < 8 && posy[i] >= 0 && posy[i] < 8)) {
if(getBoard().isEmpty(posx[i],posy[i]) || (getBoard().getColor(posx[i],posy[i])==2 && this.isWhite()) ||(getBoard().getColor(posx[i],posy[i])==1 && isBlack()) )
move.add(new int[]{posx[i], posy[i]});
}
}
return move;
}
@Override
public String toString() {
return "A";
}
}
package Pieces;
import Main.Board;
import java.util.ArrayList;
public class Pawn extends Piece {
public Pawn(int x, int y, boolean isWhite, Board board) {
super(x, y, isWhite, board);
}
@Override
public boolean isCanMove(int dx, int dy) {
if(isOutside(dx,dy)) return false;
return false;
}
@Override
public boolean isValidMove(int dx, int dy) {
return isCanMove(dx, dy);
}
@Override
public ArrayList<int[]> moves() {
ArrayList<int[]> moves = new ArrayList<>();
int x = getX();
int y = getY();
if(isWhite())
{
if(x==0)
return moves;
if(getBoard().isEmpty(x-1,y))
{
moves.add(new int[]{x-1,y});
if(x==6)
{
if(getBoard().isEmpty(4,y))
moves.add(new int[]{4,y});
}
}
if((y>0)&& ((getBoard().getColor(x-1,y-1)==2 && isWhite() ||getBoard().getColor(x-1,y-1)==1 && isBlack())))
moves.add(new int[]{x-1,y-1});
if((y<7)&&((getBoard().getColor(x-1,y+1)==2 && isWhite() ||getBoard().getColor(x-1,y+1)==1 && isBlack())))
moves.add(new int[]{x-1,y+1});
}
else
{
if(x==8)
return moves;
if(getBoard().isEmpty(x+1,y))
{
moves.add(new int[]{x+1,y});
if(x==1)
{
if(getBoard().isEmpty(3,y))
moves.add(new int[]{3,y});
}
}
if((y>0)&& ((getBoard().getColor(x+1,y-1)==2 && isWhite() ||getBoard().getColor(x+1,y-1)==1 && isBlack())))
moves.add(new int[]{x+1,y-1});
if((y<7)&&((getBoard().getColor(x+1,y+1)==2 && isWhite() ||getBoard().getColor(x+1,y+1)==1 && isBlack())))
moves.add(new int[]{x+1,y+1});
}
return moves;
}
@Override
public String toString() {
return "P";
}
}
package Pieces;
import Main.Board;
import java.util.ArrayList;
public abstract class Piece {
private int x;
private boolean isWhite;
private int y;
private Board board;
public Piece(int x , int y , boolean isWhite , Board board){
this.x = x;
this.y = y;
this.isWhite = isWhite;
this.board = board;
}
public boolean isWhite(){
return isWhite;
}
public boolean isBlack(){
return !isWhite;
}
public int getX() {
return x;
}
public Board getBoard() {
return board;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
abstract public boolean isCanMove(int dx,int dy);
abstract public boolean isValidMove(int dx,int dy);
abstract public ArrayList<int[]> moves();
public boolean isOutside(int dx, int dy){
if (dx < 0 || dx > 8 || dy < 0 || dy > 8)
return false;
return true;
}
}
package Pieces;
import Main.Board;
import java.util.ArrayList;
public class Queen extends Piece {
public Queen(int x, int y, boolean isWhite, Board board) {
super(x, y, isWhite, board);
}
@Override
public boolean isCanMove(int dx, int dy) {
if(isOutside(dx, dy)) return false;
int deltaX = Math.abs(dx-getX());
int deltaY = Math.abs(dy-getY());
if( deltaX==deltaY) return true;
if(dx ==getX())
return true;
if(dy == getY())
return true;
return false;
}
@Override
public boolean isValidMove(int dx, int dy) {
if(!isCanMove(dx, dy))return false;
for(int i = Math.min(dx,getX());i< Math.max(dx,getX());i++){
for(int j =Math.min(dy,getY());i < Math.max(dy,getY());j++){
if(getBoard().getBoard()[i][j]!=null) return false;
}
}
return true;
}
@Override
public ArrayList<int[]> moves() {
ArrayList<int[]> moves = new ArrayList<>();
moves.clear();
int tempx=getX()+1,tempy=getY()-1;
while(tempx<8&&tempy>=0)
{
if(getBoard().isEmpty(tempx,tempy))
{
moves.add(new int[]{tempx,tempy});
}
else if((getBoard().getColor(tempx,tempy)==1 && this.isWhite()) || (getBoard().getColor(tempx,tempy)==2 && this.isBlack()))
break;
else
{
moves.add(new int[]{tempx,tempy});
break;
}
tempx++;
tempy--;
}
tempx=getX()-1;tempy=getY()+1;
while(tempx>=0&&tempy<8)
{
if(getBoard().isEmpty(tempx,tempy))
{
moves.add(new int[]{tempx,tempy});
}
else if((getBoard().getColor(tempx,tempy)==1 && this.isWhite()) || (getBoard().getColor(tempx,tempy)==2 && this.isBlack()))
break;
else
{
moves.add(new int[]{tempx,tempy});
break;
}
tempx--;
tempy++;
}
tempx=getX()-1;tempy=getY()-1;
while(tempx>=0&&tempy>=0)
{
if(getBoard().isEmpty(tempx,tempy))
{
moves.add(new int[]{tempx,tempy});
}
else if((getBoard().getColor(tempx,tempy)==1 && this.isWhite()) || (getBoard().getColor(tempx,tempy)==2 && this.isBlack()))
break;
else
{
moves.add(new int[]{tempx,tempy});
break;
}
tempx--;
tempy--;
}
tempx=getX()+1;tempy=getY()+1;
while(tempx<8&&tempy<8)
{
if(getBoard().isEmpty(tempx,tempy))
{
moves.add(new int[]{tempx,tempy});
}
else if((getBoard().getColor(tempx,tempy)==1 && this.isWhite()) || (getBoard().getColor(tempx,tempy)==2 && this.isBlack()))
break;
else
{
moves.add(new int[]{tempx,tempy});
break;
}
tempx++;
tempy++;
}
int x = getX();
int y = getY();
tempx=x-1;
while(tempx>=0)
{
if(getBoard().isEmpty(tempx,y))
moves.add(new int[]{tempx,y});
else if((getBoard().getColor(tempx,y)==1 && this.isWhite()) || (getBoard().getColor(tempx,y)==2 && this.isBlack()))
break;
else
{
moves.add(new int[]{tempx,y});
break;
}
tempx--;
}
tempx=x+1;
while(tempx<8)
{
if(getBoard().isEmpty(tempx,y))
moves.add(new int[]{tempx,y});
else if((getBoard().getColor(tempx,y)==1 && this.isWhite()) || (getBoard().getColor(tempx,y)==2 && this.isBlack()))
break;
else
{
moves.add(new int[]{tempx,y});
break;
}
tempx++;
}
tempy=y-1;
while(tempy>=0)
{
if(getBoard().isEmpty(x,tempy))
moves.add(new int[]{x,tempy});
else if((getBoard().getColor(x,tempy)==1 && this.isWhite()) || (getBoard().getColor(x,tempy)==2 && this.isBlack()))
break;
else
{
moves.add(new int[]{x,tempy});
break;
}
tempy--;
}
tempy=y+1;
while(tempy<8)
{
if(getBoard().isEmpty(x,tempy))
moves.add(new int[]{x,tempy});
else if((getBoard().getColor(x,tempy)==1 && this.isWhite()) || (getBoard().getColor(x,tempy)==2 && this.isBlack()))
break;
else
{
moves.add(new int[]{x,tempy});
break;
}
tempy++;
}
return moves;
}
@Override
public String toString() {
return "Q";
}
}
package Pieces;
import Main.Board;
import java.util.ArrayList;
public class Rook extends Piece {
public Rook(int x, int y, boolean isWhite, Board board) {
super(x, y, isWhite, board);
}
@Override
public boolean isCanMove(int dx, int dy) {
if(isOutside(dx, dy)) return false;
if(dx ==getX())
return true;
if(dy == getY())
return true;
return false;
}
@Override
public boolean isValidMove(int dx, int dy) {
if(!isCanMove(dx, dy))return false;
for(int i = Math.min(dx,getX());i< Math.max(dx,getX());i++){
for(int j =Math.min(dy,getY());i < Math.max(dy,getY());j++){
if(getBoard().getBoard()[i][j]!=null) return false;
}
}
return true;
}
@Override
public ArrayList<int[]> moves() {
ArrayList<int[]> moves = new ArrayList<>();
int x = getX();
int y = getY();
int tempx=x-1;
while(tempx>=0)
{
if(getBoard().isEmpty(tempx,y))
moves.add(new int[]{tempx,y});
else if((getBoard().getColor(tempx,y)==1 && this.isWhite()) || (getBoard().getColor(tempx,y)==2 && this.isBlack()))
break;
else
{
moves.add(new int[]{tempx,y});
break;
}
tempx--;
}
tempx=x+1;
while(tempx<8)
{
if(getBoard().isEmpty(tempx,y))
moves.add(new int[]{tempx,y});
else if((getBoard().getColor(tempx,y)==1 && this.isWhite()) || (getBoard().getColor(tempx,y)==2 && this.isBlack()))
break;
else
{
moves.add(new int[]{tempx,y});
break;
}
tempx++;
}
int tempy=y-1;
while(tempy>=0)
{
if(getBoard().isEmpty(x,tempy))
moves.add(new int[]{x,tempy});
else if((getBoard().getColor(x,tempy)==1 && this.isWhite()) || (getBoard().getColor(x,tempy)==2 && this.isBlack()))
break;
else
{
moves.add(new int[]{x,tempy});
break;
}
tempy--;
}
tempy=y+1;
while(tempy<8)
{
if(getBoard().isEmpty(x,tempy))
moves.add(new int[]{x,tempy});
else if((getBoard().getColor(x,tempy)==1 && this.isWhite()) || (getBoard().getColor(x,tempy)==2 && this.isBlack()))
break;
else
{
moves.add(new int[]{x,tempy});
break;
}
tempy++;
}
return moves;
}
@Override
public String toString() {
return "R";
}
}
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