public static LudoPiece CreateLudoWithGameobject(BoardTile startingTile)
    {
        GameObject gameObject = new GameObject("Ludo piece", typeof(LudoPiece));
        LudoPiece  ludoPiece  = gameObject.GetComponent <LudoPiece>();

        ludoPiece.isInPlay     = true;
        ludoPiece.visualObject = gameObject;

        ludoPiece.MoveToTile(startingTile);

        return(ludoPiece);
    }
Exemple #2
0
    private void createBoardPiecesForPlayer(Player player)
    {
        LudoPiece[]    playerLudos = new LudoPiece[LUDO_PIECIES_PER_PLAYER];
        BoardHomeTiles homeTiles   = getHomeTilesForPlayerId(player.GetPlayerID());

        for (int i = 0; i < LUDO_PIECIES_PER_PLAYER; i++)
        {
            playerLudos[i] = LudoPiece.CreateLudoWithGameobject(startingTile: homeTiles.StagedTiles[i]);
            playerLudos[i].SetVisualApperance(ludoSprite, ludoColors[player.GetPlayerID()]);
        }

        player.SetupLudoPieces(playerLudos);
    }
Exemple #3
0
 private void acceptInputMoveOutOfPlay(LudoPiece piece)
 {
     piece.MoveOutOfPlay();
     if (hasPlayerWon())
     {
         invokePlayerWonEvent();
         endGame();
     }
     else
     {
         advanceToNextTurn();
     }
 }
Exemple #4
0
    public bool UpdateGameState(LudoAgent agent, LudoPiece piece)
    {
        LudoAgent otherAgent = GetOtherAgent(agent);

        // Game completed
        if (agent.piece1.IsFinished && agent.piece2.IsFinished)
        {
            agent.AddReward(1f);
            agent.Done();
            otherAgent.AddReward(-1f);
            otherAgent.Done();
            LudoUI.Instance.IncrementWin(agent.index);
            Debug.LogWarningFormat("[GO] {0}:{1} {2}:{3}",
                                   agent.name, agent.GetReward(),
                                   otherAgent.name, otherAgent.GetReward());
            // reset academy
            academy.AcademyReset();
            return(true);
        }

        // didn't finish the game -0.01
        agent.AddReward(-0.01f);

        // Killed another player's piece
        // provided both of the pieces of other agent were not on the same position
        LudoPiece otherPiece = null;

        if (!otherAgent.piece1.IsFinished && piece.CurrentPosition == otherAgent.piece1.CurrentPosition)
        {
            otherPiece = otherAgent.piece1;
        }
        if (!otherAgent.piece2.IsFinished && piece.CurrentPosition == otherAgent.piece2.CurrentPosition)
        {
            otherPiece = otherAgent.piece2;
        }

        if (otherPiece != null && otherAgent.piece1.CurrentPosition != otherAgent.piece2.CurrentPosition)
        {
            Debug.LogWarningFormat("[Kill] {0}:{1} {2}:{3}",
                                   agent.name, piece.CurrentPosition,
                                   otherAgent.name, otherPiece.CurrentPosition);

            agent.AddReward(1f * otherPiece.CurrentPosition / gridSize);
            LudoUI.Instance.IncrementKill(agent.index);

            otherPiece.MoveTo(0);
            otherAgent.AddReward(-0.25f);
        }

        return(false);
    }
Exemple #5
0
    private void onLudoSelected(LudoPiece ludo)
    {
        if (selectedLudo != null)
        {
            clearPreviousHighlight();
        }

        selectedLudo = ludo;
        LegalMove legalMove = getLegalMoveForLudo(ludo);

        if (legalMove.CanMoveOutOfPlay)
        {
            sendMoveOutOfPlay();
        }
        else
        {
            GameObject highlight = createHighlightObject();
            highlight.transform.position = legalMove.AvailableMove.transform.position;
            highlight.GetComponent <Clickable>().AddListener(() =>
            {
                sendMove(legalMove.AvailableMove);
            });
        }
    }
Exemple #6
0
 public void UpdateAgent(LudoAgent agent, LudoPiece piece)
 {
     currrentAgent.text = agent.name + "-" + piece.name;
 }
Exemple #7
0
 private LegalMove getLegalMoveForLudo(LudoPiece ludo)
 {
     return(Data.legalMoves.FirstOrDefault(i => i.Ludo == ludo));
 }
Exemple #8
0
 private void acceptPlayerInput(LudoPiece ludoToMove, BoardTile tile)
 {
     ludoToMove.MoveToTile(tile);
     advanceToNextTurn();
 }
Exemple #9
0
    public void InputMove(Player player, LudoPiece piece, BoardTile tile)
    {
        validateAndThrowInvalidInput(player);

        acceptPlayerInput(piece, tile);
    }
