From 00cb953ef1e3e09a3501e1ba90b2c430f23a19c9 Mon Sep 17 00:00:00 2001 From: "Kyle McCallum (mccallu4)" Date: Mon, 2 Mar 2020 09:09:01 +0000 Subject: [PATCH] Update Roguelike.cpp added high score function and leaderboard --- Roguelike.cpp | 244 ++++++++++++++++++++++++++++---------------------- 1 file changed, 138 insertions(+), 106 deletions(-) diff --git a/Roguelike.cpp b/Roguelike.cpp index 8d4a91d..a6abcd2 100644 --- a/Roguelike.cpp +++ b/Roguelike.cpp @@ -2,17 +2,27 @@ #include "Character.h" #include "Windows.h" #include "saveAndLoad.h" +#include +#include +#include using namespace std; bool gameStarted = true; +//for leaderboard +string line; +ifstream f; +string textName; +int leaderboardScore; +vector < pair < int, string >> vScore; //creates a vector and pairs the values within the text file + Character character; string lastTurnsMessages; int main() { - int monsterPresentHere=0, willWalkIntoSomebody=0; - cout << "Choices: \n1 start playing, 2 manual \nWhat do you choose? "; + int monsterPresentHere = 0, willWalkIntoSomebody = 0; + cout << "Choices: \n1 Start Playing, 2 Manual, 3 View Leaderboards \nWhat do you choose? "; int choice = 0; cin >> choice; switch (choice) @@ -23,15 +33,33 @@ int main() case 2: //Manual - a list of commands that are put out by the system cout << "How to play:\n*** Forwards: W *** Backwards: S *** Left: A *** Right: D ***\n*** Save Game: P ***" << endl; // << "*** Check Inventory: I ***"; break; + + case 3: + cout << "NAME\tSCORE\n"; + f.open("highscores.txt"); + while (f >> textName >> leaderboardScore) { + vScore.push_back(make_pair(leaderboardScore, textName)); + cout << line; + } + f.close(); + sort(vScore.begin(), vScore.end()); //sorts in ascending + reverse(vScore.begin(), vScore.end()); //flips the sort to descending + for (int i = 0; i < vScore.size(); i++) { + cout << vScore[i].second << "\t" << vScore[i].first << endl; //outputs the leaderboard to console + } + + return 0; + break; + default: break; } monster monsterArray[5]; - for (int i=0; i<2; i++) + for (int i = 0; i < 2; i++) { - cout<<"Goblin #"< nul"); // usually the console closes after one key press but this allows the user to input more key presses @@ -79,32 +107,32 @@ int main() if (GetAsyncKeyState(0x57)) // moves up + { + for (int i = 0; i < 2; i++) { - for (int i=0; i<2; i++) - { - if ((monsterArray[i].x == character.x) && (monsterArray[i].y == character.y) && (monsterArray[i].Alive==true)) monsterArray[i].wasAttacked(&character.attack, &character.xp); - + if ((monsterArray[i].x == character.x) && (monsterArray[i].y == character.y) && (monsterArray[i].Alive == true)) monsterArray[i].wasAttacked(&character.attack, &character.xp); - if ((((character.x==monsterArray[i].x-1) && (character.y==monsterArray[i].y)) || ((character.x==monsterArray[i].x+1) && (character.y==monsterArray[i].y)) || ((character.x==monsterArray[i].x) && (character.y==monsterArray[i].y+1)) || ((character.x==monsterArray[i].x)) && (character.y==monsterArray[i].y-1)) && (monsterArray[i].Alive==true)) - { - monsterArray[i].playerAttacked(&character.x, &character.y, &character.health); - } - } - if (character.xp >= 15) + + if ((((character.x == monsterArray[i].x - 1) && (character.y == monsterArray[i].y)) || ((character.x == monsterArray[i].x + 1) && (character.y == monsterArray[i].y)) || ((character.x == monsterArray[i].x) && (character.y == monsterArray[i].y + 1)) || ((character.x == monsterArray[i].x)) && (character.y == monsterArray[i].y - 1)) && (monsterArray[i].Alive == true)) { - character.LevelUp(); + monsterArray[i].playerAttacked(&character.x, &character.y, &character.health); } - for (int i=0; i<2; i++) - if ((character.x == monsterArray[i].x) && (character.y-1 == monsterArray[i].y) && (monsterArray[i].Alive == 1)) - { - willWalkIntoSomebody=1; - } - if (willWalkIntoSomebody==0) + } + if (character.xp >= 15) + { + character.LevelUp(); + } + for (int i = 0; i < 2; i++) + if ((character.x == monsterArray[i].x) && (character.y - 1 == monsterArray[i].y) && (monsterArray[i].Alive == 1)) { - character.Movement(-1, 0); + willWalkIntoSomebody = 1; } - willWalkIntoSomebody=0; + if (willWalkIntoSomebody == 0) + { + character.Movement(-1, 0); } + willWalkIntoSomebody = 0; + } @@ -112,112 +140,112 @@ int main() if (GetAsyncKeyState(0x53)) // moves down - { - for (int i=0; i<2; i++) + { + for (int i = 0; i < 2; i++) + { + if ((character.x == monsterArray[i].x) && (character.y == monsterArray[i].y - 1) && (monsterArray[i].Alive == true)) monsterArray[i].wasAttacked(&character.attack, &character.xp); + if ((((character.x == monsterArray[i].x - 1) && (character.y == monsterArray[i].y)) || ((character.x == monsterArray[i].x + 1) && (character.y == monsterArray[i].y)) || ((character.x == monsterArray[i].x) && (character.y == monsterArray[i].y + 1)) || ((character.x == monsterArray[i].x) && (character.y == monsterArray[i].y - 1))) && (monsterArray[i].Alive == true)) { - if ((character.x==monsterArray[i].x) && (character.y==monsterArray[i].y-1) && (monsterArray[i].Alive==true)) monsterArray[i].wasAttacked(&character.attack, &character.xp); - if ((((character.x==monsterArray[i].x-1) && (character.y==monsterArray[i].y)) || ((character.x==monsterArray[i].x+1) && (character.y==monsterArray[i].y)) || ((character.x==monsterArray[i].x) && (character.y==monsterArray[i].y+1)) || ((character.x==monsterArray[i].x) && (character.y==monsterArray[i].y-1))) && (monsterArray[i].Alive==true)) - { monsterArray[i].playerAttacked(&character.x, &character.y, &character.health); } - } - if (character.xp >= 15) + } + if (character.xp >= 15) + { + character.LevelUp(); + } + for (int i = 0; i < 2; i++) + if ((character.x == monsterArray[i].x) && (character.y + 1 == monsterArray[i].y) && (monsterArray[i].Alive == 1)) { - character.LevelUp(); + willWalkIntoSomebody = 1; } - for (int i=0; i<2; i++) - if ((character.x == monsterArray[i].x) && (character.y+1 == monsterArray[i].y) && (monsterArray[i].Alive == 1)) - { - willWalkIntoSomebody=1; - } - if (willWalkIntoSomebody==0) - { + if (willWalkIntoSomebody == 0) + { character.Movement(1, 0); - } - willWalkIntoSomebody=0; } + willWalkIntoSomebody = 0; + } if (GetAsyncKeyState(0x44)) //WALK RIGHT - { - for (int i=0; i<2; i++) + { + for (int i = 0; i < 2; i++) + { + if ((character.x == monsterArray[i].x - 1) && (character.y == monsterArray[i].y) && (monsterArray[i].Alive == true)) monsterArray[i].wasAttacked(&character.attack, &character.xp); + if ((((character.x == monsterArray[i].x - 1) && (character.y == monsterArray[i].y)) || ((character.x == monsterArray[i].x + 1) && (character.y == monsterArray[i].y)) || ((character.x == monsterArray[i].x) && (character.y == monsterArray[i].y + 1)) || ((character.x == monsterArray[i].x) && (character.y == monsterArray[i].y - 1))) && (monsterArray[i].Alive == true)) { - if ((character.x==monsterArray[i].x-1) && (character.y==monsterArray[i].y) && (monsterArray[i].Alive==true)) monsterArray[i].wasAttacked(&character.attack, &character.xp); - if ((((character.x==monsterArray[i].x-1) && (character.y==monsterArray[i].y)) || ((character.x==monsterArray[i].x+1) && (character.y==monsterArray[i].y)) || ((character.x==monsterArray[i].x) && (character.y==monsterArray[i].y+1)) || ((character.x==monsterArray[i].x) && (character.y==monsterArray[i].y-1))) && (monsterArray[i].Alive==true)) - { - monsterArray[i].playerAttacked(&character.x, &character.y, &character.health); - } + monsterArray[i].playerAttacked(&character.x, &character.y, &character.health); } - if (character.xp >= 15) + } + if (character.xp >= 15) + { + character.LevelUp(); + } + for (int i = 0; i < 2; i++) + if ((character.x + 1 == monsterArray[i].x) && (character.y == monsterArray[i].y) && (monsterArray[i].Alive == 1)) { - character.LevelUp(); + willWalkIntoSomebody = 1; } - for (int i=0; i<2; i++) - if ((character.x+1 == monsterArray[i].x) && (character.y == monsterArray[i].y) && (monsterArray[i].Alive == 1)) - { - willWalkIntoSomebody=1; - } - if (willWalkIntoSomebody==0) - { + if (willWalkIntoSomebody == 0) + { character.Movement(0, 1); - } - willWalkIntoSomebody=0; } + willWalkIntoSomebody = 0; + } if (GetAsyncKeyState(0x41)) //WALK LEFT { - for (int i=0; i<2; i++) + for (int i = 0; i < 2; i++) + { + if ((character.x == monsterArray[i].x + 1) && (character.y == monsterArray[i].y) && (monsterArray[i].Alive == true)) monsterArray[i].wasAttacked(&character.attack, &character.xp); + if ((((character.x == monsterArray[i].x - 1) && (character.y == monsterArray[i].y)) || ((character.x == monsterArray[i].x + 1) && (character.y == monsterArray[i].y)) || ((character.x == monsterArray[i].x) && (character.y == monsterArray[i].y + 1)) || ((character.x == monsterArray[i].x) && (character.y == monsterArray[i].y - 1))) && (monsterArray[i].Alive == true)) { - if ((character.x==monsterArray[i].x+1) && (character.y==monsterArray[i].y) && (monsterArray[i].Alive==true)) monsterArray[i].wasAttacked(&character.attack, &character.xp); - if ((((character.x==monsterArray[i].x-1) && (character.y==monsterArray[i].y)) || ((character.x==monsterArray[i].x+1) && (character.y==monsterArray[i].y)) || ((character.x==monsterArray[i].x) && (character.y==monsterArray[i].y+1)) || ((character.x==monsterArray[i].x) && (character.y==monsterArray[i].y-1))) && (monsterArray[i].Alive==true)) - { - monsterArray[i].playerAttacked(&character.x, &character.y, &character.health); - } + monsterArray[i].playerAttacked(&character.x, &character.y, &character.health); } - if (character.xp >= 15) - { - character.LevelUp(); - } - for (int i=0; i<2; i++) - if ((character.x-1 == monsterArray[i].x) && (character.y == monsterArray[i].y) && (monsterArray[i].Alive == 1)) - { - willWalkIntoSomebody=1; - } - if (willWalkIntoSomebody==0) + } + if (character.xp >= 15) + { + character.LevelUp(); + } + for (int i = 0; i < 2; i++) + if ((character.x - 1 == monsterArray[i].x) && (character.y == monsterArray[i].y) && (monsterArray[i].Alive == 1)) { - character.Movement(0, -1); + willWalkIntoSomebody = 1; } - willWalkIntoSomebody=0; + if (willWalkIntoSomebody == 0) + { + character.Movement(0, -1); + } + willWalkIntoSomebody = 0; } - + if (GetAsyncKeyState(0x50)) - { - int playerx=character.x; - int playery=character.y; - int playerhealth=character.health; - int playerattack=character.attack; //0x50 is the virtual key code for the p key, checks if P has been pressed - int playerxp=character.xp; - saveGame(playerx,playery,playerhealth,playerattack,playerxp); - } + { + int playerx = character.x; + int playery = character.y; + int playerhealth = character.health; + int playerattack = character.attack; //0x50 is the virtual key code for the p key, checks if P has been pressed + int playerxp = character.xp; + saveGame(playerx, playery, playerhealth, playerattack, playerxp); + } if (GetAsyncKeyState(0x4C)) - { - //loadGame(); - } + { + //loadGame(); + } if ((character.health < 0)) //Death Function until the classes version decides to work { @@ -225,14 +253,14 @@ int main() } - if (GetAsyncKeyState(0x49)){ - for (int i=0; i<8; i++) cout<> name; @@ -240,5 +268,9 @@ int main() scored = character.oldxp * 100; cout << std::endl << name << " you have scored... " << scored << " points!\n";//"Do you want to see the leaderboard?\n"; //Include the highscore leaderboard here + ofstream f("highscores.txt", ios::app); + f << name << " " << scored << "\n"; + f.close(); + } }