/// <summary> /// The caller is responsible for ensuring that SaveGameState() is called within a lock(bestMoveLock) block. /// </summary> public void SaveGameState() { /* Ensure there is a best move: */ if (BestMoveSoFar == null) { CellState anyPossibleMove = CurrentGameState.YourCell.GetAdjacentCellStates().Where( cs => cs.OccupationStatus == OccupationStatus.Clear).FirstOrDefault(); if (anyPossibleMove != null) { GameState bestMoveSoFar = CurrentGameState.Clone(); bestMoveSoFar.MoveToPosition(anyPossibleMove.Position); BestMoveSoFar = bestMoveSoFar; } } /* BestMoveSoFar could be null if we have just lost the game: */ if (BestMoveSoFar != null) { BestMoveSoFar.SaveGameState(BinaryGameStateFilePath, FileType.Binary); if (IsInDebugMode) { BestMoveSoFar.SaveGameState(XmlGameStateFilePath, FileType.Xml); BestMoveSoFar.CheckThatGameStateIsValid(CurrentGameState); } } }
public void Run(string tronGameFilePath) { Process process = Process.GetCurrentProcess(); StartTime = process.StartTime; DateTime now = DateTime.Now; if (StartTime + TimeSpan.FromMilliseconds(MAX_DURATION_IN_MILLISECONDS) < now) { // Process start time has probably not been set, so guess a start time of 1 second ago: StartTime = now.Subtract(TimeSpan.FromSeconds(1)); } IEnumerable <RawCellData> cells = GameState.LoadRawCellDataFromTronGameFile(tronGameFilePath); Run(cells); bool isBestMoveLocked = Monitor.TryEnter(BestMoveLock, BEST_MOVE_LOCK_TIMEOUT); try { #if DEBUG if (!isBestMoveLocked) { System.Diagnostics.Debug.WriteLine("Lock timeout with best move lock"); } #endif SaveGameState(); if (BestMoveSoFar != null) { BestMoveSoFar.SaveTronGameFile(tronGameFilePath); } } finally { if (isBestMoveLocked) { Monitor.Exit(BestMoveLock); } } }