/** * This method receives a soldier and a move offset and returns all the available moves that the soldier possibly has with the given * move offset. */ public List <Move> GetPossibleSoldierMoves(Soldier i_Soldier, byte i_MoveOffset) { CheckersCoordinates soldierLocation = i_Soldier.Location; char targetRightColumn = (char)(soldierLocation.Column + i_MoveOffset); char targetLeftColumn = (char)(soldierLocation.Column - i_MoveOffset); char targetTopRow = (char)(soldierLocation.Row - i_MoveOffset); char targetBottomRow = (char)(soldierLocation.Row + i_MoveOffset); List <Move> possibleMoves = new List <Move>(); CheckersCoordinates[] coordinates = new CheckersCoordinates[Move.k_PossibleDiagonalMoves]; // Creating the coordinates as we need them for the Move objects. coordinates[0] = new CheckersCoordinates(targetRightColumn, targetTopRow); coordinates[1] = new CheckersCoordinates(targetLeftColumn, targetTopRow); coordinates[2] = new CheckersCoordinates(targetRightColumn, targetBottomRow); coordinates[3] = new CheckersCoordinates(targetLeftColumn, targetBottomRow); foreach (CheckersCoordinates coordinate in coordinates) { Move generatedMove = new Move(soldierLocation, coordinate); generatedMove.Player = i_Soldier.Player; if (IsMoveValid(generatedMove)) { possibleMoves.Add(generatedMove); } } return(possibleMoves); }
/** * This method receives a Soldier and returns whether the soldier has valid moves according to their offset. * Offset 1 means it can move 1 step diagonally, offset 2 means it can move 2 steps diagonally (only valid for jumpable moves). */ public bool SoldierHasMoves(Soldier i_Soldier, byte i_MoveOffset) { CheckersCoordinates soldierLocation = i_Soldier.Location; List <Move> possibleMoves = GetPossibleSoldierMoves(i_Soldier, i_MoveOffset); return(possibleMoves.Count > 0); }
private void OnSoldierRemoved(CheckersCoordinates i_Coordinates) { if (SoldierRemoved != null) { SoldierRemoved.Invoke(i_Coordinates); } }
private Tile getTileByLocation(CheckersCoordinates i_Coordinates) { byte rowIndex = CheckersBoard.GetRowIndexByLetter(i_Coordinates.Row); byte colIndex = CheckersBoard.GetColumnIndexByLetter(i_Coordinates.Column); return(m_Tiles[rowIndex, colIndex]); }
/** * Constructor - creates a new soldier with a given set of coordinates and its type. */ public Soldier(eType i_SoldierType, CheckersCoordinates i_Location) { m_SoldierType = i_SoldierType; m_Location = i_Location; initPlayerType(); }
/** * This method receives a Checkers Coordinates in the board and removes the soldier located at that position from the board. */ public void RemoveSoldier(CheckersCoordinates i_Location) { byte rowIndex = GetRowIndexByLetter(i_Location.Row); byte colIndex = GetColumnIndexByLetter(i_Location.Column); m_Board[rowIndex, colIndex] = null; }
/** * This method receives a move object and returns whether the move is a legit jumpable move or not. */ public bool IsJumpableMove(Move i_Move) { bool jumpableMove = true; CheckersCoordinates currentLocation = i_Move.CurrentLocation; CheckersCoordinates targetLocation = i_Move.TargetLocation; int columnsDelta = targetLocation.Column - currentLocation.Column; int rowsDelta = targetLocation.Row - currentLocation.Row; Player movePlayer = i_Move.Player != null ? i_Move.Player : GetCurrentPlayer(); if (Math.Abs(columnsDelta) != Move.k_JumpMovesOffset || Math.Abs(rowsDelta) != Move.k_JumpMovesOffset) { jumpableMove = false; } else { Soldier jumpableSoldier = getJumpedSoldier(i_Move); // Checking that there is a jumpable soldier to jump over and that it's the opponent's soldier. if (jumpableSoldier == null || movePlayer.PlayerType == jumpableSoldier.PlayerType) { jumpableMove = false; } else if (m_LastMove != null && m_LastMove.HasDoubleJump && !currentLocation.Equals(m_LastMove.TargetLocation)) { // Checking that if the last move is a double jump move, then the last soldier played is the current soldier. jumpableMove = false; } } return(jumpableMove); }
/** * This method receives a Checkers Coordinates in the board and returns the soldier located in that position. */ public Soldier GetSoldierAt(CheckersCoordinates i_Location) { byte rowIndex = GetRowIndexByLetter(i_Location.Row); byte colIndex = GetColumnIndexByLetter(i_Location.Column); return(GetSoldierAt(rowIndex, colIndex)); }
/** * Constructor - creates a new move object using a given current location and target location. */ public Move(CheckersCoordinates i_CurrentLocation, CheckersCoordinates i_TargetLocation) { m_CurrentLocation = i_CurrentLocation; m_TargetLocation = i_TargetLocation; m_HasDoubleJump = false; initIsJumpMove(); }
private void soldiers_SoldierRemoved(CheckersCoordinates i_Coordinates) { byte rowIndex = CheckersBoard.GetRowIndexByLetter(i_Coordinates.Row); byte colIndex = CheckersBoard.GetColumnIndexByLetter(i_Coordinates.Column); // Removing the soldier button from the board. m_FormGame.Controls.Remove(m_Tiles[rowIndex, colIndex].ButtonSoldier); m_Tiles[rowIndex, colIndex].ButtonSoldier = null; }
/** * This method receives a move object that represents a jump move and returns the jumped soldier location. */ public CheckersCoordinates getJumpedSoldierLocation(Move i_Move) { CheckersCoordinates targetLocation = i_Move.TargetLocation; CheckersCoordinates currentLocation = i_Move.CurrentLocation; char middleColumn = (char)((currentLocation.Column + targetLocation.Column) / 2); char middleRow = (char)((currentLocation.Row + targetLocation.Row) / 2); return(new CheckersCoordinates(middleColumn, middleRow)); }
private void moveButtonSoldier(Move i_Move) { CheckersCoordinates sourceLocation = i_Move.CurrentLocation; CheckersCoordinates targetLocation = i_Move.TargetLocation; Tile sourceTile = getTileByLocation(sourceLocation); Tile targetTile = getTileByLocation(targetLocation); sourceTile.ButtonSoldier.AnimateMoveToTile(targetTile); }
/** * This method receives a valid string which represents a Move object and returns the Move object that the string represents. */ public static Move Parse(string i_MoveString) { char currentColumn = i_MoveString[0]; char targetColumn = i_MoveString[3]; char currentRow = i_MoveString[1]; char targetRow = i_MoveString[4]; CheckersCoordinates currentLocation = new CheckersCoordinates(currentColumn, currentRow); CheckersCoordinates targetLocation = new CheckersCoordinates(targetColumn, targetRow); Move parsed = new Move(currentLocation, targetLocation); return(parsed); }
/** * This method receives a Move object and moves a soldier in the board from the current location the soldier is located at to the target location. */ public void MoveSoldier(Move i_Move) { CheckersCoordinates currentLocation = i_Move.CurrentLocation; CheckersCoordinates targetLocation = i_Move.TargetLocation; byte currentRowIndex = GetRowIndexByLetter(currentLocation.Row); byte currentColIndex = GetColumnIndexByLetter(currentLocation.Column); byte targetRowIndex = GetRowIndexByLetter(targetLocation.Row); byte targetColIndex = GetColumnIndexByLetter(targetLocation.Column); Soldier moveSoldier = m_Board[currentRowIndex, currentColIndex]; moveSoldier.Location = targetLocation; m_Board[targetRowIndex, targetColIndex] = moveSoldier; RemoveSoldier(currentLocation); }
private void makePlayerMove(Tile i_SourceButton, Tile i_TargetButton) { char currentColumn = (char)(CheckersCoordinates.k_StartColumn + i_SourceButton.Column); char currentRow = (char)(CheckersCoordinates.k_StartRow + i_SourceButton.Row); char targetColumn = (char)(CheckersCoordinates.k_StartColumn + i_TargetButton.Column); char targetRow = (char)(CheckersCoordinates.k_StartRow + i_TargetButton.Row); CheckersCoordinates currentLocation = new CheckersCoordinates(currentColumn, currentRow); CheckersCoordinates targetLocation = new CheckersCoordinates(targetColumn, targetRow); Move soldierMove = new Move(currentLocation, targetLocation); r_GameHandler.MakeMove(soldierMove); m_PressedTile.BackColor = sr_ValidSquareColor; m_PressedTile = null; }
/** * This method receives a start row and a soldier type and intializes the soldiers lines for each soldier type starting from the given start row. */ private void initSoldiersLines(char i_StartRow, Soldier.eType i_SoldierType) { byte amountOfSoldiersLines = GetAmountOfSoldiersLines(); for (char i = i_StartRow; i < i_StartRow + amountOfSoldiersLines; i++) { char startColumn = (char)('A' + (i % 2)); for (char j = startColumn; j < startColumn + m_Size; j += (char)2) { CheckersCoordinates soldierLocation = new CheckersCoordinates(j, i); byte realRowIndex = GetRowIndexByLetter(i); byte realColIndex = GetColumnIndexByLetter(j); m_Board[realRowIndex, realColIndex] = new Soldier(i_SoldierType, soldierLocation); } } }
/** * This method receives a Move object and returns whether the soldier making the move is going in the right direction. */ private bool soldierMovingInRightDirection(Move i_Move) { CheckersCoordinates currentLocation = i_Move.CurrentLocation; int rowsDelta = i_Move.TargetLocation.Row - currentLocation.Row; // Calculating the rows delta which tells us if the direction is up or down. bool validRowDelta = rowsDelta != 0; // If the rows delta is 0 then the soldier is moving horizontally which is invalid. Player movePlayer = i_Move.Player != null ? i_Move.Player : GetCurrentPlayer(); Soldier soldier = m_CheckersBoard.GetSoldierAt(currentLocation); bool playerMovingInRightDirection; // If the rows delta is greater than zero then the soldier is moving down (which should be done by a king or by player two and vice versa). if (rowsDelta > 0) { playerMovingInRightDirection = movePlayer == m_PlayerOne; } else { playerMovingInRightDirection = movePlayer == m_PlayerTwo; } return(validRowDelta && (!playerMovingInRightDirection || soldier.IsKing())); }
/** * This method receives a Move and returns whether it is a valid move. * A valid move must be a diagonal move which doesn't overlap another soldier, it must move an actual soldier, it must not exceed the board's boundaries, * if it's a jump move then it must be jumping over the opponent's soldier and if the move isn't a jump move but the player has jump moves available, then * the move isn't valid as well. */ public bool IsMoveValid(Move i_Move) { bool validMove = true; CheckersCoordinates targetLocation = i_Move.TargetLocation; CheckersCoordinates currentLocation = i_Move.CurrentLocation; Player movePlayer = i_Move.Player != null ? i_Move.Player : GetCurrentPlayer(); if (!targetLocation.IsValid(m_CheckersBoard.Size) || !currentLocation.IsValid(m_CheckersBoard.Size)) { validMove = false; } else { Soldier soldier = m_CheckersBoard.GetSoldierAt(currentLocation); Soldier targetLocationSoldier = m_CheckersBoard.GetSoldierAt(targetLocation); // Checking that the soldier actually exists and that we're not trying to move the soldier to an existing soldier. if (soldier == null || targetLocationSoldier != null) { validMove = false; } else if (!isValidDiagonalMove(i_Move)) { validMove = false; } else if (i_Move.IsJumpMove && !IsJumpableMove(i_Move)) { validMove = false; } else if (!i_Move.IsJumpMove && PlayerHasJumpMove(movePlayer)) { validMove = false; } } return(validMove); }
/** * This method receives a Checkers Coordinates in the board and returns the soldier located in that position. * This method is a bridge between the UI and the CheckersBoard because we don't want to expose the CheckersBoard itself to the UI. */ public Soldier GetSoldierAt(CheckersCoordinates i_Location) { return(m_CheckersBoard.GetSoldierAt(i_Location)); }
/** * This method receives a move object and returns the soldier that was jumped over (in case the move was a jump move). */ private Soldier getJumpedSoldier(Move i_Move) { CheckersCoordinates jumpedSoldierLocation = getJumpedSoldierLocation(i_Move); return(m_CheckersBoard.GetSoldierAt(jumpedSoldierLocation)); }