예제 #1
0
    public static int EvaluateDecision(PieceType[,] board, PieceType type, int columnNum)
    {
        PieceType enemy;

        if (type == PieceType.RED)
        {
            enemy = PieceType.YELLOW;
        }
        else
        {
            enemy = PieceType.RED;
        }

        int value = 0;

        //Evaluate decisions to maximize value for AI's move
        PieceType[,] boardCopy = Board.BoardCopy(board);
        int        rowNum        = CreateNextState(ref boardCopy, type, columnNum);
        Vector2Int startingPoint = new Vector2Int(columnNum, rowNum);

        value = ClusterCount(startingPoint, type, boardCopy);

        //Minimizes the enemy's value if they made that move
        PieceType[,] enemyBoardCopy = Board.BoardCopy(board);
        int        enemyRowNum        = CreateNextState(ref enemyBoardCopy, enemy, columnNum);
        Vector2Int enemyStartingPoint = new Vector2Int(columnNum, enemyRowNum);

        value += ClusterCount(enemyStartingPoint, enemy, enemyBoardCopy);

        return(value);
    }
예제 #2
0
    public static bool CheckWin(PieceType[,] board, int x, int y)
    {
        //skip empty slots
        PieceType type = board[x, y];

        if (type == PieceType.Empty)
        {
            return(false);
        }

        //prevent out of bounds
        int  range         = ConnectFourController.piecesToWin - 1;
        bool insideRangeX  = x + range < board.GetLength(0);
        bool insideRangeY1 = y + range < board.GetLength(1);
        bool insideRangeY2 = y - range >= 0;

        //win
        bool winHorizontal = insideRangeX;
        bool winVertical   = insideRangeY1;
        bool winDiagonal1  = insideRangeX && insideRangeY1;
        bool winDiagonal2  = insideRangeX && insideRangeY2;

        //check conditions
        for (int w = 1; w < ConnectFourController.piecesToWin; w++)
        {
            winHorizontal = winHorizontal && (type == board[x + w, y]);
            winVertical   = winVertical && (type == board[x, y + w]);
            winDiagonal1  = winDiagonal1 && (type == board[x + w, y + w]);
            winDiagonal2  = winDiagonal2 && (type == board[x + w, y - w]);
        }

        //return true if any condition was met
        return(winHorizontal || winVertical || winDiagonal1 || winDiagonal2);
    }
예제 #3
0
    public static int GetRandomMove(PieceType[,] board)
    {
        var movements = GetPossibleMovements(board);

        System.Random r = new System.Random();
        return(movements[r.Next(0, movements.Count)].y);
    }
예제 #4
0
        // (2)
        /// <summary>
        /// Fais tourner la pièce active en sens horaire, ou sens antihoraires.
        /// </summary>
        ///
        /// <param name="piece">
        /// La pièce active qui sera tournée.
        /// </param>
        ///
        /// <param name="directionOfRotation">
        /// Détermine si la pièce sera tournée dans le sens horaire (0), ou antihoraire (1).
        /// </param>
        ///
        /// <returns>
        /// Retourne un tableau avec la nouvelle pièce tournée.
        /// </returns>
        private PieceType[,] RotatePieceArray(Piece piece, int directionOfRotation)
        {
            PieceType[,] pieceArray = piece.GetPieceArray();

            int iLength = pieceArray.GetLength(0);

            int jLength = pieceArray.GetLength(1);

            PieceType[,] newPieceArray = new PieceType[iLength, jLength];

            for (int i = iLength - 1; i >= 0; i--)
            {
                for (int j = 0; j < jLength; j++)
                {
                    if (directionOfRotation == 0)
                    {
                        newPieceArray[j, iLength - 1 - i] = pieceArray[i, j];
                    }
                    else
                    {
                        newPieceArray[i, j] = pieceArray[j, iLength - 1 - i];
                    }
                }
            }
            return(newPieceArray);
        }
