mardi 14 juin 2016

Loop a number in 2d array

This is number connect game. I am expecting to loop/move the selected number in the 2d array. But when I try to loop/move the selected number my program crashes. I need help,any solutions?

Here's the code :

#include <stdio.h>
#include <stdlib.h>

int gameboard(char box[9][9])
{
    int x,y;
    for(x = 0; x < 9; x++)
    {
        for(y = 0; y  <9; y++)
        {
            box[x][y] ='_'; 
        }
    }

    for(x = 0; x < 9; x++)
    {
        box[0][x]='#';
        box[x][8]='#';
        box[x][0]='#';
        box[8][x]='#';   
    }
}

void rnum(char box[9][9])
{
    int r1,r2,r3,r4,x;
    char r='1';

    srand( (unsigned) time(NULL) ); 
    for(x = 0; x < 7; x++)
    {
        do
        {
            r1 = 1 + rand() % 7;
            r2 = 1 + rand() % 7;
        } while(box[r1][r2] != '_');
        box[r1][r2]= r + x;

        do
        {
            r3 = 1 + rand() % 7;
            r4 = 1 + rand() % 7;
        } while(box[r3][r4] != '_');
        box[r3][r4]= r + x;
    }
}

void display(char box[9][9])
{
    char num;
    do
    {
        printf("Please select a number (1-7) : ");
        fflush(stdin);
        scanf("%c", &num);

        if(num < '1' || num > '7')
        {
            printf("Invalid!n");
            printf("Please enter another numbern");
        }
        else
        {    
            printf("nNumer %c is currently selected!n", num);
        }
    } while( num < '1' || num > '7');    
}

void playgame(char box[9][9])
{
    int a = 0, b = 0, x, y, choice2, z = 0;
    char num, input;

    printf("n[1]Moven[2]Sign outn");
    printf("nEnter choice: n");
    scanf("%d", &choice2);

    for (a = 0; a < 9; a++)
    {
        for (b = 0; b < 9; b++)
        {
            if (box[a][b] == num && z == 0)
            {              
                x=a;
                y=b;
                z++;
            }
        }
    }
    switch(choice2)
    {
    case 1: 
        printf("Press 'e' to go upn");
        printf("Press 'd' to go rightn");
        printf("Press 's' to go leftn");
        printf("Press 'x' to go downn");
        printf("Press 'q' to give upn");         
        fflush(stdin);
        scanf("%c", &input);

        if (input == 'e')
        {
            box[x-1][y] = num;
            x--;
        }
        else if (input == 'd')
        {
            box[x][y+1] = num;
            y++;
        }
        else if (input == 's')
        {
            box[x][y-1] = num;
            y--;
        }
        else if (input == 'x')
        {
            box[x+1][y] = num;
            x++;
        }
        else if(input == 'q')
        {
            printf("nThank you!n");
            exit(0);
        }
        break;
    case 2:
        printf("Bye!n");
        break;
    }   
}

int main(void)
{  
    char bx[9][9];

    int x, y, num, a, choice2;

    gameboard(bx);
    rnum(bx);     
    for(x = 0; x < 9; x++)
    {
        for(y = 0; y  < 9; y++)
        {
            printf(" %c ", bx[x][y]); 
        }
        printf("nn");
    }

    display(bx); 

    for (a = 0; a < 9; a++)
    {           
        playgame(bx);   
        for(x = 0; x < 9; x++)
        {
            for(y = 0; y  < 9; y++)
            {
                printf(" %c ", bx[x][y]);
            }
            printf("nn");
        }
   }
   return 0;
}

Aucun commentaire:

Enregistrer un commentaire