public float Minimax(Grid g, out GridCells best, float alpha, float beta, int depth = 1)
        {
            miniMaxCount++;
            best = GridCells.None;
            var bestResult = -10f;
            GridCells garbage;
            if (g.IsDraw)
                return 0f;

            if (g.CurrentIsWinner)
                return 1f / depth;

            if (g.CurrentIsLoser)
                return -1f / depth;

            foreach (var move in g.GetMoves())
            {
                var other = g.MakeMove(move);
                alpha = -Minimax(other, out garbage, -beta, -alpha, depth + 1);

                if (beta <= alpha)
                    return alpha;

                if (alpha > bestResult)
                {
                    best = move;
                    bestResult = alpha;
                }
            }
            return bestResult;
        }
示例#2
0
        public void SetSpotTests()
        {
            grid = new Grid();

            Assert.IsTrue(grid.setSpot(new Spot(), Pieces.X));
            Assert.IsFalse(grid.setSpot(new Spot(1, 1), Pieces.None));
        }
示例#3
0
        public int Minimax(Grid g, out GridCells best)
        {
            best = GridCells.None;
            var bestResult = -10;
            GridCells garbage;
            if (g.IsDraw)
                return 0;

            if (g.CurrentIsWinner)
                return 1;

            if (g.CurrentIsLoser)
                return -1;

            foreach (var move in g.GetMoves())
            {
                var other = g.MakeMove(move);
                var result = -Minimax(other, out garbage);

                if (result > bestResult)
                {
                    best = move;
                    bestResult = result;
                }
            }
            return bestResult;
        }
 public Grid MakeMove(Grid g)
 {
     GridCells move;
     miniMaxCount = 0;
     Minimax(g, out move, float.MinValue, float.MaxValue);
     Console.WriteLine("Minimax method calls: {0}", miniMaxCount);
     return g.MakeMove(move);
 }
示例#5
0
 public static void Render(Grid grid)
 {
     Console.WriteLine(" {0} | {1} | {2} ", grid.GetCell(GridCells.TL), grid.GetCell(GridCells.TC), grid.GetCell(GridCells.TR));
     Console.WriteLine("---|---|---");
     Console.WriteLine(" {0} | {1} | {2} ", grid.GetCell(GridCells.ML), grid.GetCell(GridCells.MC), grid.GetCell(GridCells.MR));
     Console.WriteLine("---|---|---");
     Console.WriteLine(" {0} | {1} | {2} ", grid.GetCell(GridCells.BL), grid.GetCell(GridCells.BC), grid.GetCell(GridCells.BR));
 }
            public void ShouldPlacePiece()
            {
                var tileMock = new Mock<ITile>();
                var tf = new Mock<ITileFactory>();
                tf.Setup(t => t.Create()).Returns(tileMock.Object);
                var grid = new Grid(tf.Object);

                grid.PlacePiece(new Move(new Point(0, 0), Piece.X));

                tileMock.Verify(t => t.PlacePiece(Piece.X));
            }
示例#7
0
 public Grid MakeMove(Grid g)
 {
     GridCells move;
     while (true)
     {
         ConsoleGridRenderer.Render(g);
         Console.WriteLine("\nEnter {0} move: ", g.CurrentIsO ? "O" : "X");
         if (Enum.TryParse<GridCells>(Console.ReadLine().ToUpper(), out move) && g.CanMove(move))
             return g.MakeMove(move);
         else
             Console.WriteLine("Invalid Move!");
     }
 }
示例#8
0
        public Board(Vector2 position, Vector2 size, int cellSize, Game game)
            : base(game)
        {
            Size = size;
            Position = position;

            _board = new Dictionary<Position, Piece>();

            _grid = new Grid(position, size, cellSize, this);

            _cleaner = new Cleaner(this);

            LoadContent();
        }
示例#9
0
 public Diagnol(int startColIndex, Grid grid)
 {
     if(startColIndex != 0 && startColIndex != 2)
     {
         throw new ArgumentException("Invalid Diagnol");
     }
     this.Index = startColIndex;
     this.Cells = new List<Cell>();
     if(startColIndex == 0)
     {
         this.Cells.AddRange(grid.Cells.Where(dc => ((dc.Position == new Point(0, 0)) || (dc.Position == new Point(1, 1)) || (dc.Position == new Point(2, 2)))));
     }
     else
     {
         this.Cells.AddRange(grid.Cells.Where(dc => ((dc.Position == new Point(2, 0)) || (dc.Position == new Point(1, 1)) || (dc.Position == new Point(0, 2)))));
     }
 }
示例#10
0
        static void Main(string[] args)
        {
            var grid = new Grid();
            string input;

            const string TRYAGAIN = "Try again.";

            int x, y;

            // Game loop
            while (grid.playing)
            {
                // Show grid to players
                Console.WriteLine(grid.ToString());

                // Get player move and check the input; see regex for current format
                input = Console.ReadLine().ToUpper();
                if (!(input.Length == 3 && Regex.IsMatch(input, "^([0-2]{2})(X|O)$")))
                {
                    Console.WriteLine(TRYAGAIN);
                    continue;
                }

                // Plugin input
                x = int.Parse(input[0].ToString());
                y = int.Parse(input[1].ToString());

                // Set the spot, returns false if spot is already set or given wrong input
                if (!grid.setSpot(new Spot(x, y), (Pieces)input[2]))
                {
                    Console.WriteLine(TRYAGAIN);
                    continue;
                }
            }

            // Game is over when execution gets here
            Console.WriteLine("Game is over!");
            Console.WriteLine("Result: " + grid.result);
            Console.WriteLine(grid.ToString());

            Console.ReadLine();
        }
示例#11
0
 public Row(int rowIndex, Grid grid)
 {
     this.Index = rowIndex;
     this.Cells = new List<Cell>();
     this.Cells.AddRange(grid.Cells.Where(c => c.Position.Y == rowIndex));
 }
示例#12
0
 public Column(int colIndex, Grid grid)
 {
     this.Index = colIndex;
     this.Cells = new List<Cell>();
     this.Cells.AddRange(grid.Cells.Where(c => c.Position.X == colIndex));
 }
示例#13
0
文件: Game.cs 项目: sumi995/TicTacToe
 public Game()
 {
     _grid   = new Grid();
     player1 = CrossDots.O;
     player2 = CrossDots.X;
 }
示例#14
0
 public Grid MakeMove(Grid g)
 {
     GridCells move;
     Minimax(g, out move);
     return g.MakeMove(move);
 }
示例#15
0
 private void InitializeGrid()
 {
     _grid = new Grid();
     _grid.GridFilled += GridFilled;
     _grid.GameComplete += GameComplete;
 }