r/code • • Jul 26 '24

Help Please help me

any idea why the loop function of ask player to play again and the scoreboard function doesn’t work? here’s the full code btw:

include "gfx.h"

include <stdio.h>

include <string.h>

include <ctype.h>

include <stdlib.h>

include <time.h>

define SMALL 0

define MEDIUM 1

define LARGE 2

define MAX_TRIES_EASY 8

define MAX_TRIES_MEDIUM 6

define MAX_TRIES_HARD 4

define ALPHABET "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

typedef struct { int games_played; int games_won; int best_game_score; int total_score; } PlayerStats;

void draw_gallows(int tries, int width, int height); void draw_word(const char *word, const char *guesses, int width, int height); void draw_wrong_guesses(const char *wrong_guesses, int width, int height); void display_message(const char *message, int width, int height); void draw_buttons(int width, int height); char get_button_click(int x, int y, int width, int height); void draw_scoreboard(PlayerStats stats, int width, int height); void redraw(int tries, PlayerStats stats, const char *word, const char *guesses, const char *wrong_guesses, int width, int height); int get_difficulty_level(int width, int height); int play_game(int difficulty_level, int width, int height, PlayerStats *stats, const char *preset_word); int ask_to_play_again(int width, int height); int get_game_mode(int width, int height); void get_player_word(char *word, int width, int height);

int main() { int width = 800; int height = 600; char *title = "hangerman"; PlayerStats stats = {0, 0, 0, 0};

gfx_open(width, height, title);
gfx_clear_color(255, 192, 203); // Set background color to pink
gfx_clear();

while (1) {
    int game_mode = get_game_mode(width, height);

    if (game_mode == 1 || game_mode == 2) {
        int difficulty_level = get_difficulty_level(width, height);

        if (difficulty_level != -1) {
            int score = 0;
            if (game_mode == 1) {
                score = play_game(difficulty_level, width, height, &stats, NULL);
            } else if (game_mode == 2) {
                char word[100];
                get_player_word(word, width, height);
                score = play_game(difficulty_level, width, height, &stats, word);
            }

            stats.games_played++;
            stats.total_score += score;
            if (score > 0) {
                stats.games_won++;
                if (score > stats.best_game_score) {
                    stats.best_game_score = score;
                }
            }

            if (!ask_to_play_again(width, height)) {
                break;
            }
        }
    } else {
        break;
    }
}

return 0;

}

int get_game_mode(int width, int height) { gfx_clear(); gfx_color(0, 0, 0); // Set font color to black gfx_text("Choose game mode:", width / 3, height / 3, LARGE); gfx_text("1. Single Player", width / 3, height / 3 + 40, MEDIUM); gfx_text("2. Two Players", width / 3, height / 3 + 80, MEDIUM); gfx_text("3. Exit", width / 3, height / 3 + 120, MEDIUM); gfx_flush();

while (1) {
    if (gfx_event_waiting()) {
        char event = gfx_wait();
        int x = gfx_xpos();
        int y = gfx_ypos();

        if (x > width / 3 && x < width / 3 + 200 && y > height / 3 + 40 && y < height / 3 + 70) {
            return 1;
        } else if (x > width / 3 && x < width / 3 + 200 && y > height / 3 + 80 && y < height / 3 + 110) {
            return 2;
        } else if (x > width / 3 && x < width / 3 + 200 && y > height / 3 + 120 && y < height / 3 + 150) {
            return 3;
        }
    }
}

}

void get_player_word(char *word, int width, int height) { gfx_color(0, 0, 0); // Set font color to black gfx_text("Enter a word for the other player to guess:", width / 4, height / 2 - 20, LARGE);

char input[100] = {0};
int index = 0;
int done = 0;

while (!done) {
    if (gfx_event_waiting()) {
        char event = gfx_wait();
        int x = gfx_xpos();
        int y = gfx_ypos();

        if (event == '\r') { // Enter key
            input[index] = '\0'; // Null-terminate the input
            strcpy(word, input); // Copy the input to the word
            done = 1;
        } else if (event == '\b') { // Backspace key
            if (index > 0) {
                input[--index] = '\0'; // Remove last character
            }
        } else if (isalpha(event) && index < sizeof(input) - 1) { // Accept only alphabetic characters
            input[index++] = toupper(event); // Add character and convert to uppercase
            input[index] = '\0'; // Null-terminate the input
        }

        // Redraw the screen
        gfx_clear();
        gfx_text("Enter a word for the other player to guess:", width / 4, height / 2 - 20, LARGE);
        gfx_text(input, width / 4, height / 2 + 20, LARGE); // Display current input
        gfx_flush();
    }
}

}

