Ejemplo n.º 1
0
    public void SpawnPiece(GridCoords coords, GameObject piece, float yRotation, PlayerType playerType)
    {
        Node pieceNode = GetNodeAt(coords.row, coords.col);

        Vector3    pRotation    = piece.transform.rotation.eulerAngles;
        Quaternion newPRotation = Quaternion.Euler(pRotation.x, yRotation, pRotation.z);

        GameObject pieceObject = Instantiate(piece, pieceNode.transform.position + Vector3.up * 1.2f, newPRotation) as GameObject;

        pieceObject.transform.localScale = Vector3.zero;         //for scaling in start from zero
        Piece pieceScript = pieceObject.GetComponent(typeof(Piece)) as Piece;

        //assign mat and player type
        Material mat    = null;
        GCPlayer player = null;

        switch (playerType)
        {
        case PlayerType.P1:
            mat    = GameManager.Instance.PieceP1;
            player = GameManager.Instance.P1;
            break;

        case PlayerType.P2:
            mat    = GameManager.Instance.PieceP2;
            player = GameManager.Instance.P2;
            break;
        }
        pieceObject.GetComponent <Renderer>().material = mat;
        player.AddPieces(pieceScript);
        pieceScript.PieceMovement = Creator.CreatePieceMovement(pieceScript.MovementType, player, pieceScript);

        if (pieceScript)        //if exists type then scale
        {
            pieceScript.ScaleIn(Random.Range(0f, 1f), Random.Range(1f, 2f), piece.transform.localScale);
        }


        pieceScript.UpdateNode(pieceNode);
    }
Ejemplo n.º 2
0
    public void GameOver(GCPlayer winner, GameOverType gameOverType)
    {
        AddGame();
        switch (gameOverType)
        {
        case GameOverType.CHECKMATE:
            if (winner == p2)
            {
                winnerText.text = "CHECKMATE: BLACK wins";
                AddScore(PLAYER_BLACK);
            }
            else if (winner == p1)
            {
                winnerText.text = "CHECKMATE: WHITE wins";
                AddScore(PLAYER_WHITE);
            }
            break;

        case GameOverType.STALEMATE:
            winnerText.text = "STALEMATE: It's a tie";
            break;

        case GameOverType.OUT_OF_TIME:
            if (winner == p1)
            {
                winnerText.text = "OUT OF TIME: WHITE wins";
                AddScore(PLAYER_WHITE);
            }
            else if (winner == p2)
            {
                winnerText.text = "OUT OF TIME: BLACK wins";
                AddScore(PLAYER_BLACK);
            }
            break;
        }
        continueButton.SetActive(true);
        Piece.AllPieces = new List <Piece>();
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }
Ejemplo n.º 3
0
    //Modifies the move if modify = true
    //not safe
    public static bool IsCheckMove(GCPlayer player, Piece piece, Node tNode, bool modify)
    {
        Node oldNode = piece.Node;

        piece.UpdateNode(tNode);
        Piece checkedBy = player.CheckedBy;

        player.ClearCheck();
        GameManager.Instance.Opponent(player).ComputePieces();
        if (player.IsChecked)
        {
            piece.UpdateNode(oldNode);
            player.CheckedBy = checkedBy;
            return(true);
        }

        if (!modify)
        {
            piece.UpdateNode(oldNode);
            player.CheckedBy = checkedBy;
        }
        return(false);
    }
Ejemplo n.º 4
0
    public void GameOver(GCPlayer winner, GameOverType gameOverType)
    {
        AddGame();
        switch (gameOverType)
        {
        case GameOverType.CHECKMATE:
            if (winner == p2)
            {
                winnerText.text = "CHECKMATE: BLACK wins";
                AddScore(PLAYER_BLACK);
            }
            else if (winner == p1)
            {
                winnerText.text = "CHECKMATE: WHITE wins";
                AddScore(PLAYER_WHITE);
            }
            break;

        case GameOverType.STALEMATE:
            winnerText.text = "STALEMATE: It's a tie";
            break;

        case GameOverType.OUT_OF_TIME:
            if (winner == p1)
            {
                winnerText.text = "OUT OF TIME: WHITE wins";
                AddScore(PLAYER_WHITE);
            }
            else if (winner == p2)
            {
                winnerText.text = "OUT OF TIME: BLACK wins";
                AddScore(PLAYER_BLACK);
            }
            break;
        }
        continueButton.SetActive(true);
    }
Ejemplo n.º 5
0
 public CrossMovement(GCPlayer player, Piece piece) : base(player, piece)
 {
     BoundComputations += ComputeBound;
 }
Ejemplo n.º 6
0
 public Movement(GCPlayer player, Piece piece)
 {
     this.player        = player;
     this.piece         = piece;
     BoundComputations += ClearPossibles;
 }
