public static void WhiteWin(CreeperBoard board) { Position startPosition; Position endPosition; Move move; bool pausePrint = false; for (int i = 0; i < 4; i++) { startPosition = new Position(i, i + 1); endPosition = new Position(i + 1, i + 2); move = new Move(startPosition, endPosition, CreeperColor.Fire); board.Move(move); board.PrintToConsole(pausePrint); } for (int i = 0; i < 3; i++) { startPosition = new Position(i, i + 2); endPosition = new Position(i + 1, i + 3); move = new Move(startPosition, endPosition, CreeperColor.Fire); board.Move(move); board.PrintToConsole(pausePrint); } startPosition = new Position(4, 6); endPosition = new Position(5, 5); move = new Move(startPosition, endPosition, CreeperColor.Fire); board.Move(move); board.PrintToConsole(pausePrint); startPosition = new Position(4, 5); endPosition = new Position(5, 4); move = new Move(startPosition, endPosition, CreeperColor.Fire); board.Move(move); board.PrintToConsole(pausePrint); if(board.GetGameState(CreeperColor.Fire) == CreeperGameState.Complete) { Console.WriteLine("White wins!"); } }
public void PushMove(Move move) { MoveHistory.Push(move); ((move.PlayerColor == CreeperColor.Ice) ? BlackPegs : WhitePegs).Remove(PegBoard[move.StartPosition.Row, move.StartPosition.Column]); PegBoard[move.StartPosition.Row, move.StartPosition.Column].Color = CreeperColor.Empty; PegBoard[move.EndPosition.Row, move.EndPosition.Column].Color = move.PlayerColor; ((move.PlayerColor == CreeperColor.Ice) ? BlackPegs : WhitePegs ).Add(PegBoard[move.EndPosition.Row, move.EndPosition.Column]); if (IsFlipMove(move)) { Flip(move); } else if (IsCaptureMove(move)) { Capture(move); } GameStateHistory.Push(CouldBeFinished(move.PlayerColor) ? GetGameState(move.PlayerColor) : CreeperGameState.Unfinished); }
public bool IsValidMove(Move move) { bool valid = true; //is the move in bounds? if (!IsValidPosition(move.StartPosition.Row, move.StartPosition.Column, PieceType.Peg) || !IsValidPosition(move.EndPosition.Row, move.EndPosition.Column, PieceType.Peg)) { valid = false; } //Does the start space have the player's piece? else if (PegBoard[move.StartPosition.Row, move.StartPosition.Column].Color != move.PlayerColor) { valid = false; } //Is the end space empty? else if (PegBoard[move.EndPosition.Row, move.EndPosition.Column].Color != CreeperColor.Empty) { valid = false; } else { if ((Math.Abs(move.StartPosition.Row - move.EndPosition.Row) > 1) || (Math.Abs(move.StartPosition.Column - move.EndPosition.Column) > 1) || (move.StartPosition.Equals(move.EndPosition))) { valid = false; } if (Math.Abs(Math.Abs(move.StartPosition.Row - move.EndPosition.Row) - Math.Abs(move.StartPosition.Column - move.EndPosition.Column)) == 2 && PegBoard[move.StartPosition.Row + ((move.EndPosition.Row - move.StartPosition.Row) / 2), move.StartPosition.Column + ((move.EndPosition.Column - move.StartPosition.Column) / 2)].Color == move.PlayerColor.Opposite()) { valid = true; } else if (move.PlayerColor == CreeperColor.Empty || move.PlayerColor == CreeperColor.Invalid) { throw new ArgumentOutOfRangeException(move.ToString()); } } return valid; }
public bool IsFlipMove(Move move) { return Math.Abs(move.StartPosition.Row - move.EndPosition.Row) * Math.Abs(move.StartPosition.Column - move.EndPosition.Column) == 1; }
public bool IsCaptureMove(Move move) { return (Math.Abs(move.StartPosition.Row - move.EndPosition.Row) == 2) != (Math.Abs(move.StartPosition.Column - move.EndPosition.Column) == 2); }
public AIBoardNode GetFlippedTileCopy(Move move) { return new AIBoardNode(GetFlippedTile(move)); }
public static Position GetFlippedPosition(Move move) { CardinalDirection direction = move.EndPosition.Row < move.StartPosition.Row ? (move.EndPosition.Column < move.StartPosition.Column ? CardinalDirection.Northwest : CardinalDirection.Northeast) : (move.EndPosition.Column < move.StartPosition.Column ? CardinalDirection.Southwest : CardinalDirection.Southeast); switch (direction) { case CardinalDirection.Northwest: return new Position(move.EndPosition.Row, move.EndPosition.Column); case CardinalDirection.Northeast: return new Position(move.EndPosition.Row, move.EndPosition.Column - 1); case CardinalDirection.Southwest: return new Position(move.EndPosition.Row - 1, move.EndPosition.Column); case CardinalDirection.Southeast: return new Position(move.StartPosition.Row, move.StartPosition.Column); default: throw new ArgumentException("No flipped tile for this move."); } }
private void UnCapture(Move move) { if (move.StartPosition.Row + 2 == move.EndPosition.Row && move.StartPosition.Column == move.EndPosition.Column) { PegBoard[move.StartPosition.Row + 1, move.StartPosition.Column].Color = move.PlayerColor.Opposite(); ((move.PlayerColor == CreeperColor.Ice) ? WhitePegs : BlackPegs).Add(PegBoard[move.StartPosition.Row + 1, move.StartPosition.Column]); } else if (move.StartPosition.Row - 2 == move.EndPosition.Row && move.StartPosition.Column == move.EndPosition.Column) { PegBoard[move.StartPosition.Row - 1, move.StartPosition.Column].Color = move.PlayerColor.Opposite(); ((move.PlayerColor == CreeperColor.Ice) ? WhitePegs : BlackPegs).Add(PegBoard[move.StartPosition.Row - 1, move.StartPosition.Column]); } else if (move.StartPosition.Row == move.EndPosition.Row && move.StartPosition.Column + 2 == move.EndPosition.Column) { PegBoard[move.StartPosition.Row, move.StartPosition.Column + 1].Color = move.PlayerColor.Opposite(); ((move.PlayerColor == CreeperColor.Ice) ? WhitePegs : BlackPegs).Add(PegBoard[move.StartPosition.Row, move.StartPosition.Column + 1]); } else if (move.StartPosition.Row == move.EndPosition.Row && move.StartPosition.Column - 2 == move.EndPosition.Column) { PegBoard[move.StartPosition.Row, move.StartPosition.Column - 1].Color = move.PlayerColor.Opposite(); ((move.PlayerColor == CreeperColor.Ice) ? WhitePegs : BlackPegs).Add(PegBoard[move.StartPosition.Row, move.StartPosition.Column - 1]); } }
private void Flip(Move move) { Piece tile = GetFlippedTile(move); if (!IsCorner(tile)) { tile.Color = move.PlayerColor; } }
private Piece GetFlippedTile(Move move) { return Tiles.At(GetFlippedPosition(move)); }
private void Capture(Move move) { Pegs.At(GetCapturedPegPosition(move)).Color = CreeperColor.Empty; }
public bool Move(Move move) { bool isValid = false; if (IsValidMove(move)) { isValid = true; Pegs.First(x => x.Position.Equals(move.StartPosition)).Color = CreeperColor.Empty; Pegs.First(x => x.Position.Equals(move.EndPosition)).Color = move.PlayerColor; if (IsFlipMove(move)) { Flip(move); } else if (IsCaptureMove(move)) { Capture(move); } } return isValid; }
public bool IsValidMove(Move move) { return Pegs.At(move.StartPosition).PossibleMoves(this).Any(x => x.EndPosition == move.EndPosition && x.PlayerColor == move.PlayerColor); }
private void Flip(Move move) { AIBoardNode tile = GetFlippedTile(move); if (tile.Color != CreeperColor.Invalid) { TileHistory.Push(tile.Color); if (tile.Color == CreeperColor.Empty) { tile.Color = move.PlayerColor; AddTileToTeam(tile); } else if (tile.Color != move.PlayerColor) { RemoveTileFromTeam(tile); tile.Color = move.PlayerColor; AddTileToTeam(tile); } UpdateListHeads(tile.Row, tile.Column, tile.Color); } }
public Move(Move move) { this.StartPosition = new Position(move.StartPosition); this.EndPosition = new Position(move.EndPosition); this.PlayerColor = move.PlayerColor; }
private AIBoardNode GetFlippedTile(Move move) { CardinalDirection direction = move.EndPosition.Row < move.StartPosition.Row ? (move.EndPosition.Column < move.StartPosition.Column ? CardinalDirection.Northwest : CardinalDirection.Northeast) : (move.EndPosition.Column < move.StartPosition.Column ? CardinalDirection.Southwest : CardinalDirection.Southeast); switch (direction) { case CardinalDirection.Northwest: return TileBoard[move.EndPosition.Row, move.EndPosition.Column]; case CardinalDirection.Northeast: return TileBoard[move.EndPosition.Row, move.EndPosition.Column - 1]; case CardinalDirection.Southwest: return TileBoard[move.EndPosition.Row - 1, move.EndPosition.Column]; case CardinalDirection.Southeast: return TileBoard[move.StartPosition.Row, move.StartPosition.Column]; default: throw new ArgumentOutOfRangeException(move.ToString() + " must be a flipping move!"); } }
public Move[] AllPossibleMoves(CreeperColor color) { if (color == CreeperColor.Invalid || color == CreeperColor.Empty) throw new ArgumentOutOfRangeException(color.ToString()); Position startPosition = new Position(); Move[] possibleMoves = new Move[100]; int index = 0; foreach (AIBoardNode peg in (color == CreeperColor.Ice) ? BlackPegs : WhitePegs) { startPosition = new Position(peg.Row, peg.Column); Move[] movesForPeg = new Move[] { new Move(startPosition, new Position(peg.Row + 1, peg.Column), color), new Move(startPosition, new Position(peg.Row - 1, peg.Column), color), new Move(startPosition, new Position(peg.Row, peg.Column + 1), color), new Move(startPosition, new Position(peg.Row, peg.Column - 1), color), new Move(startPosition, new Position(peg.Row + 1, peg.Column + 1), color), new Move(startPosition, new Position(peg.Row + 1, peg.Column - 1), color), new Move(startPosition, new Position(peg.Row - 1, peg.Column + 1), color), new Move(startPosition, new Position(peg.Row - 1, peg.Column - 1), color), new Move(startPosition, new Position(peg.Row + 2, peg.Column), color), new Move(startPosition, new Position(peg.Row - 2, peg.Column), color), new Move(startPosition, new Position(peg.Row, peg.Column + 2), color), new Move(startPosition, new Position(peg.Row, peg.Column - 2), color), }; for (int i = 0; i < movesForPeg.Length; i++) { if (IsValidMove(movesForPeg[i])) { possibleMoves[index++] = movesForPeg[i]; } } } Array.Resize(ref possibleMoves, index); return possibleMoves; }
private void UnFlip(Move move) { AIBoardNode tile = GetFlippedTile(move); if (tile.Color != CreeperColor.Invalid) { CreeperColor color = TileHistory.Pop(); if (color == CreeperColor.Empty) { RemoveTileFromTeam(tile); tile.Color = color; } // Flipping opposite team's tile. else if (color == move.PlayerColor.Opposite()) { RemoveTileFromTeam(tile); tile.Color = color; AddTileToTeam(tile); } UpdateListHeads(tile.Row, tile.Column, move.PlayerColor.Opposite()); } }
public static Position GetCapturedPegPosition(Move move) { foreach (CardinalDirection direction in new[] { CardinalDirection.North, CardinalDirection.South, CardinalDirection.East, CardinalDirection.West }) { if (move.EndPosition == move.StartPosition.AtDirection(direction).AtDirection(direction)) { return move.StartPosition.AtDirection(direction); } } throw new ArgumentException("No captured peg for this move."); }