public TurnResponse Turn(GameState gameState)
        {
            var    turnResponse = new TurnResponse();
            Player player       = gameState.Players.SingleOrDefault(x => x.Me == "true");

            if (player == null)
            {
                return(turnResponse);                             // todo: error handling
            }
            Position preMovePosition = player.Position;
            int      directionToMove = new RandomMoveLogic(gameState.GridSize, preMovePosition).Direction;

            // figure out new position, then send to shoot generator.
            Position postMovePosition = Directions.ConvertDirectionToPosition(directionToMove, preMovePosition);

            int directionToShoot = new RandomShootLogic(gameState.GridSize, postMovePosition).Direction;

            turnResponse.MoveDirection  = directionToMove;
            turnResponse.ShootDirection = directionToShoot;
            turnResponse.PlayerId       = player.Id;

            Thread.Sleep(2000);             // simulate a slow response time
            player.Log();
            turnResponse.Log();
            Console.WriteLine();
            return(turnResponse);
        }
Exemple #2
0
    // Makes the list of all the possible turns, then picks up a random one
    public override TurnResponse PlayTurn()
    {
        List <TurnResponse> possibleTurns = new List <TurnResponse>();

        PieceState[][] table = InfoGiver.table;

        // Iterates over all the table twice to find all the possible turns (yes this is disgusting, but done in 1 second)
        TurnResponse tr = null;

        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                for (int k = 0; k < 5; k++)
                {
                    for (int l = 0; l < 5; l++)
                    {
                        if (team == Team.A)
                        {
                            tr = new TurnResponse(InfoGiver.cardA1.cardName, new Vector2Int(i, j), new Vector2Int(k, l));
                            if (InfoGiver.IsTurnValid(table, InfoGiver.cardA1, InfoGiver.cardA2, Team.A, tr))
                            {
                                possibleTurns.Add(tr);
                            }

                            tr = new TurnResponse(InfoGiver.cardA2.cardName, new Vector2Int(i, j), new Vector2Int(k, l));
                            if (InfoGiver.IsTurnValid(table, InfoGiver.cardA1, InfoGiver.cardA2, Team.A, tr))
                            {
                                possibleTurns.Add(tr);
                            }
                        }
                        if (team == Team.B)
                        {
                            tr = new TurnResponse(InfoGiver.cardB1.cardName, new Vector2Int(i, j), new Vector2Int(k, l));
                            if (InfoGiver.IsTurnValid(table, InfoGiver.cardB1, InfoGiver.cardB2, Team.B, tr))
                            {
                                possibleTurns.Add(tr);
                            }

                            tr = new TurnResponse(InfoGiver.cardB2.cardName, new Vector2Int(i, j), new Vector2Int(k, l));
                            if (InfoGiver.IsTurnValid(table, InfoGiver.cardB1, InfoGiver.cardB2, Team.B, tr))
                            {
                                possibleTurns.Add(tr);
                            }
                        }
                    }
                }
            }
        }

        if (possibleTurns.Count == 0)
        {
            return(null);
        }

        int rand = Random.Range(0, possibleTurns.Count);

        return(possibleTurns[rand]);
    }
Exemple #3
0
    public void ApplyTurn(TurnResponse turn)
    {
        if (table[turn.destination.x][turn.destination.y] != null)
        {
            Destroy(table[turn.destination.x][turn.destination.y].gameObject);
            table[turn.destination.x][turn.destination.y] = null;
        }

        MovePiece(turn.source, turn.destination);
    }
    // Makes the list of all the possible turns, then picks up a random one
    public override TurnResponse PlayTurn()
    {
        PieceState[][]      PlateauCourant    = InfoGiver.table;
        List <TurnResponse> MesCoupsPossibles = ComputeCoupsPossibles(PlateauCourant, MaTeam);
        List <TurnResponse> SesCoupsPossibles = ComputeCoupsPossibles(PlateauCourant, SaTeam);

        TurnResponse MeilleurCoup = EvalFinale(PlateauCourant, MaTeam, MesCoupsPossibles, SesCoupsPossibles);

        return(MeilleurCoup);
    }
