private CheckersMove IsMoveValidCore(CheckersPiece piece, Point[] path) { // Failsafe .. be sure piece is valid if (piece == null) { throw new ArgumentNullException("piece"); } if (!pieces.Contains(piece)) { throw new ArgumentException("Argument 'piece' must be a piece in play to the current game."); } // Be sure piece can be moved if (!CanMovePiece(piece)) { throw new ArgumentException("Checkers piece cannot be moved on this turn.", "piece"); } CheckersMove move = CheckersMove.FromPath(this, piece, path); if (!move.Moved) { return(null); // No movement } if (move.MustMove) { return(null); // A move must yet be made } // Success return(move); }
/// <summary>Returns whether or not a move is valid.</summary> /// <param name="move">The CheckersMove object to check.</param> /// <returns>True if the move is valid.</returns> public bool IsValidMove(CheckersPiece piece, Point[] path) { if (!isPlaying) { throw new InvalidOperationException("Operation requires game to be playing."); } return(IsMoveValidCore(piece, path) != null); }
/// <summary>Moves a Checkers piece on the board.</summary> /// <param name="piece">The checkers piece to move.</param> /// <param name="path">The the location for the piece to be moved to, and the path taken to get there.</param> /// <returns>True if the piece was moved successfully.</returns> public bool MovePiece(CheckersPiece piece, Point[] path) { if (isReadOnly) { throw new InvalidOperationException("Game is read only."); } if (!isPlaying) { throw new InvalidOperationException("Operation requires game to be playing."); } // Check for valid move CheckersMove move = IsMoveValidCore(piece, path); // Remove jumped pieces foreach (CheckersPiece jumped in move.Jumped) { if (board[jumped.Location.X, jumped.Location.Y] == jumped) { board[jumped.Location.X, jumped.Location.Y] = null; } pieces.Remove(jumped); jumped.RemovedFromPlay(); } // Move the piece on board board[piece.Location.X, piece.Location.Y] = null; board[move.CurrentLocation.X, move.CurrentLocation.Y] = piece; piece.Moved(move.CurrentLocation); // King a pawn if reached other end of board if (move.Kinged) { piece.Promoted(); } // Remember last move lastMove = move; // Update player's turn int prevTurn = turn; if (++turn > PlayerCount) { turn = 1; } // Check for win by removal of opponent's pieces or by no turns available this turn if ((EnumPlayerPieces(prevTurn).Length == 0) || (EnumMovablePieces().Length == 0)) { DeclareWinner(prevTurn); } else if (TurnChanged != null) { TurnChanged(this, EventArgs.Empty); } return(true); }
/// <summary>Creates a Checkers move object from a given path.</summary> /// <param name="game">The Checkers game to create the movement to.</param> /// <param name="piece">The Checkers piece which will be moving.</param> /// <param name="path">The path to move along.</param> /// <returns>The new Checkers move object.</returns> internal static CheckersMove FromPath(CheckersGame game, CheckersPiece piece, Point[] path) { CheckersMove move = new CheckersMove(game, piece, true); foreach (Point p in path) { if (move.Move(p) == false) { return(null); } } return(move); }
// Will be create indirectly from a CheckersGame object via BeginMove internal CheckersMove(CheckersGame game, CheckersPiece piece, bool makeReadOnlyCopy) { this.game = game; this.piece = piece; initialGame = (((makeReadOnlyCopy) && (!game.IsReadOnly)) ? (CheckersGame.MakeReadOnly(game)) : (game)); initialPiece = initialGame.Pieces[Array.IndexOf(game.Pieces, piece)]; board = (CheckersPiece[, ])game.Board.Clone(); currentLocation = piece.Location; jumped = new ArrayList(); path = new ArrayList(); cannotMove = false; kinged = false; }
/// <summary>Begins the checkers game.</summary> public void Play() { if (isReadOnly) { throw new InvalidOperationException("Game is read only."); } if (isPlaying) { throw new InvalidOperationException("Game has already started."); } Stop(); isPlaying = true; for (int y = BoardSize.Height - 1; y >= 5; y--) { for (int x = 0; x < BoardSize.Width; x++) { if ((x % 2) == (y % 2)) { continue; } CheckersPiece piece = new CheckersPiece(this, 1, CheckersRank.Pawn, new Point(x, y), true); board[x, y] = piece; pieces.Add(piece); } } for (int y = 0; y < 3; y++) { for (int x = 0; x < BoardSize.Width; x++) { if ((x % 2) == (y % 2)) { continue; } CheckersPiece piece = new CheckersPiece(this, 2, CheckersRank.Pawn, new Point(x, y), true); board[x, y] = piece; pieces.Add(piece); } } // Set player's turn turn = firstMove; lastMove = null; if (GameStarted != null) { GameStarted(this, EventArgs.Empty); } }
/// <summary>Returns whether or not the checkers piece can be moved this turn.</summary> /// <param name="piece">The checkers piece to test.</param> /// <returns>True when piece can be moved.</returns> public bool CanMovePiece(CheckersPiece piece) { if ((!isPlaying) && (winner == 0)) { throw new InvalidOperationException("Operation requires game to be playing."); } if (piece == null) { throw new ArgumentNullException("piece"); } if (!pieces.Contains(piece)) { throw new ArgumentException("Argument 'piece' must be a piece in play to the current game."); } return(piece.Player == turn); }
private ArrayList EnumJumpMovesCore(CheckersPiece piece, Point fromLocation, out ArrayList jumped) { ArrayList moves = new ArrayList(); jumped = new ArrayList(); // Append jumps (not of same team) if ((piece.Direction == CheckersDirection.Up) || (piece.Rank == CheckersRank.King)) { if (game.InBounds(fromLocation.X - 1, fromLocation.Y - 1) && (board[fromLocation.X - 1, fromLocation.Y - 1] != null) && (board[fromLocation.X - 1, fromLocation.Y - 1].Player != piece.Player)) { if (InBounds(fromLocation.X - 2, fromLocation.Y - 2) && (board[fromLocation.X - 2, fromLocation.Y - 2] == null)) { moves.Add(new Point(fromLocation.X - 2, fromLocation.Y - 2)); jumped.Add(board[fromLocation.X - 1, fromLocation.Y - 1]); } } if (InBounds(fromLocation.X + 1, fromLocation.Y - 1) && (board[fromLocation.X + 1, fromLocation.Y - 1] != null) && (board[fromLocation.X + 1, fromLocation.Y - 1].Player != piece.Player)) { if (InBounds(fromLocation.X + 2, fromLocation.Y - 2) && (board[fromLocation.X + 2, fromLocation.Y - 2] == null)) { moves.Add(new Point(fromLocation.X + 2, fromLocation.Y - 2)); jumped.Add(board[fromLocation.X + 1, fromLocation.Y - 1]); } } } if ((piece.Direction == CheckersDirection.Down) || (piece.Rank == CheckersRank.King)) { if (InBounds(fromLocation.X - 1, fromLocation.Y + 1) && (board[fromLocation.X - 1, fromLocation.Y + 1] != null) && (board[fromLocation.X - 1, fromLocation.Y + 1].Player != piece.Player)) { if (InBounds(fromLocation.X - 2, fromLocation.Y + 2) && (board[fromLocation.X - 2, fromLocation.Y + 2] == null)) { moves.Add(new Point(fromLocation.X - 2, fromLocation.Y + 2)); jumped.Add(board[fromLocation.X - 1, fromLocation.Y + 1]); } } if (InBounds(fromLocation.X + 1, fromLocation.Y + 1) && (board[fromLocation.X + 1, fromLocation.Y + 1] != null) && (board[fromLocation.X + 1, fromLocation.Y + 1].Player != piece.Player)) { if (InBounds(fromLocation.X + 2, fromLocation.Y + 2) && (board[fromLocation.X + 2, fromLocation.Y + 2] == null)) { moves.Add(new Point(fromLocation.X + 2, fromLocation.Y + 2)); jumped.Add(board[fromLocation.X + 1, fromLocation.Y + 1]); } } } return(moves); }
/// <summary>Creates a duplicate Checkers piece object from an identical (possibly cloned) game.</summary> /// <returns>The new Checkers piece object.</returns> public CheckersPiece Clone(CheckersGame game) { CheckersPiece clonedPiece = game.PieceAt(Location); // Make sure piece exists if (clonedPiece == null) { return(null); } // Make sure piece is equivalent if ((clonedPiece.Player != Player) || (clonedPiece.InPlay != InPlay) || (clonedPiece.Rank != Rank)) { return(null); } // Return cloned piece return(clonedPiece); }
/// <summary>Creates a Checkers game from supplied game parameters.</summary> /// <param name="optionalJumping">The Optional Jumping rule.</param> /// <param name="board">The Checkers board that makes up the game.</param> /// <param name="turn">Whose turn it is.</param> /// <param name="winner">The winner, or 0 if none yet.</param> /// <returns>The Checkers game.</returns> public static CheckersGame Create(bool optionalJumping, CheckersPiece[,] board, int turn, int winner) { if ((board.GetLength(0) != BoardSize.Width) || (board.GetLength(1) != BoardSize.Height)) { throw new ArgumentOutOfRangeException("board", board, "Board's dimensions must be " + BoardSize.Width + "x" + BoardSize.Height); } CheckersGame game = new CheckersGame(optionalJumping); game.board = new CheckersPiece[BoardSize.Width, BoardSize.Height]; game.pieces = new CheckersPieceCollection(); for (int y = 0; y < BoardSize.Height; y++) { for (int x = 0; x < BoardSize.Width; x++) { CheckersPiece piece = board[x, y]; if (piece == null) { continue; } if (piece.Owner != null) { throw new ArgumentOutOfRangeException("board", board, "Board contains a Checkers piece that belongs to another Checkers game."); } if (!piece.InPlay) { throw new ArgumentOutOfRangeException("board", board, "Board contains a Checkers piece that is not in play."); } if ((piece.Location.X != x) || (piece.Location.Y != y)) { throw new ArgumentOutOfRangeException("board", board, "Board contains a Checkers piece that does not match up with it's board location."); } if ((piece.Player != 1) || (piece.Player != 2)) { throw new ArgumentOutOfRangeException("board", board, "Board contains a Checkers piece that is not associated with a valid player."); } game.pieces.Add(new CheckersPiece(game, piece.Player, piece.Rank, piece.Location, piece.InPlay)); } } game.isPlaying = true; game.turn = turn; game.winner = winner; return(game); }
/// <summary>Begins a move by creating a CheckersMove object to assist in generating a valid movement.</summary> /// <param name="piece">The Checkers piece to be moved.</param> /// <returns>The CheckersMove object.</returns> public CheckersMove BeginMove(CheckersPiece piece) { if ((!isPlaying) && (winner == 0)) { throw new InvalidOperationException("Operation requires game to be playing."); } // Failsafe .. be sure piece is valid if (piece == null) { throw new ArgumentNullException("piece"); } if (!pieces.Contains(piece)) { throw new ArgumentException("Argument 'piece' must be a piece in play to the current game."); } // Be sure piece can be moved if (!CanMovePiece(piece)) { throw new ArgumentException("Checkers piece cannot be moved on this turn.", "piece"); } return(new CheckersMove(this, piece, true)); }
/// <summary>Creates a duplicate Checkers game object.</summary> /// <returns>The new Checkers move object.</returns> public CheckersGame Clone() { CheckersGame game = new CheckersGame(optionalJumping); game.isReadOnly = isReadOnly; game.isPlaying = isPlaying; game.firstMove = firstMove; game.turn = turn; game.winner = winner; game.pieces = new CheckersPieceCollection(); game.board = new CheckersPiece[BoardSize.Width, BoardSize.Height]; foreach (CheckersPiece piece in pieces) { CheckersPiece newPiece = new CheckersPiece(game, piece.Player, piece.Rank, piece.Location, piece.InPlay); game.board[newPiece.Location.X, newPiece.Location.Y] = newPiece; game.pieces.Add(newPiece); } int lastMovePieceIndex = ((lastMove != null) ? (pieces.IndexOf(lastMove.Piece)) : (-1)); game.lastMove = ((lastMovePieceIndex != -1) ? (CheckersMove.FromPath(game, game.pieces[lastMovePieceIndex], lastMove.Path)) : (null)); return(game); }
private CheckersMove IsMoveValidCore(CheckersPiece piece, Point[] path) { // Failsafe .. be sure piece is valid if(piece == null) throw new ArgumentNullException("piece"); if(!pieces.Contains(piece)) throw new ArgumentException("Argument 'piece' must be a piece in play to the current game."); // Be sure piece can be moved if(!CanMovePiece(piece)) throw new ArgumentException("Checkers piece cannot be moved on this turn.", "piece"); CheckersMove move = CheckersMove.FromPath(this, piece, path); if(!move.Moved) return null; // No movement if(move.MustMove) return null; // A move must yet be made // Success return move; }
/// <summary>Begins the checkers game.</summary> public void Play() { if(isReadOnly) throw new InvalidOperationException("Game is read only."); if(isPlaying) throw new InvalidOperationException("Game has already started."); Stop(); isPlaying = true; for(int y = BoardSize.Height - 1; y >= 5; y--) { for(int x = 0; x < BoardSize.Width; x++) { if((x % 2) == (y % 2)) continue; CheckersPiece piece = new CheckersPiece(this, 1, CheckersRank.Pawn, new Point(x, y), true); board[x, y] = piece; pieces.Add(piece); } } for(int y = 0; y < 3; y++) { for(int x = 0; x < BoardSize.Width; x++) { if((x % 2) == (y % 2)) continue; CheckersPiece piece = new CheckersPiece(this, 2, CheckersRank.Pawn, new Point(x, y), true); board[x, y] = piece; pieces.Add(piece); } } // Set player's turn turn = firstMove; lastMove = null; if(GameStarted != null) GameStarted(this, EventArgs.Empty); }
/// <summary>Moves a Checkers piece on the board.</summary> /// <param name="piece">The checkers piece to move.</param> /// <param name="path">The the location for the piece to be moved to, and the path taken to get there.</param> /// <returns>True if the piece was moved successfully.</returns> public bool MovePiece(CheckersPiece piece, Point[] path) { if(isReadOnly) throw new InvalidOperationException("Game is read only."); if(!isPlaying) throw new InvalidOperationException("Operation requires game to be playing."); // Check for valid move CheckersMove move = IsMoveValidCore(piece, path); // Remove jumped pieces foreach(CheckersPiece jumped in move.Jumped) { if(board[jumped.Location.X, jumped.Location.Y] == jumped) board[jumped.Location.X, jumped.Location.Y] = null; pieces.Remove(jumped); jumped.RemovedFromPlay(); } // Move the piece on board board[piece.Location.X, piece.Location.Y] = null; board[move.CurrentLocation.X, move.CurrentLocation.Y] = piece; piece.Moved(move.CurrentLocation); // King a pawn if reached other end of board if(move.Kinged) piece.Promoted(); // Remember last move lastMove = move; // Update player's turn int prevTurn = turn; if(++turn > PlayerCount) turn = 1; // Check for win by removal of opponent's pieces or by no turns available this turn if((EnumPlayerPieces(prevTurn).Length == 0) || (EnumMovablePieces().Length == 0)) DeclareWinner(prevTurn); else if(TurnChanged != null) TurnChanged(this, EventArgs.Empty); return true; }
/// <summary>Returns whether or not a move is valid.</summary> /// <param name="move">The CheckersMove object to check.</param> /// <returns>True if the move is valid.</returns> public bool IsValidMove(CheckersPiece piece, Point[] path) { if(!isPlaying) throw new InvalidOperationException("Operation requires game to be playing."); return (IsMoveValidCore(piece, path) != null); }
/// <summary>Creates a duplicate Checkers game object.</summary> /// <returns>The new Checkers move object.</returns> public CheckersGame Clone() { CheckersGame game = new CheckersGame(optionalJumping); game.isReadOnly = isReadOnly; game.isPlaying = isPlaying; game.firstMove = firstMove; game.turn = turn; game.winner = winner; game.pieces = new CheckersPieceCollection(); game.board = new CheckersPiece[BoardSize.Width, BoardSize.Height]; foreach(CheckersPiece piece in pieces) { CheckersPiece newPiece = new CheckersPiece(game, piece.Player, piece.Rank, piece.Location, piece.InPlay); game.board[newPiece.Location.X, newPiece.Location.Y] = newPiece; game.pieces.Add(newPiece); } int lastMovePieceIndex = ((lastMove != null) ? (pieces.IndexOf(lastMove.Piece)) : (-1)); game.lastMove = ((lastMovePieceIndex != -1) ? (CheckersMove.FromPath(game, game.pieces[lastMovePieceIndex], lastMove.Path)) : (null)); return game; }
public void Remove(CheckersPiece item) { InnerList.Remove(item); }
public bool Contains(CheckersPiece item) { return(InnerList.Contains(item)); }
/// <summary>Creates a Checkers game from supplied game parameters.</summary> /// <param name="optionalJumping">The Optional Jumping rule.</param> /// <param name="board">The Checkers board that makes up the game.</param> /// <param name="turn">Whose turn it is.</param> /// <param name="winner">The winner, or 0 if none yet.</param> /// <returns>The Checkers game.</returns> public static CheckersGame Create(bool optionalJumping, CheckersPiece[,] board, int turn, int winner) { if((board.GetLength(0) != BoardSize.Width) || (board.GetLength(1) != BoardSize.Height)) throw new ArgumentOutOfRangeException("board", board, "Board's dimensions must be " + BoardSize.Width + "x" + BoardSize.Height); CheckersGame game = new CheckersGame(optionalJumping); game.board = new CheckersPiece[BoardSize.Width, BoardSize.Height]; game.pieces = new CheckersPieceCollection(); for(int y = 0; y < BoardSize.Height; y++) for(int x = 0; x < BoardSize.Width; x++) { CheckersPiece piece = board[x, y]; if(piece == null) continue; if(piece.Owner != null) throw new ArgumentOutOfRangeException("board", board, "Board contains a Checkers piece that belongs to another Checkers game."); if(!piece.InPlay) throw new ArgumentOutOfRangeException("board", board, "Board contains a Checkers piece that is not in play."); if((piece.Location.X != x) || (piece.Location.Y != y)) throw new ArgumentOutOfRangeException("board", board, "Board contains a Checkers piece that does not match up with it's board location."); if((piece.Player != 1) || (piece.Player != 2)) throw new ArgumentOutOfRangeException("board", board, "Board contains a Checkers piece that is not associated with a valid player."); game.pieces.Add(new CheckersPiece(game, piece.Player, piece.Rank, piece.Location, piece.InPlay)); } game.isPlaying = true; game.turn = turn; game.winner = winner; return game; }
public void Insert(int index, CheckersPiece item) { InnerList.Insert(index, item); }
public int Add(CheckersPiece item) { return(InnerList.Add(item)); }
public int Add(CheckersPiece item) { return InnerList.Add(item); }
public void AddRange(CheckersPiece[] items) { foreach(CheckersPiece item in items) Add(item); }
public int IndexOf(CheckersPiece item) { return InnerList.IndexOf(item); }
public int IndexOf(CheckersPiece item) { return(InnerList.IndexOf(item)); }
public void CopyTo(CheckersPiece[] array, int index) { InnerList.CopyTo(array, index); }
/// <summary>Begins a move by creating a CheckersMove object to assist in generating a valid movement.</summary> /// <param name="piece">The Checkers piece to be moved.</param> /// <returns>The CheckersMove object.</returns> public CheckersMove BeginMove(CheckersPiece piece) { if((!isPlaying) && (winner == 0)) throw new InvalidOperationException("Operation requires game to be playing."); // Failsafe .. be sure piece is valid if(piece == null) throw new ArgumentNullException("piece"); if(!pieces.Contains(piece)) throw new ArgumentException("Argument 'piece' must be a piece in play to the current game."); // Be sure piece can be moved if(!CanMovePiece(piece)) throw new ArgumentException("Checkers piece cannot be moved on this turn.", "piece"); return new CheckersMove(this, piece, true); }
/// <summary>Returns whether or not the checkers piece can be moved this turn.</summary> /// <param name="piece">The checkers piece to test.</param> /// <returns>True when piece can be moved.</returns> public bool CanMovePiece(CheckersPiece piece) { if((!isPlaying) && (winner == 0)) throw new InvalidOperationException("Operation requires game to be playing."); if(piece == null) throw new ArgumentNullException("piece"); if(!pieces.Contains(piece)) throw new ArgumentException("Argument 'piece' must be a piece in play to the current game."); return (piece.Player == turn); }
public bool Contains(CheckersPiece item) { return InnerList.Contains(item); }
/// <summary>Creates a Checkers game from supplied game parameters.</summary> /// <param name="optionalJumping">The Optional Jumping rule.</param> /// <param name="board">The Checkers board that makes up the game.</param> /// <param name="turn">Whose turn it is.</param> /// <returns>The Checkers game.</returns> public static CheckersGame Create(bool optionalJumping, CheckersPiece[,] board, int turn) { return Create(optionalJumping, board, turn, 0); }
/// <summary> /// Checks for a message from the opponent. /// </summary> private void CheckForClientMessage() { if (inCheckForClientMessage) { return; } inCheckForClientMessage = true; if (remotePlayer == null) { return; } try { NetworkStream ns = new NetworkStream(remotePlayer.Socket, false); BinaryReader br = new BinaryReader(ns); BinaryWriter bw = new BinaryWriter(ns); while (ns.DataAvailable) { switch ((ClientMessage)br.ReadByte()) { case ClientMessage.Closed: throw new IOException(); case ClientMessage.ChatMessage: AppendMessage(lblNameP2.Text, br.ReadString()); if (settings.FlashWindowOnGameEvents) { DoFlashWindow(); } break; case ClientMessage.AbortGame: AppendMessage("", "Game has been aborted by opponent"); CloseNetGame(); break; case ClientMessage.MakeMove: if (CheckersUI.Game.Turn != 2) { AppendMessage("", "Opponent took turn out of place; game aborted"); CloseNetGame(); bw.Write((byte)ClientMessage.AbortGame); break; } // Get move Point location = RotateOpponentPiece(br); CheckersPiece piece = CheckersUI.Game.PieceAt(location); int count = br.ReadInt32(); Point[] path = new Point[count]; for (int i = 0; i < count; i++) { path[i] = RotateOpponentPiece(br); } // Move the piece an break if successful if (piece != null) { if (CheckersUI.MovePiece(piece, path, true, true)) { if (settings.FlashWindowOnTurn) { DoFlashWindow(); } break; } } AppendMessage("", "Opponent made a bad move; game aborted"); CloseNetGame(); bw.Write((byte)ClientMessage.AbortGame); break; } } br.Close(); bw.Close(); } catch (IOException) { AppendMessage("", "Connection closed"); CloseNetGame(); remotePlayer = null; } catch (SocketException ex) { AppendMessage("", "Disconnected from opponent: " + ex.Message); CloseNetGame(); remotePlayer = null; } catch (InvalidOperationException ex) { AppendMessage("", "Disconnected from opponent: " + ex.Message); CloseNetGame(); remotePlayer = null; } inCheckForClientMessage = false; }