When I run high_score
twice, the display_high_score
function displays the scores correctly, but displays the name I inputted the second time for both. Basically, render_string(displayBuffer, name, 28 + 14 * i, 24)
seemingly has the same name for both.
struct high_score {
char *name;
int score;
};
void highscore(int *score, uint8_t *displayBuffer, struct high_score *high_score_arr) {
uint8_t original_switch = getsw();
char *alphabet_str = " abcdefghijklmnopqrstuvwxyz";
int highScrExit = 0;
int first_box_num = 0, second_box_num = 0, third_box_num = 0, fourth_box_num = 0, fifth_box_num = 0, sixth_box_num = 0;
int box_num_arr[6] = { first_box_num, second_box_num, third_box_num, fourth_box_num, fifth_box_num, sixth_box_num };
char name_letter_array[8] = { ' ', ' ', ' ', ' ', ' ', ' ', '\0' };
int box_number = 0;
while (!highScrExit) {
clearBuffer(displayBuffer);
uint8_t buttons = getBTNs();
uint8_t switches = getsw();
if (buttons & 0x1) {
box_number = (box_number + 1) % 6;
delayms(20);
}
if (buttons & 0x2) {
if (box_number > 0) {
box_number--;
} else {
box_number = 5;
}
delayms(20);
}
if (buttons & 0x8) {
box_num_arr[box_number]++; //Increment the letter for box we're on by one, meaning that [a] will go to [b]
name_letter_array[box_number] = alphabet_str[box_num_arr[box_number] % 27]; //change the letter in the name to that letter
delayms(20);
}
if (buttons & 0x4) {
if (box_num_arr[box_number] > 0) {
box_num_arr[box_number]--;
} else if (box_num_arr[box_number] <= 0) {
box_num_arr[box_number] = 26;
}
name_letter_array[box_number] = alphabet_str[box_num_arr[box_number] % 27];
delayms(20);
}
if ((!(original_switch & 0x8) && switches & 0x8) || ((original_switch & 0x8) && !(switches & 0x8))) {
struct high_score player_high_scr = { name_letter_array, *score };
int i;
for (i = 4; i >= 0; i--) {
if (player_high_scr.score > (&high_score_arr[i])->score){
high_score_arr[i + 1] = high_score_arr[i];
high_score_arr[i] = player_high_scr;
}
}
highScrExit = 1;
display_high_score_screen(displayBuffer, high_score_arr);
}
render_triangle(displayBuffer, 52, 28 - 4 * box_number);
int j;
render_switch(displayBuffer, 80, 28);
render_string(displayBuffer, "save ", 80, 20);
//For loop not working for some reason? It's ugly but works
render_button(displayBuffer, 80 + 9, 28);
render_string(displayBuffer, "up ", 80 + 9, 20);
render_button(displayBuffer, 80 + 9+9, 28);
render_string(displayBuffer, "down ", 80 + 9+9, 20);
render_button(displayBuffer, 80 + 9+9+9, 28);
render_string(displayBuffer, "left ", 80 + 9+9+9, 20);
render_button(displayBuffer, 80 + 9+9+9+9, 28);
render_string(displayBuffer, "right ", 80 + 9+9+9+9, 20);
render_string(displayBuffer, "score ", 5, 25);
render_string(displayBuffer, "enter ", 28, 26);
render_string(displayBuffer, "name ", 35, 24);
render_string(displayBuffer, name_letter_array, 45, 28);
render_score(displayBuffer, score, 15, 20);
displayUpdate(displayBuffer);
}
}
void display_high_score_screen(uint8_t *displayBuffer, struct high_score *high_score_arr) {
int mainMenuExit = 0;
while (!mainMenuExit) {
clearBuffer(displayBuffer);
uint8_t buttons = getBTNs();
if (buttons & 0x8) {
mainMenuExit = 1;
delayms(30);
}
int i;
render_string(displayBuffer, "top ", 5, 22);
render_string(displayBuffer, "scores ", 12, 25);
render_button(displayBuffer, 100, 28);
render_string(displayBuffer, "quit ", 100, 20);
for (i = 0; i < 5; i++) {
int number = i + 1;
char* name = (&high_score_arr[i])->name;
int score = (&high_score_arr[i])->score;
render_score(displayBuffer, &number, 28 + 14 * i, 30);
if (*name != '\0' && score != -1) {
render_string(displayBuffer, name, 28 + 14 * i, 24);
render_score(displayBuffer, &score, 35 + 14 * i, 18);
}
}
displayUpdate(displayBuffer);
}
}
//Call the high_score function like this:
if (gameOver) {
int j;
for (j = 0; j < 5; j++) {
if (score > (&high_score_arr[j])->score) {
j = 5;
highscore(&score, displayBuffer, high_score_arr);
}
}
//This is the part of the code that assigns name:
struct high_score player_high_scr = { name_letter_array, *score };
int i;
for (i = 4; i >= 0; i--) {
if (player_high_scr.score > (&high_score_arr[i])->score) {
high_score_arr[i+1] = high_score_arr[i];
high_score_arr[i] = player_high_scr;
}
}
highScrExit = 1;
display_high_score_screen(displayBuffer, high_score_arr);
//I should point out that this is how I initialize the array:
void init_high_scr_arr() {
int i;
char *null;
*null = '\0';
for (i = 0; i < 5; i++) {
high_score_arr[i].name = null;
high_score_arr[i].score = -1;
}
}
I've tried a lot of things, making highscore return a high_score
struct, putting that in display_high_score
and calling it. I've tried making high_score.name
into an array instead of a pointer, I've tried saving the result in some global variable, etc. Nothing changes the result. All names are the same as the last name I entered in high_score
.
If you cannot dynamically create the space for the name you could create the name space in the high_score struct, changing it to something like
char name[8]
to match the size ofname_letter_array
. Then when you store the high score in you will have to copy the string fromname_letter_array
into your high score structname
, not just pass a pointer toname_letter_array
.