Add opponent time remaining and game start time to Timer

This commit is contained in:
Sebastian Lague 2023-07-26 12:16:54 +02:00
parent 0d6abe1ff0
commit fed725de59
3 changed files with 33 additions and 8 deletions

View file

@ -5,22 +5,45 @@ namespace ChessChallenge.API
public sealed class Timer public sealed class Timer
{ {
/// <summary> /// <summary>
/// Amount of time left on clock for current player (in milliseconds) /// The amount of time (in milliseconds) that each player started the game with
/// </summary> /// </summary>
public int MillisecondsRemaining => Math.Max(0, initialMillisRemaining - (int)sw.ElapsedMilliseconds); public readonly int GameStartTimeMilliseconds;
/// <summary> /// <summary>
/// Amount of time elapsed since current player started thinking (in milliseconds) /// Amount of time elapsed since the current player started thinking (in milliseconds)
/// </summary> /// </summary>
public int MillisecondsElapsedThisTurn => (int)sw.ElapsedMilliseconds; public int MillisecondsElapsedThisTurn => (int)sw.ElapsedMilliseconds;
System.Diagnostics.Stopwatch sw; /// <summary>
readonly int initialMillisRemaining; /// Amount of time left on the clock for the current player (in milliseconds)
/// </summary>
public int MillisecondsRemaining => Math.Max(0, millisRemainingAtStartOfTurn - MillisecondsElapsedThisTurn);
/// <summary>
/// Amount of time left on the clock for the other player (in milliseconds)
/// </summary>
public readonly int OpponentMillisecondsRemaining;
readonly System.Diagnostics.Stopwatch sw;
readonly int millisRemainingAtStartOfTurn;
public Timer(int millisRemaining) public Timer(int millisRemaining)
{ {
initialMillisRemaining = millisRemaining; millisRemainingAtStartOfTurn = millisRemaining;
sw = System.Diagnostics.Stopwatch.StartNew(); 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.";
} }
} }
} }

View file

@ -145,7 +145,7 @@ namespace ChessChallenge.Application
API.Board botBoard = new(board); API.Board botBoard = new(board);
try try
{ {
API.Timer timer = new(PlayerToMove.TimeRemainingMs); API.Timer timer = new(PlayerToMove.TimeRemainingMs, PlayerNotOnMove.TimeRemainingMs, GameDurationMilliseconds);
API.Move move = PlayerToMove.Bot.Think(botBoard, timer); API.Move move = PlayerToMove.Bot.Think(botBoard, timer);
return new Move(move.RawValue); return new Move(move.RawValue);
} }
@ -414,6 +414,8 @@ namespace ChessChallenge.Application
ChessPlayer PlayerToMove => board.IsWhiteToMove ? PlayerWhite : PlayerBlack; ChessPlayer PlayerToMove => board.IsWhiteToMove ? PlayerWhite : PlayerBlack;
ChessPlayer PlayerNotOnMove => board.IsWhiteToMove ? PlayerBlack : PlayerWhite;
public int TotalGameCount => botMatchStartFens.Length * 2; public int TotalGameCount => botMatchStartFens.Length * 2;
public int CurrGameNumber => Math.Min(TotalGameCount, botMatchGameIndex + 1); public int CurrGameNumber => Math.Min(TotalGameCount, botMatchGameIndex + 1);
public string AllPGNs => pgns.ToString(); public string AllPGNs => pgns.ToString();

View file

@ -47,7 +47,7 @@ namespace ChessChallenge.Application
{ {
return baseTimeMS; return baseTimeMS;
} }
return (int)Math.Round(Math.Max(0, baseTimeMS - secondsElapsed * 1000.0)); return (int)Math.Ceiling(Math.Max(0, baseTimeMS - secondsElapsed * 1000.0));
} }
} }