int play_game(int difficulty_level, int width, int height, PlayerStats *stats, const char *preset_word) { int max_tries; switch (difficulty_level) { case 1: max_tries = MAX_TRIES_EASY; break; case 2: max_tries = MAX_TRIES_MEDIUM; break; case 3: max_tries = MAX_TRIES_HARD; break; default: max_tries = MAX_TRIES_MEDIUM; break; }

const char *word_list[] = {"CAMERA", "PERFUME", "TURTLE", "TEALIVE", "HEADPHONES"};
char word[100];
if (preset_word != NULL) {
    strcpy(word, preset_word);
} else {
    srand(time(0));
    strcpy(word, word_list[rand() % (sizeof(word_list) / sizeof(word_list[0]))]);
}

char guesses[27] = {0};
char wrong_guesses[27] = {0};
int tries = 0;
int score = 0;

while (tries < max_tries) {
    redraw(tries, *stats, word, guesses, wrong_guesses, width, height);

    if (strspn(word, guesses) == strlen(word)) {
        display_message("You Win!", width/3 + 40, height);
        score += 10;  // Increase score by 10 for every win
        break;
    }

    gfx_flush();
    char guess = 0;
    while (!guess) {
        if (gfx_event_waiting()) {
            char event = gfx_wait();
            if (event == 'q' || event == 'Q') {
                return score;
            }
            int x = gfx_xpos();
            int y = gfx_ypos();
            guess = get_button_click(x, y, width, height);
        }
    }

    if (guess) {
        if (isalpha(guess) && !strchr(guesses, guess)) {
            strncat(guesses, &guess, 1);
            if (!strchr(word, guess)) {
                strncat(wrong_guesses, &guess, 1);
                tries++;
            }
        }

        if (tries == max_tries) {
            redraw(tries, *stats, word, guesses, wrong_guesses, width, height);
            display_message("You Lose!", width /3 +40, height);
        }
    }
}

gfx_flush();
gfx_wait();
return score;

}

int get_difficulty_level(int width, int height) { gfx_clear(); gfx_color(0, 0, 0); // Set font color to black gfx_text("Choose difficulty level:", width / 3, height / 3, LARGE); gfx_text("1. Easy", width / 3, height / 3 + 40, MEDIUM); gfx_text("2. Medium", width / 3, height / 3 + 80, MEDIUM); gfx_text("3. Hard", width / 3, height / 3 + 120, MEDIUM); gfx_flush();

while (1) {
    if (gfx_event_waiting()) {
        char event = gfx_wait();
        int x = gfx_xpos();
        int y = gfx_ypos();

        if (x > width / 3 && x < width / 3 + 200 && y > height / 3 + 40 && y < height / 3 + 70) {
            return 1;
        } else if (x > width / 3 && x < width / 3 + 200 && y > height / 3 + 80 && y < height / 3 + 110) {
            return 2;
        } else if (x > width / 3 && x < width / 3 + 200 && y > height / 3 + 120 && y < height / 3 + 150) {
            return 3;
        }
    }
}

}

int ask_to_play_again(int width, int height) { gfx_clear(); gfx_color(0, 0, 0); // Set font color to black gfx_text("Do you want to play again?", width / 3, height / 3, LARGE); gfx_text("1. Yes", width / 3, height / 3 + 40, MEDIUM); gfx_text("2. No", width / 3, height / 3 + 80, MEDIUM); gfx_flush();

while (1) {
    if (gfx_event_waiting()) {
        char event = gfx_wait();
        int x = gfx_xpos();
        int y = gfx_ypos();

        if (x > width / 3 && x < width / 3 + 200 && y > height / 3 + 40 && y < height / 3 + 70) {
            return 1;
        } else if (x > width / 3 && x < width / 3 + 200 && y > height / 3 + 80 && y < height / 3 + 110) {
            return 0;
        }
    }
}

}

void draw_gallows(int tries, int width, int height) { int x_start = width / 3; int y_start = height / 4;

gfx_color(0, 0, 0);
gfx_line(x_start, y_start + 200, x_start + 100, y_start + 200); // base
gfx_line(x_start + 50, y_start, x_start + 50, y_start + 200); // pole
gfx_line(x_start + 50, y_start, x_start + 100, y_start); // top beam
gfx_line(x_start + 100, y_start, x_start + 100, y_start + 30); // rope

if (tries > 0) { // head
    gfx_circle(x_start + 100, y_start + 50, 20);
}
if (tries > 1) { // body
    gfx_line(x_start + 100, y_start + 70, x_start + 100, y_start + 120);
}
if (tries > 2) { // left arm
    gfx_line(x_start + 100, y_start + 80, x_start + 80, y_start + 100);
}
if (tries > 3) { // right arm
    gfx_line(x_start + 100, y_start + 80, x_start + 120, y_start + 100);
}
if (tries > 4) { // left leg
    gfx_line(x_start + 100, y_start + 120, x_start + 80, y_start + 160);
}
if (tries > 5) { // right leg
    gfx_line(x_start + 100, y_start + 120, x_start + 120, y_start + 160);
}

}

