/// <summary> /// We would like to achieve the following: /// - replacing our last position with null /// - knocking pieces out or passing depending on collision with ourselves or others /// </summary> /// <param name="fromSquare"></param> /// <param name="toSquare"></param> /// <param name="piece"></param> /// <param name="steps"></param> /// <param name="requestedSteps"></param> /// <returns></returns> private bool tryMove(Square fromSquare, Square toSquare, Piece piece, int steps, int requestedSteps) { if (toSquare.Occupant == null) { Debug.Write("\nMoving piece to square " ); move(piece, toSquare, fromSquare, steps); return true; } else { Piece occupyingPiece = toSquare.Occupant; Debug.Write("\nSquare is occupied"); if (occupyingPiece.Color == piece.Color) { // Two pieces of the same color cannot occupy the same space (pass) return false; } else { // We will now deActivate the colliding piece, since it's not our own // ... and insert our own piece Debug.Write("\nKnocking out piece "); move(piece, toSquare, fromSquare, steps); deActivatePiece(occupyingPiece); if (nests[(int)occupyingPiece.Color].Count == 4) { players[(int)occupyingPiece.Color].Active = false; } return true; } } }
private void move(Piece piece, Square toSquare, Square fromSquare, int steps) { if (fromSquare != null) { fromSquare.Occupant = null; } toSquare.Occupant = piece; piece.Steps += steps; piece.Position += steps; }
public int[][] squaresToArray(Square[][] squaresToConvert, int squaresPerSide) { int[][] squaresArray = new int[numOfPlayers][]; for (int i = 0; i < numOfPlayers; i++) { squaresArray[i] = new int[squaresPerSide]; for (int j = 0; j < squaresPerSide; j++) { squaresArray[i][j] = (squaresToConvert[i][j].Occupant == null) ? -1 : (int)squaresToConvert[i][j].Occupant.Color; } } return squaresArray; }
private void generateSquares() { squares = new Square[numOfPlayers][]; exitSquares = new ExitSquare[numOfPlayers][]; for (int side = 0; side < numOfPlayers; side++) { squares[side] = new Square[numOfSquaresPerSide]; for (int position = 0; position < numOfSquaresPerSide; position++) { int squarePosition = position + (side * numOfSquaresPerSide); squares[side][position] = new Square((Colors)side, squarePosition); } exitSquares[side] = new ExitSquare[numOfExitSquaresPerSide]; for (int position = 0; position < numOfExitSquaresPerSide; position++) { exitSquares[side][position] = new ExitSquare((Colors)side, position); } } }
/// <summary> /// We would like to achieve the following: /// - replacing our last position with null /// - knocking pieces out or passing depending on collision with ourselves or others /// </summary> /// <param name="fromSquare"></param> /// <param name="toSquare"></param> /// <param name="piece"></param> /// <param name="steps"></param> /// <returns></returns> private bool tryMove(Square fromSquare, Square toSquare, Piece piece, int steps) { if (toSquare.Occupant == null) { Instruction = Instructions.Move; Debug.Write("\nBoard: Moving piece"); Debug.Write("\nWalking: " + steps + " steps"); move(piece, toSquare, fromSquare, steps); return true; } else { Piece occupyingPiece = toSquare.Occupant; Debug.Write("\nBoard: Square is occupied"); if (occupyingPiece.Color == piece.Color) { Instruction = Instructions.CollisionWithSelf; // Two pieces of the same color cannot occupy the same space (pass) return false; } else { // We will now deActivate the colliding piece, since it's not our own // ... and insert our own piece Instruction = Instructions.MoveAndKnockout; Debug.Write("\nBoard: Knocking out piece "); move(piece, toSquare, fromSquare, steps); deActivatePiece(occupyingPiece); if (nests[(int)occupyingPiece.Color].Count == 4) { players[(int)occupyingPiece.Color].Active = false; } return true; } } }