public GetMove ( |
||
maze | ||
outputFilePath | String | |
logFile | ||
Результат |
public Maze GetMove(Maze maze, String outputFilePath, StreamWriter logFile)
{
var playerOutputFilePath = _workingPath + System.IO.Path.DirectorySeparatorChar + Properties.Settings.Default.SettingBotOutputFileName;
File.Delete(playerOutputFilePath);
var processName = _workingPath + System.IO.Path.DirectorySeparatorChar + _executableFileName;
var arguments = "\"" + outputFilePath + "\"";
if (Environment.OSVersion.Platform == PlatformID.Unix)
{
arguments = processName + " " + arguments;
processName = "/bin/bash";
}
var p = new Process
{
StartInfo =
{
WorkingDirectory = _workingPath,
FileName = processName,
Arguments = arguments,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
}
};
System.Diagnostics.DataReceivedEventHandler h = (sender, args) => {
if (!String.IsNullOrEmpty(args.Data))
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(_workingPath + System.IO.Path.DirectorySeparatorChar + "botlogs_capture.txt", true))
{
file.WriteLine(args.Data);
}
}
};
p.OutputDataReceived += h;
p.ErrorDataReceived += h;
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
bool didExit = p.WaitForExit(Properties.Settings.Default.SettingBotOutputTimeoutSeconds * 1000);
if (!didExit)
{
p.Kill();
logFile.WriteLine("[GAME] : Killed process " + processName);
}
if (p.ExitCode != 0)
{
logFile.WriteLine("[GAME] : Process exited " + p.ExitCode + " from player " + _playerName);
}
if (!File.Exists(playerOutputFilePath))
{
logFile.WriteLine("[GAME] : No output file from player " + _playerName);
return null;
}
try
{
var mazeFromPlayer = new Maze(playerOutputFilePath);
return mazeFromPlayer;
}
catch (UnreadableMazeException e)
{
Console.WriteLine(e.ToString());
logFile.WriteLine("[GAME] : Unreadable maze from player: " + _playerName);
}
return null;
}
public void Run(String folderPath) { var gamePlayDirectoryPath = Properties.Settings.Default.SettingPrimaryDriveName + "\\" + folderPath; Directory.CreateDirectory(gamePlayDirectoryPath); var outputFilePath = gamePlayDirectoryPath + "\\" + Properties.Settings.Default.SettingGamePlayFile; _maze.WriteMaze(outputFilePath); Player winner = null; var gameOutcome = Enums.GameOutcome.ProceedToNextRound; Directory.CreateDirectory(folderPath); Directory.CreateDirectory(folderPath + "\\" + Properties.Settings.Default.SettingReplayFolder); var logFile = new StreamWriter(folderPath + "\\" + Properties.Settings.Default.SettingMatchLogFileName); logFile.WriteLine("[GAME] : Match started"); while (gameOutcome.Equals(Enums.GameOutcome.ProceedToNextRound)) { _currentPlayer = _playerPool.GetNextPlayer(); var mazeFromPlayer = _currentPlayer.GetMove(_maze, gamePlayDirectoryPath + "\\" + Properties.Settings.Default.SettingGamePlayFile, logFile); if (mazeFromPlayer != null) { var mazeValidationOutcome = GetMazeValidationOutcome(logFile, mazeFromPlayer); if (mazeValidationOutcome.Equals(Enums.MazeValidationOutcome.ValidMaze)) { var opponentPosition = _maze.FindCoordinateOf(Properties.Settings.Default.SymbolPlayerB); var previousPosition = _maze.FindCoordinateOf(Properties.Settings.Default.SymbolPlayerA); var currentPosition = mazeFromPlayer.FindCoordinateOf(Properties.Settings.Default.SymbolPlayerA); var turnOutcome = GetTurnOutcome(mazeFromPlayer, currentPosition, previousPosition, opponentPosition, logFile); if (!turnOutcome.Equals(Enums.TurnOutcome.MoveMadeAndDroppedPoisonPillIllegally)) { gameOutcome = GetGameOutcome(logFile, gameOutcome, turnOutcome); winner = DeterminIfWinnerWinner(gameOutcome, mazeFromPlayer, winner); } else gameOutcome = ProcessIllegalMove(logFile, gameOutcome, ref winner); } else gameOutcome = ProcessIllegalMove(logFile, gameOutcome, ref winner); _maze.WriteMaze(gamePlayDirectoryPath + "\\" + Properties.Settings.Default.SettingGamePlayFile); CreateIterationStateFile(folderPath); _iteration++; _maze.Print(); } else gameOutcome = ProcessIllegalMove(logFile, gameOutcome, ref winner); } CreateMatchInfo(gameOutcome, winner, logFile); logFile.Close(); var replayMatchOutcome = new StreamWriter(folderPath + "\\replay\\matchinfo.out"); CreateMatchInfo(gameOutcome, winner, replayMatchOutcome); replayMatchOutcome.Close(); }