예제 #5
0
    /**
     * Super simple evaluation function
     */
    private int eval(PlayerTurn t, PieceType[,] board)
    {
        int ret = 0;

        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                if (board[i, j] == chooseFriendlyPiece(t))
                {
                    // Corners
                    if ((i == 0 && j == 0) ||
                        (i == 0 && j == 7) ||
                        (i == 7 && j == 0) ||
                        (i == 7 && j == 7))
                    {
                        ret += 4;
                    }
                    // Edges
                    else if (i == 0 || i == 7 || j == 0 || j == 7)
                    {
                        ret += 2;
                    }
                    // Regular Spaces
                    else
                    {
                        ret += 1;
                    }
                }
            }
        }

        return(ret);
    }
예제 #6
0
 public PolyglotBoard()
 {
     _state         = new PieceType[8, 8];
     _castlingFlags = CastlingFlags.Everything;
     _colorToMove   = ColorType.White;
     _enPassantFile = -1;
 }
예제 #7
0
        // (C)
        /// <summary>
        /// Positionne la pièce de jeu au centre de la surface de jeu et en haut de celle-ci.
        /// </summary>
        ///
        /// <param name="_type">
        /// Le type de bloc qui sera placé.
        /// </param>
        public Piece(PieceType _type = PieceType.Dead)
        {
            // Debug
            if (_type == PieceType.Dead)
            {
                // Chooses a random piece.
                type = (PieceType)Enum.GetValues(typeof(PieceType)).GetValue(RandomTetris.rnd.Next(7));
            }
            else
            {
                type = _type;
            }

            pieceArray = StaticVars.GetPieceArray(type);

            // Centers the piece and place it at the top of the grid.
            if (type != PieceType.O)
            {
                position = new Vector2i(5 - (int)Math.Ceiling((decimal)pieceArray.GetLength(1) / 2), 0);
            }
            else
            {
                position = new Vector2i(5 - (int)Math.Ceiling((decimal)pieceArray.GetLength(1) / 2), -1);
            }
        }
예제 #8
0
    public static int SimulateDrop(PieceType[,] board, int column, bool isPlayersTurn)
    {
        var        movements = GetPossibleMovements(board);
        Vector2Int indexes   = movements.Find(element => element.y == column);

        board[indexes.x, indexes.y] = isPlayersTurn ? PieceType.Red : PieceType.Blue;
        return(column);
    }
예제 #9
0
    public static void PlayMove(PieceType[,] board, PieceType type)
    {
        int choice = EvaluateBoard(board, type);

        if (choice >= 0)
        {
            mColumns[choice].SpawnToken();
        }
    }
예제 #10
0
    private static int CountHorizontal(Vector2Int startingPoint, PieceType type, PieceType[,] grid)
    {
        int value = Board.CountRight(startingPoint, type, grid) + Board.CountLeft(startingPoint, type, grid);

        if (value >= 3)
        {
            value += 100;
        }
        return(value);
    }
예제 #11
0
    //This is used to evaluate the AI's Hypothetical choice
    private static int ClusterCount(Vector2Int startingPoint, PieceType type, PieceType[,] grid)
    {
        int count = 0;

        count += CountHorizontal(startingPoint, type, grid);
        count += CountDiagTLDR(startingPoint, type, grid);
        count += CountDiagDLTR(startingPoint, type, grid);
        count += CountDown(startingPoint, type, grid);
        return(count);
    }
예제 #12
0
    private static int CountDown(Vector2Int startingPoint, PieceType type, PieceType[,] grid)
    {
        int value = Board.CountDown(startingPoint, type, grid);

        if (value >= 3)
        {
            value += 100;
        }
        return(value);
    }
예제 #13
0
        protected override void OnSizeChanged(int w, int h, int oldw, int oldh)
        {
            x_piece_count = (int)System.Math.Floor((double)w / piece_size);
            y_piece_count = (int)System.Math.Floor((double)h / piece_size) - 10;

            x_offset = 0;
            y_offset = 0;
            pieces   = new PieceType[x_piece_count, y_piece_count];

            ClearPieces();
        }
