Ejemplo n.º 1
0
        private static void PlayIAMain(params AIRule[] ias)
        {
            Game2048 game = new Game2048();

            AI.AI ia = new AI.AI(ias);

            ShowGrid(game);

            var result = ia.PlayGame(game, () =>
            {
                ShowGrid(game);
                System.Threading.Thread.Sleep(75);
            }).Item1;



            if (result == GameResult.Loss)
            {
                ShowGrid(game);
                Console.WriteLine();
                Console.WriteLine("You loose you dumb !");
                Console.WriteLine();
                Console.ReadKey();
            }
            else if (result == GameResult.Win)
            {
                ShowGrid(game);
                Console.WriteLine();
                Console.WriteLine("Congratulations, you won .... A COCONUT !");
                Console.WriteLine();
            }

            Console.WriteLine("Press any key to leave !");
        }
Ejemplo n.º 2
0
        private static void ShowGrid(Game2048 game)
        {
            Welcome();

            for (int i = 0; i < Game2048.SIZE; i++)
            {
                Console.Write("-------");
            }
            Console.WriteLine("-");
            for (int x = 0; x < Game2048.SIZE; x++)
            {
                Console.Write("|");
                for (int y = 0; y < Game2048.SIZE; y++)
                {
                    var current = game.ShowBoard[x, y].ToString();
                    Console.ForegroundColor = GetColor(current);
                    for (int i = current.Length; i < 6; i++)
                    {
                        current = ((i % 2 == 0) ? " " : "") + current + ((i % 2 == 1) ? " " : "");
                    }

                    Console.Write(current);
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.Write("|");
                }
                Console.WriteLine();
                for (int i = 0; i < Game2048.SIZE; i++)
                {
                    Console.Write("-------");
                }
                Console.WriteLine("-");
            }
        }
Ejemplo n.º 3
0
        private static void PlayIAStatsMain(params AIRule[] rules)
        {
            string text = string.Format("Running 1000 tests with selected AI : {0}",
                                        rules.Aggregate("", (x, y) => x + y.GetType().Name + " (" + y.Coefficient + "),"));

            Console.WriteLine(text);

            List <GameResult> results = new List <GameResult>();
            List <int>        scores  = new List <int>();
            List <int>        bests   = new List <int>();

            AI.AI ai = new AI.AI(rules);
            Parallel.For(0, 1000, (i) =>
            {
                Game2048 game = new Game2048();
                var res       = ai.PlayGame(game, () =>
                {
                });
                results.Add(res.Item1);
                scores.Add(game.Score);
                bests.Add(game.ShowBoard.Max(Game2048.SIZE));
            });

            Console.WriteLine("Results : ");

            var wins = results.Count(x => x == GameResult.Win);

            Console.WriteLine(" - {0} win and {1} losses.", wins, 1000 - wins);
            Console.WriteLine(" - {0} average score, {1} best, {2} worst.", scores.Average(), scores.Max(), scores.Min());
            Console.WriteLine(" - {0} average best tile, {1} best, {2} worst.", bests.Average(), bests.Max(), bests.Min());

            Console.WriteLine();
            Console.WriteLine();
        }
Ejemplo n.º 4
0
        public IActionResult Index()
        {
            var gameState = Game2048.GetInitialState();
            var id        = gamesRepository.Add(gameState);
            var gameDto   = gameState.ToDto(id);

            return(Ok(gameDto));
        }
Ejemplo n.º 5
0
        private static void PlayerMain()
        {
            Game2048 game = new Game2048();

            bool isFinished = false;

            while (!isFinished)
            {
                ShowGrid(game);

                var        key    = Console.ReadKey();
                GameResult result = GameResult.Continue;
                if (key.Key == ConsoleKey.UpArrow)
                {
                    result = game.DoAction(Direction.Top);
                }
                else if (key.Key == ConsoleKey.DownArrow)
                {
                    result = game.DoAction(Direction.Bottom);
                }
                else if (key.Key == ConsoleKey.RightArrow)
                {
                    result = game.DoAction(Direction.Right);
                }
                else if (key.Key == ConsoleKey.LeftArrow)
                {
                    result = game.DoAction(Direction.Left);
                }



                if (result == GameResult.Loss)
                {
                    isFinished = true;
                    ShowGrid(game);
                    Console.WriteLine();
                    Console.WriteLine("You loose, that's kind of tragic !");
                    Console.WriteLine();
                    Console.ReadKey();
                }
                else if (result == GameResult.Win)
                {
                    isFinished = true;
                    ShowGrid(game);
                    Console.WriteLine();
                    Console.WriteLine("Congratulations, you won !");
                    Console.WriteLine();
                }
            }

            Console.WriteLine("Press Escape to leave !");
            var nowKey = ConsoleKey.M;

            while (nowKey != ConsoleKey.Escape)
            {
                nowKey = Console.ReadKey().Key;
            }
        }
