コード例 #1
0
        /// <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);
        }
コード例 #2
0
        // Métodos internos
        #region Internos

        /// <summary>Cria um movimento a partir do caminho passado</summary>
        /// <param name="game">Jogo para o qual será criado o movimento</param>
        /// <param name="piece">A peça que será movida</param>
        /// <param name="path">O caminho pelo qual a peça será movida</param>
        /// <returns>O movimento resultante</returns>
        internal static CheckersMove FromPath(CheckersGame game, CheckersPiece piece, Point[] path)
        {
            // Cria um novo movimento
            CheckersMove move = new CheckersMove(game, piece, true);

            // Para cada casa no caminho passado
            foreach (Point p in path)
            {
                // Se não conseguiu mover a peça
                if (move.Move(p) == false)
                {
                    // Retorna null
                    return(null);
                }
            }

            // Se moveu a peça por todas localizações, retorna o movimento
            return(move);
        }
コード例 #3
0
        /// <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);
        }
コード例 #4
0
 /// <summary>
 /// Cria um movimento duplicado para o jogo (provavelmente clonado) passado
 /// </summary>
 /// <returns>Novo movimento gerado</returns>
 public CheckersMove Clone(CheckersGame game)
 {
     return(FromPath(game, this._Piece.Clone(game), Path));
 }