From 4438d69e7e816e15469c4e4cff6e08c7210138af Mon Sep 17 00:00:00 2001 From: Sebastian Lague Date: Sun, 30 Jul 2023 16:44:41 +0200 Subject: [PATCH] Add increment option --- Chess-Challenge/src/API/Timer.cs | 14 +++++++++---- .../Application/Core/ChallengeController.cs | 3 ++- .../Framework/Application/Core/Settings.cs | 1 + .../Application/Players/ChessPlayer.cs | 20 ++++++++++++------- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/Chess-Challenge/src/API/Timer.cs b/Chess-Challenge/src/API/Timer.cs index fd3800e..884cf25 100644 --- a/Chess-Challenge/src/API/Timer.cs +++ b/Chess-Challenge/src/API/Timer.cs @@ -9,6 +9,11 @@ namespace ChessChallenge.API /// public readonly int GameStartTimeMilliseconds; + /// + /// The amount of time (in milliseconds) that gets added to the clock after each turn + /// + public readonly int IncrementMilliseconds; + /// /// Amount of time elapsed since the current player started thinking (in milliseconds) /// @@ -33,12 +38,13 @@ namespace ChessChallenge.API sw = System.Diagnostics.Stopwatch.StartNew(); } - public Timer(int millisRemaining, int opponentMillisRemaining, int startingTimeMillis) + public Timer(int remainingMs, int opponentRemainingMs, int startingMs, int incrementMs = 0) { - millisRemainingAtStartOfTurn = millisRemaining; + millisRemainingAtStartOfTurn = remainingMs; sw = System.Diagnostics.Stopwatch.StartNew(); - GameStartTimeMilliseconds = startingTimeMillis; - OpponentMillisecondsRemaining = opponentMillisRemaining; + GameStartTimeMilliseconds = startingMs; + OpponentMillisecondsRemaining = opponentRemainingMs; + IncrementMilliseconds = incrementMs; } public override string ToString() diff --git a/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs b/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs index 4fc60b3..4d68228 100644 --- a/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs +++ b/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs @@ -146,7 +146,7 @@ namespace ChessChallenge.Application API.Board botBoard = new(board); try { - API.Timer timer = new(PlayerToMove.TimeRemainingMs, PlayerNotOnMove.TimeRemainingMs, GameDurationMilliseconds); + API.Timer timer = new(PlayerToMove.TimeRemainingMs, PlayerNotOnMove.TimeRemainingMs, GameDurationMilliseconds, IncrementMilliseconds); API.Move move = PlayerToMove.Bot.Think(botBoard, timer); return new Move(move.RawValue); } @@ -227,6 +227,7 @@ namespace ChessChallenge.Application { if (IsLegal(chosenMove)) { + PlayerToMove.AddIncrement(IncrementMilliseconds); if (PlayerToMove.IsBot) { moveToPlay = chosenMove; diff --git a/Chess-Challenge/src/Framework/Application/Core/Settings.cs b/Chess-Challenge/src/Framework/Application/Core/Settings.cs index ee20aa4..046c4eb 100644 --- a/Chess-Challenge/src/Framework/Application/Core/Settings.cs +++ b/Chess-Challenge/src/Framework/Application/Core/Settings.cs @@ -8,6 +8,7 @@ namespace ChessChallenge.Application // Game settings public const int GameDurationMilliseconds = 60 * 1000; + public const int IncrementMilliseconds = 0 * 1000; public const float MinMoveDelay = 0; public static readonly bool RunBotsOnSeparateThread = true; diff --git a/Chess-Challenge/src/Framework/Application/Players/ChessPlayer.cs b/Chess-Challenge/src/Framework/Application/Players/ChessPlayer.cs index c50c3e7..b340698 100644 --- a/Chess-Challenge/src/Framework/Application/Players/ChessPlayer.cs +++ b/Chess-Challenge/src/Framework/Application/Players/ChessPlayer.cs @@ -12,14 +12,15 @@ namespace ChessChallenge.Application public readonly HumanPlayer? Human; double secondsElapsed; - int baseTimeMS; + int incrementAddedMs; + int baseTimeMs; - public ChessPlayer(object instance, ChallengeController.PlayerType type, int baseTimeMS = int.MaxValue) + public ChessPlayer(object instance, ChallengeController.PlayerType type, int baseTimeMs = int.MaxValue) { this.PlayerType = type; Bot = instance as IChessBot; Human = instance as HumanPlayer; - this.baseTimeMS = baseTimeMS; + this.baseTimeMs = baseTimeMs; } @@ -39,19 +40,24 @@ namespace ChessChallenge.Application secondsElapsed += dt; } + public void AddIncrement(int incrementMs) + { + incrementAddedMs += incrementMs; + } + public int TimeRemainingMs { get { - if (baseTimeMS == int.MaxValue) + if (baseTimeMs == int.MaxValue) { - return baseTimeMS; + return baseTimeMs; } - return (int)Math.Ceiling(Math.Max(0, baseTimeMS - secondsElapsed * 1000.0)); + return (int)Math.Ceiling(Math.Max(0, baseTimeMs - secondsElapsed * 1000.0 + incrementAddedMs)); } } - public void SubscribeToMoveChosenEventIfHuman(Action action) + public void SubscribeToMoveChosenEventIfHuman(Action action) { if (Human != null) {