public Nest(Colors color, Piece[] pieces) { this.pieces = new Stack<Piece>(); this.color = color; foreach (var piece in pieces) { pushPiece(piece); } }
private List<int[]> exportPieces(Piece[][] pieces) { List<int[]> piecesList = new List<int[]>(); for (int color = 0; color < numOfPlayers; color++) { for (int index = 0; index < numOfPiecesPerPlayer; index++) { int piecePosition = pieces[color][index].Position; int pieceSteps = pieces[color][index].Steps; int[] pieceInfo = { piecePosition, color, pieceSteps }; piecesList.Add( pieceInfo ); } } return piecesList; }
/// <summary> /// tryMove(piece) /// </summary> /// <param name="piece"></param> /// <param name="dice"></param> private void tryMove(Piece piece, dice dice) { bool isMoveSuccesful = false; ludoBoard.tryMovePiece(piece, dice, ref isMoveSuccesful); string debug = (isMoveSuccesful) ? "Move succesful" : "Move not succesful"; Debug.WriteLine("\n" + debug); }
/// <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; } } }
/// <summary> /// To introduce: move a player from nest to square /// </summary> /// <param name="piece"></param> /// <returns></returns> private bool tryIntroducePiece(Piece piece) { Square firstSquare = squares[(int)piece.Color][0]; bool isIntroduced = tryMove(null, firstSquare, piece, 0, 0); return isIntroduced; }
/// <summary> /// Stack-like behaviour /// </summary> /// <param name="piece"></param> private void pushPieceToNest(Piece piece) { nests[(int)piece.Color].pushPiece(piece); }
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 Nest(Colors color, Piece[] pieces) { this.pieces = pieces; this.color = color; }
private void generatePieces(Colors color) { pieces[(int)color] = new Piece[numOfPiecesPerPlayer]; for (int i = 0; i < numOfPiecesPerPlayer; i++) { pieces[(int)color][i] = new Piece(color, i); } }
/// <summary> /// Here we need to consider 3 scenarios: /// - If we are about to roll the dice and have a chance at moving to the exit squares /// - chance at moving through the goal /// - "normal" behaviour /// </summary> /// <param name="piece"></param> /// <param name="steps"></param> /// <param name="isMoveSuccesful"></param> public void tryMovePiece(Piece piece, int steps, ref bool isMoveSuccesful) { int requestedSteps = piece.Steps + steps; // 3 scenarios // if (requestedSteps == numOfStepsToExit) { Debug.Write("\nExiting"); isMoveSuccesful = killPiece(piece); checkPlayerDeActivation(piece.Color); } // Almost quitting else if (requestedSteps > numOfStepsToExit) { Debug.Write(String.Format( "\nGoing to the exit squares. Steps walked: {0}, position: {1}, trying new pos: {2}", piece.Steps, piece.Position, piece.Position + steps)); // We will have to step back x steps from the goal square (overflow) Square currentPosition = locateSquare(piece.Steps, piece.Position, piece.Color); calculateNewStepsAfterMissedTarget(piece.Position, requestedSteps, ref steps); Square requestedPosition = locateSquare(requestedSteps, piece.Position + steps, piece.Color); isMoveSuccesful = tryMove(currentPosition, requestedPosition, piece, steps, requestedSteps); } else { Debug.Write(String.Format( "\nSteps walked: {0}, position: {1}, trying new pos: {2}", piece.Steps, piece.Position, piece.Position + steps)); Square currentPosition = locateSquare(piece.Steps, piece.Position, piece.Color); Square requestedPosition = locateSquare(requestedSteps, piece.Position + steps, piece.Color); isMoveSuccesful = tryMove(currentPosition, requestedPosition, piece, steps, requestedSteps); } }
public void deActivatePiece(Piece piece) { nests[(int)piece.Color].pushPiece(piece); piece.Active = false; }
public Square(Colors color, int number) { this.color = color; this.SquareID = number; this.occupant = null; }
public void pushPiece(Piece piece) { pieces.Push(piece); }
private bool tryMove(Piece piece, dice dice) { bool isMoveSuccesful = false; ludoBoard.tryMovePiece(piece, dice, ref isMoveSuccesful); string debug = (isMoveSuccesful) ? "Rules: Move succesful" : "Rules: Move not succesful"; Debug.WriteLine("\n" + debug); return isMoveSuccesful; }
/// <summary> /// By moving through the goal the piece gets killed /// </summary> /// <param name="piece"></param> private bool killPiece(Piece piece) { Square currentPosition = locateSquare(piece.Steps, piece.Position, piece.Color); currentPosition.Occupant = null; piece.Alive = false; piece.Active = false; return true; }
private GameState updateGameState(GameState gameState, Piece[][] pieces) { gameState.Pieces = exportPieces(pieces); gameState.Instruction = exportInstruction(); return gameState; }