Exemple #5
0
    private List <TurnResponse> GetAllTurns()
    {
        List <TurnResponse> possibleTurns = new List <TurnResponse>();

        PieceState[][] table = InfoGiver.table;

        // Iterates over all the table twice to find all the possible turns (yes this is disgusting, but done in 1 second)
        TurnResponse tr = null;

        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                for (int k = 0; k < 5; k++)
                {
                    for (int l = 0; l < 5; l++)
                    {
                        if (team == Team.A)
                        {
                            tr = new TurnResponse(InfoGiver.cardA1.cardName, new Vector2Int(i, j), new Vector2Int(k, l));
                            if (InfoGiver.IsTurnValid(table, InfoGiver.cardA1, InfoGiver.cardA2, Team.A, tr))
                            {
                                possibleTurns.Add(tr);
                            }

                            tr = new TurnResponse(InfoGiver.cardA2.cardName, new Vector2Int(i, j), new Vector2Int(k, l));
                            if (InfoGiver.IsTurnValid(table, InfoGiver.cardA1, InfoGiver.cardA2, Team.A, tr))
                            {
                                possibleTurns.Add(tr);
                            }
                        }
                        if (team == Team.B)
                        {
                            tr = new TurnResponse(InfoGiver.cardB1.cardName, new Vector2Int(i, j), new Vector2Int(k, l));
                            if (InfoGiver.IsTurnValid(table, InfoGiver.cardB1, InfoGiver.cardB2, Team.B, tr))
                            {
                                possibleTurns.Add(tr);
                            }

                            tr = new TurnResponse(InfoGiver.cardB2.cardName, new Vector2Int(i, j), new Vector2Int(k, l));
                            if (InfoGiver.IsTurnValid(table, InfoGiver.cardB1, InfoGiver.cardB2, Team.B, tr))
                            {
                                possibleTurns.Add(tr);
                            }
                        }
                    }
                }
            }
        }

        return(possibleTurns);
    }
Exemple #6
0
 public static bool HandlePassResponse(TurnResponse response,
                                       Player player,
                                       Table table)
 {
     if (player.HasDraw)
     {
         table.TurnToNextPlayer();
         table.NotifyTurnToAllPlayers();
         player.HasDraw = false;
         return(true);
     }
     player.SendError("You need to play or draw a card");
     table.NotifyYourTurnToCurrentPlayer();
     return(false);
 }
Exemple #7
0
 public static bool HandleDrawResponse(TurnResponse response,
                                       Player player,
                                       Table table)
 {
     if (!player.HasDraw)
     {
         var card = table.StackCard.PopRandomCard();
         player.Hand.AddCard(card);
         player.HasDraw = true;
         table.NotifyYourTurnToCurrentPlayer();
         return(true);
     }
     player.SendError("You cannot draw more than once");
     table.NotifyYourTurnToCurrentPlayer();
     return(false);
 }
Exemple #8
0
    // Applies the given turn to the given board ; THE TURN MUST BE VALID (risk of crash or unknown behavior)
    public static BoardState ApplyTurn(BoardState board, TurnResponse turn)
    {
        BoardState ret = board.DeepCopy();

        ret.table[turn.destination.x][turn.destination.y] = ret.table[turn.source.x][turn.source.y];
        ret.table[turn.source.x][turn.source.y]           = null;

        // Swapping the card and changing the current team
        if (ret.currentTeam == Team.A)
        {
            if (ret.cardA1.cardName == turn.cardName)
            {
                Card tmp = ret.cardA1;
                ret.cardA1   = ret.freeCard;
                ret.freeCard = tmp;
            }
            else
            {
                Card tmp = ret.cardA2;
                ret.cardA2   = ret.freeCard;
                ret.freeCard = tmp;
            }

            ret.currentTeam = Team.B;
        }
        else
        {
            if (ret.cardB1.cardName == turn.cardName)
            {
                Card tmp = ret.cardB1;
                ret.cardB1   = ret.freeCard;
                ret.freeCard = tmp;
            }
            else
            {
                Card tmp = ret.cardB2;
                ret.cardB2   = ret.freeCard;
                ret.freeCard = tmp;
            }

            ret.currentTeam = Team.A;
        }

        return(ret);
    }
