c++ - Inserting new score into sorted array while updating separate array of names -
score
array of 10 scores in ascending order. scorename
array of names associated with each score. need check if new score enough enter the high score table , if so, insert correct position , make sure scorename
updated reflect changes.
i having problems current code:
//table
for (int = 0; < leaderboardsize; i++) { cout << scorename[i] << "\t\t" << score[i] << endl; } system("pause"); //check see if made highscore if (dicetotal >= score[9]) { cout << "congrats, have made highscore table !\nenter name."; cin >> playername; (int = 9; < leaderboardsize; i--) { if (dicetotal <= score[i]) { scorename[i = + 1] = scorename[i]; score[i = + 1] = score[i]; scorename[i] = playername; score[i] = dicetotal; break; } scorename[i = + 1] = scorename[i]; score[i = + 1] = score[i]; } } here entire code: #include <iostream> #include <string> #include <time.h> using namespace std; int main() { //dice game variables int dice1 = 0; int dice2 = 0; int dicetotal = 0; int round = 0; string choice; bool isdone = true; //scoreboard const int leaderboardsize = 10; int score[10] = { 40, 33, 29, 24, 22, 19, 15, 12, 11, 9 }; string scorename[leaderboardsize] = { "jason", "steve", "bob", "timberduck", "eric", "susan", "tyler", "nick", "ninjadave", "raidengunfire" }; string playername; //random number seeder srand((unsigned int)time(null)); //game instructions cout << "dice game\n---------\ncreated by: darcy tellier\n--------------------------\ninstructions:\nroll 2 dices. try close 50 without going over." << endl; //the game loop { //resets game variables dicetotal = 0; round = 1; //in game match loop { // display round #, current dice total, ask user quit or re-roll. cout << "round\n-----\n " << round << endl; cout << "current total:" << dicetotal << endl; cout << "roll dice (y/n)?"; cin >> choice; //checks users imput invalid characters while (choice != "y" && choice != "y" && choice != "n" && choice != "n") { cout << "invalid option. choose y/n:" << endl; cin >> choice; } if (choice == "y" || choice == "y") { //roll dice round += 1; dice1 = rand() % 6 + 1; dice2 = rand() % 6 + 1; dicetotal = dicetotal + dice1 + dice2; cout << "you have rolled " << dice1 << " , " << dice2 << endl; if (dicetotal > 50) { isdone = false; } } else { //break used because "isdone = false" not work here. debugger shows when variable set false, still ignores , skips next round. break; } } while (isdone == true || dicetotal < 50); //end of round if (dicetotal > 50) { cout << "\ngameover" << endl; cout << "you went on in " << round << " turns. lose!!! " << endl; } else { cout << "you stopped @ " << round << " turns. final score: " << dicetotal << "." << endl; system("pause"); system("cls"); } //table (int = 0; < leaderboardsize; i++) { cout << scorename[i] << "\t\t" << score[i] << endl; } system("pause"); //check see if made highscore if (dicetotal >= score[9]) { cout << "congrats, have made highscore table !\nenter name."; cin >> playername; (int = 9; < leaderboardsize; i--) { if (dicetotal <= score[i]) { scorename[i] = playername; score[i] = dicetotal; break; } } } //board display #2 (int = 0; < leaderboardsize; i++) { cout << scorename[i] << "\t\t" << score[i] << endl; } system("pause"); //do want play again? cout << "do want play again"; cin >> choice; while (choice != "y" && choice != "y" && choice != "n" && choice != "n") { cout << "invalid option. choose y/n:" << endl; cin >> choice; } if (choice == "y" || choice == "y") { system("cls"); isdone = true; } else { cout << "game over" << endl; isdone = false; } } while (isdone); system("pause"); return 0; }
this copy of assignment. note: not asking guys work. want figure out highscore sorting thing.
the problem: arrays not way achieve want
you trying see if score beat score, , if so, replace , move rest of scores down. assume scores sorted, , score
int[10]
, have several problems:
1.
for (int = 9; < leaderboardsize; i--)
you attempting iterate through scores backwards, start @ 9
(which assume last index) , work way down. assuming leaderboardsize
total size of leaderboard, , 10
, i
always less leaderboardsize
, for
loop go loooong time. meant say:
for (int = 9; >= 0; i--)
2.
scorename[i = + 1] = scorename[i]; score[i = + 1] = score[i];
this assigning i
new value, ruin for
loop. should doing
scorename[i + 1] = scorename[i];
trying swap values in array in cumbersome, , trying manually, give you:
the solution: use standard library!
c++ great language, if no other reason containing standard library: library of functions , classes solve many basic problems you.
it far easier sort, insert , remove elements using standard container. let's use std::vector
:
// simple class handle player score: name , score struct playerscore { std::string name; unsigned int score; }; // create test scores. assume have means of storing high scores, perhaps in file, small purpose hard-coding scores: std::vector<playerscore> hiscores = { {"abc", 5000}, {"xjk", 10000}, {"foo", 20000}, {"egg", 4000}, {"hi", 50000} };
you may keep scores sorted, if, mine aren't sorted, can sort them std::sort
:
std::sort(hiscores.begin(), hiscores.end(), [](playerscore ps1, playerscore ps2){ return ps1.score > ps2.score; });
with out way, can proceed play game , obtain name player , score. haven't bothered, , create value score:
auto score = 10123u; // check if score beats of current high scores: auto = std::find_if(hiscores.begin(), hiscores.end(), [score](playerscore ps){ return score > ps.score; }); if (it != hiscores.end()) { // yes! beat score! std::cout << "found score: " << it->score << std::endl; // insert score before other score hiscores.insert(it, {"newscore", score}); // remove last score: hiscores.pop_back(); }
you no longer need iterate , manually try manipulate positions of scores. standard containers random access such std::vector
allow insert , access elements need to. 10 lines of code becomes 2 lines. std::vector
manages size you, if have 3 high scores initially, can grow contain 10, 100 or many more scores.
std::find_if
can find element in container without need manually iterate on every element. predicate passed in acts find condition, in our case pass our score
, check if it's greater playerscore
in container. first element it's greater returned us, , can insert
our score in front of it, remove last score via pop_back
Comments
Post a Comment