コード例 #1
0
        public Player(IGameStateReader gameStateReader, IKeyPresser keyPresser)
        {
            this.gameStateReader = gameStateReader;
            this.keyPresser      = keyPresser;
            bestMoveFinder       = new BestMoveFinder(1);

            previousState = null;

            target       = null;
            stashAllowed = true;
        }
コード例 #2
0
        public void Play()
        {
            gameStateReader.Starting();

            while (true)
            {
                var gameState = gameStateReader.ReadGameState();

                if (Broken(gameState))
                {
                    Thread.Sleep(500);
                    continue;
                }

                if (gameState.ThereIsDanger)
                {
                    Evaluator.MaxHeight = 19;
                }
                else
                {
                    Evaluator.MaxHeight = 4;
                }

                //Console.WriteLine(gameState.Board);
                //Console.WriteLine(string.Join(";", gameState.NextTetriminoes));
                var twp = gameState.FallingTetrimino;
                if (twp == null)
                {
                    continue;
                }

                Tetrimino tetrimino = twp.Tetrimino;
                if (target == null || WrongTetrimino(target.Tetrimino, tetrimino))
                {
                    target = bestMoveFinder.FindBestMove(gameState, twp, stashAllowed);
                    if (target == null)
                    {
                        continue;
                    }
                }

                var moves = new List <Move>();
                if (target.Stash)
                {
                    moves.Add(Move.Stash);
                    target       = null;
                    stashAllowed = false;
                }
                else
                {
                    if (!tetrimino.Equals(target.Tetrimino))
                    {
                        if (CanReachInOneOrTwoCWRotations(tetrimino, target.Tetrimino))
                        {
                            moves.Add(Move.Rotate_CW);
                        }
                        else
                        {
                            moves.Add(Move.Rotate_CWW);
                        }
                    }
                    if (target.Column > twp.LeftCol)
                    {
                        moves.Add(Move.Right);
                    }
                    else if (target.Column < twp.LeftCol)
                    {
                        moves.Add(Move.Left);
                    }
                    if (moves.Count == 0)
                    {
                        moves.Add(Move.Drop);
                        target       = null;
                        stashAllowed = true;
                    }
                }

                //foreach (var move in moves)
                //{
                //    Console.WriteLine($"Making move: {move}");
                //}
                keyPresser.MakeMove(moves);

                //Console.WriteLine(target);
                //Console.WriteLine("------");
            }
        }