Skip to content

Commit

Permalink
refac: piece, move, move strategy and move comparison
Browse files Browse the repository at this point in the history
Co-authored-by: Raquel Andrade <raqueelfa@gmail.com>
  • Loading branch information
lucasgabriel-2 and raquel-andrade committed Jan 6, 2025
1 parent 57f372f commit fbf57c0
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 58 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,7 @@ gradle-app.setting
# Docs specific
venv/
site/

# Others
jcc.settings.json
platform_auto.conf
38 changes: 33 additions & 5 deletions src/main/java/puzzle/Move.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package puzzle;

import java.io.Serializable;
import java.util.List;
import java.util.ArrayList;

/**
* A move consisting of a start and an end point.
* A move consisting of a start and an end point, possibly capturing pieces.
*/
public class Move implements Serializable
{
Expand All @@ -15,17 +17,30 @@ public class Move implements Serializable
/** The end position. */
protected Position end;

/** The list of captured positions (if any). */
protected List<Position> captured;

//-------- constructors --------

/**
* Create a position.
* Create a move (no captures).
*/
public Move(Position start, Position end)
{
this.start = start;
this.end = end;
this.captured = new ArrayList<>();
}

/**
* Create a move with start, end, and captured positions (for multiple captures).
*/
public Move(Position start, Position end, List<Position> captured) {
this.start = start;
this.end = end;
this.captured = captured;
}

//-------- methods --------

/**
Expand All @@ -44,16 +59,24 @@ public Position getEnd()
return end;
}

/**
* Get the list of captured positions (if any).
*/
public List<Position> getCaptured() {
return captured;
}

/**
* Test if it is a jump move.
*/
public boolean isJumpMove()
{
return Math.abs(start.getX()-end.getX())==2 || Math.abs(start.getY()-end.getY())==2;
// Check if this move has any captures
return !captured.isEmpty();
}

/**
* Test if two positions are equal.
* Test if two positions are equal (start and end of a move).
* @return True, if equal.
*/
public boolean equals(Object o)
Expand Down Expand Up @@ -84,6 +107,11 @@ public int hashCode()
*/
public String toString()
{
return getStart()+" "+getEnd();
if (isJumpMove()) {
String capturedPositions = captured.toString();
return "Jump from " + start + " to " + end + " capturing " + capturedPositions;
} else {
return "Move from " + start + " to " + end;
}
}
}
86 changes: 35 additions & 51 deletions src/main/java/puzzle/MoveComparator.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,27 @@
*/
public class MoveComparator implements Comparator<Move>
{
//-------- constants --------
//-------- constants --------

/** No strategy: try moves in order of appearance. */
/** No strategy: try moves in order of appearance. */
public static final String STRATEGY_NONE = "none";

/** The strategy preferring jump moves, but ignoring colors. */
public static final String STRATEGY_LONG = "long";

/** The strategy preferring jump moves of same color. */
public static final String STRATEGY_SAME_LONG = "same_long";
/** The strategy preferring jump moves of different colors. */
public static final String STRATEGY_ALTER_LONG = "alter_long";
//-------- attributes --------
/** The strategy preferring moves with the highest number of captures. */
public static final String STRATEGY_MAX_CAPTURES = "max_captures";

/** The strategy preferring moves that involve queens over normal pieces then number of captures. */
public static final String STRATEGY_PREFER_QUEENS = "prefer_queens";

//-------- attributes --------

/** The board (required for checking which piece is in a given position). */
protected IBoard board;

/** The strategy. */
protected String strategy;
//-------- constructors --------

//-------- constructors --------

/**
* Create a move comparator.
*/
Expand All @@ -42,40 +39,27 @@ public MoveComparator(IBoard board, String strategy)

//-------- Coparator interface --------

/**
* Compare two moves.
* @return A negative number when the first move should come before the second.
*/
public int compare(Move move1, Move move2)
/**
* Compare two moves.
* @return A negative number when the first move should come before the second.
*/
public int compare(Move move1, Move move2)
{
boolean same1 = board.wasLastMoveWhite()==board.getPiece(move1.getStart()).isWhite();
boolean same2 = board.wasLastMoveWhite()==board.getPiece(move2.getStart()).isWhite();

int compare_same = same1 && !same2 ? -1
: same2 && !same1 ? 1
: 0;

int compare_long = move1.isJumpMove() && !move2.isJumpMove() ? -1
: move2.isJumpMove() && !move1.isJumpMove() ? 1
: 0;

int ret = 0;

if(STRATEGY_LONG.equals(strategy))
{
ret = compare_long;
}
else if(STRATEGY_SAME_LONG.equals(strategy))
{
ret = compare_same!=0 ? compare_same : compare_long;
// ret = compare_long!=0 ? compare_long : compare_same;
}
else if(STRATEGY_ALTER_LONG.equals(strategy))
{
ret = compare_same!=0 ? -compare_same : compare_long;
// ret = compare_long!=0 ? compare_long : -compare_same;
}

return ret;
}

int compareCaptureCount = Integer.compare(move2.getCaptured().size(), move1.getCaptured().size());

boolean isQueen1 = board.getPiece(move1.getStart()).isQueen();
boolean isQueen2 = board.getPiece(move2.getStart()).isQueen();
int compareQueen = Boolean.compare(isQueen2, isQueen1);

int ret = 0;

if (STRATEGY_MAX_CAPTURES.equals(strategy)) {
ret = compareCaptureCount;
} else if (STRATEGY_PREFER_QUEENS.equals(strategy)) {
ret = compareQueen != 0 ? compareQueen : compareCaptureCount;
}

return ret;
}
}
26 changes: 24 additions & 2 deletions src/main/java/puzzle/Piece.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ public class Piece implements Serializable

/** The piece color (white or black). */
protected boolean is_white;

/** The piece type (normal or queen). */
protected boolean is_queen;

//-------- constructors --------

/**
* Create a new board.
* Create a new piece.
*/
public Piece(boolean is_white)
{
this.is_white = is_white;
this.is_queen = false;
}

//-------- methods --------
Expand All @@ -33,12 +37,30 @@ public boolean isWhite()
return is_white;
}

/**
* Test, if it is a queen piece.
* @return True, if it a queen piece.
*/
public boolean isQueen()
{
return is_queen;
}

/**
* Promote the piece to a queen.
*/
public void promoteToQueen() {
this.is_queen = true;
}

/**
* Get the string representation.
* @return The string representation.
*/
public String toString()
{
return isWhite()? "white": "black";
String color = isWhite() ? "white" : "black";
String type = isQueen() ? "queen" : "normal";
return color + " " + type;
}
}

0 comments on commit fbf57c0

Please sign in to comment.