Exemple #9
0
 private static bool HandlePlayResponse(TurnResponse response, Player player,
                                        Table table)
 {
     if (!CheckCardPossession(player, response.Card, table) ||
         !CheckPlayerTurn(player, table) ||
         !CheckGameIsRunning(table))
     {
         player.SendError("Received invalid informations, try again");
         table.NotifyYourTurnToCurrentPlayer();
     }
     if (table.PutCardOnTable(player, response.Card))
     {
         player.HasDraw = false;
         return(true);
     }
     player.SendError("The card cannot be put on the table, check and try again");
     table.NotifyYourTurnToCurrentPlayer();
     return(false);
 }
Exemple #10
0
    public override TurnResponse PlayTurn()
    {
        BoardState board = InfoGiver.board;

        System.Tuple <TurnResponse, TurnResponse> t = null;

        if (isOutputNeeded)
        {
            linesToWrite = new List <string>();
            System.IO.File.WriteAllText(@"E:\Données\Programmes\Unity\AIOnitama\Gabz_Output\out.txt", string.Empty);
        }

        TurnResponse ret = DeepAnalysis(board, team, startingDepth).Item1;

        if (isOutputNeeded)
        {
            System.IO.File.WriteAllLines(@"E:\Données\Programmes\Unity\AIOnitama\Gabz_Output\out.txt", linesToWrite.ToArray());
        }

        return(ret);
    }
Exemple #11
0
 public static bool HandleUnoResponse(TurnResponse response,
                                      Player player,
                                      Table table)
 {
     if (table.CurrentPlayer != player)
     {
         player.SendError("It's not your turn, please wait.");
         return(false);
     }
     if (player.Hand.Cards.Count != 2)
     {
         player.SendError("Sorry, but you UNO wrong ! :(");
         for (var i = 0; i != 2; i++)
         {
             var card = table.StackCard.PopRandomCard();
             player.Hand.AddCard(card);
         }
         table.NotifyYourTurnToCurrentPlayer();
         return(false);
     }
     player.HasUno = true;
     return(true);
 }
Exemple #12
0
        public static bool Handle(TurnResponse response, Player player, Table table)
        {
            try
            {
                var handlers =
                    new Dictionary <TurnResponse.TurnType,
                                    Func <TurnResponse, Player, Table, bool> >
                {
                    { TurnResponse.TurnType.Play, HandlePlayResponse },
                    { TurnResponse.TurnType.Draw, HandleDrawResponse },
                    { TurnResponse.TurnType.Pass, HandlePassResponse },
                    { TurnResponse.TurnType.Uno, HandleUnoResponse }
                };

                return(handlers[response.Type](response, player, table));
            }
            catch (Exception e)
            {
                player.SendError("Cannot translate you reponse, please try again.");
                Console.Error.WriteLine($"[ERR] {e.Message}");
            }
            return(false);
        }
 public static void Log(this TurnResponse turnResponse)
 {
     Console.WriteLine($"move: {turnResponse.MoveDirection}, shoot: {turnResponse.ShootDirection}");
 }
Exemple #14
0
    // UTILS

    // Returns true if and only if the described turn is valid
    public static bool IsTurnValid(PieceState[][] table, Card card1, Card card2, Team team, TurnResponse turn)
    {
        if (turn == null)
        {
            return(false);
        }
        if (team == Team.none)
        {
            return(false);
        }


        // The source and the destination have to be in the bounds of the map
        if (turn.source.x < 0 || turn.source.x >= 5 || turn.source.y < 0 || turn.source.y >= 5)
        {
            return(false);
        }
        if (turn.destination.x < 0 || turn.destination.x >= 5 || turn.destination.y < 0 || turn.destination.y >= 5)
        {
            return(false);
        }

        // The source must contain a movable Piece
        if (table[turn.source.x][turn.source.y] == null || table[turn.source.x][turn.source.y].team != team)
        {
            return(false);
        }

        // The destination must not contain a movable Piece
        if (table[turn.destination.x][turn.destination.y] != null && table[turn.destination.x][turn.destination.y].team == team)
        {
            return(false);
        }

        // The player must own the designated card
        Card playedCard = null;

        if (card1.cardName == turn.cardName)
        {
            playedCard = card1;
        }
        if (card2.cardName == turn.cardName)
        {
            playedCard = card2;
        }

        if (playedCard == null)
        {
            return(false);
        }

        // The move must be allowed by the designated card
        Vector2Int moveVector = turn.destination - turn.source;

        if (moveVector.x < -2 || moveVector.x >= 3 || moveVector.y < -2 || moveVector.y >= 3)
        {
            return(false);
        }
        if (team == Team.A && playedCard.GetMoves()[moveVector.x + 2][moveVector.y + 2] == 0)
        {
            return(false);
        }
        if (team == Team.B && playedCard.GetMovesReversed()[moveVector.x + 2][moveVector.y + 2] == 0)
        {
            return(false);
        }

        return(true);
    }
