public override GameMove <Move> DoMove(IGameStateProvider stateProvider) { var state = stateProvider.ProvideState <Move>(); var states = state.GameMoves; GameMove <Move> lastState = states[states.Length - 1]; string[][] board = lastState.State.Board; for (int r = 0; r < board.Length; r++) { string[] row = board[r]; for (int c = 0; c < row.Length; c++) { if (string.IsNullOrWhiteSpace(row[c])) { string[][] newBoard = board.Select(s => s.ToArray()).ToArray(); newBoard[r][c] = this.Id; return(new GameMove <Move>(this.Id, new Move { Board = newBoard })); } } } // If we got here then there was no valid move for us to make return(lastState); }
public override GameMove <T> DoMove(IGameStateProvider stateProvider) { string resultsDir = Path.Combine(this.cwd, this.id); Console.WriteLine($"Enure results directory exists: {resultsDir}"); Directory.CreateDirectory(resultsDir); string statePath = Path.Combine(resultsDir, "state.json"); string movePath = Path.Combine(resultsDir, "move.json"); string args = $"{this.id} \"{statePath}\" \"{movePath}\""; Console.WriteLine($"Write state to file: {statePath}"); File.WriteAllText(statePath, JsonConvert.SerializeObject(stateProvider.ProvideState <T>())); Console.WriteLine($"Starting player process: {this.exe} {string.Join(" ", args)}"); Process process = Process.Start(this.exe, args); process.WaitForExit(); Console.WriteLine($"Read new move from file: {movePath}"); GameMove <T> move = JsonConvert.DeserializeObject <GameMove <T> >(File.ReadAllText(movePath)); // TODO: clean up files return(move); }
public override GameMove <Move> DoMove(IGameStateProvider stateProvider) { var state = stateProvider.ProvideState <Move>(); var states = state.GameMoves; GameMove <Move> lastState = states[states.Length - 1]; string[][] board = lastState.State.Board; this.PrintBoard(board); int chosenColumn = -1; int chosenRow = -1; do { Console.WriteLine("Choose where to place an X by selecting one of the available numbers from 1-9"); string rawChoice = Console.ReadLine(); if (!Int32.TryParse(rawChoice, out int parsedChoice)) { continue; } int choice = parsedChoice - 1; chosenRow = choice / board.Length; chosenColumn = choice % board.Length; } while(!ValidateChoice(board, chosenRow, chosenColumn)); string[][] newBoard = board.Select(s => s.ToArray()).ToArray(); newBoard[chosenRow][chosenColumn] = this.Id; return(new GameMove <Move>(this.Id, new Move { Board = newBoard })); }