예제 #14
0
 // (2)
 /// <summary>
 /// Vérifie si la pièce peut être tournée et placée à un emplacement valide sur le tableau de jeu.
 /// </summary>
 ///
 /// <param name="piece">
 /// La pièce active qui sera tournée.
 /// </param>
 ///
 /// <param name="kickBackPosition">
 /// Décalage de la pièce active sur le tableau de jeu après avoir tourné.
 /// </param>
 ///
 /// <param name="directionOfRotation">
 /// Détermine si la pièce sera tournée dans le sens horaire (0), ou antihoraire (1).
 /// </param>
 ///
 /// <returns>
 /// Retourne « vrai » si la pièce peut tourner, ou « faux » si elle ne peut pas tourner.
 /// </returns>
 private bool CanRotatePiece(Piece piece, out int[] kickBackPosition, int directionOfRotation)
 {
     PieceType[,] newPieceArray = RotatePieceArray(piece, directionOfRotation);
     if (CheckPiecePositionValid(piece, newPieceArray, noKickBack))
     {
         kickBackPosition = noKickBack;
     }
     else if (CheckPiecePositionValid(piece, newPieceArray, kickBackToRight))
     {
         kickBackPosition = kickBackToRight;
     }
     else if (CheckPiecePositionValid(piece, newPieceArray, kickBackToLeft))
     {
         kickBackPosition = kickBackToLeft;
     }
     else if (CheckPiecePositionValid(piece, newPieceArray, kickBackDown))
     {
         kickBackPosition = kickBackDown;
     }
     else if (piece.type == PieceType.T && CheckPiecePositionValid(piece, newPieceArray, kickTDiagLeft))
     {
         kickBackPosition = kickTDiagLeft;
     }
     else if (piece.type == PieceType.T && CheckPiecePositionValid(piece, newPieceArray, kickTDiagRight))
     {
         kickBackPosition = kickTDiagRight;
     }
     else if (CheckPiecePositionValid(piece, newPieceArray, kickBackUp))
     {
         kickBackPosition = kickBackUp;
     }
     else if (piece.type == PieceType.I && CheckPiecePositionValid(piece, newPieceArray, kickLineToRight))
     {
         kickBackPosition = kickLineToRight;
     }
     else if (piece.type == PieceType.I && CheckPiecePositionValid(piece, newPieceArray, kickLineToLeft))
     {
         kickBackPosition = kickLineToLeft;
     }
     else if (piece.type == PieceType.I && CheckPiecePositionValid(piece, newPieceArray, kickLineDown))
     {
         kickBackPosition = kickLineDown;
     }
     else if (piece.type == PieceType.I && CheckPiecePositionValid(piece, newPieceArray, kickLineUp))
     {
         kickBackPosition = kickLineUp;
     }
     else
     {
         kickBackPosition = noKickBack;
         return(false);
     }
     return(true);
 }
예제 #15
0
 //returns a deep copy of the grid
 public static PieceType[,] BoardCopy(PieceType[,] board)
 {
     PieceType[,] copy = new PieceType[Board.mGridWidth, Board.mGridHeight];
     for (int i = 0; i < Board.mGridWidth; i++)
     {
         for (int j = 0; j < Board.mGridHeight; j++)
         {
             copy[i, j] = board[i, j];
         }
     }
     return(copy);
 }
예제 #16
0
    private void playTurn(Vector2 space, PlayerTurn t, PieceType[,] board)
    {
        //GameObject.Find("/Piece[" + space.x + "," + space.y + "]").GetComponent<Piece>().activatePiece(t == PlayerTurn.Black);

        Vector3Int[] linesXYCount = new Vector3Int[8];

        linesXYCount = calculateLines(space, board, t);

        int count = flipPieces(t, board, linesXYCount, space, false);

        board[(int)space.x, (int)space.y] = chooseFriendlyPiece(t);
    }
예제 #17
0
    private static int EvaluateBoard(PieceType[,] board, PieceType type)
    {
        int choice = 0;

        int[] choicesValue = new int[mColumns.Length];
        for (int i = 0; i < mColumns.Length; i++)
        {
            //evaluates all possible choices for the AI
            choicesValue[i] = AIHelper.EvaluateDecision(board, type, i);
        }
        choice = AIHelper.FindValidHighestChoice(choicesValue);
        return(choice);
    }
