ग्रिड-आधारित गेम में जब खिलाड़ी सड़कों से गुजरता है तो मैं सड़कों को गायब होने से कैसे रोकूँ? – सी प्रोग्रामिंग


मैं वर्तमान में सी में एक ग्रिड-आधारित गेम पर काम कर रहा हूं जहां मेरे पास डॉट्स (‘.’) द्वारा दर्शाई गई सड़कें हैं और ‘पी’ द्वारा दर्शाया गया एक खिलाड़ी है। खिलाड़ी का लक्ष्य गंतव्य (‘जी’) तक पहुंचना है। हालाँकि, जब खिलाड़ी उनसे होकर गुजरता है तो मुझे सड़कों को दृश्यमान बनाए रखने में एक चुनौती का सामना करना पड़ रहा है।

समस्या विवरण:

ग्रिड को सड़कों और सीमाओं के साथ आरंभ किया जाता है, लेकिन जब खिलाड़ी उन पर चलता है तो सड़कें गायब हो जाती हैं।
मूवप्लेयर फ़ंक्शन में खिलाड़ी की आवाजाही के दौरान सड़क कोशिकाओं को संरक्षित करने के मेरे प्रयासों के बावजूद, सड़कें अभी भी गायब हैं।

#include <stdio.h>
#include <stdlib.h>
#include <ncurses.h>
#include "traffic.h"

void initializeGame(char **grid, int rows, int cols) {
    int i, j;  /* Declare loop control variables */
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            if (i == 0 || i == rows - 1 || j == 0 || j == cols - 1) {
                grid[i][j] = '*'; /* Set border cells to "*" */
            } else if ((i % 2 == 0) && (j > 0 && j < cols - 1)) {
                grid[i][j] = '.'; /* Set road cells to "." on even rows */
            } else {
                grid[i][j] = ' '; /* Set other cells inside borders to empty */
            }
        }
    }

    /* Place the player at the top-left corner and the goal at the bottom-right corner */
    grid[1][1] = 'P';
    grid[rows - 2][cols - 2] = 'G';
}


/* Function to display the game grid with borders */
void displayGame(char **grid, int rows, int cols, int prevPlayerRow, int prevPlayerCol) {
    int i, j;  /* Declare loop control variables */
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            /* Move the cursor to the current cell */
            move(i, j);

            /* Print the content of the grid */
            printw("%c", grid[i][j]);
        }
    }

    mvprintw(rows, 0, "Press 'w' to move up");
    mvprintw(rows + 1, 0, "Press 'a' to move left");
    mvprintw(rows + 2, 0, "Press 's' to move down");
    mvprintw(rows + 3, 0, "Press 'd' to move right");

    refresh(); /* Refresh the screen using ncurses */
}

void movePlayer(char **grid, int rows, int cols, int move, int *win, int *prevPlayerRow, int *prevPlayerCol) {
    int i, j, playerRow, playerCol;  /* Declare loop control variables */

    /* Find the current position of the player */
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            if (grid[i][j] == 'P') {
                playerRow = i;
                playerCol = j;
            }
        }
    }

    /* Clear the player's previous position */
    grid[*prevPlayerRow][*prevPlayerCol] = ' ';

    /* Move the player based on the user's input */
    switch (move) {
        case 'w':
            if (playerRow > 1) {
                playerRow--;
            }
            break;
        case 'a':
            if (playerCol > 1) {
                playerCol--;
            }
            break;
        case 's':
            if (playerRow < rows - 2) {
                playerRow++;
            }
            break;
        case 'd':
            if (playerCol < cols - 2) {
                playerCol++;
            }
            break;
        default:
            /* Invalid move */
            break;
    }

    /* Set the player's new position */
    grid[playerRow][playerCol] = 'P';

    /* Update the previous player position */
    *prevPlayerRow = playerRow;
    *prevPlayerCol = playerCol;

    /* Check if the player reached the goal */
    if (playerRow == rows - 2 && playerCol == cols - 2) {
        *win = 1; /* Set win flag to 1 */
    }
}



