Пример #1
0
            public override Move GetBestMove(ReversiGame reversiGame)
            {
                #region rubish
                ////declare variables not war:

                //var winning = new List<Move>();
                //var next_winning = new List<Pair<Move>>();

                //var lossing = new List<Move>();
                //var next_lossing = new List<Pair<Move>>();

                //var drawing = new List<Move>();
                //var next_drawing = new List<Pair<Move>>();

                //var comboing = new List<Move>();
                //var next_comboing = new List<Pair<Move>>();

                //var normal = new List<Pair<Move>>();


                //for(int x=0; x<this.gameVsMachine.board.Width; x++)
                //{
                //    for(int y=0; y<this.gameVsMachine.board.Height; y++)
                //    {
                //        ReversiGame experiment = this.Clone() as ReversiGame;
                //        Move move = new Move(x, y, experiment);
                //        if (move.IsValid)
                //        {
                //            switch (move.Execute())
                //            {
                //                case GameStatus.Normal:

                //                    ReversiGame second_experimental = experiment.Clone() as ReversiGame;
                //                    Move opponents_move = Machine_LVL_2_Algorithm.GetBestMove(second_experimental);

                //                    switch (opponents_move.Execute())
                //                    {
                //                        case GameStatus.Normal:
                //                            Move next_machine_best_move = Machine_LVL_2_Algorithm.GetBestMove(second_experimental);
                //                            normal.Add(new Pair<Move>(move, next_machine_best_move));

                //                            break;
                //                        case GameStatus.BlackWon:
                //                            if (this.PlayerColor == PlayerColor.White)
                //                            {
                //                                next_lossing.Add(new Pair<Move>(move, opponents_move));
                //                            }
                //                            else if (this.PlayerColor == PlayerColor.Black)
                //                            {
                //                                next_winning.Add(new Pair<Move>(move, opponents_move));
                //                            }
                //                            break;
                //                        case GameStatus.WhiteWon:
                //                            if (this.PlayerColor == PlayerColor.Black)
                //                            {
                //                                next_lossing.Add(new Pair<Move>(move, opponents_move));
                //                            }
                //                            else if (this.PlayerColor == PlayerColor.White)
                //                            {
                //                                next_winning.Add(new Pair<Move>(move, opponents_move));
                //                            }
                //                            break;
                //                        case GameStatus.Draw:
                //                            next_drawing.Add(new Pair<Move>(move, opponents_move));
                //                            break;
                //                        case GameStatus.BlackCombo:
                //                        case GameStatus.WhiteCombo:
                //                            next_comboing.Add(new Pair<Move>(move, opponents_move));
                //                            break;
                //                        default:
                //                            break;
                //                    }


                //                    break;
                //                case GameStatus.BlackWon:
                //                    if (this.PlayerColor == PlayerColor.White)
                //                    {
                //                        winning.Add(move);
                //                    }
                //                    else if (this.PlayerColor == PlayerColor.Black)
                //                    {
                //                        lossing.Add(move);
                //                    }
                //                    break;
                //                case GameStatus.WhiteWon:
                //                    if (this.PlayerColor == PlayerColor.Black)
                //                    {
                //                        winning.Add(move);
                //                    }
                //                    else if (this.PlayerColor == PlayerColor.White)
                //                    {
                //                        lossing.Add(move);
                //                    }
                //                    break;
                //                case GameStatus.Draw:
                //                    drawing.Add(move);
                //                    break;
                //                case GameStatus.BlackCombo:
                //                case GameStatus.WhiteCombo:
                //                    comboing.Add(move);
                //                    break;
                //                default:
                //                    break;

                //            }
                //        }
                //    }
                //}
                //Move get_executable_move(Move move)
                //{
                //    return new Move(move.Coordinates, this);
                //}
                //if (winning.Count > 0)
                //{
                //    List<Move> best_moves = winning.GetMaxProperties("ProfitPoints", "Count") as List<Move>;
                //    return get_executable_move(best_moves[new Random().Next(0, best_moves.Count)]);
                //}
                //else if (next_winning.Count > 0)
                //{
                //    List<Pair<Move>> best_solutions = next_winning.GetMaxProperties("A", "ProfitPoints", "Count") as List<Pair<Move>>;
                //    return get_executable_move(best_solutions[new Random().Next(0, best_solutions.Count)].A);
                //}
                //else if (comboing.Count > 0)
                //{
                //    List<Move> best_moves = comboing.GetMaxProperties("ProfitPoints", "Count") as List<Move>;
                //    return get_executable_move(best_moves[new Random().Next(0, best_moves.Count)]);
                //}
                //else if (normal.Count > 0)
                //{
                //    Dictionary<Move, int> moves_with_total_profits_count = new Dictionary<Move, int>();

                //    for(int i=0; i<normal.Count; i++)
                //    {
                //        moves_with_total_profits_count.Add(normal[i].A, normal[i].A.ProfitPoints.Count + normal[i].B.ProfitPoints.Count);
                //    }

                //    var best = moves_with_total_profits_count.ToList().GetMaxProperties("Value") as List<KeyValuePair<Move, int>>;
                //    return get_executable_move(best[new Random().Next(0, best.Count)].Key);
                //}
                //else if (next_drawing.Count > 0)
                //{
                //    List<Pair<Move>> best_solutions = next_drawing.GetMaxProperties("A", "ProfitPoints", "Count") as List<Pair<Move>>;
                //    return get_executable_move(best_solutions[new Random().Next(0, best_solutions.Count)].A);
                //}
                //else if (drawing.Count > 0)
                //{
                //   // List<Move> best_moves = drawing.GetMaxProperties("ProfitPoints", "Count") as List<Move>;
                //  //  return get_executable_move(best_moves[new Random().Next(0, best_moves.Count)]);
                //    return get_executable_move(drawing[new Random().Next(0, drawing.Count)]);
                //}
                //else if (next_comboing.Count > 0)
                //{
                //    List<Pair<Move>> best_solutions = next_comboing.GetMaxProperties("A", "ProfitPoints", "Count") as List<Pair<Move>>;
                //    return get_executable_move(best_solutions[new Random().Next(0, best_solutions.Count)].A);
                //}
                //else if (next_lossing.Count > 0)
                //{
                //    List<Pair<Move>> best_solutions = next_lossing.GetMaxProperties("A", "ProfitPoints", "Count") as List<Pair<Move>>;
                //    return get_executable_move(best_solutions[new Random().Next(0, best_solutions.Count)].A);
                //}
                //else if (lossing.Count > 0)
                //{
                //    List<Move> best_moves = lossing.GetMaxProperties("ProfitPoints", "Count") as List<Move>;
                //    return get_executable_move(best_moves[new Random().Next(0, best_moves.Count)]);
                //}
                //else
                //{
                //    return null;
                //}
                #endregion
                List <Move> winning, lossing, drawing, comboing;
                winning = lossing = drawing = comboing = new List <Move>();

                Dictionary <Move, int> moves_with_balances = new Dictionary <Move, int>();
                for (int x = 0; x < this.gameVsMachine.board.Width; x++)
                {
                    for (int y = 0; y < this.gameVsMachine.board.Height; y++)
                    {
                        ReversiGame experimental = reversiGame.Clone() as ReversiGame;
                        Move        move         = new Move(x, y, experimental);
                        int         profit_count = move.ProfitPoints.Count;
                        if (move.IsValid)
                        {
                            switch (move.Execute())
                            {
                            case GameStatus.Normal:
                                moves_with_balances.Add(move, profit_count - new Machine_LVL_1_Algorithm(gameVsMachine).GetBestMove(experimental).ProfitPoints.Count);
                                break;

                            case GameStatus.BlackWon:
                                if (this.PlayerColor == PlayerColor.White)
                                {
                                    winning.Add(move);
                                }
                                else if (this.PlayerColor == PlayerColor.Black)
                                {
                                    lossing.Add(move);
                                }
                                break;

                            case GameStatus.WhiteWon:
                                if (this.PlayerColor == PlayerColor.White)
                                {
                                    lossing.Add(move);
                                }
                                else if (this.PlayerColor == PlayerColor.Black)
                                {
                                    winning.Add(move);
                                }
                                break;

                            case GameStatus.Draw:
                                drawing.Add(move);
                                break;

                            case GameStatus.BlackCombo:
                            case GameStatus.WhiteCombo:
                                comboing.Add(move);
                                break;

                            default:
                                break;
                            }
                        }
                    }
                }
                if (winning.Count > 0)
                {
                    var best = winning.GetMaxProperties("ProfitPoints", "Count") as List <Move>;
                    return(GetExecutableMove(best[new Random().Next(0, best.Count)]));
                }
                else if (comboing.Count > 0)
                {
                    var best = comboing.GetMaxProperties("ProfitPoints", "Count") as List <Move>;
                    return(GetExecutableMove(best[new Random().Next(0, best.Count)]));
                }
                else if (moves_with_balances.Count > 0)
                {
                    var best_solutions = moves_with_balances.ToList().GetMaxProperties("Value") as List <KeyValuePair <Move, int> >;
                    return(GetExecutableMove(best_solutions[new Random().Next(0, best_solutions.Count)].Key));
                }
                else if (drawing.Count > 0)
                {
                    return(GetExecutableMove(drawing[new Random().Next(0, drawing.Count)]));
                }
                else if (lossing.Count > 0)
                {
                    var best = lossing.GetMaxProperties("ProfitPoints", "Count") as List <Move>;
                    return(GetExecutableMove(best[new Random().Next(0, best.Count)]));
                }
                else
                {
                    return(null);
                }
            }
