Ejemplo n.º 1
0
        static void PlayGame(Player p1, Player p2)
        {
            var b = new Board();

            b.Draw();
            var color  = -1;
            var p      = new[] { p1, p2 };
            var column = -1;

            while (true)
            {
                column = p[(color + 1) / 2].SelectMove(b, column, color);
                var m   = new Move(column, color);
                var win = b.IsWinningMove(m);
                Console.WriteLine("playing " + m.Column);
                Console.WriteLine();
                b.DoMove(m);
                b.Draw();

                if (win)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("good game");
                    break;
                }

                color = -color;
            }
        }
Ejemplo n.º 2
0
 protected static int Win(Board b, int color)
 {
     foreach (var m in PossibleMoves(b).Where(column => b.IsWinningMove(new Move(column, color))))
     {
         return(m);
     }
     return(-1);
 }
Ejemplo n.º 3
0
        int WinInXMoves(Board b, int color, int X)
        {
            var pm = PossibleMoves(b);

            foreach (var column in pm)
            {
                if (b.IsWinningMove(new Move(column, color)))
                {
                    return(column);
                }
            }

            var oppWins = 0;

            foreach (var column in PossibleMoves(b))
            {
                if (b.IsWinningMove(new Move(column, -color)))
                {
                    pm = new List <int>()
                    {
                        column
                    };
                    oppWins++;
                }
            }

            if (oppWins > 1)
            {
                return(-1);
            }

            foreach (var column in pm)
            {
                var copy = b.Copy();
                copy.DoMove(new Move(column, color));

                var winner = true;
                foreach (var column2 in PossibleMoves(copy))
                {
                    var copy2 = copy.Copy();
                    copy2.DoMove(new Move(column2, -color));

                    int m;

                    if (X <= 1)
                    {
                        m = Win(copy2, color);
                    }
                    else
                    {
                        m = WinInXMoves(copy2, color, X - 1);
                    }
                    if (m < 0)
                    {
                        winner = false;
                        break;
                    }
                }

                if (winner)
                {
                    return(column);
                }
            }

            return(-1);
        }