예제 #18
0
    public Simulation(bool isPlayersTurn, PieceType[,] board)
    {
        this.isPlayersTurn = isPlayersTurn;

        simulatedBoard = new PieceType[board.GetLength(0), board.GetLength(1)];
        for (int x = 0; x < board.GetLength(0); x++)
        {
            for (int y = 0; y < board.GetLength(1); y++)
            {
                simulatedBoard[x, y] = board[x, y];
            }
        }
    }
예제 #19
0
        // this is explicitely called so properties can be used
        private BoardState(BoardState oldState) : this()
        {
            pieces = new PieceType[8, 8];
            Array.Copy(oldState.pieces, pieces, oldState.pieces.Length);

            CurrentPlayer = oldState.CurrentPlayer;

            RedMovesLeft   = oldState.RedMovesLeft;
            BlackMovesLeft = oldState.BlackMovesLeft;

            LastMove       = oldState.LastMove;
            MoveBeforeLast = oldState.MoveBeforeLast;
        }
예제 #20
0
        // (C)
        /// <summary>
        /// Appelle la fonction qui vérifie si la pièce active peut bouger dans la
        /// direction passée en paramètre, et ensuite appelle la fonction qui déplace
        /// la pièce active si elle peut être déplacée. Appelle aussi les fonctions qui
        /// gèrent la pièce fantôme.
        /// </summary>
        ///
        /// <param name="piece">
        /// La pièce active qui sera déplacée.
        /// </param>
        ///
        /// <param name="newPosition">
        /// La direction dans laquelle la pièce active sera déplacée.
        /// </param>
        public void MovePiece(Piece piece, Vector2i newPosition)
        {
            PieceType[,] pieceArray = piece.GetPieceArray();

            if (CanPlacePiece(piece, newPosition))
            {
                RemoveGhostPiece(piece);
                RemovePiece(piece);

                AddPiece(piece, newPosition);
                PlaceGhostPiece(piece);
            }
        }
예제 #21
0
 public BoardState()
 {
     _state = new [, ]
     {
         { PieceType.WRook, PieceType.WKnight, PieceType.WBishop, PieceType.WQueen, PieceType.WKing, PieceType.WBishop, PieceType.WKnight, PieceType.WRook },
         { PieceType.WPawn, PieceType.WPawn, PieceType.WPawn, PieceType.WPawn, PieceType.WPawn, PieceType.WPawn, PieceType.WPawn, PieceType.WPawn },
         { PieceType.None, PieceType.None, PieceType.None, PieceType.None, PieceType.None, PieceType.None, PieceType.None, PieceType.None },
         { PieceType.None, PieceType.None, PieceType.None, PieceType.None, PieceType.None, PieceType.None, PieceType.None, PieceType.None },
         { PieceType.None, PieceType.None, PieceType.None, PieceType.None, PieceType.None, PieceType.None, PieceType.None, PieceType.None },
         { PieceType.None, PieceType.None, PieceType.None, PieceType.None, PieceType.None, PieceType.None, PieceType.None, PieceType.None },
         { PieceType.BPawn, PieceType.BPawn, PieceType.BPawn, PieceType.BPawn, PieceType.BPawn, PieceType.BPawn, PieceType.BPawn, PieceType.BPawn },
         { PieceType.BRook, PieceType.BKnight, PieceType.BBishop, PieceType.BQueen, PieceType.BKing, PieceType.BBishop, PieceType.BKnight, PieceType.BRook }
     };
 }
예제 #22
0
    private PieceType[,] copyGameBoard(PieceType[,] board)
    {
        PieceType[,] ret = new PieceType[8, 8];

        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                ret[i, j] = board[i, j];
            }
        }

        return(ret);
    }
예제 #23
0
    public static void PrintBoard(PieceType[,] board)
    {
        string str = "";

        for (int j = 0; j < mGridHeight; j++)
        {
            for (int i = 0; i < mGridWidth; i++)
            {
                str += board[i, j] + " ";
            }
            str += "\n";
        }
        Debug.Log(str);
    }
예제 #24
0
        void DrawActivePieceHardDrop()
        {
            PieceType[,] pieceArray = activePiece.GetPieceArray();

            for (int i = 0; i < pieceArray.GetLength(0); i++)
            {
                for (int j = 0; j < pieceArray.GetLength(1); j++)
                {
                    if (pieceArray[i, j] == activePiece.type)
                    {
                        StaticVars.drawGrid[Math.Max(0, activePiece.position.Y + i - 2), activePiece.position.X + j].Texture = StaticVars.blockTextures[(int)activePiece.type];
                    }
                }
            }
        }
