Skip to content

Commit 0701569

Browse files
committed
move setting corrected
1 parent be98376 commit 0701569

File tree

1 file changed

+35
-11
lines changed
  • src/main/java/main/java/hackerearth/taunt

1 file changed

+35
-11
lines changed

src/main/java/main/java/hackerearth/taunt/Taunt.java

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,6 @@ public Board(final Board game) {
466466
board[i][j] = game.board[i][j] == null ? null : new Piece(game.board[i][j]);
467467
}
468468
}
469-
//todo: piece is being reused
470469
for (int i = 0; i < PLAYERS; i++) {
471470
for (int j = 0; j < game.options[i]; j++) {
472471
moves[i][j] = new Move(game.moves[i][j]);
@@ -563,15 +562,15 @@ public void makeMove(final Move move) {
563562
} else if (y < 0) {
564563
y = -y;
565564
}
566-
final List<Piece> captures = new ArrayList<>(2);
567565
if (x == move.end.x && y == move.end.y) {
566+
final List<Piece> captures = new ArrayList<>(2);
567+
if (board[x][y] != null) {
568+
captures.add(board[x][y]);
569+
}
568570
moves[opponent][options[opponent]++] = new Move(start,
569571
new Cell(x, y),
570572
board[i][j],
571573
captures);
572-
if (board[x][y] != null) {
573-
captures.add(board[x][y]);
574-
}
575574
} else if (board[x][y] != null && !board[x][y].coin.equals(Coin.PAWN)) {
576575
if (Math.abs(movesTo[1]) == 2) {
577576
int intermediateX = x + movesTo[1] / 2 * direction;
@@ -586,8 +585,33 @@ public void makeMove(final Move move) {
586585
} else if (intermediateY < 0) {
587586
intermediateY = -intermediateY;
588587
}
588+
int destinationX = x + movesTo[1] * direction;
589+
int destinationY = y + movesTo[0];
590+
if (destinationX >= ROWS) {
591+
destinationX = ROWS - (destinationX + 1 - ROWS) - 1;
592+
} else if (destinationX < 0) {
593+
destinationX = -destinationX;
594+
}
595+
if (destinationY >= COLS) {
596+
destinationY = COLS - (destinationY + 1 - COLS) - 1;
597+
} else if (destinationY < 0) {
598+
destinationY = -destinationY;
599+
}
589600
if (intermediateX == move.end.x && intermediateY == move.end.y) {
590-
captures.add(board[intermediateX][intermediateY]);
601+
final int finalX = x;
602+
final int finalY = y;
603+
final int finalIntermediateX = intermediateX;
604+
final int finalIntermediateY = intermediateY;
605+
final int finalDestinationX = destinationX;
606+
final int finalDestinationY = destinationY;
607+
Arrays.stream(moves[opponent])
608+
.filter(Objects::nonNull)
609+
.filter(m -> m.start.x == finalX)
610+
.filter(m -> m.start.y == finalY)
611+
.filter(m -> m.end.x == finalDestinationX)
612+
.filter(m -> m.end.y == finalDestinationY)
613+
.findAny()
614+
.ifPresent(m -> m.capturedPieces.add(board[finalIntermediateX][finalIntermediateY]));
591615
}
592616
}
593617
}
@@ -618,7 +642,6 @@ public void makeMove(final Move move) {
618642
}
619643
if (board[x][y] == null || board[x][y].player != player) {
620644
final List<Piece> captures = new ArrayList<>(2);
621-
moves[player][options[player]++] = new Move(start, new Cell(x, y), board[i][j], captures);
622645
if (board[x][y] != null) {
623646
captures.add(board[x][y]);
624647
}
@@ -642,6 +665,7 @@ public void makeMove(final Move move) {
642665
}
643666
}
644667
}
668+
moves[player][options[player]++] = new Move(start, new Cell(x, y), board[i][j], captures);
645669
}
646670
}
647671
}
@@ -690,10 +714,6 @@ private Piece[][] convertToBoard(final byte[][] board) {
690714
}
691715
if (pieces[x][y] == null || pieces[x][y].player != player) {
692716
final List<Piece> captures = new ArrayList<>(2);
693-
moves[player][options[player]++] = new Move(start,
694-
new Cell(x, y),
695-
pieces[i][j],
696-
captures);
697717
if (pieces[x][y] != null) {
698718
captures.add(pieces[x][y]);
699719
}
@@ -717,6 +737,10 @@ private Piece[][] convertToBoard(final byte[][] board) {
717737
}
718738
}
719739
}
740+
moves[player][options[player]++] = new Move(start,
741+
new Cell(x, y),
742+
pieces[i][j],
743+
captures);
720744
}
721745
}
722746
}

0 commit comments

Comments
 (0)