private bool PiecesCanBeJumped() { // check the entire board for pieces that can be jumped for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { // grab the curently selected board space BoardSpace CurrSpace = m_Spaces[i, j]; // if the piece is not the current player's piece, disregard it if (CurrSpace.Owner != m_CurrPlayerTurn || CurrSpace.IsEmpty) { continue; } // if there is a jump available for this piece, then return true if (JumpAvailable(CurrSpace)) { return(true); } } } // if we fall through the loop without return true, then there is no way return(false); }
protected override void OnMouseDown(MouseEventArgs e) { if (e.Button == MouseButtons.Left) { m_mouseDownIn = GetSpaceFromPoint(e.X, e.Y); } base.OnMouseDown(e); }
protected override void OnMouseUp(MouseEventArgs e) { if (e.Button == MouseButtons.Left && m_mouseDownIn != null) { BoardSpace spaceMouseUp = GetSpaceFromPoint(e.X, e.Y); if (spaceMouseUp == m_mouseDownIn) { SpaceClick(m_mouseDownIn); this.Refresh(); } m_mouseDownIn = null; } base.OnMouseUp(e); }
public bool Move(BoardSpace sourceSpace, BoardSpace destinationSpace) { try { //Make sure this is moving a distance of 1 only. if (Math.Abs(sourceSpace.XPosition - destinationSpace.XPosition) > 1 || Math.Abs(sourceSpace.YPosition - destinationSpace.YPosition) > 1) throw new Exception("Invalid move! The chip may only move one space."); // Disallow bottom player from moving down if (sourceSpace.ChipInContainer.Color == ChipColor.Black) if (Math.Abs(sourceSpace.YPosition) < (destinationSpace.YPosition)) throw new Exception("Invalid move! You cant move that chip backwards."); // Disallow top player from moving up if (sourceSpace.ChipInContainer.Color == ChipColor.Red) if (Math.Abs(sourceSpace.YPosition) > (destinationSpace.YPosition)) throw new Exception("Invalid move! You can't move that chip backwards."); //We are clear to move here if (destinationSpace.ChipInContainer == null) { //Add the new chip in the new space destinationSpace.ChipInContainer = sourceSpace.ChipInContainer; //Get the old chip out of it's space sourceSpace.ChipInContainer = null; } else { throw new Exception("Invalid move! There is already a chip on this position!"); } return true; } catch (Exception ex) { throw ex; } }
public void SwapPieces(BoardSpace b) { // store the current occupying piece CheckerMarker temp = b.OccupyingPiece; // set the other board spaces piece to this ones b.SetPiece(this.m_occupyingpiece); // set this space's occupying piece to the one // the other board space contained m_occupyingpiece = temp; // if this space can king red pieces and the new piece is a red piece, then king the piece if (!this.Empty() && this.m_ableToKing == CanKing.YesRed && this.m_occupyingpiece.Owner == Player.Red) { this.OccupyingPiece.KingMe(); } else // or if this space can king a black piece and it has a black piece, king that piece if (!this.Empty() && this.m_ableToKing == CanKing.YesBlack && this.m_occupyingpiece.Owner == Player.Black) { this.OccupyingPiece.KingMe(); } else // of if the other space can king black pieces and it has a black piece, king the other space's piece if (!b.Empty() && b.AbleToKing == CanKing.YesBlack && b.OccupyingPiece.Owner == Player.Black) { b.OccupyingPiece.KingMe(); } else // or if the other space has a red piece and it can king red pieces, king the other space's piece if (!b.Empty() && b.AbleToKing == CanKing.YesRed && b.OccupyingPiece.Owner == Player.Red) { b.OccupyingPiece.KingMe(); } }
public ValidMove(BoardSpace moveto, BoardSpace Jumped) { moveTo = moveto; toBeJumped = Jumped; }
private void SpaceClick(BoardSpace WasClicked) { // if the space is a clickable space if (WasClicked.Selectable) { // if there is a double jump if (m_DblJumpExist) { // try and make a move to the space that was clicked if (TryToMakeMove(WasClicked.Row, WasClicked.Col)) { return; } } else { // if there is no piece selected if (!SpaceSelected()) { // if the space is not empty and the piece is the color matches the player who is taking their turn if (!WasClicked.IsEmpty && WasClicked.Owner == m_CurrPlayerTurn) { // select that row and col m_sel_col = WasClicked.Col; m_sel_row = WasClicked.Row; // highlight the selection WasClicked.Highlight(true); // get all the valid moves for this space and highlight them GetValidMoves(); foreach (ValidMove b in CurrentValidMoves) { b.MoveTo.Highlight(true); } } } // otherwise... else { // if the space is empty if (WasClicked.IsEmpty) { // try and make a move if (TryToMakeMove(WasClicked.Row, WasClicked.Col)) { return; } } else { // otherwise, if the row and col match the current selection if (WasClicked.Row == m_sel_row && WasClicked.Col == m_sel_col) { // deselect the piece WasClicked.Highlight(false); m_sel_col = -1; m_sel_row = -1; // unhighlight all the valid moves associated foreach (ValidMove b in CurrentValidMoves) { b.MoveTo.Highlight(false); } CurrentValidMoves.Clear(); } else { // otherwise, if the piece is that of the current players if (WasClicked.Owner == m_CurrPlayerTurn) { // unhighlight the other selected one and unhighlight all its valid moves m_Spaces[m_sel_row, m_sel_col].Highlight(false); foreach (ValidMove b in CurrentValidMoves) { b.MoveTo.Highlight(false); } CurrentValidMoves.Clear(); // select and highlight the new selection, find all its valid moves and highlight those WasClicked.Highlight(true); m_sel_col = WasClicked.Col; m_sel_row = WasClicked.Row; GetValidMoves(); foreach (ValidMove b in CurrentValidMoves) { b.MoveTo.Highlight(true); } } } } } } } }
private List <ValidMove> GetValidMoveList(BoardSpace CurrSelSpace) { List <ValidMove> currValidMoves = new List <ValidMove>(); int start, end; // start and end indices for the array of differences // if the space has a king in it if (CurrSelSpace.HoldingKing) { // go ahead and look at the entire list of differences, they are all valid start = 0; end = 3; } // otherwise else { // if its a red piece, look at the last two items in the array if (CurrSelSpace.Owner == Player.Red) { start = 2; end = 3; } // otherwise else { // look at the first two items of the list because the piece is gray/black start = 0; end = 1; } } // for each of the determined applicable differences for the space for (int i = start; i <= end; i++) { // calculate where the piece would go int new_row = CurrSelSpace.Row + Differences[i].DeltaRow; int new_col = CurrSelSpace.Col + Differences[i].DeltaCol; // if the target space is not off the board if ((new_row >= 0 && new_row <= 7) && (new_col >= 0 && new_col <= 7)) { // if the space is empty (not owned) if (m_Spaces[new_row, new_col].Owner == Player.None) { // if no jumps exist if (!m_JumpsExist) { // add the current space to the list of valid moves currValidMoves.Add(new ValidMove(m_Spaces[new_row, new_col], null)); } } // otherwise else { // if the currently selected space and target space dont have the same color pieces, and the target space does not contain a king // and the currently selected space doesnt have a regular piece if (CurrSelSpace.Owner != m_Spaces[new_row, new_col].Owner && !(m_Spaces[new_row, new_col].HoldingKing && !CurrSelSpace.HoldingKing)) { // the current space can be jumped BoardSpace MayBeJumped = m_Spaces[new_row, new_col]; // calculate the new target space new_row = new_row + Differences[i].DeltaRow; new_col = new_col + Differences[i].DeltaCol; // if the new target space is on the board if ((new_row >= 0 && new_row <= 7) && (new_col >= 0 && new_col <= 7)) { // and it is empty, we have a valid jump. Add it to the list if (m_Spaces[new_row, new_col].Owner == Player.None) { currValidMoves.Add(new ValidMove(m_Spaces[new_row, new_col], MayBeJumped)); } } } } } } return(currValidMoves); }
private bool JumpAvailable(BoardSpace CurrSpace) { int start, end; // start and end indices for the array of differences // if the space has a king in it if (CurrSpace.HoldingKing) { // go ahead and look at the entire list of differences, they are all valid start = 0; end = 3; } // otherwise else { // if its a red piece, look at the last two items in the array if (CurrSpace.Owner == Player.Red) { start = 2; end = 3; } // otherwise else { // look at the first two items of the list because the piece is gray/black start = 0; end = 1; } } // for each of the determined applicable differences for the space for (int k = start; k <= end; k++) { // calculate where the piece would go int new_row = CurrSpace.Row + Differences[k].DeltaRow; int new_col = CurrSpace.Col + Differences[k].DeltaCol; // if the target space is not off the board if ((new_row >= 0 && new_row <= 7) && (new_col >= 0 && new_col <= 7)) { if (m_Spaces[new_row, new_col].Owner == Player.None) { continue; } // if the currently selected space and target space dont have the same color pieces, and the target space does not contain a king // and the currently selected space doesnt have a regular piece if (CurrSpace.Owner != m_Spaces[new_row, new_col].Owner && !(m_Spaces[new_row, new_col].HoldingKing && !CurrSpace.HoldingKing)) { // calculate the new target space new_row = new_row + Differences[k].DeltaRow; new_col = new_col + Differences[k].DeltaCol; // if the new target space is on the board if ((new_row >= 0 && new_row <= 7) && (new_col >= 0 && new_col <= 7)) { // and it is empty, we have a valid jump. Then a jump is possible, return true if (m_Spaces[new_row, new_col].Owner == Player.None) { return(true); } } } } } return(false); }
public void RestartGame(bool isRestart) { // board is currently invalid m_isValid = false; // if the game is not a restart but a fresh instance of the game... if (!isRestart) { // calculate the base increment for the position based on the current size float base_x = (float)base.Width / 8; float base_y = (float)base.Height / 8; // for every space on the board, initalize the array of board spaces for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { // the decider helps determine what selection to make for each space int decider = (i + j) % 2; // initialize the board with its row, col, color, owner, and if its selectable m_Spaces[i, j] = new BoardSpace(i, j, decider == 1 ? Color.DarkGray : Color.Tan, Player.None, (decider == 0)); // if the current row is the first row, it can king red pieces if (i == 0) { m_Spaces[i, j].IsAbleToKing(CanKing.YesRed); } // if the current row is the last row, it can king the black/gray pieces if (i == 7) { m_Spaces[i, j].IsAbleToKing(CanKing.YesBlack); } } } } // need new list of boardspaces for valid moves CurrentValidMoves = new List <ValidMove>(); // black goes first so the current player will be black, "Smoke kills before fire" m_CurrPlayerTurn = Player.Black; // theres no way jumps can exist, the board has been reset m_JumpsExist = false; m_DblJumpExist = false; // each player has 12 pieces m_BlkPieceCount = 12; m_RedPieceCount = 12; // since all spaces are now initialized in the array, the board is now valid and no null references exist m_isValid = true; // for each row on the board for (int i = 0; i < 8; i++) { // assume black owns the space Player temp = Player.Black; // if we are currently at the two middle rows of the board, then there are no space ownders if (i == 3 || i == 4) { temp = Player.None; } // of we are now at the 6th row, we need to change the color of the pieces if (i >= 5) { temp = Player.Red; } // for all slots in the row where pieces are allowed to be, place a marker for (int j = 0; j < 8; j = j + 2) { // offset is based on the row int offset = i % 2; // place a marker on each of these spots m_Spaces[i, (j + offset)].PlaceMarker(temp); } } this.Refresh(); }
public void initEmptySpaces() { for (int i = 0; i < mRows; i++) { for (int j = 0; j < mColumns; j++) { Button btn = new Button(); btn.Click += new EventHandler(chip_Click); //btn.Text = i + "," + j; btn.ForeColor = System.Drawing.Color.DarkBlue; btn.Size = new System.Drawing.Size(mButtonWidth, mButtonHeight); btn.Location = new Point(i * mButtonWidth, j * mButtonHeight); BoardSpace thisBoardSpace = new BoardSpace(i, j); btn.Tag = thisBoardSpace; //If this is the first boardspace, we are instanciating our list for the first time when it's null game.BoardSpaces = game.BoardSpaces == null ? new List<BoardSpace>() : game.BoardSpaces; //Add this BoardSpace to our collection in the game itself game.BoardSpaces.Add(thisBoardSpace); //Add button to the actual panel pnlBoard.Controls.Add(btn); } } }
void chip_Click(object sender, EventArgs e) { Button btn; try { if (sender is Button) { btn = (Button)sender; if (btn.Tag != null) { if (btn.Tag is BoardSpace) { //Get the current space clicked on BoardSpace thisSpace = (BoardSpace)btn.Tag; //If the previous space is null, we have nothing selected if (previousBoardSpace == null) { //Does out new selection have a chip in it? if (thisSpace.ChipInContainer == null) throw new Exception("This board space does not contain a chip to move!"); else { //Our new selection has a chip in it. We set previousBoardSpace to this space (note: this will be a player's previousBoardSpace at the least) previousBoardSpace = thisSpace; //Change the cursor to something else so that the user knows they have a move pending. this.Cursor = Cursors.NoMove2D; } } else { //The previous space is not null, so we must be clicking a destination space now. Does the previous space have a chip in it? It always should now that I added the check, but just to be sure this can't hurt! if (previousBoardSpace.ChipInContainer != null) { //Get the chip in my hand by looking at the previous board space's chip Chip chipInMyHand = previousBoardSpace.ChipInContainer; //Call move function passing the old space and the new space if (chipInMyHand.Move(previousBoardSpace, thisSpace)) { //Set back the cursor, the move was successful! this.Cursor = Cursors.Default; //Call draw chips again. This will draw the entire board based on our lists. drawChips(); //Set previous BoardSpace to null because we are done with it and it has moved. previousBoardSpace = null; } } else throw new Exception("Previously clicked board space does not have a chip in it! Not sure how this happened!"); } } } } } catch (Exception ex) { MessageBox.Show(String.Format("{0}", ex.Message)); } }