Exemple #15
0
    public bool ValidateTurn(TurnResponse turn, Team team)
    {
        if (turn == null)
        {
            GameManager.instance.EndGame(team == Team.A ? Team.B : Team.A, "No response given");
            return(false);
        }
        if (team == Team.none)
        {
            GameManager.instance.EndGame(team == Team.A ? Team.B : Team.A, "Invalid team");
            return(false);
        }

        // The source and the destination have to be in the bounds of the map
        if (turn.source.x < 0 || turn.source.x >= 5 || turn.source.y < 0 || turn.source.y >= 5)
        {
            GameManager.instance.EndGame(team == Team.A ? Team.B : Team.A, "The move source is out of bounds");
            return(false);
        }
        if (turn.destination.x < 0 || turn.destination.x >= 5 || turn.destination.y < 0 || turn.destination.y >= 5)
        {
            GameManager.instance.EndGame(team == Team.A ? Team.B : Team.A, "The move destination is out of bounds");
            return(false);
        }

        // The source must contain a movable Piece
        if (table[turn.source.x][turn.source.y] == null || table[turn.source.x][turn.source.y].team != team)
        {
            GameManager.instance.EndGame(team == Team.A ? Team.B : Team.A, "The move source does not contain a movable piece");
            return(false);
        }

        // The destination must not contain a movable Piece
        if (table[turn.destination.x][turn.destination.y] != null && table[turn.destination.x][turn.destination.y].team == team)
        {
            Debug.Log(turn.destination.x.ToString() + " - " + turn.destination.y.ToString());
            Debug.Log(table[turn.destination.x][turn.destination.y].type);
            Debug.Log(table[turn.destination.x][turn.destination.y].team);

            GameManager.instance.EndGame(team == Team.A ? Team.B : Team.A, "The move destination contains a movable piece");
            return(false);
        }

        // The player must own the designated card
        Card playedCard = null;

        if (team == Team.A && GameManager.instance.cardA1.cardName == turn.cardName)
        {
            playedCard = GameManager.instance.cardA1;
        }
        if (team == Team.A && GameManager.instance.cardA2.cardName == turn.cardName)
        {
            playedCard = GameManager.instance.cardA2;
        }
        if (team == Team.B && GameManager.instance.cardB1.cardName == turn.cardName)
        {
            playedCard = GameManager.instance.cardB1;
        }
        if (team == Team.B && GameManager.instance.cardB2.cardName == turn.cardName)
        {
            playedCard = GameManager.instance.cardB2;
        }
        if (playedCard == null)
        {
            GameManager.instance.EndGame(team == Team.A ? Team.B : Team.A, "The player does not own the right card");
            return(false);
        }

        // The move must be allowed by the designated card
        Vector2Int moveVector = turn.destination - turn.source;

        if (moveVector.x < -2 || moveVector.x >= 3 || moveVector.y < -2 || moveVector.y >= 3)
        {
            GameManager.instance.EndGame(team == Team.A ? Team.B : Team.A, "This move exceeds 2 cells in height or width");
            return(false);
        }
        if (team == Team.A && playedCard.GetMoves()[moveVector.x + 2][moveVector.y + 2] == 0)
        {
            GameManager.instance.EndGame(team == Team.A ? Team.B : Team.A, "The \"" + turn.cardName + "\" card does not allow this move");
            return(false);
        }
        if (team == Team.B && playedCard.GetMovesReversed()[moveVector.x + 2][moveVector.y + 2] == 0)
        {
            GameManager.instance.EndGame(team == Team.A ? Team.B : Team.A, "The \"" + turn.cardName + "\" card does not allow this move");
            return(false);
        }

        return(true);
    }
