Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            String dest;
            int width;
            int buffSiz;
            TetrisSolver game = new TetrisSolver();

            #region input parsing

            dest = args[0].Replace("\\", @"\");     //game pieces
            if (!File.Exists(dest))
            {
                Console.WriteLine("input file does not exist, please try again.");
                Console.ReadLine();     //user hits enter
                return;
            }

            try
            {
                width = int.Parse(args[1]);    //game board width
            }
            catch
            {
                Console.WriteLine("invalid width value, please try again.");
                Console.ReadLine();     //user hits enter
                return;
            }

            try
            {
                buffSiz = int.Parse(args[2]);    //buffer size
            }
            catch
            {
                Console.WriteLine("invalid buffer size value, please try again.");
                Console.ReadLine();     //user hits enter
                return;
            }

            //wrap streamreader in using to ensure safe disposal
            //strictly optional
            //once this is complete, we have a buffer full of pieces in order
            using (var file = new StreamReader(dest))
            {
                int count = 0;

                //for each line
                while ((file.ReadLine()) != null)
                {
                    count++;
                }

                if (count == 0)
                {
                    Console.WriteLine("input file is empty, please try again.");
                    Console.ReadLine();     //user hits enter
                    return;
                }
            }

            #endregion

            //get our pieces
            var list = read(dest);

            //get the output
            BoardState.SetWidth(width);
            GameState.SetHandSiz(buffSiz);
            var placement = game.evaluate(list);

            final = placement.Last();

            Thread newWindowThread = new Thread(new ThreadStart(showGameBoard));
            newWindowThread.SetApartmentState(ApartmentState.STA);
            newWindowThread.IsBackground = true;
            newWindowThread.Start();

            //print entries to console output
            foreach (var entry in placement)
            {
                string x = "";

                //iterate through the three values that associate the placement of a piece
                /*foreach (var value in entry)
                {
                    //add to string
                    x += value + " ";
                }*/

                Console.WriteLine(x);
            }

            //print pieces to file
               print(game.finishedGame, args[0]);
               Console.ReadKey();
               return;
        }
Ejemplo n.º 2
0
        private bool process(int callCount, GameState seed)
        {
            //LOL JKs the magic actually happens here.

            for (int i = 0; i < MAX_DEPTH; i++)
            {
                var gameStates = new List<GameState>();
                #region branch for first time
                if (i == 0 && callCount == 0)
                {
                    foreach (var piece in seed.hand.Keys.Cast<String>().ToList().Select(int.Parse).ToList())
                    {
                        if ((int)seed.hand[piece.ToString()] > 0)
                        {
                            for (int col = 0; col < seed.game.cols.Count(); col++)
                            {
                                for (int rot = 0; rot < Block.getPiece(piece).rotations + 1; rot++)//do once if piece has no rotations
                                {
                                    //create new variables
                                    var instance = new GameState(seed);
                                    var newScore = instance.game.getScore(piece, col, rot);
                                    if (newScore != null)
                                    {
                                        //Add block to boardstate
                                        instance.game.addBlock(newScore);
                                        instance.shoot(piece);
                                        instance.droppedPiece = newScore.shape;
                                        instance.lastPiece = seed.droppedPiece;

                                        //calculate a new score score
                                        newScore.score += instance.game.totalScore.score;
                                        instance.game.totalScore = newScore;
                                        //add instance to list of gamestates
                                        gameStates.Add(instance);
                                    }
                                }
                            }
                        }
                    }
                    listOfGameStates.Add(trimBest(gameStates));
                 }
                #endregion
                #region work off branches
                else
                {
                    //This should iterate MAX_BRANCHES times
                    foreach (var gameState in listOfGameStates.Last())
                    {
                        foreach (var piece in gameState.hand.Keys.Cast<String>().ToList().Select(int.Parse).ToList())
                        {
                            if ((int)gameState.hand[piece.ToString()] > 0)
                            {
                                for (int col = 0; col < gameState.game.cols.Count(); col++)
                                {
                                    for (int rot = 0; rot < Block.getPiece(piece).rotations + 1; rot++)
                                    {
                                        //create new variables
                                        var instance = new GameState(gameState);
                                        var newScore = instance.game.getScore(piece, col, rot);
                                        if (piece != 1 && rot > 0)
                                        {
                                        }
                                        if (newScore != null)
                                        {
                                            //Add block to boardstate
                                            instance.game.addBlock(newScore);
                                            instance.shoot(piece);
                                            instance.droppedPiece = newScore.shape;
                                            instance.lastPiece = gameState.droppedPiece;

                                            //calculate a new score score
                                            newScore.score += instance.game.totalScore.score;
                                            instance.game.totalScore = newScore;
                                            //add instance to list of gamestates
                                            gameStates.Add(instance);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    //trim off all the crappy gamestates that no one loves
                    if (gameStates.Any() == true)
                    {
                        listOfGameStates.Add(trimBest(gameStates));
                    }
                }
                #endregion
                //GC.Collect();
                if (!listOfGameStates.Last().First().deck.Any() && isEmptyTable(listOfGameStates.Last().First().hand))
                {
                    return false;
                }
            }
            return true;
        }
Ejemplo n.º 3
0
 public GameState(GameState g)
 {
     lastPiece = g.lastPiece;
     deck = new List<int>(g.deck);
     hand = new Hashtable(g.hand);
     game = new BoardState(g.game);
     droppedPiece = g.droppedPiece;
 }
Ejemplo n.º 4
0
 //the important method
 public List<GameState> evaluate(List<int> list)
 {
     //the magic happens here
     //starring vin diesel, nicholas cage, and nicholas cage as vin diesel
     //this summer, get ready for the movie that will define a generation
     //evaluation, the new thriller from the studio that brought you a dozen other projects
     //in a cinema near you in december
     int callCount = 0;
     var seed = new GameState(list);
     while (process(callCount, seed ))
     {
         callCount++;
     }
     completeGame(listOfGameStates);
     return finishedGame;
 }