예제 #25
0
        public void RemovePiece(Piece piece)
        {
            PieceType[,] pieceArray = piece.GetPieceArray();

            for (int i = 0; i < pieceArray.GetLength(0); i++)
            {
                for (int j = 0; j < pieceArray.GetLength(1); j++)
                {
                    if (NotOutOfRange(piece.position.Y + i, piece.position.X + j) && grid[piece.position.Y + i, piece.position.X + j] == piece.type)
                    {
                        grid[piece.position.Y + i, piece.position.X + j] = PieceType.Empty;
                    }
                }
            }
        }
예제 #26
0
    public static int CheckWin(PieceType[,] board, bool isPlayersTurn)
    {
        for (int i = 0; i < board.GetLength(0); i++)
        {
            for (int j = 0; j < board.GetLength(1); j++)
            {
                bool win = CheckWin(board, i, j);
                if (win)
                {
                    return(isPlayersTurn ? 1 : -1);
                }
            }
        }

        return(0);
    }
예제 #27
0
        // (C)
        /// <summary>
        /// Initialise les lignes et les colonnes du tableau de jeu.
        /// </summary>
        ///
        /// <param name="teacher">
        /// Paramètre qui détermine la taille de la grille de jeu.
        /// </param>
        public Grid(bool teacher = false)
        {
            if (teacher)
            {
                grid        = new PieceType[21, 9];
                returnArray = new PieceType[19, 9];

                StaticVars.drawGrid = new SFML.Graphics.Sprite[19, 9];
            }
            else
            {
                grid        = new PieceType[22, 10];
                returnArray = new PieceType[20, 10];
            }

            SetUpGrid();
        }
예제 #28
0
        // 是否剩下一步 ---------------------------------------------------------------------
        private bool theLastStep(PieceType [,] PiecePlace)
        {
            bool finished = true;

            for (int i = 0; i < numberOfDirs + 1; i++)
            {
                for (int j = 0; j < numberOfDirs + 1; j++)
                {
                    if (PiecePlace[i, j] != PieceType.NONE)
                    {
                        finished = false;
                        break;
                    }
                }
            }
            return(finished);
        }
예제 #29
0
    public LevelSolver(int w, int h, params PieceType[] pieces)
    {
        board = new PieceType[h, w];
        int k = 0;

        for (int i = 0; i < h; i++)
        {
            for (int j = 0; j < w; j++)
            {
                if (k < pieces.Length)
                {
                    board[i, j] = pieces[k];
                    k++;
                }
            }
        }
    }
예제 #30
0
    //Count all ways you can win based on the last token dropped
    public static int CountDown(Vector2Int startingPoint, PieceType type, PieceType[,] grid)
    {
        int count = 0;

        for (int i = startingPoint.y + 1; i < mGridHeight; i++)
        {
            if (grid[startingPoint.x, i] == type)
            {
                count++;
            }
            else
            {
                break;
            }
        }
        return(count);
    }
        private void InitializeBoard()
        {
            _board = new PieceType[TotalRows, TotalColumns];

            for (var row = 0; row < TotalRows; row++)
            {
                var pieceType = PieceType.None;

                if (row < RowsOfPieces)
                {
                    pieceType = PieceType.Black;
                }
                else if (TotalColumns - RowsOfPieces - 1 < row)
                {
                    pieceType = PieceType.White;
                }

                for (var column = 0; column < TotalColumns; column++)
                {
                    _board[row, column] = pieceType;
                }
            }
        }
예제 #32
0
        protected override void OnSizeChanged(int w, int h, int oldw, int oldh)
        {
            x_piece_count = (int)System.Math.Floor ((double)w / piece_size);
            y_piece_count = (int)System.Math.Floor ((double)h / piece_size)-10;

            x_offset = 0;
            y_offset = 0;
            pieces = new PieceType[x_piece_count, y_piece_count];

            ClearPieces ();
        }