static void ResumeChallenge(int level, int stage, Grid grid) { int width = 19 + (stage * 5); int height = width; int max = 4 + stage; Challenge(grid, width, height, max, stage, level); }
public static Grid Import(string save) { string[] lines = save.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); if(lines.Length < 4) { Console.Error.WriteLine("ERROR: Invalid save file, returning random Grid"); return new Grid(); } int w = int.Parse(lines[0].Split(new[] { ':' })[1]); int h = int.Parse(lines[1].Split(new[] { ':' })[1]); int m = int.Parse(lines[2].Split(new[] { ':' })[1]); string array_data = lines[3]; Grid grid = new Grid(w, h, m); for(int x = 0; x < w; x++) { for(int y = 0; y < h; y++) { int val = int.Parse(array_data[(x * w) + y].ToString()); if(val > m) { Console.Error.WriteLine("WARNING: Value bigger than MAX at {0}, {1}, truncating to MAX", x, y); val = m; } grid.arr[x, y] = val; } } return grid; }
private static void PrintGame(Grid grid, bool challengeMode, string display, int moves) { Console.SetCursorPosition(0,0); ClearCurrentConsoleLine(); Console.Write(display); if(challengeMode) Console.WriteLine(", {0} moves left.", moves); grid.PrintOut(); }
// 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; }
private static void Challenge(Grid start, int c_width = 19, int c_height = 19, int c_max = 4, int level = 1, int stage = 1) { bool first = true; for(int i = stage; i <= STAGES; i++) { for(int k = level; k <= LEVELS_PER_STAGE; k++) { GameResult win = GameResult.Empty; if(first) { win = Game(start, LEVELS_PER_STAGE - k, string.Format("Stage {0}, level {1}", i, k), true, i, k); first = false; } else { win = Game(new Grid(c_width, c_height, c_max), LEVELS_PER_STAGE - k, string.Format("Stage {0}, level {1}", i, k), true, i, k); } if(win == GameResult.Lose || win == GameResult.Quit) { Console.Clear(); return; } } c_width += 5; c_height += 5; c_max++; } }
public static void SaveGame(Grid grid, bool challengeMode, int stage, int level) { Console.WriteLine("Saving game..."); string save = grid.Export(); if(!Directory.Exists(SAVE_DIR)) Directory.CreateDirectory(SAVE_DIR); string stamp = DateTimeToUnixTimestamp(DateTime.Now).ToString(); if(challengeMode) { stamp += "_"; save += stage + ":" + level; } File.WriteAllText(SAVE_DIR + "/" + stamp, save); Console.WriteLine("Done. Press any key to continue"); Console.ReadKey(true); }
public int GetNumberOfSteps() { Grid gr = new Grid(localwidth, localheight, localmax, seed); string str = gr.Solve(true); return str.Length; }