Ejemplo n.º 7
0
    public void OnInputEvent(InputActionType action)
    {
        switch (action)
        {
        case InputActionType.GRAB_PIECE:
            if (GameManager.Instance.temporal_move != null)
            {
                break;
            }
            Node gNode = Finder.RayHitFromScreen <Node>(Input.mousePosition);
            if (gNode == null)
            {
                break;
            }
            piece = gNode.Piece;
            if (piece == null)
            {
                break;
            }
            if (!piece.IsReady)
            {
                break;
            }
            if (Click(gNode) && piece && Has(piece) && Click(piece))
            {
                piece.Pickup();
                piece.Compute();
                piece.HighlightPossibleMoves();
                piece.HighlightPossibleEats();
                GameManager.Instance.temporal_source_position = piece.ChessCoords;
                GameManager.Instance.GameState.Grab();
            }

            //check clickable for tile and piece then pass Player
            //check if player has piece - PIECE
            //check if player has piece if not empty - NODE
            break;

        case InputActionType.CANCEL_PIECE:
            if (piece != null)
            {
                //if (!piece.IsReady) break;
                piece.Drop();
                piece = null;
                GameManager.Instance.GameState.Cancel();
            }
            break;

        case InputActionType.PLACE_PIECE:
            // Event afer select a target position
            Node tNode = Finder.RayHitFromScreen <Node>(Input.mousePosition);
            if (tNode == null)
            {
                break;
            }
            Piece tPiece = tNode.Piece;
            GameManager.Instance.temporal_target_position = tNode.ChessCoords;
            if (tPiece == null)
            {
                if (piece.IsPossibleMove(tNode))
                {
                    if (Rules.IsCheckMove(this, piece, tNode, true))
                    {
                        Debug.Log("Move checked");                                 // do nothing
                    }
                    else
                    {
                        piece.MoveToXZ(tNode, Drop);
                        GameManager.Instance.GameState.Place();
                    }
                }
            }
            else
            {
                if (piece.IsPossibleEat(tNode))
                {
                    if (Rules.IsCheckEat(this, piece, tNode, true))
                    {
                        Debug.Log("Eat checked");                                 // do nothing
                    }
                    else
                    {
                        GCPlayer oppPlayer = GameManager.Instance.Opponent(this);
                        oppPlayer.RemovePiece(tPiece);
                        AddEatenPieces(tPiece);
                        tPiece.ScaleOut(0.2f, 1.5f);
                        piece.MoveToXZ(tNode, Drop);
                        GameManager.Instance.GameState.Place();
                    }
                }
            }
            if (GameManager.Instance.temporal_move == null)
            {
                GameManager.Instance.temporal_move = Tuple.Create <string, string>(
                    GameManager.Instance.temporal_source_position, GameManager.Instance.temporal_target_position);
            }
            break;
        }
    }
Ejemplo n.º 8
0
    public void UpdateAI()
    {
        if (grid == null)
        {
            grid = GameObject.FindObjectOfType <Grid>();
        }

        if (myTurn && !didTurn)
        {
            didTurn = true;

            State boardState = GetBoardState();

            Debug.Log("State visited counter: " + boardState.TimesVisited);

            if (brain == null)
            {
                brain = new StateAgent(boardState);
            }
            else
            {
                brain.SetState(boardState);
            }

            //get action from brain, execute.

            StateAction action = brain.GetChosenActionForCurrentState();

            string[] moves = Regex.Split(action.ActionString, "to");
            string[] from  = Regex.Split(moves[0], "-");
            string[] to    = Regex.Split(moves[1], "-");

            Debug.Log(action.ActionString + ", Quality: " + action.GetDeepEvaluation() + " (" + action.ActionEvaluation + ") --- " + brain.LearnedStates + "///" + brain.EvaluatedActions);

            if (action.GetDeepEvaluation() != action.ActionEvaluation)
            {
                Debug.Log("///////////////////////////////////////////////////////////////");
            }

            foreach (Node n in grid.grid)
            {
                n.UnhighlightEat();
                n.UnhighlightMove();
            }

            Node fromNode = grid.GetNodeAt(int.Parse(from[1]), int.Parse(from[0]));
            Node toNode   = grid.GetNodeAt(int.Parse(to[1]), int.Parse(to[0]));

            fromNode.HighlightMove();
            toNode.HighlightEat();

            piece = fromNode.Piece;
            piece.Pickup();
            GameManager.Instance.GameState.Grab();
            int reward = 0;

            Piece tPiece = toNode.Piece;
            if (tPiece == null)
            {
                if (piece.IsPossibleMove(toNode))
                {
                    if (Rules.IsCheckMove(this, piece, toNode, true))
                    {
                        Debug.Log("Move checked, not allowed"); // do nothing

                        brain.EvaluateLastAction(-10000);
                        GameManager.Instance.GameState.Checkmate();
                        GameManager.Instance.GameOver(GameManager.Instance.PlayerOponent, GameOverType.CHECKMATE);
                    }
                    else
                    {
                        piece.MoveToXZ(toNode, Drop);
                        GameManager.Instance.GameState.Place();
                    }
                }
            }
            else
            {
                if (piece.IsPossibleEat(toNode))
                {
                    if (Rules.IsCheckEat(this, piece, toNode, true))
                    {
                        Debug.Log("Eat checked"); // do nothing

                        brain.EvaluateLastAction(-10000);
                        GameManager.Instance.GameState.Checkmate();
                        GameManager.Instance.GameOver(GameManager.Instance.PlayerOponent, GameOverType.CHECKMATE);
                    }
                    else
                    {
                        GCPlayer oppPlayer = GameManager.Instance.Opponent(this);

                        oppPlayer.brain.EvaluateLastAction(-tPiece.GetPieceValue());
                        reward = tPiece.GetPieceValue();

                        oppPlayer.RemovePiece(tPiece);
                        AddEatenPieces(tPiece);
                        tPiece.ScaleOut(0.2f, 1.5f);
                        piece.MoveToXZ(toNode, Drop);
                        GameManager.Instance.GameState.Place();
                    }
                }
            }

            State newState = GetBoardState();

            brain.PerformStateAction(action, newState);
            brain.EvaluateLastAction(reward);
        }
    }