/// <summary>Cancel the result of this command.</summary> public override void Undo() { preventConflict(stackBefore, stackAfter, transitionStack); List <IAnimation> animations = new List <IAnimation>(5); if (transitionStack == stackAfter) { animations.Add(new EmptyPlayerHandAnimation(playerGuid, transitionStack)); animations.Add(new RearrangeStackAnimation(transitionStack, pieces)); } else { animations.Add(new SplitStackAnimation(stackAfter, pieces, transitionStack)); } animations.Add(new MoveToFrontOfBoardAnimation(transitionStack, stackBefore.Board)); for (int i = 0; i < pieces.Length; ++i) { IPiece piece = pieces[i]; if (piece.RotationAngle != rotationAnglesBefore[i]) { int totalDetentsBefore = (int)(piece.RotationAngle * (12.0f / (float)Math.PI) + 0.5f) * 120; int totalDetentsAfter = (int)(rotationAnglesBefore[i] * (12.0f / (float)Math.PI) + 0.5f) * 120; int rotationIncrements = totalDetentsAfter - totalDetentsBefore; animations.Add(new InstantRotatePiecesAnimation(new IPiece[] { piece }, rotationIncrements)); } if (piece.Side != sidesBefore[i]) { animations.Add(new InstantFlipPiecesAnimation(new IPiece[] { piece })); } } animations.Add(new MoveStackFromHandAnimation(transitionStack, stackBefore.Position)); animations.Add(new MergeStacksAnimation(stackBefore, transitionStack, bottomIndex)); model.AnimationManager.LaunchAnimationSequence(animations.ToArray()); }
private void ChefIfPieceDoesNotExist(IPiece piece) { if (!this.pieces.Contains(piece)) { throw new InvalidOperationException("Player does not have this piece!"); } }
private void ChefIfPieceExists(IPiece piece) { if (this.pieces.Contains(piece)) { throw new InvalidOperationException("Player has this piece already!"); } }
public void RemovePiece(IPiece piece) { ObjectValidator.CheckIfObjectIsNull(piece, ErrorMessages.NullPieceError); //TODO: check piece and color ChefIfPieceDoesNotExist(piece); pieces.Remove(piece); }
public void Attacking(IPiece piece, Square[][] matrix) { this.AttackedSquaresY(-1, piece.Position.File, matrix, piece); this.AttackedSquaresX(piece.Position.Rank, -1, matrix, piece); this.AttackedSquaresY(1, piece.Position.File, matrix, piece); this.AttackedSquaresX(piece.Position.Rank, 1, matrix, piece); }
public List<Position> applyMove(Board board, IPiece piece) { Position p = piece.Position; Position possible; List<Position> moves = new List<Position>(); int scalar; for (scalar = 1; scalar < Chess.Board.BOARD_DIMENSION; scalar++) { possible = new Position(p.Column + scalar * x, p.Row + scalar * y); Position.Placement placement = board.validPosition(piece, possible); switch (placement) { case Position.Placement.VALID: case Position.Placement.CAPTURE: moves.Add(possible); break; case Position.Placement.BLOCKED: case Position.Placement.ILLEGAL: default: break; } if (placementWillTerminateVector(placement)) break; } return moves; }
public void RequestToSetPiece(IntegerVector2 position, IPiece piece) { if (game.CurrentPlayer != player) { logger.Log(new NotYourTurnMessage()); return; } if (piece == null) { logger.Log(new NoPieceSelectedMessage()); return; } IBoard board = game.Board; logger.Log(new SetPieceMessage(player, piece, position)); bool result = board.SetPiece(position, piece); if (!result) { logger.Log(new SetPieceFailureMessage()); } else { player.UseCapturedPiece(piece); game.TurnEnd(); } }
/// <summary> /// Initializes a new instance of the <see cref="PlayerCheckers"/> class. /// </summary> /// <param name="color">The piece color.</param> /// <param name="gameboard">The gameboard.</param> public PlayerCheckers(IPieceColor color, Gameboard gameboard) : base(color) { IPiece newPiece; int offset = 0, startPos = 0; this.gameboard = gameboard; this.moveCounter = 0; this.selectedPiece = null; if (color == IPieceColor.White) { offset = 5; startPos = 1; } else if (color == IPieceColor.Black) { offset = 0; startPos = 0; } for (int j = 0; j < 3; j++) { startPos = (++startPos) % 2; for (int i = startPos; i < 8; i += 2) { newPiece = new Man(color, gameboard, new Position(i, j + offset)); newPiece.GameEvent += new PieceEvent(this.Piece_GameEvent); this.PieceList.Add(newPiece); } } }
public void AddPiece(IPiece piece) { ObjectValidator.CheckIfObjectIsNull(piece, ErrorMessages.NullPieceError); ChefIfPieceExists(piece); this.pieces.Add(piece); //TODO: check piece and color }
public int OnSolve( IPiece<int> sender, EventArgs e, bool solved) { on = solved; return 0; }
public void Add(IPiece piece) { if (piece == null) throw new ArgumentNullException("Piece must not be null!"); else _pieces.Add(piece); }
//http://tetris.wikia.com/wiki/Wall_kick public override bool RotateCounterClockwise(IPiece piece) { // Special case: cannot place piece at starting location. if (!CheckNoConflict(piece)) return false; // Try to rotate IPiece tempPiece = piece.Clone(); tempPiece.RotateCounterClockwise(); if (!CheckNoConflict(tempPiece)) { // Try to move right then rotate tempPiece.CopyFrom(piece); tempPiece.Translate(1, 0); tempPiece.RotateCounterClockwise(); if (!CheckNoConflict(tempPiece)) { // Try to move left then rotate tempPiece.CopyFrom(piece); tempPiece.Translate(-1, 0); tempPiece.RotateCounterClockwise(); if (!CheckNoConflict(tempPiece)) return false; else piece.Translate(-1, 0); } else piece.Translate(1, 0); } // Perform rotation (wall kick translation has been done before if needed) piece.RotateCounterClockwise(); return true; }
public void Capture(IPieceBag pieceBag, IPiece startingPiece, Direction direction) { foreach (var piece in GetCapturablePieces(pieceBag, startingPiece, direction)) { piece.Flip(); } }
public List<ILocation> AllowedMoves(IPiece piece) { if (piece != null) { switch (piece.Type) { case PieceType.pawn: return PawnMoves(piece); case PieceType.rook: return RookMoves(piece); case PieceType.knight: return KnightMoves(piece); case PieceType.bishop: return BishopMoves(piece); case PieceType.queen: return QueenMoves(piece); case PieceType.king: return KingMoves(piece); default: return new List<ILocation>(); } } else return new List<ILocation>(); }
public void MakeMove(Cell i_Source, Cell i_Destination) { Move move = new Move(this, i_Source, i_Destination); if (move.Result) { i_Source.Piece.Position = new Tuple <int, int>(i_Destination.Row, i_Destination.Col); i_Destination.Piece = i_Source.Piece; i_Source.Piece = null; if (move.IsJump) { int inBetweenCol = i_Destination.Col > i_Source.Col ? i_Source.Col + 1 : i_Source.Col - 1; int inBetweenRow = i_Destination.Row > i_Source.Row ? i_Source.Row + 1 : i_Source.Row - 1; IPiece pieceToRemove = m_Board.GetCell(inBetweenRow, inBetweenCol).Piece; if (m_Player1.RemovePiece(pieceToRemove)) { m_Board.GetCell(inBetweenRow, inBetweenCol).Piece = null; } else { m_Player2.RemovePiece(pieceToRemove); m_Board.GetCell(inBetweenRow, inBetweenCol).Piece = null; } } //isKing? CheckToMakeKing(i_Destination.Piece); MoveHaveBeenMade.Invoke(); //is game ended? if (DoesGameEnded()) { GameEnded.Invoke(); return; } //another turn? if (move.IsJump) { if (isAnotherTurn(move)) { if (m_CurrentPlayer is PcPlayer) { (m_CurrentPlayer as PcPlayer).getNextMoveFromPc(this, move); return; } else { //next move from human return; } } } switchCurrentTurn(); } }
/// <summary> /// Sets <paramref name="space"/> IsSelected to true, and adds to <paramref name="board"/> selection. /// </summary> /// <param name="board">The board.</param> /// <param name="piece">The piece to select.</param> /// <remarks>Opposite of <see cref="DeselectSpace"/>.</remarks> public static void SelectPiece(this IBoard board, IPiece piece) { board.SelectedPiece = piece; piece.IsSelected = true; piece.MoveableSpaces = piece.MovementRules.GetLegalMoves(board, piece); piece.MoveableSpaces.SetCanMoveOnSpaces(true); }
protected void someMethodThatNeedsAFallingPiece() { if (fallingPiece == null) { IPiece piece = pieceGenerator.Generate(); fallingPiece = fallingPieceGenerator.generate(piece); } }
public WhitePawnMoveFixture() { _sut = new PawnMove(); _piece = Helper.GetMockedPieceAt(1, 0, PieceColor.White); _pieces = new List <IPiece> { _piece }; }
public DragDropPieceIntoSameStackCommand(IModel model, IPiece piece, int insertionIndex) : base(model) { Debug.Assert(piece.Stack.Pieces.Length > 1 && insertionIndex <= piece.Stack.Pieces.Length); this.piece = piece; indexInStackAfter = (piece.IndexInStackFromBottomToTop < insertionIndex ? insertionIndex - 1 : insertionIndex); stack = piece.Stack; }
bool IsCapturable(IPlayer player, IPiece movingPiece, IPiece targetPiece) { bool canMovingPieceTakePiece = movingPiece.CanTakePiece(); bool isPieceCapturable = targetPiece.IsCapturable(); bool isSameOwner = targetPiece.Owner == player; return(canMovingPieceTakePiece && isPieceCapturable && !isSameOwner); }
public DiagonalMoveFixture() { _sut = new DiagonalMove(); _piece = Helper.GetMockedPieceAt(4, 4, PieceColor.Black); _pieces = new List <IPiece> { _piece }; }
public Move(string startString, string endString, IPiece piece, MoveType type) { Start = new Position(startString); End = new Position(endString); Piece = piece; Type = type; IsAMove = true; }
public KingUserControl(IPiece piece) : this() { _Piece = piece; Figure.Fill = _Piece.Color == Color.White ? new SolidColorBrush(Colors.Khaki) : new SolidColorBrush(Colors.Black); }
private void HandlePromotion(IPiece piece, Position newPosition) { if ((piece.Color == PieceColor.White && newPosition.Rank == 8) || (piece.Color == PieceColor.Black && newPosition.Rank == 1)) { _eventAggregator.GetEvent <PromotePieceEvent>().Publish(piece.CurrentPosition); } }
public void CopyFrom(IPiece piece) { // TODO: test if same type of cell PosX = piece.PosX; PosY = piece.PosY; Orientation = piece.Orientation; Value = piece.Value; }
public CaptureResult(bool isSuccess, IPiece capturer, IPiece captured, IPlayer formerOwner, IntegerVector2 from) { IsSuccess = isSuccess; Capturer = capturer; Captured = captured; FormerOwner = formerOwner; From = from; }
public CapturingPromotionMove(ISquare fromSquare, ISquare toSquare, IPiece movingPiece, IPiece capturedPiece, PieceType promotedToPieceType) { FromSquare = fromSquare; ToSquare = toSquare; MovingPiece = movingPiece; CapturedPiece = capturedPiece; PromotedToPieceType = promotedToPieceType; }
public CapturingMove(ISquare fromSquare, ISquare toSquare, IPiece movingPiece, IPiece capturedPiece, bool captured = true) { Captured = captured; CapturedPiece = capturedPiece; FromSquare = fromSquare; MovingPiece = movingPiece; ToSquare = toSquare; }
public EnPassantMove(ISquare fromSquare, ISquare toSquare, IPiece movingPiece, IPiece capturedPiece, ISquare capturedPieceSquare) { FromSquare = fromSquare; ToSquare = toSquare; MovingPiece = movingPiece; CapturedPiece = capturedPiece; CapturedPieceSquare = capturedPieceSquare; }
public PromotionMove(ISquare fromSquare, ISquare toSquare, IPiece movingPiece, PieceType promotedToPieceType, bool promoted = true) { FromSquare = fromSquare; ToSquare = toSquare; MovingPiece = movingPiece; PromotedToPieceType = promotedToPieceType; Promoted = promoted; }
public void MovePiece(IPiece piece, Cell destinationCell) { var previousPosition = piece.DestinationCell; destinationCell.Piece = (Piece)piece; previousPosition.Piece = null; piece.DestinationCell = destinationCell; }
public ChessPieceMoveIntegrationFixture() { _piece = Helper.GetMockedPieceAt(0, 0, PieceColor.Black); _pieces = new List <IPiece> { _piece }; _sut = new ChessPiece(new Position(), PieceColor.Black, PieceType.Knight); }
public void GivePiece(IPiece piece) { if (piece != null && !pieces.Contains(piece)) { pieces.Add(piece); onPieceStrageCahnged.OnNext(Unit.Default); } }
public BlackPawnMoveFixture() { _sut = new PawnMove(); _piece = Helper.GetMockedPieceAt(1, 7); _pieces = new List <IPiece> { _piece }; }
/// <summary> /// Constructor. /// </summary> /// <param name="before">The before status</param> /// <param name="from">The starting square</param> /// <param name="to">The ending square</param> /// <param name="actor"></param> internal Move(BoardStatus before, int @from, int to, IPiece actor) { this.before = before; after = before;// the after board status is initialized with the before status this.from = from; this.to = to; Actor = actor; }
public static BattleResult DrawResult(IPiece attacker, IPiece attacked) { var drawResult = new BattleResult(); drawResult.LostPieces.Add(attacker); drawResult.LostPieces.Add(attacked); return(drawResult); }
public Move(IPiece piece, Position from, Position to, float heuristic = 0, long evals = 0) { _piece = piece; _from = from; _to = to; _heuristic = heuristic; _evals = evals; }
private IPiece[] CreatePieces(int count, IPlayer owner = null) { var pieces = new IPiece[count]; for (var i = 0; i < count; i++) { pieces[i] = new DefaultPiece(owner); } return pieces; }
public bool CanPlay(IPiece piece) { if (IsOutOfBounds(piece.Position) || IsOccupied(piece.Position)) { return false; } return CapturesOpponentPiecesInAnyDirection(piece); }
/// <summary> /// Initializes a new instance of the <see cref="PieceBase"/> class. /// </summary> /// <param name="piece">The piece.</param> public PieceBase(IPiece piece) : this(piece.Color, null, piece.Position) { if (piece is PieceBase) { this.GameEvent = (piece as PieceBase).GameEvent; this.gameboard = (piece as PieceBase).Gameboard; } }
public void TestInit() { var locationMock = new Mock<ILocation>(); locationMock.SetupGet(m => m.X).Returns(1); locationMock.SetupGet(m => m.Y).Returns(1); _piece = _PIECE_BUILDER .ColorIs(_color) .TypeIs(_type) .LocationIs(locationMock.Object) .Build(); }
public bool MoveIsAllowed(IPiece piece, ILocation target) { if (!Board.IsInBounds(target)) return false; foreach (var l in AllowedMoves(piece)) if (l.X == target.X && l.Y == target.Y) return true; return false; }
public void PerformMove(IPiece piece, ILocation target) { if(MoveIsAllowed(piece, target)) { if(ThrowAtTarget(piece.Color, target)) { _board.Remove(_board.GetPiece(target)); } _board.SetLocation(piece, target); } }
/// <summary> /// Place a piece on the board. Only a VALID placement is acceptable. /// </summary> /// <param name="piece">Piece to be placed</param> /// <returns>True if placement was successful</returns> public Boolean placeOnBoard(IPiece piece) { if (validPosition(piece, piece.Position) != Position.Placement.VALID) { return false; } board[piece.Position.Column, piece.Position.Row] = piece; return true; }
public void Play(IPiece piece) { if (!CanPlay(piece)) { throw new InvalidOperationException($"Invalid move for {piece}"); } pieceBag.Add(piece); foreach (var direction in Direction.All()) { contiguousOpponentPieces.Capture(pieceBag, piece, direction); } }
public override void Apply(IGameState state) { if (state is ChessState) { CapturedPiece = state[EnemyPawn(state as ChessState)]; RemovePiece(CapturedPiece.Pos, state); MovePiece(From, To, state); } else { throw new ArgumentException(); } }
public Image GetImage(IPiece figure) { int row = GetRow(figure.Color); int column = GetColumn(figure.Type); if (_imageArray[row, column] == null) { var bmp = new System.Drawing.Bitmap(_image); var rect = new Rectangle(133 * column , 133 * row, 133, 133); _imageArray[row, column] = bmp.Clone(rect, _image.PixelFormat); } return _imageArray[row, column]; }
/// <summary> /// Initializes a new instance of the <see cref="PlayerChess"/> class. /// </summary> /// <param name="color">The piece color.</param> /// <param name="gameboard">The gameboard.</param> public PlayerChess(IPieceColor color, Gameboard gameboard) : base(color) { IPiece newPiece; int offset = 0; this.gameboard = gameboard; this.selectedPiece = null; if (color == IPieceColor.White) { offset = 7; } else if (color == IPieceColor.Black) { offset = 0; } for (int i = 0; i < 8; i++) { newPiece = new Pawn(color, gameboard, new Position(i, Math.Abs(offset - 1))); newPiece.GameEvent += new PieceEvent(this.Piece_GameEvent); this.PieceList.Add(newPiece); } newPiece = new Rook(color, gameboard, new Position(0, offset)); newPiece.GameEvent += new PieceEvent(this.Piece_GameEvent); this.PieceList.Add(newPiece); newPiece = new Rook(color, gameboard, new Position(7, offset)); newPiece.GameEvent += new PieceEvent(this.Piece_GameEvent); this.PieceList.Add(newPiece); newPiece = new Knight(color, gameboard, new Position(1, offset)); newPiece.GameEvent += new PieceEvent(this.Piece_GameEvent); this.PieceList.Add(newPiece); newPiece = new Knight(color, gameboard, new Position(6, offset)); newPiece.GameEvent += new PieceEvent(this.Piece_GameEvent); this.PieceList.Add(newPiece); newPiece = new Bishop(color, gameboard, new Position(2, offset)); newPiece.GameEvent += new PieceEvent(this.Piece_GameEvent); this.PieceList.Add(newPiece); newPiece = new Bishop(color, gameboard, new Position(5, offset)); newPiece.GameEvent += new PieceEvent(this.Piece_GameEvent); this.PieceList.Add(newPiece); newPiece = new Queen(color, gameboard, new Position(3, offset)); newPiece.GameEvent += new PieceEvent(this.Piece_GameEvent); this.PieceList.Add(newPiece); newPiece = new King(color, gameboard, new Position(4, offset)); newPiece.GameEvent += new PieceEvent(this.Piece_GameEvent); this.PieceList.Add(newPiece); this.King = newPiece; }
private IEnumerable<IPiece> GetContiguousOpponentPieces(IPieceBag pieceBag, IPiece startingPiece, Direction direction) { var piece = GetNextPieceFrom(pieceBag, startingPiece, direction); while (piece != null) { if (piece.Side == startingPiece.Side) { yield break; } yield return piece; piece = GetNextPieceFrom(pieceBag, piece, direction); } }
public void AddPieceToBoard(short x, short y, IPiece piece) { if(piece != null) { if (piece.Player == Player.Dark) { _darkPieces.Add(piece); } else { _lightPieces.Add(piece); } } SetPiece(x, y, piece); }
private IEnumerable<IPiece> GetCapturablePieces(IPieceBag pieceBag, IPiece startingPiece, Direction direction) { var opponents = GetContiguousOpponentPieces(pieceBag, startingPiece, direction) .ToArray(); if (!opponents.Any()) { return Enumerable.Empty<IPiece>(); } var nextPiece = GetNextPieceFrom(pieceBag, opponents.Last(), direction); if (nextPiece == null || nextPiece.Side != startingPiece.Side) { return Enumerable.Empty<IPiece>(); } return opponents; }
public bool MovePiece(IPiece piece, BoardLocation newLocation) { RemovePiece (piece); piece.Location = newLocation; AddPiece (piece); return true; // if (piece.CanPieceMove (newLocation)) // { // RemovePiece (piece); // piece.Location = newLocation; // AddPiece (piece); // return true; // } // else // { // return false; // } }
private void Board_Click(object sender, System.EventArgs e) { IPiece clickedPiece = GetClickedPiece((Button)sender); if (_activePiece == null && clickedPiece != null) { if (clickedPiece.Color == Color.white) { _activePiece = clickedPiece; _activeButton = (Button)sender; } } else if(_activePiece != null) { _moveModule.PerformMove(_activePiece, _buttonHandler.GetButtonLocation((Button)sender)); _activePiece = null; _activeButton = null; } UpdateBoard(); }
public Object GetPresentation(IPiece p) { var piece = p as ChessPiece; if (piece == null) throw new ArgumentException(); var c = (piece.Type == PieceTypes.Pawn) ? 'p' : ((char?)Template.GetPresentation(piece)).Value; switch (p.Player.Order) { case 1: c = Char.ToUpper(c); break; case 2: c = Char.ToLower(c); break; } return c; }
public List<Position> applyMove(Board board, IPiece piece) { Position p = piece.Position; Position possible = new Position(p.Column + x, p.Row + y); List<Position> moves = new List<Position>(); Position.Placement placement = board.validPosition(piece, possible); switch (placement) { case Position.Placement.VALID: case Position.Placement.CAPTURE: moves.Add(possible); break; case Position.Placement.BLOCKED: case Position.Placement.ILLEGAL: default: break; } return moves; }
public IMoveBuilder PieceIs(IPiece piece) { _piece = piece; return this; }
private Move(IPiece piece, ILocation target) { _piece = piece; _target = target; }
/// <summary> /// Initializes a new instance of the <see cref="ChessPiece"/> class. /// </summary> /// <param name="piece">The piece.</param> public ChessPiece(IPiece piece) : base(piece) { }
public SpawnPiece(IPiece piece, Location location) { Piece = piece; Location = location; }
private void SpawnPiece(Rank rank, File file, IPiece piece) { _events.Add(new SpawnPiece(piece, new Location(rank, file))); }