/* Main function, entry point of the program */
int main(int argc, char *argv[]) {
    /* Check the number of command-line arguments */
    if (argc != 3) {
        printf("Usage: %s <map_row> <map_col>\n", argv[0]);
        return 1;
    }

    /* Parse command-line arguments */
    int mapRows = atoi(argv[1]);
    int mapCols = atoi(argv[2]);

    /* Check for valid map size */
    if (mapRows < 3 || mapRows % 2 == 0 || mapCols < 5) {
        printf("Invalid map size. Please ensure <map_row> is an odd number >= 3 and <map_col> is >= 5.\n");
        return 1;
    }

    /* Dynamically allocate memory for the 2D char array */
    char **gameGrid = (char **)malloc(mapRows * sizeof(char *));
    int i;  /* Declare loop control variable */
    for (i = 0; i < mapRows; i++) {
        gameGrid[i] = (char *)malloc(mapCols * sizeof(char));
    }

    /* Initialize the game grid with "*" and place the player and goal inside borders */
    initializeGame(gameGrid, mapRows, mapCols);

    initscr(); /* Initialize ncurses */

    timeout(0); /* Set non-blocking input */

    char move;
    int win = 0; /* Win flag */
    int prevPlayerRow = 1; /* Initialize with player's initial row */
    int prevPlayerCol = 1; /* Initialize with player's initial column */

    do {
        /* Display the updated game grid with borders */
        displayGame(gameGrid, mapRows, mapCols, prevPlayerRow, prevPlayerCol);

        /* Get user input for player movement */
        move = getch(); /* Get the key without waiting for 'Enter' */

        /* Move the player based on user input */
        movePlayer(gameGrid, mapRows, mapCols, move, &win, &prevPlayerRow, &prevPlayerCol);

        /* Check if the player has won */
        if (win) {
            clear(); /* Clear the screen */
            printw("Congratulations! You have cleared the level.\n");
            printw("Press any key to exit.\n");
            refresh(); /* Refresh the screen to display the message */
            getch(); /* Wait for a key press before ending the program */
        }

    } while (move != 'q' && !win); /* Loop until the user enters 'q' or wins */

    endwin(); /* End ncurses */

    /* Free the dynamically allocated memory */
    for (i = 0; i < mapRows; i++) {
        free(gameGrid[i]);
    }
    free(gameGrid);
    

    return 0; /* Properly reach the end of the main function */
}

मैंने क्या प्रयास किया है:

मैंने यह सुनिश्चित करने के लिए मूवप्लेयर फ़ंक्शन की समीक्षा की है कि प्लेयर के दौरान रोड सेल संशोधित न हों
मैंने सड़क कोशिकाओं और खिलाड़ी आंदोलन के लिए तर्क को अलग करने का प्रयास किया लेकिन अभी भी गायब हो रही सड़कों की समस्या का सामना करना पड़ रहा है।

समाधान 1

नामक एक वैश्विक वैरिएबल बनाएं lastSurfaceType और इसे “सड़क” से प्रारंभ करें।
जब आप किसी खिलाड़ी को घुमाते हैं, तो उस वर्ग को सेट करें जिस पर वह है lastSurfaceType इससे पहले कि आप कोई कदम उठाएं, फिर वेरिएबल को उस प्रकार पर सेट करें जिस पर खिलाड़ी आगे बढ़ने वाला है।

इस तरह, अगली बार जब आप खिलाड़ी को स्थानांतरित करेंगे तो आपके पास “पी” को बदलने के लिए सही प्रकार की सतह तैयार होगी जिसे आप स्थानांतरित करने वाले हैं।

समाधान 2

उद्धरण:

ग्रिड-आधारित गेम में जब खिलाड़ी सड़कों से गुजरता है तो मैं सड़कों को गायब होने से कैसे रोकूँ? – सी प्रोग्रामिंग

प्रतिस्थापित करें

सी++
/* Clear the player's previous position */
grid[*prevPlayerRow][*prevPlayerCol] = ' ';

साथ

सी++
/* Clear the player's previous position */
grid[*prevPlayerRow][*prevPlayerCol] = '.';

जब आप किसी सेल से प्लेयर हटाते हैं, तो उसे खाली करने के बजाय उसे एक सड़क बना दें।

コメント

タイトルとURLをコピーしました