Exemple #10
0
 public void InputPutLudoOutOfPlay(Player player, LudoPiece piece)
 {
     validateAndThrowInvalidInput(player);
     acceptInputMoveOutOfPlay(piece);
 }
Exemple #11
0
    public override void AgentAction(float[] vectorAction, string textAction)
    {
        bool gameFinished = false;

        if (lastDiceRoll != 0 && brain.brainParameters.vectorActionSpaceType == SpaceType.discrete)
        {
            int       action     = Mathf.FloorToInt(vectorAction[0]);
            LudoPiece piece      = null;
            LudoPiece otherPiece = null;

            // no action
            if (action == 0)
            {
                if (piece1.CanMove(lastDiceRoll) || piece2.CanMove(lastDiceRoll))
                {
                    Debug.LogWarningFormat("[NoMove:A]:{0} [D]:{1} [P]:{2}:{3} [OP]:{4}:{5}",
                                           gameObject.name, lastDiceRoll,
                                           piece1.name, piece1.CurrentPosition,
                                           piece2.name, piece2.CurrentPosition);
                    AddReward(-0.25f);
                }
                else
                {
                    Debug.LogFormat("[NoMove:A]:{0} [D]:{1} [P]:{2}:{3} [OP]:{4}:{5}",
                                    gameObject.name, lastDiceRoll,
                                    piece1.name, piece1.CurrentPosition,
                                    piece2.name, piece2.CurrentPosition);
                    AddReward(0.1f);
                }
            }
            // move piece 1
            else if (action == 1)
            {
                piece      = piece1;
                otherPiece = piece2;
            }
            // move piece 2
            else if (action == 2)
            {
                piece      = piece2;
                otherPiece = piece1;
            }

            if (piece != null)
            {
                if (piece.CanMove(lastDiceRoll))
                {
                    piece.MoveForward(lastDiceRoll);
                    gameFinished = game.UpdateGameState(this, piece);
                    Debug.LogFormat("[A]:{0} [D]:{1} [P]:{2}:{3} [OP]:{4}:{5}",
                                    gameObject.name, lastDiceRoll,
                                    piece.name, piece.CurrentPosition,
                                    otherPiece.name, otherPiece.CurrentPosition);
                    // UI
                    game.UpdateAgent(this, piece);
                    game.UpdateDice(lastDiceRoll);
                }
                else
                {
                    if (!otherPiece.CanMove(lastDiceRoll))
                    {
                        // neither piece can move
                        AddReward(-0.1f);
                        Debug.LogWarningFormat("[NA:NN]:{0} [D]:{1} [P]:{4}:{5} [OP]:{2}:{3}",
                                               gameObject.name, lastDiceRoll,
                                               piece.name, piece.CurrentPosition,
                                               otherPiece.name, otherPiece.CurrentPosition);
                    }
                    else //if (otherPiece.CanMove(lastDiceRoll))
                    {
                        AddReward(-0.1f);
                        Debug.LogWarningFormat("[NA:NP]:{0} [D]:{1} [P]:{4}:{5} [OP]:{2}:{3}",
                                               gameObject.name, lastDiceRoll,
                                               piece.name, piece.CurrentPosition,
                                               otherPiece.name, otherPiece.CurrentPosition);
                    }

                    if (piece.IsFinished)
                    {
                        AddReward(-0.1f);
                        Debug.LogWarningFormat("[NA:F]:{0} [D]:{1} [P]:{2}:{3} [OP]:{4}:{5}",
                                               gameObject.name, lastDiceRoll,
                                               piece.name, piece.CurrentPosition,
                                               otherPiece.name, otherPiece.CurrentPosition);
                    }
                }
            }
        }

        if (!gameFinished)
        {
            if (brain.brainType == BrainType.External)
            {
                game.RequestNextDecision(this);
            }
            else   //if (brain.brainType == BrainType.Internal)
            {
                StartCoroutine(RequestNextDecisionIn(2f));
            }
        }
    }
Exemple #12
0
    public void MovePiece(int pieceIndex, int position)
    {
        LudoPiece piece = pieceIndex == 1 ? piece1 : piece2;

        piece.MoveTo(position);
    }
 public LegalMove(LudoPiece ludo, BoardTile move, bool canMoveOutOfPlay)
 {
     this.Ludo             = ludo;
     this.AvailableMove    = move;
     this.CanMoveOutOfPlay = canMoveOutOfPlay;
 }
Exemple #14
0
 public void AppendLudo(LudoPiece ludo)
 {
     IsOccupied  = true;
     CurrentLudo = ludo;
 }
Exemple #15
0
 /// <summary>
 /// Reset the board tile to the default state, where it is not occupied by any piece
 /// </summary>
 public void ResetState()
 {
     IsOccupied  = false;
     CurrentLudo = null;
 }