コード例 #1
0
        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
ファイル: GridTests.cs プロジェクト: RyanBreaker/TicTacToe
        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
ファイル: MinimaxPlayer.cs プロジェクト: crmmvio/TicTacToe
        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;
        }
コード例 #4
0
 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));
 }
コード例 #6
0
            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
ファイル: Board.cs プロジェクト: shivinsky/TicTacToe
        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
ファイル: Grid.cs プロジェクト: joshisreedhar/TicTacToe
 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
ファイル: Program.cs プロジェクト: RyanBreaker/TicTacToe
        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
ファイル: Grid.cs プロジェクト: joshisreedhar/TicTacToe
 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
ファイル: Grid.cs プロジェクト: joshisreedhar/TicTacToe
 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
ファイル: MinimaxPlayer.cs プロジェクト: crmmvio/TicTacToe
 public Grid MakeMove(Grid g)
 {
     GridCells move;
     Minimax(g, out move);
     return g.MakeMove(move);
 }
コード例 #15
0
ファイル: MainForm.cs プロジェクト: joshisreedhar/TicTacToe
 private void InitializeGrid()
 {
     _grid = new Grid();
     _grid.GridFilled += GridFilled;
     _grid.GameComplete += GameComplete;
 }