Ejemplo n.º 6
0
 private void ResetGame()
 {
     status          = Status.PlayGame;
     Game2048.Score  = 0;
     Game2048.Matrix = new int[Rows, Cols];
     Game2048.CreateRandomNumber();
     Game2048.CreateRandomNumber();
     Draw();
 }
Ejemplo n.º 7
0
        public Tuple <GameResult, int[, ]> PlayGame(Game2048 game, Action onPlayed = null)
        {
            GameResult result = GameResult.Continue;

            while (result == GameResult.Continue)
            {
                result = game.DoAction(GetBestDirection(game));
                if (onPlayed != null)
                {
                    onPlayed();
                }
            }
            return(new Tuple <GameResult, int[, ]>(result, game.WorkBoard));
        }
Ejemplo n.º 8
0
        private Direction GetBestDirection(Game2048 game)
        {
            Direction bestDirection = Direction.Right;
            double    bestScore     = double.MinValue;

            foreach (var direction in Directions.All)
            {
                var gameAfterMove = game.TryDirection(direction);

                // verify no move is forbidden
                if (!game.WorkBoard.IsDifferent(gameAfterMove, Game2048.SIZE, Game2048.SIZE))
                {
                    continue;
                }

                double afterMoveScore = AfterMoveRules.Sum(rule => rule.CalculatePoints(gameAfterMove, direction, Game2048.SIZE));

                double worstCaseScore = double.MaxValue;
                // after moving the gameboard, let's check what new item position is the worst possible, and use this score.
                gameAfterMove.Foreach(Game2048.SIZE, (x, y, content) =>
                {
                    if (content != 0)
                    {
                        return;
                    }

                    foreach (int possibleRandomValue in Game2048.DISTINCT_RANDOM_ITEMS)
                    {
                        gameAfterMove[x, y]  = possibleRandomValue;
                        double possibleScore = WorstCaseRules.Sum(rule => rule.CalculatePoints(gameAfterMove, direction, Game2048.SIZE));

                        if (possibleScore < worstCaseScore)
                        {
                            worstCaseScore = possibleScore;
                        }

                        gameAfterMove[x, y] = 0;
                    }
                });

                double score = worstCaseScore + afterMoveScore;

                if (score > bestScore)
                {
                    bestScore     = score;
                    bestDirection = direction;
                }
            }
            return(bestDirection);
        }
Ejemplo n.º 9
0
        public static void CreateNewGame(ulong userId, RestUserMessage message)
        {
            var game = new G2048Game()
            {
                Grid     = Game2048.GetNewGameBoard(),
                Score    = 0,
                State    = Game2048.GameState.Playing,
                PlayerId = userId,
                Message  = message,
                Move     = 0
            };

            games.Add(game);
            UpdateMessage(game, userId);
        }
Ejemplo n.º 10
0
        public IActionResult Moves(Guid gameId, [FromBody] UserInputDto userInput)
        {
            var gameState = gamesRepository.Get(gameId);

            if (gameState == null)
            {
                return(NotFound());
            }

            var vector = new VectorDto(0, 0);

            switch (userInput.KeyPressed)
            {
            case 87:
                vector.Y = 1;
                break;

            case 83:
                vector.Y = -1;
                break;

            case 65:
                vector.X = -1;
                break;

            case 68:
                vector.X = 1;
                break;
            }

            var gameLogic     = new Game2048();
            var nextGameState = gameLogic.MoveStep(gameState, vector);
            var randomHelper  = new RandomHelper();

            nextGameState.AddCell(randomHelper.GetRandomPos(nextGameState), randomHelper.Random2Or4());
            gamesRepository.Update(gameId, nextGameState, out var isUpdated);
            var updatedGameDto = nextGameState.ToDto(gameId);

            return(Ok(updatedGameDto));
        }
