Skip to content
Snippets Groups Projects
Commit e2ac73d6 authored by ACIKBAS TUNA's avatar ACIKBAS TUNA
Browse files

Finished piece mouvement and Rook

parent d11350f3
Branches
1 merge request!5Finished Piece movements
This commit is part of merge request !5. Comments created here will be created in the context of that merge request.
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="DuplicatedCode" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<Languages>
<language minSize="74" name="Java" />
</Languages>
</inspection_tool>
</profile>
</component>
\ No newline at end of file
......@@ -2,7 +2,8 @@ package model;
import java.util.ArrayList;
public class Bishop extends Pawn{
public class Bishop extends Piece {
public Bishop(Position p, int color) {
super(p, color);
}
......@@ -14,13 +15,75 @@ public class Bishop extends Pawn{
ArrayList<Position> moves = new ArrayList<Position>();
Position arrival;
if(color == Piece.WHITE) { // black and white have an opposite direction behavior
// King white move
}
else {
// King black move
Position Cursor = new Position(p.getX(), p.getY());
int i = 1;
boolean leftTop = false;
boolean rightTop = false;
boolean leftDown = false;
boolean rightDown = false;
while (i < 8) {
//left diagonal
arrival = new Position(p.getX() + i, p.getY() + i);
if (!leftTop) {
if (b.isEnnemy(arrival, p) || b.isFree(arrival)) {
moves.add(arrival);
}
}
if (!b.isFree(arrival)) {
leftTop = true;
}
//right diagonal
arrival = new Position(p.getX() - i, p.getY() + i);
if (!rightTop) {
if (b.isEnnemy(arrival, p) || b.isFree(arrival)) {
moves.add(arrival);
}
}
if (!b.isFree(arrival)) {
rightTop = true;
}
arrival = new Position(p.getX() + i, p.getY() - i);
if (!leftDown) {
if (b.isEnnemy(arrival, p) || b.isFree(arrival)) {
moves.add(arrival);
}
}
if (!b.isFree(arrival)) {
leftDown = true;
}
arrival = new Position(p.getX() - i, p.getY() - i);
if (!rightDown) {
if (b.isEnnemy(arrival, p) || b.isFree(arrival)) {
moves.add(arrival);
}
}
if (!b.isFree(arrival)) {
rightDown = true;
}
i++;
}
return moves;
}
}
......@@ -73,7 +73,7 @@ public class Board {
}
public boolean isFree(Position p) {
return (pieces[p.getID()] == null);
return (Position.isCorrect(p) && pieces[p.getID()] == null);
}
public boolean isBlack(Position p) {
return (!isFree(p) && pieces[p.getID()].getColor() == Piece.BLACK);
......@@ -84,7 +84,7 @@ public class Board {
}
public boolean isEnnemy(Position p, Position origin) {
return (!isFree(p) && pieces[p.getID()].getColor() != pieces[origin.getID()].getColor());
return (Position.isCorrect(p) && !isFree(p) && pieces[p.getID()].getColor() != pieces[origin.getID()].getColor() );
}
public boolean isKing(Position p) {
return (!isFree(p) && pieces[p.getID()].getClass() == King.class);
......
......@@ -4,8 +4,13 @@ import java.util.ArrayList;
public class King extends Piece {
private boolean Casteling = true;
private final Position original;
public King(Position p, int color) {
super(p, color);
this.original = p;
}
......@@ -15,13 +20,44 @@ public class King extends Piece {
ArrayList<Position> moves = new ArrayList<Position>();
Position arrival;
if(color == Piece.WHITE) { // black and white have an opposite direction behavior
// King white move
}
else {
// King black move
}
// up
arrival = new Position(p.getX(), p.getY() + 1);
if(b.isEnnemy(arrival,p) || b.isFree(arrival)) { moves.add(arrival); }
//down
arrival = new Position(p.getX(), p.getY() - 1);
if(b.isEnnemy(arrival,p) || b.isFree(arrival)) { moves.add(arrival); }
//left
arrival = new Position(p.getX()+1, p.getY());
if(b.isEnnemy(arrival,p) || b.isFree(arrival)) { moves.add(arrival); }
//right
arrival = new Position(p.getX()-1, p.getY());
if(b.isEnnemy(arrival,p) || b.isFree(arrival)) { moves.add(arrival); }
//up left
arrival = new Position(p.getX()-1, p.getY() + 1);
if(b.isEnnemy(arrival,p) || b.isFree(arrival)) { moves.add(arrival); }
//up right
arrival = new Position(p.getX()+1, p.getY() + 1);
if(b.isEnnemy(arrival,p) || b.isFree(arrival)) { moves.add(arrival); }
//down left
arrival = new Position(p.getX()-1, p.getY() -1);
if(b.isEnnemy(arrival,p) || b.isFree(arrival)) { moves.add(arrival); }
//down right
arrival = new Position(p.getX()+1, p.getY() - 1);
if(b.isEnnemy(arrival,p) || b.isFree(arrival)) { moves.add(arrival); }
arrival = new Position(p.getX(), p.getY() + 1);
if(b.isEnnemy(arrival,p) || b.isFree(arrival)) { moves.add(arrival); }
return moves;
}
}
......
......@@ -2,7 +2,9 @@ package model;
import java.util.ArrayList;
public class Knight extends Pawn{
public class Knight extends Piece {
public Knight(Position p, int color) {
super(p, color);
}
......@@ -14,13 +16,53 @@ public class Knight extends Pawn{
ArrayList<Position> moves = new ArrayList<Position>();
Position arrival;
if(color == Piece.WHITE) { // black and white have an opposite direction behavior
// King white move
arrival = new Position(p.getX() - 1, p.getY() + 2);
if (b.isEnnemy(arrival, p) || b.isFree(arrival)) {
moves.add(arrival);
}
// up right
arrival = new Position(p.getX() + 1, p.getY() + 2);
if (b.isEnnemy(arrival, p) || b.isFree(arrival)) {
moves.add(arrival);
}
// down left
arrival = new Position(p.getX() - 1, p.getY() - 2);
if (b.isEnnemy(arrival, p) || b.isFree(arrival)) {
moves.add(arrival);
}
//down right
arrival = new Position(p.getX() + 1, p.getY() - 2);
if (b.isEnnemy(arrival, p) || b.isFree(arrival)) {
moves.add(arrival);
}
arrival = new Position(p.getX() - 2, p.getY() + 1);
if (b.isEnnemy(arrival, p) || b.isFree(arrival)) {
moves.add(arrival);
}
// up right
arrival = new Position(p.getX() + 2, p.getY() + 1);
if (b.isEnnemy(arrival, p) || b.isFree(arrival)) {
moves.add(arrival);
}
// down left
arrival = new Position(p.getX() - 2, p.getY() - 1);
if (b.isEnnemy(arrival, p) || b.isFree(arrival)) {
moves.add(arrival);
}
else {
// King black move
//down right
arrival = new Position(p.getX() + 2, p.getY() - 1);
if (b.isEnnemy(arrival, p) || b.isFree(arrival)) {
moves.add(arrival);
}
return moves;
}
}
......@@ -3,10 +3,13 @@ package model;
import java.util.ArrayList;
public class Pawn extends Piece {
private final Position original;
public Pawn(Position p, int color) {
super(p, color);
this.original = p;
}
public ArrayList<Position> getAvailableMoves(Board b) {
// creates a dynamic list of available moves
......@@ -15,6 +18,13 @@ public class Pawn extends Piece {
Position arrival;
if(color == Piece.WHITE) { // black and white have an opposite direction behavior
if(this.p.getID() == this.original.getID()){
// up, free movement first time
arrival = new Position(p.getX(), p.getY() + 2);
if(Position.isCorrect(arrival) && b.isFree(arrival)) { moves.add(arrival); }
}
// up, free movement
arrival = new Position(p.getX(), p.getY() + 1);
if(Position.isCorrect(arrival) && b.isFree(arrival)) { moves.add(arrival); }
......@@ -30,6 +40,12 @@ public class Pawn extends Piece {
}
else {
if(this.p.getID() == this.original.getID()){
// up, free movement first time
arrival = new Position(p.getX(), p.getY() - 2);
if(Position.isCorrect(arrival) && b.isFree(arrival)) { moves.add(arrival); }
}
// down, free movement
arrival = new Position(p.getX(), p.getY() - 1);
if(Position.isCorrect(arrival) && b.isFree(arrival)) { moves.add(arrival); }
......
......@@ -14,13 +14,127 @@ public class Queen extends Piece{
ArrayList<Position> moves = new ArrayList<Position>();
Position arrival;
if(color == Piece.WHITE) { // black and white have an opposite direction behavior
// King white move
}
else {
// King black move
int i = 1;
boolean left = false;
boolean right = false;
boolean top = false;
boolean down = false;
boolean leftTop = false;
boolean rightTop = false;
boolean leftDown = false;
boolean rightDown = false;
while (i < 8) {
// Bishop moves
arrival = new Position(p.getX() + i, p.getY() + i);
if (!leftTop) {
if (b.isEnnemy(arrival, p) || b.isFree(arrival)) {
moves.add(arrival);
}
}
if (!b.isFree(arrival)) {
leftTop = true;
}
//right diagonal
arrival = new Position(p.getX() - i, p.getY() + i);
if (!rightTop) {
if (b.isEnnemy(arrival, p) || b.isFree(arrival)) {
moves.add(arrival);
}
}
if (!b.isFree(arrival)) {
rightTop = true;
}
arrival = new Position(p.getX() + i, p.getY() - i);
if (!leftDown) {
if (b.isEnnemy(arrival, p) || b.isFree(arrival)) {
moves.add(arrival);
}
}
if (!b.isFree(arrival)) {
leftDown = true;
}
arrival = new Position(p.getX() - i, p.getY() - i);
if (!rightDown) {
if (b.isEnnemy(arrival, p) || b.isFree(arrival)) {
moves.add(arrival);
}
}
if (!b.isFree(arrival)) {
rightDown = true;
}
// RooK moves
arrival = new Position(p.getX() +i, p.getY());
if (!left) {
if (b.isEnnemy(arrival, p) || b.isFree(arrival)) {
moves.add(arrival);
}
}
if (!b.isFree(arrival)) {
left = true;
}
//right diagonal
arrival = new Position(p.getX()- i , p.getY() );
if (!right) {
if (b.isEnnemy(arrival, p) || b.isFree(arrival)) {
moves.add(arrival);
}
}
if (!b.isFree(arrival)) {
right = true;
}
arrival = new Position(p.getX(), p.getY() + i);
if (!top) {
if (b.isEnnemy(arrival, p) || b.isFree(arrival)) {
moves.add(arrival);
}
}
if (!b.isFree(arrival)) {
top = true;
}
arrival = new Position(p.getX(), p.getY() - i);
if (!down) {
if (b.isEnnemy(arrival, p) || b.isFree(arrival)) {
moves.add(arrival);
}
}
if (!b.isFree(arrival)) {
down = true;
}
i++;
}
return moves;
}
}
......@@ -2,9 +2,14 @@ package model;
import java.util.ArrayList;
public class Rook extends Pawn{
public class Rook extends Piece {
private final Position original;
public Rook(Position p, int color) {
super(p, color);
this.original = p;
}
......@@ -14,13 +19,85 @@ public class Rook extends Pawn{
ArrayList<Position> moves = new ArrayList<Position>();
Position arrival;
if(color == Piece.WHITE) { // black and white have an opposite direction behavior
// King white move
}
else {
// King black move
int i = 1;
boolean left = false;
boolean right = false;
boolean top = false;
boolean down = false;
while (i < 8) {
//left
arrival = new Position(p.getX() + i, p.getY());
if (Position.isCorrect(arrival)) {
if (!left) {
if (b.isEnnemy(arrival, p) || b.isFree(arrival)) {
moves.add(arrival);
}
if (b.isKing(arrival)) {
moves.add(arrival);
}
}
if (!b.isFree(arrival)) {
left = true;
}
}
//right
arrival = new Position(p.getX() - i, p.getY());
if (Position.isCorrect(arrival)) {
if (!right) {
if (b.isEnnemy(arrival, p) || b.isFree(arrival)) {
moves.add(arrival);
}
if (b.isKing(arrival)) {
moves.add(arrival);
}
}
if (!b.isFree(arrival)) {
right = true;
}
}
arrival = new Position(p.getX(), p.getY() + i);
if (Position.isCorrect(arrival)) {
if (!top) {
if (b.isEnnemy(arrival, p)|| b.isFree(arrival)) {
moves.add(arrival);
}
}
if (!b.isFree(arrival)) {
top = true;
}
}
arrival = new Position(p.getX(), p.getY() - i);
if(Position.isCorrect(arrival)||b.isFree(arrival)) {
if (!down) {
if (b.isEnnemy(arrival, p)) {
moves.add(arrival);
}
}
if (!b.isFree(arrival)) {
down = true;
}
}
i++;
}
return moves;
}
}
No preview for this file type
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