public Point NextMove(Disc[,] board, Disc playerColor)
        {
            Point bestMove  = new Point(0, 0);
            int   bestScore = int.MinValue;

            foreach (Point p1 in ReversiGame.ValidMoves(board, playerColor))
            {
                Disc[,] newBoard = ReversiGame.PlayTurn(board, p1, playerColor);
                foreach (Point p2 in ReversiGame.ValidMoves(newBoard, playerColor.Reversed()))
                {
                    int score = ReversiGame.Score(newBoard, playerColor);
                    if (score > bestScore)
                    {
                        bestMove  = p1;
                        bestScore = score;
                        break;
                    }
                }
            }

            if (ReversiGame.IsValidMove(board, bestMove, playerColor))
            {
                return(bestMove);
            }

            return(ReversiGame.ValidMoves(board, playerColor).First());
        }
        /// <summary>
        /// Plays a single game and dumps the results
        /// </summary>
        /// <param name="white">The white (first) player</param>
        /// <param name="black">The black (second) player</param>
        /// <param name="dump">A text writer to act as a dump</param>
        /// <returns>The winner (Disc.Empty if draw)</returns>
        public static Disc PlayGame(IReversiPlayer white, IReversiPlayer black,
                                    TextWriter dump = null)
        {
            if (dump == null)
            {
                dump = Console.Out;
            }
            dump.WriteLine("{0},{1}#", white.GetName(), black.GetName());
            ReversiGame game = new ReversiGame();

            dump.WriteLine(game.board.AsString() + "$");
            game.currentPlayer = Disc.White;
            int passes = 0;

            while (passes < 2)
            {
                if (ValidMoves(game.board, game.currentPlayer).Count == 0)
                {
                    passes++;
                    game.currentPlayer = game.currentPlayer.Reversed();
                }
                else
                {
                    try
                    {
                        Program.RunActionWithTimeout(
                            () =>
                            game.PlaySingleTurn(
                                game.currentPlayer == Disc.White ? white : black),
                            MOVE_TIMEOUT);
                        passes = 0;
                    }
                    catch (Exception e)
                    {
                        dump.WriteLine(e);
                        dump.WriteLine(e.StackTrace);
                        return(game.currentPlayer.Reversed());
                    }
                }
                dump.WriteLine(game.board.AsString() + "$");
            }
            int score = Score(game.board, Disc.White);

            if (score > 0)
            {
                return(Disc.White);
            }
            else if (score < 0)
            {
                return(Disc.Black);
            }
            else
            {
                return(Disc.Empty);
            }
        }
