コード例 #1
0
ファイル: Program.cs プロジェクト: zz110/ai-edu
        static async Task RunBot(int roomId)
        {
            GoldenNumberService service = new GoldenNumberService(new HttpClient());

            service.BaseUrl = "https://goldennumber.aiedu.msra.cn/";

            try
            {
                // Create a player
                var user = await service.NewUserAsync($"AI Player {new Random().Next() % 10000}");

                var userId = user.UserId;
                Console.WriteLine($"Player: {user.NickName}  Id: {user.UserId}");

                Console.WriteLine($"Room id: {roomId}");

                while (true)
                {
                    // Get the room state
                    var state = await service.GetStateAsync(userId, roomId);

                    if (state.State == 2)
                    {
                        Console.WriteLine($"The game has finished");
                        break;
                    }

                    if (state.State == 1)
                    {
                        Console.WriteLine($"The game has not started, query again after 1 second");
                        await Task.Delay(1000);

                        continue;
                    }

                    if (state.HasSubmitted)
                    {
                        Console.WriteLine($"Already submitted this round, wait for next round");
                        if (state.MaxUserCount == 0)
                        {
                            await Task.Delay((state.LeftTime + 1) * 1000);
                        }
                        else
                        {
                            // One round can be finished when all players submitted their numbers if the room have set the max count of users, need to check the state every second.
                            await Task.Delay(1000);
                        }
                        continue;
                    }

                    Console.WriteLine($"This is round {state.FinishedRoundCount + 1}");

                    // Get history of golden numbers
                    var todayGoldenList = await service.GetTodayGoldenListAsync(roomId);

                    if (todayGoldenList.GoldenNumberList.Count != 0)
                    {
                        Console.WriteLine($"Last golden number is: {todayGoldenList.GoldenNumberList.Last()}");
                    }

                    // Predict
                    var    numbers = GeneratePredictionNumbers(todayGoldenList.GoldenNumberList.ToList(), state.Numbers);
                    double number1 = numbers.Item1;
                    double number2 = numbers.Item2;

                    // Submit prediction number
                    try
                    {
                        if (state.Numbers == 2)
                        {
                            await service.SubmitAsync(userId, state.RoundId.ToString(), number1.ToString(), number2.ToString());

                            Console.WriteLine($"You submit numbers: {number1},{number2}");
                        }
                        else
                        {
                            await service.SubmitAsync(userId, state.RoundId.ToString(), number1.ToString(), "0");

                            Console.WriteLine($"You submit number: {number1}");
                        }
                    }
                    catch (SwaggerException <BadRequestRspModel> ex)
                    {
                        Console.WriteLine($"Error: {ex.Result.Message}");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Error: {ex.Message}");
                    }
                }
            }
            catch (SwaggerException <BadRequestRspModel> ex)
            {
                Console.WriteLine($"Error: {ex.Result.Message}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: wdnmd-RushB/ai-edu
        static async Task RunBot(int roomId)
        {
            GoldenNumberService service = new GoldenNumberService(new HttpClient());

            try
            {
                // 创建用户
                var user = await service.NewUserAsync($"AI玩家{new Random().Next() % 10000}");

                var userId = user.UserId;
                Console.WriteLine($"玩家:{user.NickName}  Id:{user.UserId}");

                Console.WriteLine($"房间号:{roomId}");

                while (true)
                {
                    // 查询房间状态
                    var state = await service.GetStateAsync(userId, roomId);

                    if (state.State == 2)
                    {
                        Console.WriteLine($"房间已结束,退出");
                        break;
                    }

                    if (state.State == 1)
                    {
                        Console.WriteLine($"房间尚未开始,1秒后轮询");
                        await Task.Delay(1000);

                        continue;
                    }

                    if (state.HasSubmitted)
                    {
                        Console.WriteLine($"已提交数据,等待下一回合");
                        await Task.Delay((state.LeftTime + 1) * 1000);

                        continue;
                    }

                    Console.WriteLine($"进入第{state.FinishedRoundCount + 1}回合");

                    // 查询历史黄金点
                    var todayGoldenList = await service.GetTodayGoldenListAsync(roomId);

                    if (todayGoldenList.GoldenNumberList.Count != 0)
                    {
                        Console.WriteLine($"上一回合黄金点值为:{todayGoldenList.GoldenNumberList.Last()}");
                    }

                    // 计算预测值
                    var    numbers = GetNumber(todayGoldenList.GoldenNumberList.ToList(), state.Numbers);
                    double number1 = numbers.Item1;
                    double number2 = numbers.Item2;

                    // 提交预测值
                    try
                    {
                        if (state.Numbers == 2)
                        {
                            await service.SubmitAsync(userId, state.RoundId.ToString(), number1.ToString(), number2.ToString());

                            Console.WriteLine($"本回合提交的预测值为:{number1},{number2}");
                        }
                        else
                        {
                            await service.SubmitAsync(userId, state.RoundId.ToString(), number1.ToString(), "0");

                            Console.WriteLine($"本回合提交的预测值为:{number1}");
                        }
                    }
                    catch (SwaggerException <BadRequestRspModel> ex)
                    {
                        Console.WriteLine($"Error:{ex.Result.Message}");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Error:{ex.Message}");
                    }
                }
            }
            catch (SwaggerException <BadRequestRspModel> ex)
            {
                Console.WriteLine($"Error:{ex.Result.Message}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error:{ex.Message}");
            }
        }