// TODO: Clean this up and break it into functions private static GameResult Game(Grid grid, int tolerance = 5, string display = "", bool challenge = false, int stage = -1, int level = -1) { Console.Clear(); int moves = grid.GetNumberOfSteps() + tolerance; bool savedirty = false; // only confirm quitting if game is not saved. if(challenge && (stage > 1 || level > 1)) savedirty = true; // the user has made some progress PrintGame(grid, challenge, display, moves); while(!grid.Solved) { if(challenge && moves == 0) { Console.WriteLine("You're out of moves!\nPress any key to continue."); Console.ReadKey(); return GameResult.Lose; } char c = Console.ReadKey(true).KeyChar; if((c == 'H' || c == 'h')) {//why is this only possible in challenge mode? grid.Solve(false); break; } else if((c == 'S' || c == 's')) { SaveGame(grid, challenge, stage, level); savedirty = false; } else if(c == 'Q' || c == 'q') { if(!savedirty || QuitGame(challenge)) { return GameResult.Quit; } } else if(char.IsDigit(c)) { bool validDigit = grid.Move(c); if(validDigit) { moves--; savedirty = true; } } else {//no valid key was pressed - wait for next key. continue; } //All input is handled - print the updated grid and check if challenge mode is lost PrintGame(grid, challenge, display, moves); } if(grid.Solved) {//this should always be true - if it's not something has gone wrong. PrintGame(grid, challenge, display, moves); if(challenge) Console.WriteLine("You won with {0} moves left! \nPress any key to continue.", moves); else Console.WriteLine("You won! \nPress any key to continue."); Console.ReadKey(true); return GameResult.Win; } //basically unreachable code. return GameResult.Quit; }
public int GetNumberOfSteps() { Grid gr = new Grid(localwidth, localheight, localmax, seed); string str = gr.Solve(true); return str.Length; }