Exemplo n.º 1
0
        public void TestAreMovePositionsPossibleMethod(
            char initialHorizontal,
            int initialVertical,
            char targetHorizontal,
            int targetVertical,
            ChessColors pawnColor,
            bool isValid)
        {
            var arePositionsPossibleMethod = this.PawnType.GetMethod("AreMovePositionsPossible");

            var normalChessMoveType    = ChessGameLogicProvider.GetType("ChessGameLogic.ChessMoves.NormalChessMovePositions");
            var chessBoardPositionType = ChessGameLogicProvider.GetType("ChessGameLogic.ChessBoardPosition");

            var move = normalChessMoveType.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic)[0]
                       .Invoke(new object[]
            {
                initialHorizontal,
                initialVertical,
                targetHorizontal,
                targetVertical,
            });

            bool actualResult;

            if (pawnColor == ChessColors.White)
            {
                actualResult = (bool)arePositionsPossibleMethod.Invoke(this.WhitePawnInstance, new object[] { move });
            }
            else
            {
                actualResult = (bool)arePositionsPossibleMethod.Invoke(this.BlackPawnInstance, new object[] { move });
            }

            Assert.AreEqual(isValid, actualResult);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Obfuscate the board so <c>obfPlayer</c> does not see the enemies pieces,
        /// castling information, or en passant.
        /// </summary>
        /// <param name="obfPlayer">The player to hide the opponents pieces from</param>
        public void Obfuscate(ChessColors obfPlayer)
        {
            for (int i = 0; i < board.Size; i++)
            {
                for (int j = 0; j < board.Size; j++)
                {
                    var piece = board.GetPiece(new BoardPosition(i, j));
                    if (piece != null && piece.Color != obfPlayer)
                    {
                        piece.Promote(PieceTypes.Unknown);
                    }
                }
            }

            if (obfPlayer == ChessColors.White)
            {
                castling.blackKing      = false;
                castling.blackQueen     = false;
                castling.blackLeftRook  = new BoardPosition(-1, -1);
                castling.blackRightRook = new BoardPosition(-1, -1);
            }
            else
            {
                castling.whiteKing      = false;
                castling.whiteQueen     = false;
                castling.whiteLeftRook  = new BoardPosition(-1, -1);
                castling.whiteRightRook = new BoardPosition(-1, -1);
            }

            enPassant = null;
        }
Exemplo n.º 3
0
        public void TestIsPositionProducableMethod(
            char positionHorizontal,
            int positionVertical,
            ChessColors pawnColor,
            bool isProducable)
        {
            var isPositionProducableMethod = this.PawnType.GetMethod(
                "IsPositionProducable",
                BindingFlags.Instance | BindingFlags.NonPublic);

            var chessBoardPositionType = ChessGameLogicProvider.GetType("ChessGameLogic.ChessBoardPosition");

            bool isPositionProducableActualResult;

            if (pawnColor == ChessColors.Black)
            {
                isPositionProducableActualResult = (bool)isPositionProducableMethod.Invoke(
                    this.BlackPawnInstance,
                    new object[] { chessBoardPositionType.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic).First().Invoke(new object[] { positionHorizontal, positionVertical }), });
            }
            else
            {
                isPositionProducableActualResult = (bool)isPositionProducableMethod.Invoke(
                    this.WhitePawnInstance,
                    new object[] { chessBoardPositionType.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic).First().Invoke(new object[] { positionHorizontal, positionVertical }), });
            }

            Assert.AreEqual(isProducable, isPositionProducableActualResult);
        }
Exemplo n.º 4
0
 public ChessPiece(ChessPieces name, ChessColors color, Icon icon)
 {
     this.name = name;
     this.pieceColor = color;
    
     this.lastMove = 0;
     image = (new Icon(icon, 48, 48)).ToBitmap();
 }
Exemplo n.º 5
0
 public virtual bool IsValidMove(BoardSquare[,] board, int fromRow, int fromCol, int toRow, int toCol, ChessColors turn, int turnNumber)
 {
     if (!(InRange(fromRow) && InRange(fromCol) && InRange(toRow) && InRange(toCol)))
         return false;
     else if (fromRow == toRow && fromCol == toCol)
         return false;
     else
         return true;
 }
