public void Update(KaroGameManager manager, MouseClick click) { IEnumerable<MoveWrapper> legalMoves = manager.FindLegalMoves(manager.CurrentPlayer); MoveWrapper move = legalMoves.FirstOrDefault(m => m.GetFromCell() == manager.CurrentMove.GetFromCell() && m.GetToCell() == manager.CurrentMove.GetToCell() && m.HasUsedCell() && m.GetUsedCell() == new Vector2DWrapper(click.Position.X, click.Position.Y)); if (move != null) { CommunicationProtocolConversionUtility util = new CommunicationProtocolConversionUtility(manager.Game); //Debug.WriteLine(util.MoveWrapperToString(move)); Debug.WriteLine(util.TurnToString(util.ConvertMoveToTurn(move))); // We now have a valid move. Execute it! Debug.WriteLine("Clicked on moveable tile."); manager.ExecuteMove(move); manager.CurrentMove = null; if (!(manager.CurrentState is ComputerState) && !(manager.CurrentState is WaitForUndoState)) { manager.ChangeState(PieceSourceState.Instance); } } else { // Clicked on invalid tile. Back to PieceSourceState. Debug.WriteLine("Clicked on a non moveable tile."); manager.SendMoveIsNotValid(); manager.CurrentMove = null; manager.ChangeState(PieceSourceState.Instance); } }
public void Update(KaroGameManager manager, MouseClick click) { IEnumerable<MoveWrapper> legalMoves = manager.FindLegalMoves(manager.CurrentPlayer); MoveWrapper move = legalMoves.FirstOrDefault(m => m.GetToCell() == new Vector2DWrapper(click.Position.X, click.Position.Y)); // We have a valid move. if (move != null) { Debug.WriteLine(move.GetToCell()); CommunicationProtocolConversionUtility util = new CommunicationProtocolConversionUtility(manager.Game); Debug.WriteLine(util.TurnToString(util.ConvertMoveToTurn(move))); manager.ExecuteMove(move); Debug.WriteLine("Placed a new piece."); } else { manager.SendMoveIsNotValid(); Debug.WriteLine("Can't place a new piece"); } // Change state to Piece source state if all 6 pieces are on the board. if (manager.Board.GetOccupiedCells().Count == MaxPieceCount) { if (!(manager.CurrentState is ComputerState) && !(manager.CurrentState is WaitForUndoState)) { Debug.WriteLine("All {0} pieces are placed at the board.", MaxPieceCount); manager.ChangeState(PieceSourceState.Instance); } } }
public void Update(KaroGameManager manager, MouseClick click) { IEnumerable<MoveWrapper> legalMoves = manager.FindLegalMoves(manager.CurrentPlayer); // Get the move with the correct source tile from the last click. IEnumerable<MoveWrapper> sourceLegalMoves = legalMoves.Where(m => m.GetFromCell() == manager.CurrentMove.GetFromCell()); // Get the move (if it exists) with the correct destination tile. MoveWrapper move = sourceLegalMoves.FirstOrDefault(m => m.GetToCell() == new Vector2DWrapper(click.Position.X, click.Position.Y)); if (move != null) { var usedTile = move.HasUsedCell(); Debug.WriteLine("Clicked on valid destination"); // Check if the destination tile exists or if a tile has to be moved. if (move.HasUsedCell()) { Debug.WriteLine("Empty tile must be moved to destination"); manager.CurrentMove = move; manager.ChangeState(CellSourceState.Instance); } // Valid move, execute it. else { Debug.WriteLine("Moving tile at {0} to {1}", move.GetFromCell(), move.GetToCell()); manager.ExecuteMove(move); CommunicationProtocolConversionUtility util = new CommunicationProtocolConversionUtility(manager.Game); Debug.WriteLine(util.TurnToString(util.ConvertMoveToTurn(move))); if (!(manager.CurrentState is ComputerState) && !(manager.CurrentState is WaitForUndoState)) { manager.ChangeState(PieceSourceState.Instance); } //Debug.WriteLine(move.GetToCell()); } } else { // Clicked on invalid destination tile. Get rid of the current // move and go back to PieceSourceState. Debug.WriteLine("Can't move selected piece to tile.", click); manager.SendMoveIsNotValid(); manager.CurrentMove = null; manager.ChangeState(PieceSourceState.Instance); } }
public void Update(KaroGameManager manager, MouseClick click) { IEnumerable<MoveWrapper> legalMoves = manager.FindLegalMoves(manager.CurrentPlayer); // See if there is any move with the same source as the clicked tile. MoveWrapper move = legalMoves.FirstOrDefault(m => m.GetFromCell() == new Vector2DWrapper(click.Position.X, click.Position.Y)); // Valid source piece clicked, save the move. if (move != null) { Debug.WriteLine("Valid source piece selected"); manager.CurrentMove = move; manager.ChangeState(PieceDestinationState.Instance); } else { Debug.WriteLine("Clicked tile does not have a moveable piece"); } }