Exemple #16
0
    private List <TurnResponse> GetAllTurns(BoardState board, Team team)
    {
        List <TurnResponse> possibleTurns = new List <TurnResponse>();

        PieceState[][] table = board.table;

        // Making the list of all the moves allowed by card 1;
        Card card1 = team == Team.A ? board.cardA1 : board.cardB1;

        int[][]           card1MovesTable = team == Team.A ? card1.GetMoves() : card1.GetMovesReversed();
        List <Vector2Int> card1Moves      = new List <Vector2Int>();

        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                if (card1MovesTable[i][j] == 1)
                {
                    card1Moves.Add(new Vector2Int(i, j));
                }
            }
        }

        // Making the list of all the moves allowed by card 2;
        Card card2 = team == Team.A ? board.cardA2 : board.cardB2;

        int[][]           card2MovesTable = team == Team.A ? card2.GetMoves() : card2.GetMovesReversed();
        List <Vector2Int> card2Moves      = new List <Vector2Int>();

        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                if (card2MovesTable[i][j] == 1)
                {
                    card2Moves.Add(new Vector2Int(i, j));
                }
            }
        }

        // Going through the table of the board to find allied pieces and check their possible moves
        TurnResponse tr = null;

        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                if (table[i][j] != null && table[i][j].team == team)
                {
                    // Going through card1 moves and trying them all
                    foreach (Vector2Int vect in card1Moves)
                    {
                        // At the end of the line, the "-2"s are added to center vect on (2, 2)
                        tr = new TurnResponse(card1.cardName, new Vector2Int(i, j), new Vector2Int(i - 2, j - 2) + vect);
                        if (InfoGiver.IsTurnValid(table, card1, card2, team, tr))
                        {
                            possibleTurns.Add(tr);
                        }
                    }
                    // Going through card2 moves and trying them all
                    foreach (Vector2Int vect in card2Moves)
                    {
                        // At the end of the line, the "-2"s are added to center vect on (2, 2)
                        tr = new TurnResponse(card2.cardName, new Vector2Int(i, j), new Vector2Int(i - 2, j - 2) + vect);
                        if (InfoGiver.IsTurnValid(table, card1, card2, team, tr))
                        {
                            possibleTurns.Add(tr);
                        }
                    }
                }
            }
        }

        return(possibleTurns);
    }
Exemple #17
0
    public void NextTurn()
    {
        if (status == GameStatus.end)
        {
            return;
        }

        if (!GameConstants.manualMode)
        {
            status = GameStatus.autoplaying;
        }

        if (currentPlayer == Team.A)
        {
            if (AIA != null)
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();

                TurnResponse turn = AIA.PlayTurn();

                stopwatch.Stop();
                Debug.Log("Player A played in " + stopwatch.ElapsedMilliseconds.ToString() + " milliseconds!");

                if (stopwatch.ElapsedMilliseconds > GameConstants.maxTurnTimeMillis)
                {
                    EndGame(Team.B, "Too much thinking time: " + stopwatch.ElapsedMilliseconds.ToString() + " milliseconds");
                    return;
                }

                if (!board.ValidateTurn(turn, Team.A))
                {
                    return;
                }
                else
                {
                    board.ApplyTurn(turn);
                    UseCard(turn.cardName, Team.A);
                }
            }
            else
            {
                Debug.Log("Player A turn");
            }

            SetCurrentPlayer(Team.B);
        }
        else
        {
            if (AIB != null)
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();

                TurnResponse turn = AIB.PlayTurn();

                stopwatch.Stop();
                Debug.Log("Player B played in " + stopwatch.ElapsedMilliseconds.ToString() + " milliseconds!");

                if (stopwatch.ElapsedMilliseconds > GameConstants.maxTurnTimeMillis)
                {
                    EndGame(Team.A, "Too much thinking time: " + stopwatch.ElapsedMilliseconds.ToString() + " milliseconds");
                    return;
                }

                if (!board.ValidateTurn(turn, Team.B))
                {
                    return;
                }
                else
                {
                    board.ApplyTurn(turn);
                    UseCard(turn.cardName, Team.B);
                }
            }
            else
            {
                Debug.Log("Player B turn");
            }

            SetCurrentPlayer(Team.A);
        }

        Team winner = board.HasGameEnded();

        if (winner != Team.none)
        {
            return;
        }
    }