Exemplo n.º 6
0
        private static Tuple <ChessColors, ChessTimeControl, ChessGameVariants, string, string, string> ParseSettings(string settings)
        {
            //!chess new color:{Color}, timemode:{TimeControl}, variant:{GameVariant}, fen:{string}, increment:{int}, time:{double}
            int settingsIndexStart = settings.IndexOf("new", StringComparison.Ordinal) + 4;

            if (settings.Length <= settingsIndexStart)
            {
                return
                    (new Tuple <ChessColors, ChessTimeControl, ChessGameVariants, string, string, string>(
                         ChessColors.Random, ChessTimeControl.Unlimited, ChessGameVariants.Standard,
                         "", "8", "5.0"));
            }



            string[] settingStrings = settings.Substring(settingsIndexStart).ToLower().Replace(" ", "").Split(',');
            string   color          = string.Empty;
            string   timemode       = string.Empty;
            string   variant        = string.Empty;
            string   fen            = string.Empty;
            string   increment      = "8";
            string   time           = "5.0";

            foreach (string setting in settingStrings)
            {
                if (setting.Contains("color"))
                {
                    color = setting.Substring(6, setting.Length - 6);
                }
                else if (setting.Contains("timemode"))
                {
                    timemode = setting.Substring(9, setting.Length - 9);
                }
                else if (setting.Contains("variant"))
                {
                    variant = setting.Substring(8, setting.Length - 8);
                }
                else if (setting.Contains("fen"))
                {
                    fen = setting.Substring(4, setting.Length - 4);
                }
                else if (setting.Contains("increment"))
                {
                    increment = setting.Substring(10, setting.Length - 10);
                }
                else if (setting.Contains("time"))
                {
                    time = setting.Substring(5, setting.Length - 5);
                }
            }

            ChessColors       chessColors       = (ChessColors)(string.IsNullOrEmpty(color) ? ChessColors.Random : color.GetEnum <ChessColors>());
            ChessTimeControl  chessTimeControl  = (ChessTimeControl)(string.IsNullOrEmpty(timemode) ? ChessTimeControl.Unlimited : timemode.GetEnum <ChessTimeControl>());
            ChessGameVariants chessGameVariants = (ChessGameVariants)(string.IsNullOrEmpty(variant) ? ChessGameVariants.Standard : variant.GetEnum <ChessGameVariants>());

            return(new Tuple <ChessColors, ChessTimeControl, ChessGameVariants, string, string, string>(chessColors, chessTimeControl, chessGameVariants, fen, increment, time));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Get a move from the user
        /// </summary>
        /// <param name="gameState">The current state of the game</param>
        /// <param name="color">The color of the player</param>
        /// <returns></returns>
        public ChessMove GetMove(GameState gameState, ChessColors color)
        {
            this.gameState   = gameState;
            this.playerColor = color;
            ChessMove move = storedMove.Var;

            legalMoves.Clear();
            return(move);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Convert a chess color to a character
        /// </summary>
        /// <para>
        /// White becomes 'w' and black becomes 'b'
        /// </para>
        /// <seealso cref="FromChar"/>
        public static char ToChar(this ChessColors color)
        {
            switch (color)
            {
            case ChessColors.Black:
                return('b');

            default:
                return('w');
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Return the color that is not this one
        /// </summary>
        public static ChessColors Other(this ChessColors color)
        {
            switch (color)
            {
            case ChessColors.Black:
                return(ChessColors.White);

            default:
                return(ChessColors.Black);
            }
        }
Exemplo n.º 10
0
        private void InitBoard()
        {
            toSq = null;
            fromSq = null;
            board = new BoardSquare[8, 8];

            int width = panelChessBoard.Width/8;
            int height = panelChessBoard.Height/8;

            for (int row=0; row < 8; row++)
                for (int col = 0; col < 8; col++)
                {
                    board[row, col] = new BoardSquare(row, col, width, height);
                    panelChessBoard.Controls.Add(board[row, col]);
                    board[row, col].Click += new System.EventHandler(Square_Click);
                }
            
            board[0, 0].ChessPiece = new Rook(ChessColors.Black);
            board[0, 7].ChessPiece = new Rook(ChessColors.Black);

            board[7, 0].ChessPiece = new Rook(ChessColors.White);
            board[7, 7].ChessPiece = new Rook(ChessColors.White);

            board[0, 2].ChessPiece = new Bishop(ChessColors.Black);
            board[0, 5].ChessPiece = new Bishop(ChessColors.Black);

            board[7, 2].ChessPiece = new Bishop(ChessColors.White);
            board[7, 5].ChessPiece = new Bishop(ChessColors.White);

            board[0, 1].ChessPiece = new Knight(ChessColors.Black);
            board[0, 6].ChessPiece = new Knight(ChessColors.Black);

            board[7, 1].ChessPiece = new Knight(ChessColors.White);
            board[7, 6].ChessPiece = new Knight(ChessColors.White);

            board[0, 3].ChessPiece = new King(ChessColors.Black);
            board[7, 4].ChessPiece = new King(ChessColors.White);

            board[0, 4].ChessPiece = new Queen(ChessColors.Black);
            board[7, 3].ChessPiece = new Queen(ChessColors.White);

            
            for (int i = 0; i <= 7; i++)
                board[1, i].ChessPiece = new Pawn(ChessColors.Black);
            for (int i = 0; i <= 7; i++)
                board[6, i].ChessPiece = new Pawn(ChessColors.White);

            turn = ChessColors.White;
            label2.Text = turn.ToString();
            label2.Refresh();
            //board[0, 0].Image = (new Icon(Properties.Resources.Black_Rook, 48, 48)).ToBitmap();
        }
Exemplo n.º 11
0
 /// <summary>
 /// Construct a game state given all of its parameters
 /// </summary>
 /// <param name="board"></param>
 /// <param name="taken"></param>
 /// <param name="player"></param>
 /// <param name="castling"></param>
 /// <param name="enPassant"></param>
 /// <param name="halfmoveClock"></param>
 /// <param name="fullmoveClock"></param>
 /// <param name="gameType"></param>
 public GameState(Board board, List <Piece> taken, ChessColors player,
                  CastlingInfo castling, Nullable <BoardPosition> enPassant,
                  int halfmoveClock, int fullmoveClock, GameTypes gameType)
 {
     this.board         = board;
     this.taken         = taken;
     this.player        = player;
     this.castling      = castling;
     this.enPassant     = enPassant;
     this.halfmoveClock = halfmoveClock;
     this.fullmoveClock = fullmoveClock;
     this.gameType      = gameType;
 }
Exemplo n.º 12
0
 /// <summary>
 /// Construct a game state given only a board
 /// </summary>
 /// <param name="board">The game board</param>
 public GameState(Board board)
 {
     this.board    = board;
     this.taken    = new List <Piece>();
     this.player   = ChessColors.White;
     this.castling = new CastlingInfo {
         whiteKing  = true,
         whiteQueen = true,
         blackKing  = true,
         blackQueen = true
     };
     enPassant = null;
     gameType  = GameTypes.Normal;
 }
Exemplo n.º 13
0
        public BoardSquare(int row, int col, int width, int height)
        {
            this.row = row;
            this.col = col;

            this.Location = new Point(col * width, row * height);
            this.Size = new Size(width, height);

            squareColor = (row + col) % 2 == 0 ? ChessColors.Black : ChessColors.White;

            this.BackColor = squareColor == ChessColors.Black ? Color.Black : Color.White;

            this.SizeMode = PictureBoxSizeMode.CenterImage;
            chessPiece = null;
        }
Exemplo n.º 14
0
        public override bool IsValidMove(BoardSquare[,] board, int fromRow, int fromCol, int toRow, int toCol, ChessColors turn, int turnNumber)
        {
            if (!base.IsValidMove(board, fromRow, fromCol, toRow, toCol, turn, turnNumber))
                return false;
            if (turn == ChessColors.Black)
            {
                if (fromRow > toRow)
                    return false;
            }
            if (turn == ChessColors.White)
            {
                if (fromRow < toRow)
                    return false;
            }

            if (board[toRow, toCol].ChessPiece == null)
            {
                if (Math.Abs(fromRow - toRow) == 1 && fromCol == toCol)
                    return true;
                //en passant implemented here
                else if (Math.Abs(fromRow - toRow) == 1 && Math.Abs(toCol - fromCol) == 1)
                {
                    if (board[fromRow, toCol].ChessPiece.Name == ChessPieces.Pawn &&board[fromRow, toCol].ChessPiece.PieceColor != turn && board[fromRow, toCol].ChessPiece.LastMove == turnNumber - 1)
                        return true;
                    else
                        return false;
                }
                else if (Math.Abs(fromRow - toRow) == 2 && toCol == fromCol)
                {
                    if (board[fromRow,fromCol].ChessPiece.LastMove == 0)
                        return true;
                    else
                        return false;
                }
                else
                    return false;
            }
            else if (board[toRow, toCol].ChessPiece != null)
            {
                if (board[toRow, toCol].ChessPiece.PieceColor != turn && Math.Abs(fromRow - toRow) == 1 && Math.Abs(toCol-fromCol) == 1)
                    return true;
                else
                    return false;
            }
            return false;
            
                    
        }
Exemplo n.º 15
0
	public override bool IsValidMove(BoardSquare[,] board, int fromRow, int fromCol, int toRow, int toCol, ChessColors turn, int turnNumber)
        { 
        	if (!base.IsValidMove(board, fromRow, fromCol, toRow, toCol, turn, turnNumber))
                	return false;
        	if (Math.Abs(toRow-fromRow) > 2 || Math.Abs(toCol-fromCol) > 2 || toRow == fromRow || toCol == fromCol)
                	return false;
        	else if (Math.Abs(toRow-fromRow) == 2 && Math.Abs(toCol-fromCol) == 1)
		    {
            		if (board[toRow,toCol].ChessPiece == null)
                    {
                        
                        return true;

                    }	
			        else if(board[toRow,toCol].ChessPiece.PieceColor != turn)
                    {
                        
                        return true;

                    }
           	 	    else
                		return false;
		}	
        	else if(Math.Abs(toRow-fromRow) == 1 && Math.Abs(toCol-fromCol) == 2)
            {
			    if (board[toRow,toCol].ChessPiece == null)
                {
                    
                    return true;

                }
			    else if(board[toRow,toCol].ChessPiece.PieceColor != turn)
                {
                  
                    return true;

                }
           	 	else
                	return false;
		}
        	else 
	        	return false;
	


}
Exemplo n.º 16
0
        public override bool IsValidMove(BoardSquare[,] board, int fromRow, int fromCol, int toRow, int toCol, ChessColors turn, int turnNumber)
        {
            if (!base.IsValidMove(board, fromRow, fromCol, toRow, toCol, turn, turnNumber))
                return false;
            if (toCol == fromCol || toRow == fromRow)
                return false;
            if (Math.Abs(fromRow - toRow) == Math.Abs(fromCol - toCol))
            {
		
                for (int i = 1; i < Math.Abs(fromRow-toRow); i++)
                {
			        if (fromRow> toRow && fromCol>toCol)
			        {
				        if (board[fromRow-i,fromCol-i].ChessPiece != null)
					        return false;
			        }
			        if (fromRow> toRow && fromCol<toCol)
			        {
			            if (board[fromRow-i,fromCol+i].ChessPiece != null)
					        return false;
			        }
			        if (fromRow<toRow && fromCol<toCol)
			        {
			            if (board[fromRow+i,fromCol+i].ChessPiece != null)
			                return false;
			        }
			        if (fromRow< toRow && fromCol>toCol)
			        {   
				        if (board[fromRow+i,fromCol-i].ChessPiece != null)
					        return false;
			        }
                    
		        }
                if (board[toRow, toCol].ChessPiece == null)
                    return true;
                else if (board[toRow, toCol].ChessPiece.PieceColor != turn)
                    return true;
                else
                    return false;
               
            }
	        else
            	return false;
            //if absolute vaalue of (fromRow - toRow) == absolute value of (toRow - toCol) Math.Abs(----) 
        }
Exemplo n.º 17
0
        /// <summary>
        /// Get a move from the AI using the minimax algorithm
        /// </summary>
        /// <param name="color">The color of the AI</param>
        public ChessMove GetMove(GameState _, ChessColors color)
        {
            if (gameState.gameType == GameTypes.SkaktegoPrep)
            {
                return(GetPrepMove());
            }

            bool maximizingPlayer = color == ChessColors.White;

            MiniMax(gameState, SearchDepth, double.NegativeInfinity, double.PositiveInfinity, maximizingPlayer);

            if (!bestMove.HasValue)
            {
                throw new InvalidOperationException("No best move found");
            }

            return(bestMove.Value);
        }
Exemplo n.º 18
0
        /// <summary>
        /// Update and draw the UI
        /// </summary>
        public void Update()
        {
            PollEvents();

            // When the player switches, hide the screen
            if (gameState != null &&
                gameState.player != lastPlayer &&
                gameState.gameType != GameTypes.Normal &&
                !aiPlaying
                )
            {
                screenHidden = true;
                lastPlayer   = gameState.player;
            }

            // Update screen rect
            screenRect = renderer.OutputRect();

            //Reset if the user has already pressed a button this frame
            pressedSomething = false;

            // Check if the user wants to quit.
            if (events.Any(e => e.type == SDL.SDL_EventType.SDL_QUIT))
            {
                quit = true;
            }


            if (isGaming)
            {
                if (gameState != null)
                {
                    UpdateGame();
                }
            }
            else
            {
                UpdateMainMenu();
            }
        }
Exemplo n.º 19
0
        private void btn_Click(object sender, EventArgs e)
        {
            if (toSq == null || fromSq == null)
            {
                MessageBox.Show("Must select to/from square");
            }
            else
            {
                if(fromSq.ChessPiece.IsValidMove(board, fromSq.Row, fromSq.Col, toSq.Row, toSq.Col, turn, turnNumber))
                {
                    if(board[fromSq.Row,fromSq.Col].ChessPiece.Name == ChessPieces.King)
                    {
                        if(board[fromSq.Row,fromSq.Col].ChessPiece.PieceColor == ChessColors.Black)
                        {
                            
                            	BlackKing[0] = toSq.Row;
                                BlackKing[1] = toSq.Col;
                                 
                        }
			        if(board[fromSq.Row,fromSq.Col].ChessPiece.PieceColor == ChessColors.White)
                        {
                            
				                WhiteKing[0] = toSq.Row;
                                WhiteKing[1] = toSq.Col;
                        }
                    }
                    bool enpassant = false;
                    ChessPiece tmp;
                    if (fromSq.ChessPiece.Name == ChessPieces.Pawn && toSq.ChessPiece == null && Math.Abs(toSq.Row - fromSq.Row) == Math.Abs(toSq.Col - fromSq.Col))
                    {
                        enpassant = true;
                        tmp = board[fromSq.Row, toSq.Col].ChessPiece;
                        toSq.ChessPiece = fromSq.ChessPiece;
                        fromSq.ChessPiece = null;
                        board[fromSq.Row, toSq.Col].ChessPiece = null;
                        
                    }
                    else
                    {
                        tmp = toSq.ChessPiece;
                        toSq.ChessPiece = fromSq.ChessPiece;
                        fromSq.ChessPiece = null;
                    }
		            bool check = false;
                    //MessageBox.Show("Valid");
                    for (int i = 0; i < 8; i++)
                    {
                        for (int j = 0; j < 8; j++)
                        {
                                if (board[i, j].ChessPiece == null) { }
                                else if (board[i, j].ChessPiece.PieceColor == ChessColors.Black) 
				                { 
				                    if (board[i, j].ChessPiece.IsValidMove(board, i, j, WhiteKing[0], WhiteKing[1], ChessColors.Black, turnNumber))
                                    {
					                    if(turn == ChessColors.Black)
						                    MessageBox.Show("Puts White King in Check");
					                    if(turn ==ChessColors.White)
					                    {
                                        	MessageBox.Show("Invalid: Puts king in check");
                                        	check = true;
					                    }
                                    }
				                }
                                else if (board[i, j].ChessPiece.PieceColor == ChessColors.White)
                                {
                                    if (board[i, j].ChessPiece.IsValidMove(board, i, j, BlackKing[0], BlackKing[1], ChessColors.White, turnNumber))
                                    {
					                    if(turn == ChessColors.White)
						                    MessageBox.Show("Puts Black King in Check");
					                    if(turn == ChessColors.Black)
					                    {
                                        	MessageBox.Show("Invalid: Puts king in check");
                                        	check = true;
					                    }
                                    }
                                }
                            }
                            

                        }
                    
		    if(!check)
		    {
                toSq.ChessPiece.LastMove = turnNumber;
                    	turnNumber++;
                    	if (turn == ChessColors.Black)
                        	turn = ChessColors.White;
                    	else
                        	turn = ChessColors.Black;
                    	label2.Text = turn.ToString();
                    	label2.Refresh();
		     }
		     else
            {
                if (enpassant)
                {
                    fromSq.ChessPiece = toSq.ChessPiece;
                    toSq.ChessPiece = null;
                    board[fromSq.Row, toSq.Col].ChessPiece = tmp;
                    enpassant = false;
                }
                else
                {
                    fromSq.ChessPiece = toSq.ChessPiece;
                    toSq.ChessPiece = tmp;
                }
                if (board[fromSq.Row, fromSq.Col].ChessPiece.Name == ChessPieces.King)
                {
                    if (board[fromSq.Row, fromSq.Col].ChessPiece.PieceColor == ChessColors.Black)
                    {
                        
                        BlackKing[0] = fromSq.Row;
                        BlackKing[1] = fromSq.Col;

                    }
                    if (board[fromSq.Row, fromSq.Col].ChessPiece.PieceColor == ChessColors.White)
                    {
                       
                        WhiteKing[0] = fromSq.Row;
                        WhiteKing[1] = fromSq.Col;
                    }
                }
                check = false;

            }
                }
                else
                {
                    MessageBox.Show("Not a Valid Move");
                }

            }
        }
Exemplo n.º 20
0
        public Pawn(ChessColors color)
            : base(ChessPieces.Pawn, color, color == ChessColors.Black ? Properties.Resources.Black_Pawn : Properties.Resources.White_Pawn)
        {

        }
Exemplo n.º 21
0
        public Bishop(ChessColors color)
            : base(ChessPieces.Bishop, color, color == ChessColors.Black ? Properties.Resources.Black_Bishop : Properties.Resources.White_Bishop)
        {

        }
Exemplo n.º 22
0
Arquivo: King.cs Projeto: B-Rich/Chess
        public King(ChessColors color)
            : base(ChessPieces.King, color, color == ChessColors.Black ? Properties.Resources.Black_King : Properties.Resources.White_King)
        {

        }
Exemplo n.º 23
0
        /// <summary>
        /// Convert a string to a game state
        /// </summary>
        /// <para>
        /// The string notation is very close to FEN,
        /// but modified to accomidate for skaktego rules.
        /// </para>
        /// <seealso cref="ToString"/>
        public static GameState FromString(string stateStr)
        {
            string[] splitStr     = stateStr.Split(' ');
            var      boardStr     = splitStr[0];
            var      takenStr     = splitStr[1];
            var      playerStr    = splitStr[2];
            var      castlingStr  = splitStr[3];
            var      enPassantStr = splitStr[4];
            var      halfmoveStr  = splitStr[5];
            var      fullmoveStr  = splitStr[6];
            var      gameTypeStr  = splitStr[7];

            // Construct all of the elements of the game state from the
            // string parts.
            Board board = Board.FromString(boardStr);

            List <Piece> taken = new List <Piece>();

            if (takenStr != "-")
            {
                foreach (char pieceChar in takenStr)
                {
                    Piece piece = Piece.FromChar(pieceChar);
                    taken.Add(piece);
                }
            }

            ChessColors  player   = ChessColorsMethods.FromChar(playerStr[0]);
            CastlingInfo castling = CastlingInfo.FromString(castlingStr);

            Nullable <BoardPosition> enPassant = null;

            if (enPassantStr != "-")
            {
                enPassant = BoardPosition.FromString(enPassantStr);
            }

            int halfmoveClock = int.Parse(halfmoveStr);
            int fullmoveClock = int.Parse(fullmoveStr);

            GameTypes gameType;

            switch (gameTypeStr)
            {
            case "s":
                gameType = GameTypes.Normal;
                break;

            case "st":
                gameType = GameTypes.Skaktego;
                break;

            case "stp":
                gameType = GameTypes.SkaktegoPrep;
                break;

            default:
                throw new ArgumentException("'" + gameTypeStr + "' is not a valid string");
            }

            return(new GameState(board, taken, player,
                                 castling, enPassant, halfmoveClock,
                                 fullmoveClock, gameType));
        }
Exemplo n.º 24
0
        public void TestGetPositionsInTheWayOfMoveMethod(
            char initialHorizontal,
            int initialVertical,
            char targetHorizontal,
            int targetVertical,
            ChessColors pawnColor,
            object[] positionsInTheWayOfMove)
        {
            List <Tuple <char, int> > positions = new List <Tuple <char, int> >();

            if (positionsInTheWayOfMove != null)
            {
                for (int i = 0; i < positionsInTheWayOfMove.Length; i++)
                {
                    if (i % 2 == 0)
                    {
                        positions.Add(new Tuple <char, int>((char)positionsInTheWayOfMove[i], (int)positionsInTheWayOfMove[i + 1]));
                    }
                }
            }

            var getPositionsInTheWayOfMoveMethod = this.PawnType.GetMethod("GetPositionsInTheWayOfMove");

            var normalChessMoveType    = ChessGameLogicProvider.GetType("ChessGameLogic.ChessMoves.NormalChessMovePositions");
            var chessBoardPositionType = ChessGameLogicProvider.GetType("ChessGameLogic.ChessBoardPosition");

            var move = normalChessMoveType.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic)[0]
                       .Invoke(new object[]
            {
                initialHorizontal,
                initialVertical,
                targetHorizontal,
                targetVertical,
            });

            ICollection actualPositionsInTheWayOfMove = null;

            if (pawnColor == ChessColors.White)
            {
                actualPositionsInTheWayOfMove = (ICollection)getPositionsInTheWayOfMoveMethod.Invoke(this.WhitePawnInstance, new object[] { move });
            }
            else
            {
                actualPositionsInTheWayOfMove = (ICollection)getPositionsInTheWayOfMoveMethod.Invoke(this.BlackPawnInstance, new object[] { move });
            }

            if (positionsInTheWayOfMove == null)
            {
                Assert.IsNull(actualPositionsInTheWayOfMove);
            }
            else
            {
                Assert.AreEqual(positions.Count, actualPositionsInTheWayOfMove.Count);

                foreach (var postition in positions)
                {
                    bool actualPositionsContainCurrentExpectedPosition = false;

                    foreach (var actualPosition in actualPositionsInTheWayOfMove)
                    {
                        var actualHorizontal = (char)chessBoardPositionType
                                               .GetProperty("Horizontal", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(actualPosition);
                        var actualVertical = (int)chessBoardPositionType
                                             .GetProperty("Vertical", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(actualPosition);

                        if (actualHorizontal == postition.Item1 && actualVertical == postition.Item2)
                        {
                            actualPositionsContainCurrentExpectedPosition = true;
                            break;
                        }
                    }

                    Assert.That(actualPositionsContainCurrentExpectedPosition);
                }
            }
        }
Exemplo n.º 25
0
 protected Figure(ChessColors color)
 {
     this.Color = color;
 }
Exemplo n.º 26
0
 /// <summary>
 /// Create a new piece given a color and type
 /// </summary>
 /// <param name="color">The color the piece belongs to.</param>
 /// <param name="type">The type of the piece.</param>
 public Piece(ChessColors color, PieceTypes type, bool hasMoved = true)
 {
     Color         = color;
     Type          = type;
     this.hasMoved = hasMoved;
 }
Exemplo n.º 27
0
        public override bool IsValidMove(BoardSquare[,] board, int fromRow, int fromCol, int toRow, int toCol, ChessColors turn, int turnNumber)
        {
            if (!base.IsValidMove(board, fromRow, fromCol, toRow, toCol, turn, turnNumber))
                return false;
            if (fromRow != toRow && fromCol != toCol)
                return false;
            //horiz or vert?
            if (fromRow == toRow ) 
            {
                    if (Math.Abs(fromCol - toCol) == 1)
                    {
                        if (board[toRow, toCol].ChessPiece == null)
                            return true;
                        else if (board[toRow, toCol].ChessPiece.PieceColor != turn)
                            return true;
                        else
                            return false;
                    }


                    for (int i = 1; i <= Math.Abs(toCol - fromCol) - 1; i++)
                    {
                        if (fromCol > toCol)
                        {
                            if (board[fromRow, fromCol - i].ChessPiece != null)
                                return false;
                        }
                        if (fromCol < toCol)
                        {
                            if (board[fromRow, fromCol+i].ChessPiece != null)
                                return false;
                        }
                    }
                   
                }
                if (fromCol == toCol)
                {
                    if (Math.Abs(fromRow - toRow) == 1)
                    {
                        if (board[toRow, toCol].ChessPiece == null)
                            return true;
                        else if (board[toRow, toCol].ChessPiece.PieceColor != turn)
                            return true;
                        else
                            return false;
                    }
                    for (int i = 1; i <= Math.Abs(toRow - fromRow) - 1; i++)
                    {
                        if (fromRow > toRow)
                        {
                            if (board[fromRow-i, fromCol].ChessPiece != null)
                                return false;
                        }
                        if (fromCol < toCol)
                        {
                            if (board[fromRow+i, fromCol].ChessPiece != null)
                                return false;
                        }
                    }
                    
                }
                if (board[toRow, toCol].ChessPiece == null)
                    return true;
                else if (board[toRow, toCol].ChessPiece.PieceColor != turn)
                    return true;
                else
                    return false;
            //to Empty or OppColor
            //Math.Sign(----) +1, -1, 0
            
            
            //Castling
            //check?
            //1. Make move
            //2. Ask each opposing piece if it can move to king location
            //3. if all no --> commit , else --> undo
        }
Exemplo n.º 28
0
        public Rook(ChessColors color)
            : base(ChessPieces.Rook, color, color == ChessColors.Black ? Properties.Resources.Black_Rook : Properties.Resources.White_Rook)
        {

        }
Exemplo n.º 29
0
        public Queen(ChessColors color)
            : base(ChessPieces.Queen, color, color == ChessColors.Black ? Properties.Resources.Black_Queen : Properties.Resources.White_Queen)
        {

        }
Exemplo n.º 30
0
        public override bool IsValidMove(BoardSquare[,] board, int fromRow, int fromCol, int toRow, int toCol, ChessColors turn, int turnNumber)
        {
            if (!base.IsValidMove(board, fromRow, fromCol, toRow, toCol, turn, turnNumber))
                return false;
            if (fromRow != toRow && fromCol != toCol && Math.Abs(fromRow - toRow) != Math.Abs(fromCol - toCol))
                return false;
            if (fromRow == toRow)
            {
                if (Math.Abs(fromCol - toCol) == 1)
                {
                    if (board[toRow, toCol].ChessPiece == null)
                        return true;
                    else if (board[toRow, toCol].ChessPiece.PieceColor != turn)
                        return true;
                    else
                        return false;
                }


                for (int i = 1; i <= Math.Abs(toCol - fromCol) - 1; i++)
                {
                    if (fromCol > toCol)
                    {
                        if (board[fromRow, fromCol - i].ChessPiece != null)
                            return false;
                    }
                    if (fromCol < toCol)
                    {
                        if (board[fromRow, fromCol + i].ChessPiece != null)
                            return false;
                    }
                }

            }
            if (fromCol == toCol)
            {
                if (Math.Abs(fromRow - toRow) == 1)
                {
                    if (board[toRow, toCol].ChessPiece == null)
                        return true;
                    else if (board[toRow, toCol].ChessPiece.PieceColor != turn)
                        return true;
                    else
                        return false;
                }
                for (int i = 1; i <= Math.Abs(toRow - fromRow) - 1; i++)
                {
                    if (fromRow > toRow)
                    {
                        if (board[fromRow - i, fromCol].ChessPiece != null)
                            return false;
                    }
                    if (fromRow < toRow)
                    {
                        if (board[fromRow + i, fromCol].ChessPiece != null)
                            return false;
                    }
                }

            }
            if (board[toRow, toCol].ChessPiece == null)
                return true;
            else if (board[toRow, toCol].ChessPiece.PieceColor != turn)
                return true;
            
            if (Math.Abs(fromRow - toRow) == Math.Abs(fromCol - toCol))
            {

                for (int i = 1; i < Math.Abs(fromRow - toRow); i++)
                {
                    if (fromRow > toRow && fromCol > toCol)
                    {
                        if (board[fromRow - i, fromCol - i].ChessPiece != null)
                            return false;
                    }
                    if (fromRow > toRow && fromCol < toCol)
                    {
                        if (board[fromRow - i, fromCol + i].ChessPiece != null)
                            return false;
                    }
                    if (fromRow < toRow && fromCol < toCol)
                    {
                        if (board[fromRow + i, fromCol + i].ChessPiece != null)
                            return false;
                    }
                    if (fromRow < toRow && fromCol > toCol)
                    {
                        if (board[fromRow + i, fromCol - i].ChessPiece != null)
                            return false;
                    }

                }
                if (board[toRow, toCol].ChessPiece == null)
                    return true;
                else if (board[toRow, toCol].ChessPiece.PieceColor != turn)
                    return true;
                else
                    return false;

            }
            else
                return false;
        }
Exemplo n.º 31
0
        public Knight(ChessColors color)
            : base(ChessPieces.Knight, color, color == ChessColors.Black ? Properties.Resources.Black_Knight : Properties.Resources.White_Knight)
        {

        }
Exemplo n.º 32
0
 internal Bishop(ChessColors color)
     : base(color)
 {
 }