Esempio n. 1
0
    public override void Start()
    {
        base.Start();
        simulatedTimeScale = 1.0f;
        OnGameOverEvents  += () => StartCoroutine(StartSlowMoCoroutine());

        if (UnityAds.Instance)
        {
            if (PlayerPrefs.GetInt("NextAdvertCountdown", Random.Range(2, 5)) == 0)
            {
                PlayerPrefs.SetInt("NextAdvertCountdown", Random.Range(2, 5));
                UnityAds.Instance.ShowRewardedAdWhenReady();
            }
            PlayerPrefs.SetInt("NextAdvertCountdown", PlayerPrefs.GetInt("NextAdvertCountdown", Random.Range(2, 5)) - 1);
        }
    }
Esempio n. 2
0
    public void Awake()
    {
        state = InitiateBoardState();
        InitiateMoveNotificationDelegate();
        turnSwapper = false;

        promoSprites = InitiatePawnPromotionObjects();

        p[0] = new GameObject().AddComponent <Human>();
//        p[0] = new GameObject().AddComponent<Computer>();
        p[0].isWhite = true;
        p[1]         = new GameObject().AddComponent <Computer>();
//        p[1] = new GameObject().AddComponent<Human>();
        gameState = p[0].PickAPiece;

        //ply calculations
//        depthSearch = gameObject.AddComponent<DepthSearching>();
//        gameState = depthSearch.DepthSearch;
    }
Esempio n. 3
0
    void GameOver()
    {
        //Game state to display game over screen after a checkmate occurred or a stalemate

        //Need to let player navigate menu and click on options
        //A generalized mouse-hovering-button-clicking script might be nice here
        Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        //Start with option to reset board (call state = InitiateBoardState(), with possibility of inverting pieces?)
        if (Input.GetMouseButtonDown(0)) //Left click
        {
            Debug.Log("Reset the game");

            ResetBoard();

            //MAYBE INSTEAD OF REINITILIAZING, I CAN UNDO EVERY MOVE UP TO THE FIRST ONE

            gameState = p[0].PickAPiece;
        }
    }
Esempio n. 4
0
    public void IsItGameOver()
    {
        bool IsEnemyKingChecked()
        {
            //Verify that king is attacked by grabbedPiece
            List <Tile> pieceAttacks = new List <Tile>();

            if (grabbedPiece.isPawn == null)
            {
                pieceAttacks = grabbedPiece.PossibleMoves();
            }
            else
            {
                pieceAttacks = grabbedPiece.isPawn.Attacks();
            }
            for (int i = 0; i < pieceAttacks.Count; i++)
            {
                if (pieceAttacks[i] == (grabbedPiece.isWhite ? bKTile : wKTile))
                {
                    return(true);
                }
            }
            return(false);
        }

        bool EnemyCanMove() //Look for possible moves that protect enemy king - return false as soon as one is found, otherwise it has to be checkmate
        {
            for (int i = 0; i < 64; i++)
            {
                if (state[i].piece != null && state[i].piece.isWhite != grabbedPiece.isWhite)
                {
                    List <Tile> possibleMoves = state[i].piece.PossibleMoves();
                    if (possibleMoves != null)
                    {
                        for (int j = 0; j < possibleMoves.Count; j++)
                        {
                            if (state[i].piece.CanMove(state[i], possibleMoves[j]))
                            {
                                return(true);
                            }
                        }
                    }
                }
            }
            return(false);
        }

        bool HaveThreeMovesBeenRepeated()
        {
            //**
            //**
            //**
            //**
            //Refer to TrackMoves(p, t, t) method
            //**
            //**
            //**
            //**
            return(false);
        }

        bool HasADeadPositionBeenReached()
        {
            //Dead positions:
            //kings only
            //king vs king + bishop
            //king vs king + knight
            //king + bishop vs king + bishop where both bishops are of the same tile color

            //Would be faster to just keep two arrays stored throughout the game which get updated by the eating function
            List <Piece> p0Pieces = new List <Piece>();
            List <Piece> p1Pieces = new List <Piece>();

            p0Pieces.Add(p[0].isWhite ? wKTile.piece : bKTile.piece);
            p1Pieces.Add(p[1].isWhite ? wKTile.piece : bKTile.piece);
            for (int i = 0; i <  64; i++)
            {
                if (state[i].piece != null)
                {
                    if (state[i].piece.isWhite == p[0].isWhite && state[i].piece.isKing == null)
                    {
                        p0Pieces.Add(state[i].piece);
                    }
                    else if (state[i].piece.isWhite == p[1].isWhite && state[i].piece.isKing == null)
                    {
                        p1Pieces.Add(state[i].piece);
                    }
                }
            }

            if (p0Pieces.Count == 1 && p1Pieces.Count == 1) //First piece in the list is always the king, so a count of 1 means the kings are the only pieces left on the board
            {
                return(true);
            }

            //One knight on each side - apparently this isn't a dead position
            else if (p0Pieces.Count == 2 && p0Pieces[1].isKnight != null && p1Pieces.Count == 2 && p1Pieces[0].isKnight != null)
            {
                return(true);
            }

            //One bishop or one knight left on either side of the board
            else if (((p0Pieces.Count == 2 && (p0Pieces[1].isBishop != null || p0Pieces[1].isKnight != null)) && p1Pieces.Count == 1) || ((p1Pieces.Count == 2 && (p1Pieces[1].isBishop != null || p1Pieces[1].isKnight != null)) && p0Pieces.Count == 1))
            {
                return(true);
            }

            //There are other situations where a dead position may be reached, such as the case where all pawns are blocking each other to the point that none can move
            //and they block the kings from crossing, and there is no piece that can ever target any of the enemy pawns
            //E.G. black pawns on black tiles blocking white pawns on white tiles, with white bishop on white tile (that can't reach white pawns) and black bishop on black tile (that can't reach white pawns)
            //For this I think I need to verify that the possible moves come from bishops, and that the bishop can never hit any of the pawns
            //this is a highly complicated scenario, that even chess.com doesn't do well, so maybe I should avoid trying to figure it out

            return(false);
        } //Find whether state is unwinnable

        bool hasADeadPositionBeenReached = HasADeadPositionBeenReached();
        bool haveThreeMovesBeenRepeated  = HaveThreeMovesBeenRepeated();
        bool moveCountCapReached         = moveCount >= 500; //STANDARDS ARE 50, BUT FOR COMPUTER VS COMPUTER SHENANIGANS, IT'S MORE ENTERTAINING TO HAVE A HIGH CAP
        bool enemyCanMove       = EnemyCanMove();            //Look for checkmate/stalemate
        bool isEnemyKingChecked = IsEnemyKingChecked();

        if (haveThreeMovesBeenRepeated || hasADeadPositionBeenReached || moveCountCapReached)
        {
            if (haveThreeMovesBeenRepeated)
            {
                Debug.Log("Stalemate - 3 moves repeated");
            }
            else if (hasADeadPositionBeenReached)
            {
                Debug.Log("Stalemate - game state cannot be won");
            }
            else if (moveCountCapReached)
            {
                Debug.Log("Stalemate - move count exceeded " + moveCount.ToString());
            }
            Debug.Log("moveCount: " + moveCount);
            gameState = GameOver;
            return;
        }

        if (!enemyCanMove)
        {
            if (!isEnemyKingChecked)
            {
                Debug.Log("Stalemate - Enemy can't move");
                gameState = GameOver;
            }
            else
            {
                Debug.Log("Checkmate");
                gameState = GameOver;
            }
        }
    }