public void SimulateCreatePiece(int[] cell, char pieceChar) { boardSimulation[cell[0], cell[1]] = new PieceSquare2D(); boardSimulation[cell[0], cell[1]].Initialize( new int[] { cell[0], cell[1] }, this.gameObject, turnCoordinator.turn + 1, boardGenerator.playerList[turnCoordinator.turn].team, boardGenerator.playerList[turnCoordinator.turn].color, new List<Move>(), boardGenerator.playerDirections[turnCoordinator.turn], 0, pieceChar, PieceType.NONE, null ); }
public void CheckMoves(PieceSquare2D[,] board, bool[,] existingCells) { avaliableMoves.Clear(); foreach (Move baseMove in moves) { Move move = new Move(new int[baseMove.move.Length], baseMove.style, baseMove.type); for (int i = 0; i < baseMove.move.Length; i++) { int newindex = i + Math.Abs(direction) - 1; if (newindex >= baseMove.move.Length) { newindex -= baseMove.move.Length; } if (direction >= 0) { move.move[newindex] = baseMove.move[i]; } else { move.move[newindex] = -baseMove.move[i]; } } int x = position[0] + move.move[0]; int y = position[1] + move.move[1]; if ((x >= 0) && (x < board.GetLength(0)) && (y >= 0) && (y < board.GetLength(1)) && existingCells[x, y]) { PieceSquare2D objectiveCell = board[x, y]; switch (move.style) { case Style.FINITE: // FINITE ==================================== int minorAxis = 0; if (Math.Abs(move.move[1]) < Math.Abs(move.move[0])) { minorAxis = 1; } int mayorAxis = 1 - minorAxis; bool blocked = false; if (move.move[minorAxis] != 0) { for (int i = 1; i < Math.Abs(move.move[minorAxis]) && !blocked; i++) { if ((move.move[mayorAxis] * i) % move.move[minorAxis] == 0) { int tmpX = position[0] + ((move.move[0] / Math.Abs(move.move[minorAxis])) * i); int tmpY = position[1] + ((move.move[1] / Math.Abs(move.move[minorAxis])) * i); if (!(board[tmpX, tmpY] is null)) { blocked = true; } } } } else { for (int i = 1; i < Math.Abs(move.move[mayorAxis]) && !blocked; i++) { int tmpX = position[0]; int tmpY = position[1]; if (mayorAxis == 0) { tmpX += i * (move.move[mayorAxis] > 0 ? 1 : -1); } else { tmpY += i * (move.move[mayorAxis] > 0 ? 1 : -1); } if (!(board[tmpX, tmpY] is null)) { blocked = true; } } } if (!blocked) { switch (move.type) { case Type.BOTH: if ((objectiveCell == null) || (objectiveCell.team != this.team)) { avaliableMoves.Add(new int[] { x, y }); } break; case Type.CAPTURE: if (objectiveCell != null) { if (objectiveCell.team != this.team) { avaliableMoves.Add(new int[] { x, y }); } } break; case Type.MOVE: if (objectiveCell == null) { avaliableMoves.Add(new int[] { x, y }); } break; } } break; case Style.INFINITE: // INFINITE ==================================== int minorAxis1 = 0; if (Math.Abs(move.move[1]) < Math.Abs(move.move[0])) { minorAxis1 = 1; } int mayorAxis1 = 1 - minorAxis1; int[] tmpPosition2 = position; bool possible2 = true; while (possible2) { bool blocked1 = false; if (move.move[minorAxis1] != 0) { for (int i = 1; i < Math.Abs(move.move[minorAxis1]) && !blocked1; i++) { if ((move.move[mayorAxis1] * i) % move.move[minorAxis1] == 0) { int tmpX = tmpPosition2[0] + ((move.move[0] / Math.Abs(move.move[minorAxis1])) * i); int tmpY = tmpPosition2[1] + ((move.move[1] / Math.Abs(move.move[minorAxis1])) * i); if (!(board[tmpX, tmpY] is null)) { blocked1 = true; } } } } else { for (int i = 1; i < Math.Abs(move.move[mayorAxis1]) && !blocked1; i++) { int tmpX = tmpPosition2[0] + i; int tmpY = tmpPosition2[1] + i; if (!(board[tmpX, tmpY] is null)) { blocked1 = true; } } } if (!blocked1) { switch (move.type) { case Type.BOTH: if (objectiveCell == null) { avaliableMoves.Add(new int[] { x, y }); } else if (objectiveCell.team != this.team) { avaliableMoves.Add(new int[] { x, y }); possible2 = false; } else { possible2 = false; } break; case Type.CAPTURE: if (objectiveCell != null) { if (objectiveCell.team != this.team) { avaliableMoves.Add(new int[] { x, y }); } else { possible2 = false; } } break; case Type.MOVE: if (objectiveCell == null) { avaliableMoves.Add(new int[] { x, y }); } else { possible2 = false; } break; } } else { possible2 = false; } if (possible2 && move.type != Type.CAPTURE) { tmpPosition2 = new int[] { x, y }; x = tmpPosition2[0] + move.move[0]; y = tmpPosition2[1] + move.move[1]; if ((x >= 0) && (x < board.GetLength(0)) && (y >= 0) && (y < board.GetLength(1)) && existingCells[x, y]) { objectiveCell = board[x, y]; } else { possible2 = false; } } else { possible2 = false; } } break; case Style.INFINITEJUMP: // INFINITE JUMP ==================================== bool possible1 = true; int[] tmpPosition1 = position; while (possible1) { switch (move.type) { case Type.BOTH: if (objectiveCell == null) { avaliableMoves.Add(new int[] { x, y }); } else if (objectiveCell.team != this.team) { avaliableMoves.Add(new int[] { x, y }); possible1 = false; } else { possible1 = false; } break; case Type.CAPTURE: if (objectiveCell != null) { if (objectiveCell.team != this.team) { avaliableMoves.Add(new int[] { tmpPosition1[0] + move.move[0], tmpPosition1[1] + move.move[1] }); } } break; case Type.MOVE: if (objectiveCell == null) { avaliableMoves.Add(new int[] { x, y }); } else { possible1 = false; } break; } if (possible1 && move.type != Type.CAPTURE) { tmpPosition1 = new int[] { x, y }; x = tmpPosition1[0] + move.move[0]; y = tmpPosition1[1] + move.move[1]; if ((x >= 0) && (x < board.GetLength(0)) && (y >= 0) && (y < board.GetLength(1)) && existingCells[x, y]) { objectiveCell = board[x, y]; } else { possible1 = false; } } else { possible1 = false; } } break; case Style.JUMP: // JUMP ==================================== switch (move.type) { case Type.BOTH: if ((objectiveCell == null) || (objectiveCell.team != this.team)) { avaliableMoves.Add(new int[] { x, y }); } break; case Type.CAPTURE: if (objectiveCell != null) { if (objectiveCell.team != this.team) { avaliableMoves.Add(new int[] { x, y }); } } break; case Type.MOVE: if (objectiveCell == null) { avaliableMoves.Add(new int[] { x, y }); } break; } break; } } } }
public void CheckKing() { int totalAvailableMoves = 0; // == CHECK NORMAL MOVES ================================================================= for (int i = 0; i < board.GetLength(0); i++) for (int j = 0; j < board.GetLength(1); j++) { if (board[i,j] != null) { if (board[i,j].player == turnCoordinator.turn+1) { List<int[]> movesToRemove = new List<int[]>(); foreach (int[] avaliableMove in board[i,j].avaliableMoves) { if (board[i,j].type != PieceType.KING) { PieceSquare2D[,] tmpBoard = new PieceSquare2D[board.GetLength(0), board.GetLength(1)]; for (int a = 0; a < board.GetLength(0); a++) for (int b = 0; b < board.GetLength(1); b++) tmpBoard[a,b] = board[a,b]; tmpBoard[avaliableMove[0],avaliableMove[1]] = tmpBoard[i,j]; tmpBoard[i,j] = null; foreach (PieceSquare2D piece in tmpBoard) if (piece != null) if (piece.team != turnCoordinator.players[turnCoordinator.turn].team) { List<int[]> attacks = piece.GetAttacks(tmpBoard, existingCells); foreach (PieceSquare2D king in kings) if (king.player == turnCoordinator.turn + 1) foreach (int[] attack in attacks) if (attack[0] == king.position[0] && attack[1] == king.position[1]) try { movesToRemove.Add(avaliableMove); break; } catch { } } } else { PieceSquare2D[,] tmpBoard = new PieceSquare2D[board.GetLength(0), board.GetLength(1)]; for (int a = 0; a < board.GetLength(0); a++) for (int b = 0; b < board.GetLength(1); b++) tmpBoard[a,b] = board[a,b]; tmpBoard[avaliableMove[0],avaliableMove[1]] = tmpBoard[i,j]; tmpBoard[i,j] = null; foreach (PieceSquare2D piece in tmpBoard) if (piece != null) if (piece.team != turnCoordinator.players[turnCoordinator.turn].team) { List<int[]> attacks = piece.GetAttacks(tmpBoard, existingCells); foreach (int[] attack in attacks) if (attack[0] == avaliableMove[0] && attack[1] == avaliableMove[1]) try { movesToRemove.Add(avaliableMove); break; } catch { } } } } foreach (int[] removedMove in movesToRemove) board[i,j].avaliableMoves.Remove(removedMove); totalAvailableMoves += board[i,j].avaliableMoves.Count; } } } // == CHECK SPECIAL MOVES ============================================================================= List<SpecialMove> specialsToRemove = new List<SpecialMove>(); foreach (SpecialMove move in availableSpecials) { move.SimulateMove(this); foreach (PieceSquare2D piece in boardSimulation) if (piece != null) if (piece.team != turnCoordinator.players[turnCoordinator.turn].team) { List<int[]> attacks = piece.GetAttacks(boardSimulation, existingCells); foreach (PieceSquare2D king in kings) if (king.player == turnCoordinator.turn + 1) foreach (int[] attack in attacks) { if (attack[0] == king.position[0] && attack[1] == king.position[1]) try { board[move.GetMove().Item1[0], move.GetMove().Item1[1]].availableSpecials.Remove(move.GetMove().Item2); specialsToRemove.Add(move); break; } catch { } } } this.EndSimulation(); } foreach (SpecialMove move in specialsToRemove) availableSpecials.Remove(move); totalAvailableMoves += availableSpecials.Count; // == CHECK IF NO MOVES AVAILABLE ===================================================================== if (totalAvailableMoves == 0) { turnCoordinator.EliminateActualPlayer(); } }