Example #3
0
        /// <summary>
        /// Runs a simulation of a single match
        /// </summary>
        /// <param name="i1">Index of the white player</param>
        /// <param name="i2">Index of the black player</param>
        private void RunSimulation(int i1, int i2)
        {
            IReversiPlayer p1 = players[i1], p2 = players[i2];
            string         filename = String.Format("dumps\\{0}_{1}.txt", p1.GetName(), p2.GetName());
            StreamWriter   sw       = new StreamWriter(filename);
            Disc           winner   = ReversiGame.PlayGame(p1, p2, sw);

            Scoreboard[i1, i2] = (int)winner;
            sw.Close();
        }
 public void AllPlayerTest()
 {
     foreach (Type t in Assembly.GetCallingAssembly().GetTypes())
     {
         if (t.GetInterface("IReversiPlayer") != null)
         {
             IReversiPlayer player = Activator.CreateInstance(t) as IReversiPlayer;
             ReversiGame rg = new ReversiGame();
             rg.PlaySingleTurn(player);
         }
     }
 }
        public Point NextMove(Disc[,] board, Disc playerColor)
        {
            Point bestMove = new Point(0, 0);
            int   bestScore = int.MinValue;
            bool  flag = false, flag2 = false;

            foreach (Point p in ReversiGame.ValidMoves(board, playerColor))
            {
                if ((p.X == 0 && p.Y == 0) || (p.X == 0 && p.Y == 7) || (p.Y == 0 && p.X == 7) || (p.X == 7 && p.Y == 7))
                {
                    return(p);
                }
                else
                {
                    if ((p.Y == 0) || (p.Y == 7) || (p.X == 0) || (p.X == 7))
                    {
                        return(p);
                    }
                }
                int score = ReversiGame.Score(ReversiGame.PlayTurn(board, p, playerColor), playerColor);
                if ((score > bestScore) && ((p.X != 1) && (p.Y != 1) && (p.Y != 6) && (p.X != 6)))

                {
                    flag      = true;
                    bestMove  = p;
                    bestScore = score;
                }
                else
                {
                }
            }
            if (flag == false)
            {
                foreach (Point p in ReversiGame.ValidMoves(board, playerColor))
                {
                    if ((p.X != 1 && p.Y != 1) && (p.X != 1 && p.Y != 6) && (p.X != 6 && p.Y != 1) && (p.X != 6 && p.Y != 6))
                    {
                        flag2 = true;
                        return(p);
                    }
                }
                if (flag2 == false)
                {
                    bestMove = ReversiGame.ValidMoves(board, playerColor).First();
                }
            }
            return(bestMove);
        }
        public Point NextMove(Disc[,] board, Disc playerColor)
        {
            Point bestMove  = new Point(0, 0);
            int   bestScore = int.MinValue;

            foreach (Point p in ReversiGame.ValidMoves(board, playerColor))
            {
                int score = ReversiGame.Score(ReversiGame.PlayTurn(board, p, playerColor), playerColor);
                if (score > bestScore)
                {
                    bestMove  = p;
                    bestScore = score;
                }
            }
            return(bestMove);
        }
        public Point NextMove(Disc[,] board, Disc playerColor)
        {
            Point bestMove = new Point(0, 0);
            int   MaxD     = 1;

            foreach (Point p in ReversiGame.ValidMoves(board, playerColor))
            {
                if ((p.X == 7 && p.Y == 7) || (p.X == 7 && p.Y == 0) || (p.X == 0 && p.Y == 7) || (p.X == 0 && p.Y == 0))
                {
                    return(p);
                }
                int score = ReversiGame.Score(ReversiGame.PlayTurn(board, p, playerColor), playerColor);
                if (board.At(p) == playerColor)
                {
                }

                if (score > MaxD)
                {
                    bestMove = p;
                    MaxD     = score;
                }
            }
            return(bestMove);
        }
        public Point NextMove(Disc[,] board, Disc playerColor)
        {
            Point bestMove= new Point(0,0);
            int bestScore = int.MinValue;
            Point DRCorner = new Point(7, 7);
            bool isDRfull =false;
            Point URCorner = new Point(7, 0);
            bool isURfull =false;
            Point DLCorner = new Point(0, 7);
            bool isDLfull =false;
            Point ULCorner = new Point(0, 0);
            bool isULfull =false;
            bool shouldDoDumm = true;

            if (ReversiGame.IsValidMove(board,DRCorner,playerColor))
            {
                bestMove = DRCorner;
                shouldDoDumm = false;
                isDRfull = true;
            }
            else if (ReversiGame.IsValidMove(board, URCorner, playerColor))
            {
                bestMove = URCorner;
                shouldDoDumm = false;
                isURfull = true;
            }
            else if (ReversiGame.IsValidMove(board, DLCorner, playerColor))
            {
                bestMove = DLCorner;
                shouldDoDumm = false;
                isDLfull = true;
            }
            else if (ReversiGame.IsValidMove(board, ULCorner, playerColor))
            {
                bestMove = ULCorner;
                shouldDoDumm = false;
                isULfull = true;
            }
                if(shouldDoDumm)
                {
                    foreach (Point p in ReversiGame.ValidMoves(board, playerColor))
                    {
                        int score = ReversiGame.Score(ReversiGame.PlayTurn(board, p, playerColor), playerColor);
                        if (score > bestScore)
                        {
                            bestMove = p;
                            bestScore = score;
                        }
                    }
                    ReversiGame.PlayTurn(board, bestMove, playerColor);
                    foreach (Point p in ReversiGame.ValidMoves(board, playerColor))
                    {
                        int score = ReversiGame.Score(ReversiGame.PlayTurn(board, p, playerColor), playerColor);
                        if (score > bestScore)
                        {
                            bestMove = p;
                            bestScore = score;
                        }
                    }
                    ReversiGame.PlayTurn(board, bestMove, playerColor);
                    foreach (Point p in ReversiGame.ValidMoves(board, playerColor))
                    {
                        int score = ReversiGame.Score(ReversiGame.PlayTurn(board, p, playerColor), playerColor);
                        if (score > bestScore)
                        {
                            bestMove = p;
                            bestScore = score;
                        }

                    }
                    ReversiGame.PlayTurn(board, bestMove, playerColor);
                    foreach (Point p in ReversiGame.ValidMoves(board, playerColor))
                    {
                        int score = ReversiGame.Score(ReversiGame.PlayTurn(board, p, playerColor), playerColor);
                        if (score > bestScore)
                        {
                            bestMove = p;
                            bestScore = score;
                        }

                    }
                    ReversiGame.PlayTurn(board, bestMove, playerColor);
                    foreach (Point p in ReversiGame.ValidMoves(board, playerColor))
                    {
                        int score = ReversiGame.Score(ReversiGame.PlayTurn(board, p, playerColor), playerColor);
                        if (score > bestScore)
                        {
                            bestMove = p;
                            bestScore = score;
                        }

                    }
                    ReversiGame.PlayTurn(board, bestMove, playerColor);
                    foreach (Point p in ReversiGame.ValidMoves(board, playerColor))
                    {
                        int score = ReversiGame.Score(ReversiGame.PlayTurn(board, p, playerColor), playerColor);
                        if (score > bestScore)
                        {
                            bestMove = p;
                            bestScore = score;
                        }

                    }
                }
            }
 /// <summary>
 /// Plays a single game and dumps the results
 /// </summary>
 /// <param name="white">The white (first) player</param>
 /// <param name="black">The black (second) player</param>
 /// <param name="dump">A text writer to act as a dump</param>
 /// <returns>The winner (Disc.Empty if draw)</returns>
 public static Disc PlayGame(IReversiPlayer white, IReversiPlayer black,
     TextWriter dump = null)
 {
     if(dump == null)
         dump = Console.Out;
     dump.WriteLine("{0},{1}#", white.GetName(), black.GetName());
     ReversiGame game = new ReversiGame();
     dump.WriteLine(game.board.AsString() + "$");
     game.currentPlayer = Disc.White;
     int passes = 0;
     while (passes < 2)
     {
         if (ValidMoves(game.board, game.currentPlayer).Count == 0)
         {
             passes++;
             game.currentPlayer = game.currentPlayer.Reversed();
         }
         else
             try
             {
                 Program.RunActionWithTimeout(
                   () =>
                     game.PlaySingleTurn(
                     game.currentPlayer == Disc.White ? white : black),
                   MOVE_TIMEOUT);
                 passes = 0;
             }
             catch (Exception e)
             {
                 dump.WriteLine(e);
                 dump.WriteLine(e.StackTrace);
                 return game.currentPlayer.Reversed();
             }
         dump.WriteLine(game.board.AsString() + "$");
     }
     int score = Score(game.board, Disc.White);
     if (score > 0)
         return Disc.White;
     if (score < 0)
         return Disc.Black;
     return Disc.Empty;
 }
 public Point NextMove(Disc[,] board, Disc playerColor)
 {
     return(ReversiGame.ValidMoves(board, playerColor).First());
 }