我在打印点和框网格时遇到问题

时间:2019-12-08 21:54:07

标签: c arrays game-engine

在c的点和盒游戏中,当用户输入输入以画一条线时,程序将在网格中打印此线,但是当他输入另一条时,不会打印前一行,而只打印最后一行,因此我如何使其不被删除,因此网格的形状将在每次绘制线和不删除先前的行时进行更新。

#include <stdio.h>
#include <stdlib.h>
void grid(int gameMode,int r,int c){
    if (gameMode == 1) {                                                             // Beginner Mode Grid
        char gridArray[5][5];
        printf("\t\t\t     0   1   2   3   4");
        for(int i=0 ; i<5 ; i++) {
            printf("\n\n");
            printf("\t\t\t%d    ",i);
            for (int j=0 ; j<5 ; j++) {                                             // loop for printing the grid with the play of the user
            if (i%2 == 0)                                                           // when i is even
                if (j%2 == 0) {                                                     // i is even and j is even
                    gridArray[i][j] = 254;
                    printf("%c",gridArray[i][j]);
                }
                else                                                                // i is even and j is odd
                {
                    if (r != i || c != j) {                                         // when the entered row doesn't equal i or when the entered column doesn't equal j
                        gridArray[i][j] = ' ';
                        printf("   %c   ",gridArray[i][j]);
                    }
                    else if (r == i && c == j) {                                    // when the entered row = i and the entered column = j
                        i=r;                                                        // store r and c in i and j respectively
                        j=c;
                        gridArray[i][j] = 205;
                        printf("   %c   ",gridArray[i][j]);
                    }
                }
            }
            else                                                                    // when i is odd
            {
                if (j%2 == 1) {                                                     // when i is odd and j is odd too
                    gridArray[i][j] = ' ';
                    printf("%c   ",gridArray[i][j]);
                }
                else {                                                              // when i is odd and j is  even
                    if (r != i || c!= j) {                                          // when the entered row doesn't equal i or when the entered column doesn't equal j
                    gridArray[i][j] = ' ';
                    printf("%c   ",gridArray[i][j]);
                    }
                    else if (r == i && c == j) {                                    // when the entered row = i and the entered column = j
                    i=r;
                    j=c;
                    gridArray[i][j] = 186;
                    printf("%c   ",gridArray[i][j]);
                    }
                }
            }
            }
    }
}
    else if (gameMode == 2) {                                                                    // Expert Mode Grid
        char gridArray[11][11];
        printf("\t\t\t\t0   1   2   3   4   5   6   7   8   9   10");
        for(int i=0 ; i<11 ; i++) {
            printf("\n\n");
            printf("\t\t\t%d\t",i);
            for (int j=0 ; j<11 ; j++) {
            if (i%2 == 0) {
                if (j%2 == 0) {
                    gridArray[i][j] = 254;
                    printf("%c",gridArray[i][j]);
                }
                else
                {
                    if (r != i || c != j) {
                        gridArray[i][j] = ' ';
                        printf("   %c   ",gridArray[i][j]);
                    }
                    else if (r == i && c == j) {
                        i=r;
                        j=c;
                        gridArray[i][j] = 205;
                        printf("   %c   ",gridArray[i][j]);
                    }
                }
            }
            else {

                if (j%2 == 1) {
                    gridArray[i][j] = ' ';
                    printf("%c   ",gridArray[i][j]);
                }
                else {
                    if (r != i || c!= j) {
                        gridArray[i][j] = ' ';
                        printf("%c   ",gridArray[i][j]);
                    }
                    else if (r == i && c == j) {
                        i=r;
                        j=c;
                        gridArray[i][j] = 186;
                        printf("%c   ",gridArray[i][j]);
                    }
                }
       }
   }
 }
}
}
int main()
{
    printf("      Hello Gamer!\n");
    printf("(1) Choose Mode Of The Game\n");
    printf("   \" BEGINNER \" Press 1\n    \" EXPERT \"  Press 2\n");
    int  gameMode, NoOfLines;
    scanf("%d",&gameMode);

    if (gameMode == 1) {
            NoOfLines = 12;                                                                  // Places where the gamer can connect 2 dots in beginner mode
    }
    else if (gameMode == 2) {
            NoOfLines = 30;                                                                  // Places where the gamer can connect 2 dots in expert mode
    }
    for (int i=0 ; i<NoOfLines ; i++) {
    printf("Enter Row Then Column Of The Required Line as 0 3 --> ");
    int r , c;                                                                               // r = row , c = column
    scanf("%d %d",&r,&c);
    printf("\n");
    grid(gameMode,r,c);                                                                      // function for printing the grid
    printf("\n\n");
    }
    return 0;
}

0 个答案:

没有答案