Ejemplo n.º 11
0
        public static void MakeMove(ulong userId, Game2048.MoveDirection direction)
        {
            if (!UserIsPlaying(userId))
            {
                return;
            }


            var game    = games.FirstOrDefault(g => g.PlayerId == userId);
            var gamesID = games.IndexOf(game);

            var result = Game2048.MakeMove(game.Grid, direction);

            game.Score += result.GainedScore;
            game.State  = result.State;
            game.Grid   = result.Board;
            game.Move++;

            games[gamesID] = game;

            UpdateMessage(game, userId);
        }
Ejemplo n.º 12
0
        public GameResult PlayGame()
        {
            Game2048 game = new Game2048();

            return(PlayGame(game, null).Item1);
        }
Ejemplo n.º 13
0
        //点击游戏
        private void Game1_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;

            switch (button.Name)
            {
            case "game1":
            {
                MessageWindow messageWindow = new MessageWindow();
                messageWindow.textBlock1.Text       = "该游戏暂未发行!";
                messageWindow.textBlock1.Foreground = Brushes.Red;
                messageWindow.Owner = this;
                messageWindow.ShowDialog();
                break;
            }

            case "game2":
            {
                MessageWindow messageWindow = new MessageWindow();
                messageWindow.textBlock1.Text       = "该游戏暂未发行!";
                messageWindow.textBlock1.Foreground = Brushes.Red;
                messageWindow.Owner = this;
                messageWindow.ShowDialog();
                break;
            }

            case "game3":
            {
                ChessWindow chess = new ChessWindow();
                chess.Owner    = this;
                chess.pet.Text = "游戏昵称:" + petname;
                chess.Show();
                break;
            }

            case "game4":
            {
                MessageWindow messageWindow = new MessageWindow();
                messageWindow.textBlock1.Text       = "该游戏暂未发行!";
                messageWindow.textBlock1.Foreground = Brushes.Red;
                messageWindow.Owner = this;
                messageWindow.ShowDialog();
                break;
            }

            case "game5":
            {
                MessageWindow messageWindow = new MessageWindow();
                messageWindow.textBlock1.Text       = "该游戏暂未发行!";
                messageWindow.textBlock1.Foreground = Brushes.Red;
                messageWindow.Owner = this;
                messageWindow.ShowDialog();
                break;
            }

            case "game6":
            {
                MessageWindow messageWindow = new MessageWindow();
                messageWindow.textBlock1.Text       = "该游戏暂未发行!";
                messageWindow.textBlock1.Foreground = Brushes.Red;
                messageWindow.Owner = this;
                messageWindow.ShowDialog();
                break;
            }

            case "game7":
            {
                MessageWindow messageWindow = new MessageWindow();
                messageWindow.textBlock1.Text       = "该游戏暂未发行!";
                messageWindow.textBlock1.Foreground = Brushes.Red;
                messageWindow.Owner = this;
                messageWindow.ShowDialog();
                break;
            }

            case "game8":
            {
                MessageWindow messageWindow = new MessageWindow();
                messageWindow.textBlock1.Text       = "该游戏暂未发行!";
                messageWindow.textBlock1.Foreground = Brushes.Red;
                messageWindow.Owner = this;
                messageWindow.ShowDialog();
                break;
            }

            case "game9":
            {
                Game2048 game2048 = new Game2048();
                game2048.Owner = this;
                game2048.Show();
                break;
            }

            case "game10":
            {
                MessageWindow messageWindow = new MessageWindow();
                messageWindow.textBlock1.Text       = "该游戏暂未发行!";
                messageWindow.textBlock1.Foreground = Brushes.Red;
                messageWindow.Owner = this;
                messageWindow.ShowDialog();
                break;
            }

            case "game11":
            {
                MessageWindow messageWindow = new MessageWindow();
                messageWindow.textBlock1.Text       = "该游戏暂未发行!";
                messageWindow.textBlock1.Foreground = Brushes.Red;
                messageWindow.Owner = this;
                messageWindow.ShowDialog();
                break;
            }

            case "game12":
            {
                MessageWindow messageWindow = new MessageWindow();
                messageWindow.textBlock1.Text       = "该游戏暂未发行!";
                messageWindow.textBlock1.Foreground = Brushes.Red;
                messageWindow.Owner = this;
                messageWindow.ShowDialog();
                break;
            }
            }
        }
Ejemplo n.º 14
0
 private void Btn_Right_Click(object sender, RoutedEventArgs e)
 {
     Game2048.Move(Direction.Right);
     Draw();
 }
Ejemplo n.º 15
0
 private void Btn_Down_Click(object sender, RoutedEventArgs e)
 {
     Game2048.Move(Direction.Down);
     Draw();
 }