Add increment option

This commit is contained in:
Sebastian Lague 2023-07-30 16:44:41 +02:00
parent 78f59b46c4
commit 4438d69e7e
4 changed files with 26 additions and 12 deletions

View file

@ -9,6 +9,11 @@ namespace ChessChallenge.API
/// </summary> /// </summary>
public readonly int GameStartTimeMilliseconds; public readonly int GameStartTimeMilliseconds;
/// <summary>
/// The amount of time (in milliseconds) that gets added to the clock after each turn
/// </summary>
public readonly int IncrementMilliseconds;
/// <summary> /// <summary>
/// Amount of time elapsed since the current player started thinking (in milliseconds) /// Amount of time elapsed since the current player started thinking (in milliseconds)
/// </summary> /// </summary>
@ -33,12 +38,13 @@ namespace ChessChallenge.API
sw = System.Diagnostics.Stopwatch.StartNew(); 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(); sw = System.Diagnostics.Stopwatch.StartNew();
GameStartTimeMilliseconds = startingTimeMillis; GameStartTimeMilliseconds = startingMs;
OpponentMillisecondsRemaining = opponentMillisRemaining; OpponentMillisecondsRemaining = opponentRemainingMs;
IncrementMilliseconds = incrementMs;
} }
public override string ToString() public override string ToString()

View file

@ -146,7 +146,7 @@ namespace ChessChallenge.Application
API.Board botBoard = new(board); API.Board botBoard = new(board);
try 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); API.Move move = PlayerToMove.Bot.Think(botBoard, timer);
return new Move(move.RawValue); return new Move(move.RawValue);
} }
@ -227,6 +227,7 @@ namespace ChessChallenge.Application
{ {
if (IsLegal(chosenMove)) if (IsLegal(chosenMove))
{ {
PlayerToMove.AddIncrement(IncrementMilliseconds);
if (PlayerToMove.IsBot) if (PlayerToMove.IsBot)
{ {
moveToPlay = chosenMove; moveToPlay = chosenMove;

View file

@ -8,6 +8,7 @@ namespace ChessChallenge.Application
// Game settings // Game settings
public const int GameDurationMilliseconds = 60 * 1000; public const int GameDurationMilliseconds = 60 * 1000;
public const int IncrementMilliseconds = 0 * 1000;
public const float MinMoveDelay = 0; public const float MinMoveDelay = 0;
public static readonly bool RunBotsOnSeparateThread = true; public static readonly bool RunBotsOnSeparateThread = true;

View file

@ -12,14 +12,15 @@ namespace ChessChallenge.Application
public readonly HumanPlayer? Human; public readonly HumanPlayer? Human;
double secondsElapsed; 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; this.PlayerType = type;
Bot = instance as IChessBot; Bot = instance as IChessBot;
Human = instance as HumanPlayer; Human = instance as HumanPlayer;
this.baseTimeMS = baseTimeMS; this.baseTimeMs = baseTimeMs;
} }
@ -39,19 +40,24 @@ namespace ChessChallenge.Application
secondsElapsed += dt; secondsElapsed += dt;
} }
public void AddIncrement(int incrementMs)
{
incrementAddedMs += incrementMs;
}
public int TimeRemainingMs public int TimeRemainingMs
{ {
get 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<ChessChallenge.Chess.Move> action) public void SubscribeToMoveChosenEventIfHuman(Action<Chess.Move> action)
{ {
if (Human != null) if (Human != null)
{ {