public async Task <ActionResult> OnGet(int?levelStateId, int?col) { if (levelStateId == null) { var settings = SettingsHandler.GetSavedSettingsElseNew(); LevelState = new LevelState(settings); await AddGameStateToDb(LevelState); } else { LevelState = RestoreGameStateFromDb((int)levelStateId); } // don't allow moves if there is a win or a tie if (LevelState.IsGameOver()) { return(Page()); } if (col != null) { LevelState = LevelUpdate.UpdateLevel(LevelState, LevelState.Turn, (int)col); } while (LevelState.GetCurrentPlayer().isAI) { LevelState = LevelUpdate.UpdateLevel(LevelState, LevelState.Turn); } await UpdateLevelStateInDb(LevelState); return(Page()); }
private static void Play(LevelState state) { int cursorPos = 1; while (state.GetWinnerElseNull() == null && !state.IsTie) { var scene = RenderScene(state); var currentPlayer = state.GetCurrentPlayer(); PrintBoard(state, cursorPos, scene, currentPlayer); if (currentPlayer.isAI) { state = LevelUpdate.UpdateLevel(state, state.Turn); Thread.Sleep(500); // add delay for dramatic effect continue; } Key input; while ((input = GetUserDirectionInput()) != Key.Down) { switch (input) { case Key.Left: cursorPos = cursorPos <= 1 ? cursorPos : cursorPos - 1; break; case Key.Right: cursorPos = cursorPos >= state.Width ? cursorPos : cursorPos + 1; break; case Key.Save: Console.Clear(); SaveGame(state); break; case Key.Exit: goto ExitLoop; } PrintBoard(state, cursorPos, scene, currentPlayer); } state = LevelUpdate.UpdateLevel(state, state.Turn, cursorPos); } ExitLoop: PrintBoard(state, cursorPos); PrintWinMessage(state); }