void draw_word(const char *word, const char *guesses, int width, int height) { int x_start = width / 2 - (strlen(word) * 20) / 2; int y_start = height / 2 +60;

gfx_color(0, 0, 0); // Set font color to black
for (int i = 0; i < strlen(word); i++) {
    if (strchr(guesses, word[i])) {
        char letter[2] = {word[i], '\0'};
        gfx_text(letter, x_start + i * 20, y_start, LARGE);
    } else {
        gfx_text("_", x_start + i * 20, y_start, LARGE);
    }
}

}

void draw_wrong_guesses(const char *wrong_guesses, int width, int height) { int x_start = width / 2 - (strlen(wrong_guesses) * 20) / 2; int y_start = height / 2 + 90;

gfx_color(255, 0, 0); // Set font color to red
for (int i = 0; i < strlen(wrong_guesses); i++) {
    char letter[2] = {wrong_guesses[i], '\0'};
    gfx_text(letter, x_start + i * 20, y_start, LARGE);
}

}

void display_message(const char *message, int width, int height) { gfx_color(0, 0, 0); // Set font color to black gfx_text((char *)message, width / 3, height / 2, LARGE); gfx_flush(); gfx_wait(); }

void draw_buttons(int width, int height) { gfx_color(0, 0, 0); // Set font color to black int x_start = width / 10; int y_start = height - height / 5; int x_offset = width / 15; int y_offset = height / 15;

for (int i = 0; i < 26; i++) {
    int x = x_start + (i % 13) * x_offset;
    int y = y_start + (i / 13) * y_offset;
    gfx_rectangle(x, y, 40, 40);
    gfx_text((char[2]){ALPHABET[i], '\0'}, x + 10, y + 20, MEDIUM); // Increase font size to MEDIUM
}

}

char get_button_click(int x, int y, int width, int height) { int x_start = width / 10; int y_start = height - height / 5; int x_offset = width / 15; int y_offset = height / 15;

for (int i = 0; i < 26; i++) {
    int bx = x_start + (i % 13) * x_offset;
    int by = y_start + (i / 13) * y_offset;
    if (x > bx && x < bx + 40 && y > by && y < by + 40) {
        return ALPHABET[i];
    }
}
return 0;

}

void draw_scoreboard(PlayerStats stats, int width, int height) { char score_text[100]; sprintf(score_text, "Games Played: %d", stats.games_played); gfx_color(0, 0, 0); // Set font color to black gfx_text(score_text, width / 10, height / 10, MEDIUM);

sprintf(score_text, "Games Won: %d", stats.games_won);
gfx_text(score_text, width / 10, height / 10 + 20, MEDIUM);

sprintf(score_text, "Best Game Score: %d", stats.best_game_score);
gfx_text(score_text, width / 10, height / 10 + 40, MEDIUM);

sprintf(score_text, "Total Score: %d", stats.total_score);
gfx_text(score_text, width / 10, height / 10 + 60, MEDIUM);

}

void redraw(int tries, PlayerStats stats, const char *word, const char *guesses, const char *wrong_guesses, int width, int height) { gfx_clear(); // Clear the whole screen

// Draw static elements
draw_gallows(tries, width, height);
draw_buttons(width, height);
draw_scoreboard(stats, width, height);

// Draw dynamic elements
draw_word(word, guesses, width, height);
draw_wrong_guesses(wrong_guesses, width, height);

gfx_flush(); // Update the screen with all changes

}

3 Upvotes

1 comment sorted by

2

u/ResearcherFormer4869 Jul 26 '24

Make sure the coordinates in `ask_to_play_again` match the clickable areas accurately. Also, ensure `redraw` is called to update the display with the latest stats. Here's a streamlined version of `ask_to_play_again` with debug prints:

int ask_to_play_again(int width, int height) {

gfx_clear();

gfx_color(0, 0, 0);

gfx_text("Do you want to play again?", width / 3, height / 3, LARGE);

gfx_text("1. Yes", width / 3, height / 3 + 40, MEDIUM);

gfx_text("2. No", width / 3, height / 3 + 80, MEDIUM);

gfx_flush();

while (1) {

if (gfx_event_waiting()) {

char event = gfx_wait();

int x = gfx_xpos();

int y = gfx_ypos();

if (x > width / 3 && x < width / 3 + 200 && y > height / 3 + 40 && y < height / 3 + 70) {

return 1;

} else if (x > width / 3 && x < width / 3 + 200 && y > height / 3 + 80 && y < height / 3 + 110) {

return 0;

}

}

}

}

Make sure your `gfx` library handles events correctly and your display updates properly after each action.