I am new to c programming. My aim is to break out the loop and move the number to connect to each other. I am not sure why my code is not working, I tried many times but still no luck. I really need help...please...
Here's my code :
#include <stdio.h>
#include <stdlib.h>
void 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 x,r1,r2,r3,r4;
char r='1';
srand( (unsigned) time(NULL));
for(x=0;x<8;x++)
{
r1 = 1 + rand()%7;
r2 = 1 + rand()%7;
r3 = 1 + rand()%7;
r4 = 1 + rand()%7;
box[r1][r2]= r + x;
box[r3][r4]= r + x;
}
}
void playgame(char box[9][9])
{
int x, y, choice2,num;
char input;
do{
printf("Please select a number (1-7) : ");
scanf("%i",&num);
if(num==0 ||num > 7)
{
printf("Invalid!\n");
}
else
{
printf("\nNumer %i is currently selected!\n", num);
}
}while(num==0 ||num > 7);
printf("\n[1]Move\n[2]Sign out\n");
printf("\nEnter choice: \n");
scanf("%d", &choice2);
switch(choice2)
{
case 1:
{
printf("Press 'e' to go up\n");
}
{
printf("Press 'd' to go right\n");
}
{
printf("Press 's' to go left\n");
}
{
printf("Press 'x' to go down\n");
}
fflush(stdin);
scanf("%c", &input);
break;
case 2: printf("Bye!\n");
}
for(x=0; x<9; x++)
for(y=0; y<9; y++)
{
if(input == 'e')
if(box[x][y]==num)
{
box[x-1][y]=num;
}
else if(input == 'd')
if(box[x][y]==num)
{
box[x][y+1] =num;
}
else if(input == 's')
if(box[x][y]== num)
{
box[x][y-1] = num;
}
else if(input == 'x')
if(box[x][y]==num)
{
box[x+1][y] =num;
}
}
}
int main(void)
{
char box[9][9];
char name[20];
int x,y,choice,num;
printf("************* WELCOME TO NUMBER CONNECT ! ******************\n\n");
printf("Please key in your name :");
scanf("%s", &name);
printf("***Good day %s, let's start the game...all the best!!!***\n\n", name);
gameboard(box);
rnum(box);
for(x = 0; x < 9; x++)
{
for(y = 0; y < 9; y++)
{
printf(" %c ",box[x][y]);
}
printf("\n\n");
}
playgame(box);
for(x = 0; x < 9; x++)
{
for(y = 0; y < 9; y++)
{
printf(" %c ",box[x][y]);
}
printf("\n\n");
}
do {
printf("[1] Game Start\n[2] Logout\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: printf("Please select a number (1-7) : ");
scanf("%i",&num);
break;
case 2: printf("Bye!\n");
break;
}
}while(choice!=2);
return 0;
}
For example:
Connect a pair number '2'
(http://i.stack.imgur.com/Q2Kn1.jpg)))
It looks like you're new here. If you want to get involved, click one of these buttons!