/// <summary> /// Function to save scores current file by adding scores from list /// </summary> public void SaveScoreOnFile(GameScore score) { string FILENAME = string.Format("Highscores_{0}x{1}.txt", GameBoard.RowSize, GameBoard.ColSize); // Append text to the file content // StreamWriter fileContent = new StreamWriter(FILENAME); StreamWriter fileContent = File.AppendText(FILENAME); // Add scores to scorelist scoreList.Add(score); fileContent.WriteLine(score.Name + SEPARATOR + score.Score); fileContent.Close(); }
//StreamWriter fileContent = new StreamWriter(FILENAME); /// <summary> /// Function to read file created with specific game dimensions name /// and print out its content. /// </summary> public void LoadScoreFromFile(GameScore score) { string FILENAME = string.Format("Highscores_{0}x{1}.txt", GameBoard.RowSize, GameBoard.ColSize); StreamReader file = new StreamReader(FILENAME); string reader = null; while ((reader = file.ReadLine()) != null) { string[] nameAndScore = reader.Split(SEPARATOR); string name = nameAndScore[0]; double scores = Convert.ToSingle(nameAndScore[1]); GameScore newScore = new GameScore(name, scores); scoreList.Capacity = 8; scoreList.Sort((x, y) => x.Score.CompareTo(y.Score)); if (scoreList.Count < scoreList.Capacity) { scoreList.Add(newScore); } else { if (newScore.Score > scoreList[0].Score) { scoreList.RemoveAt(0); scoreList.Add(newScore); } } scoreList.Sort((x, y) => x.Score.CompareTo(y.Score)); scoreList.Reverse(); } file.Close(); foreach (GameScore scorerr in scoreList) { Console.WriteLine($"Player {scorerr.Name} has a score of {Math.Round(scorerr.Score*100)/100}"); } scoreList.Clear(); }
/// <summary> /// Function to save scores current file by adding scores from list /// </summary> public void SaveScoreOnFile(GameScore score) { // name of the file to be created for specified board settings string FILENAME = string.Format("Highscores_{0}x{1}.txt", GameBoard.RowSize, GameBoard.ColSize); // Append text to the file content StreamWriter fileContent = File.AppendText(FILENAME); // Add scores to scorelist scoreList.Add(score); // write the current player's name and score separated on the file fileContent.WriteLine(score.Name + SEPARATOR + score.Score); // close the file fileContent.Close(); }
/// <summary> /// This method contains the main loop cycle for the game, showing the /// start menu and registering player input for each action every turn, /// finishing whenever someone wins, loses or quits /// it returns nothing /// </summary> public void Loop(int chosenDiff) { // create new level based on the chosen difficulty and current lvl newLevel = new Levels(lvlCount, (chosenDiff * lvlCount)); // initialise the PrintText class so different texts can be printed PrintText gameInfo = new PrintText(); // only show the menu and create board at startup if (!start) { menu.Menu(); board.DefineBoard(newLevel); } // set start to true to hide menu and set the board start = true; // For reading the player's input ConsoleKey answer; // cycle to print board and run the game while conditions not met do { // render the board anew each time the cycle loops board.RenderBoard(newLevel); // Showcase player's current stats through level progression gameInfo.GameText(newLevel, board.player, board.inventory, key); // Read user's single key input answer = Console.ReadKey().Key; // Clear console for ease of view Console.Clear(); // check the action given by the player // activate look around if (answer == ConsoleKey.D5) { List <CurrentMapObjects> lookAroundItems = new List <CurrentMapObjects>(); // get the objects around the player foreach (CurrentMapObjects item in board.itemList) { board.player.Surroundings(lookAroundItems, item, board); } // show the look around menu gameInfo.LookAroundText(lookAroundItems); // clear the items for next movement positions lookAroundItems.Clear(); } // move to the south if (answer == ConsoleKey.S && Position.IsValidPosition ((new Position((board.player.Position.Row + 1), (board.player.Position.Col))), (GameBoard.RowSize - 1), (GameBoard.ColSize - 1))) { // set the key to print the action key = 'S'; // remove 1hp for turn board.player.HealthChange(-1); // reset the cell the player was on board.cells[board.player.Position.Row, board.player.Position.Col] = new BoardCells((char)Chars.path, true); // set the cell at new player position board.player.Position.Row++; board.cells[board.player.Position.Row, board.player.Position.Col] = new BoardCells(board.player.name, false); } if (answer == ConsoleKey.W && Position.IsValidPosition ((new Position((board.player.Position.Row - 1), (board.player.Position.Col))), (GameBoard.RowSize - 1), (GameBoard.ColSize - 1))) { key = 'W'; board.player.HealthChange(-1); board.cells[board.player.Position.Row, board.player.Position.Col] = new BoardCells((char)Chars.path, true); board.player.Position.Row--; board.cells[board.player.Position.Row, board.player.Position.Col] = new BoardCells(board.player.name, false); } if (answer == ConsoleKey.A && Position.IsValidPosition((new Position((board.player.Position.Row), (board.player.Position.Col - 1))), (GameBoard.RowSize - 1), (GameBoard.ColSize - 1))) { key = 'A'; board.player.HealthChange(-1); board.cells[board.player.Position.Row, board.player.Position.Col] = new BoardCells((char)Chars.path, true); board.player.Position.Col--; board.cells[board.player.Position.Row, board.player.Position.Col] = new BoardCells(board.player.name, false); } if (answer == ConsoleKey.D && Position.IsValidPosition(( new Position((board.player.Position.Row), (board.player.Position.Col + 1))), (GameBoard.RowSize - 1), (GameBoard.ColSize - 1))) { key = 'D'; board.player.HealthChange(-1); board.cells[board.player.Position.Row, board.player.Position.Col] = new BoardCells((char)Chars.path, true); board.player.Position.Col++; board.cells[board.player.Position.Row, board.player.Position.Col] = new BoardCells(board.player.name, false); } if (answer == ConsoleKey.Q && Position.IsValidPosition(( new Position((board.player.Position.Row - 1), (board.player.Position.Col - 1))), (GameBoard.RowSize - 1), (GameBoard.ColSize - 1))) { key = 'Q'; board.player.HealthChange(-1); board.cells[board.player.Position.Row, board.player.Position.Col] = new BoardCells((char)Chars.path, true); board.player.Position.Col--; board.player.Position.Row--; board.cells[board.player.Position.Row, board.player.Position.Col] = new BoardCells(board.player.name, false); } if (answer == ConsoleKey.E && Position.IsValidPosition(( new Position((board.player.Position.Row - 1), (board.player.Position.Col + 1))), (GameBoard.RowSize - 1), (GameBoard.ColSize - 1))) { key = 'E'; board.player.HealthChange(-1); board.cells[board.player.Position.Row, board.player.Position.Col] = new BoardCells((char)Chars.path, true); board.player.Position.Col++; board.player.Position.Row--; board.cells[board.player.Position.Row, board.player.Position.Col] = new BoardCells(board.player.name, false); } if (answer == ConsoleKey.Z && Position.IsValidPosition(( new Position((board.player.Position.Row + 1), (board.player.Position.Col - 1))), (GameBoard.RowSize - 1), (GameBoard.ColSize - 1))) { key = 'Z'; board.player.HealthChange(-1); board.cells[board.player.Position.Row, board.player.Position.Col] = new BoardCells((char)Chars.path, true); board.player.Position.Col--; board.player.Position.Row++; board.cells[board.player.Position.Row, board.player.Position.Col] = new BoardCells(board.player.name, false); } if (answer == ConsoleKey.X && Position.IsValidPosition((new Position(( board.player.Position.Row + 1), (board.player.Position.Col + 1))), (GameBoard.RowSize - 1), (GameBoard.ColSize - 1))) { key = 'X'; board.player.HealthChange(-1); board.cells[board.player.Position.Row, board.player.Position.Col] = new BoardCells((char)Chars.path, true); board.player.Position.Col++; board.player.Position.Row++; board.cells[board.player.Position.Row, board.player.Position.Col] = new BoardCells(board.player.name, false); } if (answer == ConsoleKey.D2) { if (board.map.FallenInto(board.player)) { key = 'M'; board.itemList.Remove(board.map); foreach (CurrentMapObjects items in board.itemList) { board.cells[items.Position.Row, items.Position.Col] = new BoardCells((char)items.Name, false); } } else { key = 'P'; foreach (Food foode in board.food) { if (foode.FallenInto(board.player)) { board.player.Inventory.AddToInventory( new Food(foode.Position, foode.Name, foode.Info, foode.Weight, foode.HPIncrease)); } } board.inventory.WriteInfo(board, board.player, "pickup"); } } if (answer == ConsoleKey.D3) { key = 'D'; if (board.inventory.foodInInventory.Count > 0 || board.inventory.weaponsInInventory.Count > 0) { board.inventory.WriteInfo(board, board.player, "use"); } else { Console.Clear(); Console.WriteLine("your inventory is empty!!"); Console.ReadKey(); } } if (answer == ConsoleKey.D4) { if (board.inventory.foodInInventory.Count > 0 || board.inventory.weaponsInInventory.Count > 0) { board.inventory.WriteInfo(board, board.player, "drop"); } else { Console.Clear(); Console.WriteLine("your inventory is empty!!"); Console.ReadKey(); } } if (answer == ConsoleKey.D1) { gameInfo.EnemyAttackText(); } if (answer == ConsoleKey.D6) { gameInfo.HelpText(board.food, board.weapons, board.traps, board.inventory, board.player); } if (answer == ConsoleKey.D8) { Console.Clear(); Console.WriteLine("are you sure you wish to quit? y/n"); ConsoleKey quit = Console.ReadKey().Key; if (quit == ConsoleKey.Y) { //insert score stuff Console.WriteLine("thank you for playing!"); board.player.Health = 0; } } foreach (Enemy enemy in board.enemies) { if (enemy.FallenInto(board.player)) { board.player.HealthChange(-rnd.Next(enemy.AttackPower, enemy.MaxDamage)); key = 'L'; } } foreach (Trap trap in board.traps) { if (trap.FallenInto(board.player)) { board.player.HealthChange(-rnd.Next(trap.MaxDamage)); key = 'T'; trap.Active = false; } } if (board.cells[board.player.Position.Row, board.player.Position.Col] == board.cells[board.exit.Position.Row, board.exit.Position.Col]) { Console.WriteLine("Congratulations! you've reached the exit!"); Console.WriteLine("Press any key to continue to the next level"); Console.ReadKey(); Console.Clear(); start = true; board.ResetBoard(); board.DefineBoard(newLevel); lvlCount++; Loop(chosenDiff); } } // run the loop while the player hasn't won, lost or quit while (board.player.Health > 0); Console.WriteLine("please input your name for the score"); string name = Console.ReadLine(); score = new GameScore(name, newLevel.ScoreSetter(lvlCount)); score.SaveScoreOnFile(score); Environment.Exit(0); }
/// <summary> /// Method to store all information needed for the Main menu. /// This method returns nothing /// </summary> public void Menu() { // Clear console Console.Clear(); // Creation of instance of gameloop GameLoop loop = new GameLoop(); // Creation of instance of GameScore GameScore score = new GameScore(); // Creation of instance of PrintText PrintText menuInfo = new PrintText(); // Get the text menu method menuInfo.MenuText(); // Variable ConsoleKey answer; answer = Console.ReadKey().Key; Console.WriteLine(); // Depending on user key input switch to the respective case and // Perform according to its case body switch (answer) { // Start game case ConsoleKey.A: // loop.Loop(); break; // Load Score screen case ConsoleKey.B: Console.WriteLine(); if (File.Exists(($"Highscores_{GameBoard.RowSize}x{GameBoard.ColSize}.txt")) == false) { Console.WriteLine("No highscores for this board yet!"); Console.WriteLine(); Console.WriteLine("Press any key to return"); Console.ReadKey(); } else { menuInfo.ScoreText(); score.LoadScoreFromFile(loop.score); Console.WriteLine(); Console.WriteLine("Press any key to return"); Console.ReadKey(); } Menu(); break; // Load credits screen case ConsoleKey.C: Console.WriteLine("Game developed by:"); Console.WriteLine(); Console.WriteLine("Ana dos Santos - a21801899"); Console.WriteLine("Diana Levay - a21801515"); Console.WriteLine(); Console.WriteLine("Press any key to return"); Console.ReadKey(); Menu(); break; // End game case ConsoleKey.D: Console.WriteLine("Thank you for playing!"); Environment.Exit(0); break; // In case other unknown key are typed by the user. Print the // message and go back to menu. default: Console.WriteLine("Please select one of the letters in the menu!"); Console.WriteLine("Press any key to return"); Console.ReadKey(); Menu(); break; } }
/// <summary> /// Function to read file created with specific game dimensions name /// and print out its content. /// </summary> public void LoadScoreFromFile(GameScore score) { // name of the file that's been created for the specific board string FILENAME = string.Format("Highscores_{0}x{1}.txt", GameBoard.RowSize, GameBoard.ColSize); // to read the file's contents StreamReader file = new StreamReader(FILENAME); string reader = null; // while reading the file, print the top 8 highscores while ((reader = file.ReadLine()) != null) { // get the name and score from each line in file string[] nameAndScore = reader.Split(SEPARATOR); // separate name from above string name = nameAndScore[0]; // separate and convert score from above double scores = Convert.ToSingle(nameAndScore[1]); // create new placeholder score to check top 8 GameScore newScore = new GameScore(name, scores); // set scorelist's capacity to 8 (maximum highscore chart) scoreList.Capacity = 8; // sort the scores in the list of current scores scoreList.Sort((x, y) => x.Score.CompareTo(y.Score)); // add the scores to the display on menu if less than 8 if (scoreList.Count < scoreList.Capacity) { scoreList.Add(newScore); } // add the score that's higher than the displayed or remain else { if (newScore.Score > scoreList[0].Score) { scoreList.RemoveAt(0); scoreList.Add(newScore); } } // sort the list after changes scoreList.Sort((x, y) => x.Score.CompareTo(y.Score)); // reverse list order for printing purposes on program scoreList.Reverse(); } // close the file file.Close(); // print out the top 8 highscores in the list rounded foreach (GameScore scorerr in scoreList) { Console.WriteLine($"Player {scorerr.Name} has a score of " + $"{Math.Round(scorerr.Score*100)/100}"); } // clear scorelist for new additions scoreList.Clear(); }