예제 #1
0
        public void InitializePlayfield(ref bool isWhiteTurn, ref bool isGameDone, 
            List<int[]> highlight, Piece[,] playfield, Piece[,] whiteTaken, 
            Piece[,] blackTaken, ref int[] whiteTakenIndeces, ref int[] blackTakenIndeces)
        {
            //reseting parameters
            df.Clear();
            isWhiteTurn = true;
            isGameDone = false;
            highlight.Clear();
            whiteTakenIndeces = new int[] { 0, 0 };
            blackTakenIndeces = new int[] { 0, 0 };
            for (int row = 0; row < Board.Rows; row++)
            {
                for (int col = 0; col < Board.Cols; col++)
                {
                    //set piece type
                    playfield[row, col] = new Piece() { Row = row, Col = col };
                    if (row == Board.FirstRow || row == Board.LastRow)
                    {
                        if (col == Board.FirstCol || col == Board.LastCol)
                            playfield[row, col].PieceType = Piece.Type.Rook;
                        if (col == Board.FirstCol + 1 || col == Board.LastCol - 1)
                            playfield[row, col].PieceType = Piece.Type.Knight;
                        if (col == Board.FirstCol + 2 || col == Board.LastCol - 2)
                            playfield[row, col].PieceType = Piece.Type.Bishop;
                        if (col == Board.FirstCol + 3)
                            playfield[row, col].PieceType = Piece.Type.Queen;
                        if (col == Board.FirstCol + 4)
                            playfield[row, col].PieceType = Piece.Type.King;
                    }
                    if (row == Board.FirstRow + 1 || row == Board.LastRow - 1)
                        playfield[row, col].PieceType = Piece.Type.Pawn;
                    //set piece color
                    //all pieces in the top two rows are black
                    if (row <= Board.FirstRow + 1)
                        playfield[row, col].PieceColour = Piece.Colour.Black;
                    //all pieces in the bottom two rows are white
                    if (row >= Board.LastRow - 1)
                        playfield[row, col].PieceColour = Piece.Colour.White;
                    //set those pieces to existing
                    if (row <= Board.FirstRow + 1 || row >= Board.LastRow - 1)
                        playfield[row, col].Exists = true;
                }

                //Reset the taken grids
                for (int col = 0; col < 2; col++)
                {
                    whiteTaken[row, col] = new Piece();
                    blackTaken[row, col] = new Piece();
                }
            }
        }
예제 #2
0
 public void InitializePlayfield(ref bool whiteTurn, ref bool gameDone, List<int[]> highlight, Piece[,] playfield, Piece[,] whiteTaken, Piece[,] blackTaken, ref int[] whiteTakenIndeces, ref int[] blackTakenIndeces)
 {
     //reseting parameters
     df.Clear();
     whiteTurn = true;
     gameDone = false;
     highlight.Clear();
     whiteTakenIndeces = new int[] { 0, 0 };
     blackTakenIndeces = new int[] { 0, 0 };
     for (int row = 0; row < 8; row++)
     {
         for (int col = 0; col < 8; col++)
         {
             //set piece type
             playfield[row, col] = new Piece() { Row = row, Col = col };
             if (row == 0 || row == 7)
             {
                 if (col == 0 || col == 7)
                     playfield[row, col].Type = rook;
                 if (col == 1 || col == 6)
                     playfield[row, col].Type = knight;
                 if (col == 2 || col == 5)
                     playfield[row, col].Type = bishop;
                 if (col == 3)
                     playfield[row, col].Type = queen;
                 if (col == 4)
                     playfield[row, col].Type = king;
             }
             if (row == 1 || row == 6)
                 playfield[row, col].Type = pawn;
             //set piece color
             if (row <= 1)
                 playfield[row, col].Colour = black;
             if (row >= 6)
                 playfield[row, col].Colour = white;
             if (row <= 1 || row >= 6)
                 playfield[row, col].Exists = true;
         }
         for (int col = 0; col < 2; col++)
         {
             whiteTaken[row, col] = new Piece();
             blackTaken[row, col] = new Piece();
         }
     }
 }
