diff --git a/Chess-Challenge/src/API/Timer.cs b/Chess-Challenge/src/API/Timer.cs index 96ec7d8..fd3800e 100644 --- a/Chess-Challenge/src/API/Timer.cs +++ b/Chess-Challenge/src/API/Timer.cs @@ -5,22 +5,45 @@ namespace ChessChallenge.API public sealed class Timer { /// - /// Amount of time left on clock for current player (in milliseconds) + /// The amount of time (in milliseconds) that each player started the game with /// - public int MillisecondsRemaining => Math.Max(0, initialMillisRemaining - (int)sw.ElapsedMilliseconds); + public readonly int GameStartTimeMilliseconds; + /// - /// Amount of time elapsed since current player started thinking (in milliseconds) + /// Amount of time elapsed since the current player started thinking (in milliseconds) /// public int MillisecondsElapsedThisTurn => (int)sw.ElapsedMilliseconds; - System.Diagnostics.Stopwatch sw; - readonly int initialMillisRemaining; + /// + /// Amount of time left on the clock for the current player (in milliseconds) + /// + public int MillisecondsRemaining => Math.Max(0, millisRemainingAtStartOfTurn - MillisecondsElapsedThisTurn); + + /// + /// Amount of time left on the clock for the other player (in milliseconds) + /// + public readonly int OpponentMillisecondsRemaining; + + readonly System.Diagnostics.Stopwatch sw; + readonly int millisRemainingAtStartOfTurn; public Timer(int millisRemaining) { - initialMillisRemaining = millisRemaining; + millisRemainingAtStartOfTurn = millisRemaining; sw = System.Diagnostics.Stopwatch.StartNew(); + } + public Timer(int millisRemaining, int opponentMillisRemaining, int startingTimeMillis) + { + millisRemainingAtStartOfTurn = millisRemaining; + sw = System.Diagnostics.Stopwatch.StartNew(); + GameStartTimeMilliseconds = startingTimeMillis; + OpponentMillisecondsRemaining = opponentMillisRemaining; + } + + public override string ToString() + { + return $"Game start time: {GameStartTimeMilliseconds} ms. Turn elapsed time: {MillisecondsElapsedThisTurn} ms. My time remaining: {MillisecondsRemaining} Opponent time remaining: {OpponentMillisecondsRemaining} ms."; } } } \ No newline at end of file diff --git a/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs b/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs index e7e089a..5fb1138 100644 --- a/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs +++ b/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs @@ -145,7 +145,7 @@ namespace ChessChallenge.Application API.Board botBoard = new(board); try { - API.Timer timer = new(PlayerToMove.TimeRemainingMs); + API.Timer timer = new(PlayerToMove.TimeRemainingMs, PlayerNotOnMove.TimeRemainingMs, GameDurationMilliseconds); API.Move move = PlayerToMove.Bot.Think(botBoard, timer); return new Move(move.RawValue); } @@ -414,6 +414,8 @@ namespace ChessChallenge.Application ChessPlayer PlayerToMove => board.IsWhiteToMove ? PlayerWhite : PlayerBlack; + ChessPlayer PlayerNotOnMove => board.IsWhiteToMove ? PlayerBlack : PlayerWhite; + public int TotalGameCount => botMatchStartFens.Length * 2; public int CurrGameNumber => Math.Min(TotalGameCount, botMatchGameIndex + 1); public string AllPGNs => pgns.ToString(); diff --git a/Chess-Challenge/src/Framework/Application/Players/ChessPlayer.cs b/Chess-Challenge/src/Framework/Application/Players/ChessPlayer.cs index 85422a7..c50c3e7 100644 --- a/Chess-Challenge/src/Framework/Application/Players/ChessPlayer.cs +++ b/Chess-Challenge/src/Framework/Application/Players/ChessPlayer.cs @@ -47,7 +47,7 @@ namespace ChessChallenge.Application { return baseTimeMS; } - return (int)Math.Round(Math.Max(0, baseTimeMS - secondsElapsed * 1000.0)); + return (int)Math.Ceiling(Math.Max(0, baseTimeMS - secondsElapsed * 1000.0)); } }