// make move public void makeMove(Move Move) { board[Move.getNewX(), Move.getNewY()] = board[Move.getPrevX(), Move.getPrevY()]; board[Move.getPrevX(), Move.getPrevY()] = null; if (Move.getIsBeating()) { if (board[Move.getBeatingX(), Move.getBeatingY()].isFirstPlayer()) { --firstPawns; } else if (!board[Move.getBeatingX(), Move.getBeatingY()].isFirstPlayer()) { --secondPawns; } board[Move.getBeatingX(), Move.getBeatingY()] = null; if (Move.getNextBeating() != null) { makeMove(Move.getNextBeating()); } else { grandForKing(Move.getNewX(), Move.getNewY()); } } else { grandForKing(Move.getNewX(), Move.getNewY()); } }
//deep copy public Move(Move move) { this.prevX = move.getPrevX(); this.prevY = move.getPrevY(); this.newX = move.getNewX(); this.newY = move.getNewY(); this.beatenX = move.getBeatingX(); this.beatenY = move.getBeatingY(); this.isBeating = move.getIsBeating(); this.prevBeating = move.getPrevBeating(); this.nextBeating = move.getNextBeating(); }
// checks all possibilities of multiple beatings private void multipleBeatings(bool isFirstPlayer, List <Move> possibleMovements, Move move, int startX, int startY) { int x = move.getNewX(); int y = move.getNewY(); int beatenX = move.getBeatingX(); int beatenY = move.getBeatingY(); Pawn beatenPawn = board[beatenX, beatenY]; board[move.getBeatingX(), move.getBeatingY()] = null; if (!board[startX, startY].checkIfKing()) { if (isFirstPlayer) { if (isInBoard(x - 2, y - 2) && board[x - 2, y - 2] == null && board[x - 1, y - 1] != null && board[x - 1, y - 1].isFirstPlayer() != isFirstPlayer || isInBoard(x - 2, y + 2) && board[x - 2, y + 2] == null && board[x - 1, y + 1] != null && board[x - 1, y + 1].isFirstPlayer() != isFirstPlayer) { if (isInBoard(x - 2, y - 2) && board[x - 2, y - 2] == null && board[x - 1, y - 1] != null && board[x - 1, y - 1].isFirstPlayer() != isFirstPlayer) { Move newMovement = new Move(x, y, x - 2, y - 2, x - 1, y - 1, move); move.setNextBeating(newMovement); multipleBeatings(isFirstPlayer, possibleMovements, newMovement, startX, startY); } if (isInBoard(x - 2, y + 2) && board[x - 2, y + 2] == null && board[x - 1, y + 1] != null && board[x - 1, y + 1].isFirstPlayer() != isFirstPlayer) { Move newMovement = new Move(x, y, x - 2, y + 2, x - 1, y + 1, move); move.setNextBeating(newMovement); multipleBeatings(isFirstPlayer, possibleMovements, newMovement, startX, startY); } } else { while (move.getPrevBeating() != null) { move = new Move(move.getPrevBeating()); } possibleMovements.Add(move); } } else { if (isInBoard(x + 2, y - 2) && board[x + 2, y - 2] == null && board[x + 1, y - 1] != null && board[x + 1, y - 1].isFirstPlayer() != isFirstPlayer || isInBoard(x + 2, y + 2) && board[x + 2, y + 2] == null && board[x + 1, y + 1] != null && board[x + 1, y + 1].isFirstPlayer() != isFirstPlayer) { if (isInBoard(x + 2, y - 2) && board[x + 2, y - 2] == null && board[x + 1, y - 1] != null && board[x + 1, y - 1].isFirstPlayer() != isFirstPlayer) { Move newMovement = new Move(x, y, x + 2, y - 2, x + 1, y - 1, move); move.setNextBeating(newMovement); multipleBeatings(isFirstPlayer, possibleMovements, newMovement, startX, startY); } if (isInBoard(x + 2, y + 2) && board[x + 2, y + 2] == null && board[x + 1, y + 1] != null && board[x + 1, y + 1].isFirstPlayer() != isFirstPlayer) { Move newMovement = new Move(x, y, x + 2, y + 2, x + 1, y + 1, move); move.setNextBeating(newMovement); multipleBeatings(isFirstPlayer, possibleMovements, newMovement, startX, startY); } } else { while (move.getPrevBeating() != null) { move = new Move(move.getPrevBeating()); } possibleMovements.Add(move); } } } else //kings { if (isInBoard(x - 2, y - 2) && board[x - 2, y - 2] == null && board[x - 1, y - 1] != null && board[x - 1, y - 1].isFirstPlayer() != isFirstPlayer || isInBoard(x - 2, y + 2) && board[x - 2, y + 2] == null && board[x - 1, y + 1] != null && board[x - 1, y + 1].isFirstPlayer() != isFirstPlayer || isInBoard(x + 2, y - 2) && board[x + 2, y - 2] == null && board[x + 1, y - 1] != null && board[x + 1, y - 1].isFirstPlayer() != isFirstPlayer || isInBoard(x + 2, y + 2) && board[x + 2, y + 2] == null && board[x + 1, y + 1] != null && board[x + 1, y + 1].isFirstPlayer() != isFirstPlayer) { if (isInBoard(x - 2, y - 2) && board[x - 2, y - 2] == null && board[x - 1, y - 1] != null && board[x - 1, y - 1].isFirstPlayer() != isFirstPlayer) { Move newMovement = new Move(x, y, x - 2, y - 2, x - 1, y - 1, move); move.setNextBeating(newMovement); multipleBeatings(isFirstPlayer, possibleMovements, newMovement, startX, startY); } if (isInBoard(x - 2, y + 2) && board[x - 2, y + 2] == null && board[x - 1, y + 1] != null && board[x - 1, y + 1].isFirstPlayer() != isFirstPlayer) { Move newMovement = new Move(x, y, x - 2, y + 2, x - 1, y + 1, move); move.setNextBeating(newMovement); multipleBeatings(isFirstPlayer, possibleMovements, newMovement, startX, startY); } if (isInBoard(x + 2, y - 2) && board[x + 2, y - 2] == null && board[x + 1, y - 1] != null && board[x + 1, y - 1].isFirstPlayer() != isFirstPlayer) { Move newMovement = new Move(x, y, x + 2, y - 2, x + 1, y - 1, move); move.setNextBeating(newMovement); multipleBeatings(isFirstPlayer, possibleMovements, newMovement, startX, startY); } if (isInBoard(x + 2, y + 2) && board[x + 2, y + 2] == null && board[x + 1, y + 1] != null && board[x + 1, y + 1].isFirstPlayer() != isFirstPlayer) { Move newMovement = new Move(x, y, x + 2, y + 2, x + 1, y + 1, move); move.setNextBeating(newMovement); multipleBeatings(isFirstPlayer, possibleMovements, newMovement, startX, startY); } } else { while (move.getPrevBeating() != null) { move = new Move(move.getPrevBeating()); } possibleMovements.Add(move); } } board[beatenX, beatenY] = beatenPawn; beatenPawn = null; }
private void playerControl(int x, int y) { int[,] b = board.checkPawns(); if (b[x, y] == 1 || b[x, y] == 3 || b[x, y] == 2 || b[x, y] == 4) { pawnX = x; pawnY = y; pawnChosen = true; for (int xx = 0; xx < 8; xx++) { for (int yy = 0; yy < 8; yy++) { if (fields[xx, yy] != null) { fields[xx, yy].Image = unsigned.Image; } } } fields[x, y].Image = signed.Image; foreach (Move move in playerMovements) { if (move.getPrevX() == x && move.getPrevY() == y) { fields[move.getNewX(), move.getNewY()].Image = signed.Image; } } foreach (Move move in playerBeatings) { if (move.getPrevX() == x && move.getPrevY() == y) { fields[move.getNewX(), move.getNewY()].Image = signed.Image; } } } else if (pawnChosen) { moveX = x; moveY = y; foreach (Move move in playerMovements) { if (move.getPrevX() == pawnX && move.getPrevY() == pawnY && move.getNewX() == moveX && move.getNewY() == moveY && playerBeatings.Count() == 0) { moveDone = true; pawnChosen = false; board.makeMove(move); for (int xx = 0; xx < 8; xx++) { for (int yy = 0; yy < 8; yy++) { if (fields[xx, yy] != null) { fields[xx, yy].Image = unsigned.Image; } } } } } foreach (Move move in playerBeatings) { if (move.getPrevX() == pawnX && move.getPrevY() == pawnY && move.getNewX() == moveX && move.getNewY() == moveY && !moveDone) { moveDone = true; pawnChosen = false; board.makeMove(move); for (int xx = 0; xx < 8; xx++) { for (int yy = 0; yy < 8; yy++) { if (fields[xx, yy] != null) { fields[xx, yy].Image = unsigned.Image; } } } } } } label4.Text = "Player pawn: x= " + pawnX + ", y= " + pawnY + ", move: x= " + moveX + ", y= " + moveY; }