Exemplo n.º 1
0
        public static async Task MainGameLoop(grpcClientConnection grpcClientConnection, string playerName, bool connectionTesting)
        {
            Log(Environment.NewLine);
            var startInformation = await grpcClientConnection.Initialize(playerName);

            Log($"Received game start information.");
            if (startInformation.WhitePlayer)
            {
                Log($"{playerName} starts the game.");
            }
            else
            {
                Log($"Opponent starts the game.");
            }

            Log(Environment.NewLine);

            Log("Starting logic...");
            LogicBase ai;

            if (connectionTesting)
            {
                ai = new ConnectionTesterLogic(startInformation.WhitePlayer);
            }
            else
            {
                ai = new Logic(startInformation);
            }

            Log("Start game loop");

            // Inject ai to connection module and play game
            // TODO cancellation token here
            await grpcClientConnection.Play(ai);
        }
Exemplo n.º 2
0
        public static void Start(grpcClientConnection grpcClientConnection, string playerName, bool connectionTesting)
        {
            // We could use while(true) to play games indefinitely. But probably better to play single game per opened client
            try
            {
                // https://stackoverflow.com/questions/9343594/how-to-call-asynchronous-method-from-synchronous-method-in-c
                AsyncHelper.RunSync(() => MainGameLoop(grpcClientConnection, playerName, connectionTesting));

                // Exception deadlock
                //var task = MainGameLoop(grpcClientConnection, playerName, connectionTesting);
                //task.Wait();

                Log("Game ended, reason: ____TODO____");
            }
            catch (GameEndedException e)
            {
                Log($"Game ended, reason: {e.E.Status.Detail}");
            }
            catch (Exception e)
            {
                Log($"Main game loop failed: {e.ToString()}");
            }
        }
Exemplo n.º 3
0
        static void Main()
        {
            using var connection = new grpcClientConnection("127.0.0.1:30052");
            var startInformation = connection.Initialize("example player");

            // Wait for the server to respond
            startInformation.Wait();

            // Server tells the client is either starting player (white),
            // or black. If black, info also contains white players first move
            var result = startInformation.Result;

            // Initialize your own ai
            var ai = new ExampleAiLogic(result.WhitePlayer, result);

            // Inject ai to connection module and play game
            var playTask = connection.Play(ai);

            playTask.Wait();

            // Game finished
            // Dispose should handle connection closing
            // connection.CloseConnection();
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            // Given arguments saved to private properties
            CommandLine.Parser.Default.ParseArguments <Options>(args)
            .WithParsed(RunOptions)
            .WithNotParsed(HandleParseError);

            if (_stopArgsGiven)
            {
                return;
            }

            Log($"Chess ai vergiBlue [{_currentVersion}]");

            while (true)
            {
                if (_gameMode <= 0)
                {
                    // User did not explicitly set gamemode in command line arguments
                    _gameMode = InputGameMode();
                }

                if (_gameMode < 0)
                {
                    break;
                }

                if (_gameMode == 1)
                {
                    using var connection = new grpcClientConnection(_fullAddress);
                    NetworkGame.Start(connection, _playerName, false);
                    break;
                }
                else if (_gameMode == 2)
                {
                    Log(Environment.NewLine);
                    Log("Give player name: ");
                    Console.Write(" > ");
                    var playerName = Console.ReadLine() ?? _playerName;
                    Log($"Chess ai {playerName} [{_currentVersion}]");
                    using var connection = new grpcClientConnection(_fullAddress);
                    NetworkGame.Start(connection, playerName, false);
                    break;
                }
                else if (_gameMode == 3)
                {
                    LocalGame.Start(_minimumDelayBetweenMoves, null);
                }
                else if (_gameMode == 4)
                {
                    LocalGame.Start(Math.Max(1000, _minimumDelayBetweenMoves), null);
                }
                else if (_gameMode == 5)
                {
                    LocalGame.CustomStart();
                }
                else if (_gameMode == 9)
                {
                    using var connection = new grpcClientConnection(_fullAddress);
                    NetworkGame.Start(connection, "Connection test AI", true);
                    break;
                }
                else
                {
                    break;
                }

                Log(Environment.NewLine);
            }
        }