private bool PieceCanMoveInDirection(CheckerPiece piece, MoveDirection direction) { return(piece.IsKing || direction == MoveDirection.ForwardLeft || direction == MoveDirection.ForwardRight ); }
public static CheckerPiece AsKing(int row, int col, PieceColor owner) { var piece = new CheckerPiece(row, col, owner); piece.IsKing = true; return(piece); }
/// <summary> /// Places a piece on the board. Note: This does not /// take in the actual piece object to ensure that we never end up /// with other places being able to modify the pieces on the board. /// </summary> /// <param name="color">color of the piece</param> /// <param name="row">row the piece will be placed</param> /// <param name="col">column the piece will be placed</param> public void AddPiece(PieceColor color, int row, int col, bool isKing = false) { var piece = new CheckerPiece(row, col, color); piece.IsKing = isKing; AddPiece(piece); }
public void MakeMove(Move move) { if (IsValidMove(move)) { _board.RemovePiece(move.Piece); int newRow = move.Piece.Row; int newCol = move.Piece.Col; foreach (MoveDirection direction in move.Direction) { newRow += MoveUtil.GetRowMoveAmountByColor(move.Piece.Owner, direction); newCol += MoveUtil.GetColMoveAmount(direction); CheckerPiece pieceInPosition = _board.GetPiece(newRow, newCol); if (pieceInPosition != null) { _board.RemovePiece(pieceInPosition); newRow += MoveUtil.GetRowMoveAmountByColor(move.Piece.Owner, direction); newCol += MoveUtil.GetColMoveAmount(direction); } } var pieceAfterMove = new CheckerPiece(move.Piece); pieceAfterMove.Row = newRow; pieceAfterMove.Col = newCol; _board.AddPiece(pieceAfterMove); } else { throw new ArgumentException("Invalid Move"); } }
public Move(CheckerPiece piece, MoveDirection direction) { this.Piece = piece; this.Direction = new List <MoveDirection> { direction }; }
public void AddPiece(CheckerPiece piece) { //if (!TileIsInBounds(row, col) || (boardState[row, col] != null)) // throw new ArgumentException(String.Format("Error: Piece was attempted to be placed at invalid position Row: {0}, Col: {1}", row, col)); var copyOfPiece = new CheckerPiece(piece); boardState[copyOfPiece.Row, copyOfPiece.Col] = copyOfPiece; }
public void UpdateDisplay(CheckerBoard board) { for (int row = 0; row < CheckerBoard.SIZE; row++) { for (int col = 0; col < CheckerBoard.SIZE; col++) { CheckerPiece tile = board.GetPiece(row, col); string tileRep = GetTileDisplayRepresentationWithPadding(tile); _display.DisplayText(tileRep); } _display.DisplayText($"{Environment.NewLine}"); } }
private static string GetTileDisplayRepresentationWithPadding(CheckerPiece piece) { if (piece == null) { return("- "); } else if (piece.Owner == PieceColor.Black) { return(piece.IsKing ? "B* " : "B "); } else { return(piece.IsKing ? "W* " : "W "); } }
/// <summary> /// Returns a deep copy /// of the current board /// flipped about the the x-axis. /// </summary> /// <returns></returns> public CheckerBoard FlipBoard() { var reversedBoard = new CheckerBoard(); for (int i = 0; i < SIZE; i++) { int row = SIZE - i - 1; for (int j = 0; j < SIZE; j++) { CheckerPiece pieceToCopy = boardState[row, j]; if (pieceToCopy != null) { reversedBoard.AddPiece(pieceToCopy.Owner, i, j); } } } return(reversedBoard); }
private void StartDrag(MouseEventArgs e) { IsDragging = true; CheckerPiece src; if (e.Source.GetType() == typeof(RedChecker)) { src = (RedChecker)e.Source; } else { src = (BlackChecker)e.Source; } currentPiece = src; DataObject data = new DataObject(System.Windows.DataFormats.Text.ToString(), "abcd"); DragDropEffects de = DragDrop.DoDragDrop(this.grdBoard, data, DragDropEffects.All); IsDragging = false; }
//public bool MakeMove(Move move) //{ //PieceColor thisColor = move.Piece.Owner; //if (!GetLegalMoves(thisColor).Contains(move)) // return false; //var tempBoard = (move.Piece.Owner == PieceColor.White) ? FlipBoard() : boardState; //var opposingColor = Piece.GetOppositeColor(move.Piece.Owner); //int rowMove = GetRowMoveAmount(move.Direction); //int colMove = GetColMoveAmount(move.Direction); //if (TileCanBeJumped(move.Piece.Row + rowMove, move.Piece.Col + colMove, opposingColor, boardState, move.Direction)) //{ // // move piece two times further // // capture opposing piece // // check that piece can't jump again // if (PieceCanJump(move.Piece, opposingColor, boardState)) // { // // get move from player to jump // } //} //else //{ // // move piece //} //return true; //} public List <Move> GetLegalMoves(PieceColor player) { PieceColor opposingColor = CheckerPiece.GetOppositeColor(player); CheckerBoard boardToCheck = this; var legalMoves = new List <Move>(); for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { var piece = boardToCheck.GetPiece(i, j); if ((piece == null) || (piece.Owner == opposingColor)) { continue; } var moves = new BoardMoveGenerator(boardToCheck, piece.Row, piece.Col).Moves; legalMoves.AddRange(moves); } } return(legalMoves); }
public CheckerPiece(CheckerPiece copy) : this(copy.Row, copy.Col, copy.Owner) { IsKing = copy.IsKing; }
/// <summary> /// Here, the source is the drop target which in this case is a Label. This is needed to get /// a reference to the underlying grid cell. That way we know the cell to which to add the new /// image. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void grdBoard_Drop(object sender, DragEventArgs e) { // use the label in the cell to get the current row and column EmptySpace l = e.Source as EmptySpace; int r = Grid.GetRow((EmptySpace)e.Source); int c = Grid.GetColumn((EmptySpace)e.Source); bool okToMove = false; // Because both RedChecker and BlackChecker derive from CheckerPiece, we can use polymorphism // to create the correct piece. Get the correct piece and determine if the move is valid. // A valid move is one row forward to an unoccupied space. CheckerPiece checker; if (currentPiece is RedChecker) { if (currentTurn != Turn.Red) { System.Windows.Forms.MessageBox.Show("It's not your turn"); return; // it's not your turn. } // It's red's turn... checker = new RedChecker(); if (l.row == currentPiece.row + 1 && (l.col == currentPiece.col + 1 || l.col == currentPiece.col - 1)) { okToMove = true; } //now check to see if we captured anything BlackChecker opponentPiece; if (c == currentPiece.col + 2) { opponentPiece = grdBoard.Children.OfType <BlackChecker>().Where(p => p.row == currentPiece.row + 1 && (p.col == currentPiece.col + 1)).SingleOrDefault(); } else { opponentPiece = grdBoard.Children.OfType <BlackChecker>().Where(p => p.row == currentPiece.row + 1 && (p.col == currentPiece.col - 1)).SingleOrDefault(); } if (opponentPiece != null && l.row - currentPiece.row == 2) { int validCol = (opponentPiece.col > currentPiece.col) ? currentPiece.col + 2 : currentPiece.col - 2; if (r == currentPiece.row + 2 && c == validCol) { Storyboard PieceCaptured = opponentPiece.Resources["PieceCaptured"] as Storyboard; capturedPiece = opponentPiece; if (PieceCaptured != null) { PieceCaptured.Completed += new EventHandler(RemovePiece); PieceCaptured.Begin(); } okToMove = true; } } if (okToMove) { currentTurn = Turn.Black; } } else { if (currentTurn != Turn.Black) { System.Windows.Forms.MessageBox.Show("It's not your turn"); return; // it's not your turn. } // It's black's turn... checker = new BlackChecker(); if (l.row == currentPiece.row - 1 && (l.col == currentPiece.col + 1 || l.col == currentPiece.col - 1)) { okToMove = true; } RedChecker opponentPiece = null; if (c == currentPiece.col + 2) { opponentPiece = grdBoard.Children.OfType <RedChecker>().Where(p => p.row == currentPiece.row - 1 && (p.col == currentPiece.col + 1)).SingleOrDefault(); } else if (c == currentPiece.col - 2) { opponentPiece = grdBoard.Children.OfType <RedChecker>().Where(p => p.row == currentPiece.row - 1 && (p.col == currentPiece.col - 1)).SingleOrDefault(); } //FIXME: capturing a piece to the left isn't working if (opponentPiece != null && currentPiece.row - l.row == 2) { int validCol = (opponentPiece.col > currentPiece.col) ? currentPiece.col + 2 : currentPiece.col - 2; if (r == currentPiece.row - 2 && c == validCol) { capturedPiece = opponentPiece; Storyboard PieceCaptured = opponentPiece.Resources["PieceCaptured"] as Storyboard; if (PieceCaptured != null) { PieceCaptured.Completed += new EventHandler(RemovePiece); PieceCaptured.Begin(); } okToMove = true; } } if (okToMove) { currentTurn = Turn.Red; } } if (okToMove) { checker.col = c; checker.row = r; // bind the mouse events checker.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(grdBoard_PreviewMouseLeftButtonDown); checker.PreviewMouseMove += new MouseEventHandler(grdBoard_PreviewMouseMove); checker.Cursor = Cursors.Hand; checker.AllowDrop = false; // add the piece to the board Grid.SetRow(checker, r); Grid.SetColumn(checker, c); this.grdBoard.Children.Remove(currentPiece); grdBoard.Children.Add(checker); Storyboard DropPiece = checker.Resources["DropPiece"] as Storyboard; if (DropPiece != null) { DropPiece.Begin(); } } }
public void RemovePiece(CheckerPiece piece) { RemovePiece(piece.Row, piece.Col); }
public static bool TileIsOpposingColor(this CheckerBoard board, int row, int col, PieceColor opposingColor) { CheckerPiece piece = board.GetPiece(row, col); return((piece != null) && (piece.Owner == opposingColor)); }
public Move(CheckerPiece piece, IEnumerable <MoveDirection> direction) { this.Piece = piece; this.Direction = direction.ToList(); }