예제 #3
0
파일: AI.cs 프로젝트: mapplet/Chess
        public void Move(Gameboard gameboard, State currentState)
        {
            Rulebook rulebook = new Rulebook();
            List<Tuple<Piece, Piece>> topValidMoves = new List<Tuple<Piece, Piece>>();
            foreach(Piece piece in gameboard.getTeam(thisTeam))
            {
                List<Tuple<int, int>> validDestinations = rulebook.getValidMoves(piece, gameboard);
                foreach (Tuple<int,int> coordinate in validDestinations)
                {
                    if (validDestinations.Count() == 0)
                        break;
                    else if (topValidMoves.Count() == 0 || rewards[topValidMoves[0].Item2.type] == rewards[gameboard.getPiece(coordinate.Item1, coordinate.Item2).type])
                        topValidMoves.Add(new Tuple<Piece, Piece>(piece, gameboard.getPiece(coordinate.Item1, coordinate.Item2)));
                    else if (rewards[topValidMoves[0].Item2.type] < rewards[gameboard.getPiece(coordinate.Item1, coordinate.Item2).type])
                    {
                        topValidMoves.Clear();
                        topValidMoves.Add(new Tuple<Piece, Piece>(piece, gameboard.getPiece(coordinate.Item1, coordinate.Item2)));
                    }
                }

            }
            if (topValidMoves.Count() != 0)
            {
                Random rand = new Random();
                int index = rand.Next(topValidMoves.Count());
                gameboard.Move(topValidMoves[index].Item1, topValidMoves[index].Item2);

                if (topValidMoves[index].Item1.type == (int)type.pawn && (topValidMoves[index].Item1.row == 0 || topValidMoves[index].Item1.row == 7))
                {
                    Piece bestPieceFromDead = new Piece();
                    foreach (Piece deadPiece in gameboard.getDead(thisTeam))
                    {
                        if (rewards[deadPiece.type] > rewards[bestPieceFromDead.type])
                            bestPieceFromDead = deadPiece;
                    }
                    gameboard.tradePawn(topValidMoves[index].Item1, bestPieceFromDead);
                }

                //gameboard.checkChessMate(currentState.getWhosTurn());
            }
            //currentState.swapTurn();
        }
예제 #4
0
파일: AI.cs 프로젝트: georgelbaxter/chessAI
 private void updatePieceList(List<Piece> pieces, Piece[,] board, string colour)
 {
     pieces.Clear();
     for (int row = 0; row < 8; row++)
         for (int col = 0; col < 8; col++)
         {
             if (board[row, col].Colour == colour)
                 pieces.Add(board[row, col]);
         }
 }
예제 #5
0
파일: AI.cs 프로젝트: georgelbaxter/chessAI
 private void updateMoveList(List<Piece> pieces, Piece[,] board)
 {
     allMovesList.Clear();
     List<int[]> moveable = new List<int[]>();
     List<int[]> threatList = new List<int[]>();
     List<int[]> enPassantList = new List<int[]>();
     foreach (Piece piece in pieces)
     {
         moveable.Clear();
         threatList.Clear();
         enPassantList.Clear();
         mhf.highlightMoveable(board[piece.Row, piece.Col], board, piece.Row, piece.Col, moveable);
         thf.highlightThreatened(board[piece.Row, piece.Col], piece.Row, piece.Col, threatList, enPassantList, board);
         foreach (int[] pair in moveable)
             if (!cf.createCheck(piece.Row, piece.Col, pair[0], pair[1], board))
                 allMovesList.Add(new AvailableMove() { Move = pair, Piece = piece });
         foreach (int[] pair in threatList)
             if (board[pair[0], pair[1]].Exists && !cf.createCheck(piece.Row, piece.Col, pair[0], pair[1], board))
                 allMovesList.Add(new AvailableMove() { Move = pair, Piece = piece });
         foreach (int[] pair in enPassantList)
             if (!cf.createCheck(piece.Row, piece.Col, pair[0], pair[1], board))
                 allMovesList.Add(new AvailableMove() { Move = pair, Piece = piece });
     }
 }