Пример #2
0
            public override Move GetBestMove(ReversiGame reversiGame)
            {
                //MessageBox.Show("Ctrl1: "+this.PlayerColor.ToString());
                //return base.GetBestMove();
                #region rubbish

                /*List<Move> less_most_profitable_for_opponent_moves = new List<Move>();
                 * for(int x=0; x<gameVsMachine.board.Width; x++)
                 * {
                 *  for(int y=0; y<gameVsMachine.board.Height; y++)
                 *  {
                 *      Chessboard.Square[,] test_matrix = gameVsMachine.board.Squares;
                 *      ReversiGame tester_game = new ReversiGame(ref test_matrix);
                 *      Move move = new Move(x, y, tester_game);
                 *      if (!move.IsValid) continue;
                 *      GameStatus testerGameStatus = tester_game.ExecuteMove(move);
                 *
                 *      switch (testerGameStatus)
                 *      {
                 *          case GameStatus.Normal://checking the opponent's situation:
                 *              Move opponents_best_move = Machine_LVL_1_Algorithm.GetBestMove(tester_game);
                 *              for(int i=0; i<less_most_profitable_for_opponent_moves.Count; i++)
                 *              {
                 *                  if (opponents_best_move.ProfitPoints.Count > less_most_profitable_for_opponent_moves[i].ProfitPoints.Count) break;
                 *
                 *                  else
                 *                  {
                 *
                 *                      less_most_profitable_for_opponent_moves.Add(opponents_best_move);
                 *                  }
                 *              }
                 *              break;
                 *          case GameStatus.BlackWon:
                 *          case GameStatus.WhiteWon:
                 *          case GameStatus.Draw:
                 *              return base.GetBestMove();
                 *          default://combo:
                 *              break;
                 *      }
                 *
                 *
                 *  }
                 * }*/
                #endregion
                //return GetBestMove(this);
                var move_with_user_best_answer_pairs = new List <Pair <Move> >();
                var winning_moves  = new List <Move>();
                var losing_moves   = new List <Move>();
                var drawing_moves  = new List <Move>();
                var comboing_moves = new List <Move>();



                for (int x = 0; x < reversiGame.Matrix.GetLength(0); x++)
                {
                    for (int y = 0; y < reversiGame.Matrix.GetLength(1); y++)
                    {
                        //PlayerColor[,] experimental_matrix = gameVsMachine.board.FetchMatrix();
                        //ReversiGame experimental_game = new ReversiGame(ref experimental_matrix, this.PlayerColor);
                        ReversiGame experimental_game = reversiGame.Clone() as ReversiGame;
                        //MessageBox.Show(experimental_game.PlayerColor.ToString());
                        Move move = new Move(x, y, experimental_game);
                        if (move.IsValid)
                        {
                            switch (move.Execute())
                            {
                            case GameStatus.Normal:
                                move_with_user_best_answer_pairs.Add(new Pair <Move>(new Move(x, y, experimental_game), new Machine_LVL_1_Algorithm(gameVsMachine).GetBestMove(experimental_game)));
                                break;

                            case GameStatus.WhiteWon:
                                if (experimental_game.PlayerColor == PlayerColor.Black)
                                {    //winning
                                    winning_moves.Add(move);
                                }
                                else if (experimental_game.PlayerColor == PlayerColor.White)
                                {    //lossing
                                    losing_moves.Add(move);
                                }
                                break;

                            case GameStatus.BlackWon:
                                if (experimental_game.PlayerColor == PlayerColor.White)
                                {    //winning
                                    winning_moves.Add(move);
                                }
                                else if (experimental_game.PlayerColor == PlayerColor.Black)
                                {    //lossing
                                    losing_moves.Add(move);
                                }
                                break;

                            case GameStatus.Draw:
                                drawing_moves.Add(move);
                                break;

                            case GameStatus.BlackCombo:
                            case GameStatus.WhiteCombo:
                                comboing_moves.Add(move);
                                break;
                            }
                        }
                    }
                }
                //MessageBox.Show("Ctrl2: " + this.PlayerColor.ToString());
                Random random = new Random();
                if (winning_moves.Count > 0)
                {
                    return(GetExecutableMove(winning_moves[random.Next(0, winning_moves.Count)]));
                }
                else if (comboing_moves.Count > 0)
                {
                    /*List<Move> best_moves = new List<Move>();
                     *
                     * for (int i = 0; i < comboing_moves.Count; i++)
                     * {
                     *  bool best = true;
                     *  List<Move> same_best_moves = new List<Move>();
                     *  for (int j = 0; j < best_moves.Count; j++)
                     *  {
                     *      if (comboing_moves[i].ProfitPoints.Count < best_moves[j].ProfitPoints.Count)
                     *      {
                     *          best = false;
                     *          break;
                     *      }
                     *      else if (comboing_moves[i].ProfitPoints.Count == best_moves[j].ProfitPoints.Count)
                     *      {
                     *          same_best_moves.Add(best_moves[j]);
                     *      }
                     *  }
                     *  if (best)
                     *  {
                     *      best_moves.Clear();
                     *      best_moves.AddRange(same_best_moves);
                     *      best_moves.Add(comboing_moves[i]);
                     *  }
                     * }*/
                    List <Move> best_moves = comboing_moves.GetMaxProperties("ProfitPoints", "Count") as List <Move>;
                    return(GetExecutableMove(best_moves[random.Next(0, best_moves.Count)]));
                }
                else if (move_with_user_best_answer_pairs.Count > 0)
                {
                    //MessageBox.Show("Ctrl3: " + this.PlayerColor.ToString());

                    /*var best_solutions = new List<Pair<Move>>();
                     * for (int i = 0; i < move_with_user_best_answer_pairs.Count; i++)
                     * {
                     *  bool best = true;
                     *  var same_best_solutions = new List<Pair<Move>>();
                     *  for (int j = 0; j < best_solutions.Count; j++)
                     *  {
                     *      if (move_with_user_best_answer_pairs[i].B.ProfitPoints.Count > best_solutions[j].B.ProfitPoints.Count)
                     *      {
                     *          best = false;
                     *          break;
                     *      }
                     *      else if (move_with_user_best_answer_pairs[i].B.ProfitPoints.Count == best_solutions[j].B.ProfitPoints.Count)
                     *      {
                     *          same_best_solutions.Add(best_solutions[j]);
                     *      }
                     *  }
                     *  if (best)
                     *  {
                     *      best_solutions.Clear();
                     *      best_solutions.AddRange(same_best_solutions);
                     *      best_solutions.Add(move_with_user_best_answer_pairs[i]);
                     *  }
                     * }*/
                    List <Pair <Move> > best_solutions = move_with_user_best_answer_pairs.GetMinProperties("B", "ProfitPoints", "Count") as List <Pair <Move> >;
                    //Move choosen_move = best_solutions[random.Next(0, best_solutions.Count)].A;
                    //Move output = new Move(choosen_move.Coordinates, this);
                    return(GetExecutableMove(best_solutions[random.Next(0, best_solutions.Count)].A));
                }
                else if (drawing_moves.Count > 0)
                {
                    return(GetExecutableMove(drawing_moves[random.Next(0, drawing_moves.Count)]));
                }
                else if (losing_moves.Count > 0)
                {
                    return(new Machine_LVL_1_Algorithm(gameVsMachine).GetBestMove(reversiGame));
                }
                else
                {
                    return(null);
                }
            }