Exemple #1
0
 public Move PlayBot(UserSession session, int gameIndex)
 {
     var game = _gameProvider.GetGame(session.ActiveGames[gameIndex]);
     var bot = game.Players.Find(p => string.Equals(p.Id, AppConfig.BotUserId));
     Move botMove = PlayBotMove(session, bot, gameIndex);
     return botMove;
 }
Exemple #2
0
        private Game PlayWith(UserSession session, string playWith, string gameId, ref List<string> gameTiles)
        {
            Game game = null;
            if (string.IsNullOrEmpty(gameId))
            {
                List<string> myTiles = _movesDataProvider.GetRandomTiles(6, ref gameTiles);
                game = _gameDataProvider.CreateGame(session.Account.Id, GameStatus.Invited, false, myTiles,gameTiles);

                myTiles = _movesDataProvider.GetRandomTiles(6, ref gameTiles);
                _gameDataProvider.StartGame(game.Id, playWith, GameStatus.Invited, true, myTiles, gameTiles);

                SetInvite(playWith, game.Id); // set the invite for the user in the cache.
            }
            else
            {
                var invatedGame = GetGame(gameId);
                _gameDataProvider.SaveGameStatus(gameId, GameStatus.Started);
                invatedGame.Status = GameStatus.Started.ToString();
                game = invatedGame;
            }
            return game;
        }
Exemple #3
0
 private Game GetRandomGame(UserSession session, ref List<string> gameTiles)
 {
     List<string> tiles = _movesDataProvider.GetRandomTiles(6,ref gameTiles);
     Game game = null;
     bool isGameFound = false;
     List<string> waitingList = _gameDataProvider.GetWaitingGamePlayers(session.ActiveGames);
     if (waitingList.Count > 0)
     {
         game = GetGame(waitingList[0]);
         lock (_lockObj)
         {
             var exGameTiles = GetGameTiles(game.Id);
             tiles = _movesDataProvider.GetRandomTiles(6, ref exGameTiles);
             isGameFound = _gameDataProvider.StartGame(game.Id, session.Account.Id, GameStatus.Started, true, tiles, exGameTiles);
             if (!isGameFound)
             {
                 waitingList = _gameDataProvider.GetWaitingGamePlayers(session.ActiveGames);
                 if (waitingList.Count > 0)
                 {
                     game = GetGame(waitingList[0]);
                     exGameTiles = GetGameTiles(game.Id);
                     tiles = _movesDataProvider.GetRandomTiles(6, ref exGameTiles);
                     isGameFound = _gameDataProvider.StartGame(game.Id, session.Account.Id, GameStatus.Started, true, tiles, exGameTiles);
                 }
             }
             if (isGameFound)
             {
                 gameTiles = exGameTiles;
                 game.Players.Add(new Player(session.Account)
                 {
                     IsActive = true,
                     IsHost = false,
                     Tiles = tiles,
                     TilesRemaining = AppConfig.MaxTilesPerPlayer
                 });
                 game.Status = GameStatus.Started.ToString();
             }
         }
     }
     if (!isGameFound)
     {
         game = _gameDataProvider.CreateGame(session.Account.Id, GameStatus.Waiting, false, tiles, gameTiles); // host the game
     }
     return game;
 }
Exemple #4
0
 private Game GetBotGame(UserSession session)
 {
     //List<string> myTiles = _movesDataProvider.GetRandomTiles(6, new List<string>());
     //Game game = _gameDataProvider.CreateGame(session.Account.Id, GameStatus.Started, true, myTiles);
     //game.Players.Add(new Player(session.Account)
     //{
     //    Points = 0,
     //    IsHost = true,
     //    IsActive = true,
     //    TilesRemaining = 50,
     //});
     //List<string> botTiles = _movesDataProvider.GetRandomTiles(6, new List<string>());
     //_gameDataProvider.StartGame(game.Id, AppConfig.BotUserId, false, botTiles);
     //game.Players.Add(new Player()
     //{
     //    Id = AppConfig.BotUserId,
     //    UserName = "******",
     //    FirstName = "Mahesh",
     //    LastName = "Tokle",
     //    Points = 0,
     //    IsHost = true,
     //    IsActive = false,
     //    TilesRemaining = 50,
     //    Level = 1
     //});
     //return game;
     return null;
 }
Exemple #5
0
        private void SaveGameStatus(string gameId, Game game, Player opponant, Player player, UserSession session)
        {
            game.Status = GameStatus.Finished.ToString();
            _gameProvider.SaveGameStatus(gameId, GameStatus.Finished);
            var tilesDiff = opponant.Tiles.Count;
            player.Points = player.Points + tilesDiff;
            opponant.Points = opponant.Points - tilesDiff;

            var winner = player.Points >= opponant.Points ? player : opponant;
            _gameProvider.SetGameWinner(gameId, winner.Id);

            int totalPoints = 0, level = 0;
            _gameProvider.UpdatePlayerProfile(winner.Id, winner.Points, out level, out totalPoints);
            if (string.Equals(session.Account.Id, winner.Id))
            {
                session.Account.Points = totalPoints;
                session.Account.Level = level;
            }
        }
Exemple #6
0
        private Move PlayBotMove(UserSession session, Player bot, int gameIndex)
        {
            var game = _gameProvider.GetGame(session.ActiveGames[gameIndex]);
            var moves = _gameProvider.GetGameMoves(game.Id);
            var gameTiles = ToGameTiles(moves);
            List<string> tilesToBePlayed = GetMaxTilesCanBePlayed(bot.Tiles);
            int maxPoints = 0;
            List<GameTile> nextMoves = new List<GameTile>();
            foreach (var tile in tilesToBePlayed)
            {

                //Get all tile moves which match the color of the tile
                List<GameTile> matchingGameTiles = GetMatchingTileMoves(tile, gameTiles);
                //for each tile move get all sorrounding empty tile positions.
                foreach (var matchingGameTile in matchingGameTiles)
                {
                    //Get all empty position for the matching tile.
                    List<GameTile> emptyPositions = GetEmptyTilePositions(gameTiles, matchingGameTile, tile);
                    //for each empty position place all the tiles to be played in all direction and get the maximum points.
                    var myTiles = new List<string>();
                    myTiles.AddRange(tilesToBePlayed);
                    var tileIndex = myTiles.FindIndex(t => t == tile);
                    myTiles.RemoveAt(tileIndex);
                    foreach (var emptyPosition in emptyPositions)
                    {
                        GetMaxPoints("-x", gameTiles, tile, myTiles, emptyPosition, matchingGameTile, ref maxPoints, ref nextMoves);
                        GetMaxPoints("+x", gameTiles, tile, myTiles, emptyPosition, matchingGameTile, ref maxPoints, ref nextMoves);
                        GetMaxPoints("-y", gameTiles, tile, myTiles, emptyPosition, matchingGameTile, ref maxPoints, ref nextMoves);
                        GetMaxPoints("+y", gameTiles, tile, myTiles, emptyPosition, matchingGameTile, ref maxPoints, ref nextMoves);
                    }
                }
            }
            //parse nextmoves into bot move and return
            Move move = new Move() {Player = bot.Id, Points = maxPoints};
            List<string> tileMoves = new List<string>();
            foreach (var nextMove in nextMoves)
            {
                tileMoves.Add(string.Format("{0},{1},{2}", nextMove.Row, nextMove.Col, nextMove.Tile));
            }
            move.MoveCode = string.Join("|", tileMoves);
            return move;
        }