03-21-2013, 07:56 PM
(03-20-2013, 08:01 PM)EpicEbilninja Wrote: I took some time today to finaly write my first actual program in c
The game is very basic, all you have to do is sort characters in a 4x4 grid/array.
I think I abused "printf()" a bit, there's no graphics and the codes could be a bit better but I atleast got it to function correctly...
just posting the code for now though
Code:#include <stdio.h>
int main()
{
int control=1;
char select='0';
char gameGrid[4][4]={
'2','1','2','4',
'1','2','3','2',
'3','4','4','3',
'4','3','1','1'
};
char a;
char b;
char c;
char d;
int turnNum=0;
int level=1;
char YorN='0';
int win=0;
for(;;)
{
if(control==1)
{
control=2;
printf("\n Sort the characters!\n");
printf("\n Game %d...\n" ,level);
printf("\n a b c d\n");
printf("\n 1 %c %c %c %c\n",gameGrid[0][0],gameGrid[0][1],gameGrid[0][2],gameGrid[0][3]);
printf(" 2 %c %c %c %c\n",gameGrid[1][0],gameGrid[1][1],gameGrid[1][2],gameGrid[1][3]);
printf(" 3 %c %c %c %c\n",gameGrid[2][0],gameGrid[2][1],gameGrid[2][2],gameGrid[2][3]);
printf(" 4 %c %c %c %c\n\n\n",gameGrid[3][0],gameGrid[3][1],gameGrid[3][2],gameGrid[3][3]);
printf(" So far, you spent \"%d\" turns.\n" ,turnNum);
printf("\n\n Please select from numbers \"1-4\" or letters \"a-d\" to move the the grid.\n\n\n\n\n");
}
if(win==0){select=getchar();}
if(win==0)
{
if(select=='1')
{
printf("\n You selected \"1\"!\n\n");
a=gameGrid[0][0]; b=gameGrid[0][1]; c=gameGrid[0][2]; d=gameGrid[0][3];
gameGrid[0][0]=b; gameGrid[0][1]=c; gameGrid[0][2]=d; gameGrid[0][3]=a;
control=1;
turnNum++;
select='0';
}
if(select=='2')
{
printf("\n You selected \"2\"!\n\n");
a=gameGrid[1][0]; b=gameGrid[1][1]; c=gameGrid[1][2]; d=gameGrid[1][3];
gameGrid[1][0]=b; gameGrid[1][1]=c; gameGrid[1][2]=d; gameGrid[1][3]=a;
control=1;
turnNum++;
select='0';
}
if(select=='3')
{
printf("\n You selected \"3\"!\n\n");
a=gameGrid[2][0]; b=gameGrid[2][1]; c=gameGrid[2][2]; d=gameGrid[2][3];
gameGrid[2][0]=b; gameGrid[2][1]=c; gameGrid[2][2]=d; gameGrid[2][3]=a;
control=1;
turnNum++;
select='0';
}
if(select=='4')
{
printf("\n You selected \"4\"!\n\n");
a=gameGrid[3][0]; b=gameGrid[3][1]; c=gameGrid[3][2]; d=gameGrid[3][3];
gameGrid[3][0]=b; gameGrid[3][1]=c; gameGrid[3][2]=d; gameGrid[3][3]=a;
control=1;
turnNum++;
select='0';
}
if(select=='a' || select=='A')
{
printf("\n You selected \"a\"!\n\n");
a=gameGrid[0][0]; b=gameGrid[1][0]; c=gameGrid[2][0]; d=gameGrid[3][0];
gameGrid[0][0]=b; gameGrid[1][0]=c; gameGrid[2][0]=d; gameGrid[3][0]=a;
control=1;
turnNum++;
select='0';
}
if(select=='b' || select=='B')
{
printf("\n You selected \"b\"!\n\n");
a=gameGrid[0][1]; b=gameGrid[1][1]; c=gameGrid[2][1]; d=gameGrid[3][1];
gameGrid[0][1]=b; gameGrid[1][1]=c; gameGrid[2][1]=d; gameGrid[3][1]=a;
control=1;
turnNum++;
select='0';
}
if(select=='c' || select=='C')
{
printf("\n You selected \"c\"!\n\n");
a=gameGrid[0][2]; b=gameGrid[1][2]; c=gameGrid[2][2]; d=gameGrid[3][2];
gameGrid[0][2]=b; gameGrid[1][2]=c; gameGrid[2][2]=d; gameGrid[3][2]=a;
control=1;
turnNum++;
select='0';
}
if(select=='d' || select=='D')
{
printf("\n You selected \"d\"!\n\n");
a=gameGrid[0][3]; b=gameGrid[1][3]; c=gameGrid[2][3]; d=gameGrid[3][3];
gameGrid[0][3]=b; gameGrid[1][3]=c; gameGrid[2][3]=d; gameGrid[3][3]=a;
control=1;
turnNum++;
select='0';
}
}
if(gameGrid[0][0]=='1' && gameGrid[0][1]=='1' && gameGrid[0][2]=='1' && gameGrid[0][3]=='1' &&
gameGrid[1][0]=='2' && gameGrid[1][1]=='2' && gameGrid[1][2]=='2' && gameGrid[1][3]=='2' &&
gameGrid[2][0]=='3' && gameGrid[2][1]=='3' && gameGrid[2][2]=='3' && gameGrid[2][3]=='3' && win==0)
{win=1;
printf("You win!!! Want to play level 2? Y/N\n");}
if(gameGrid[0][0]=='a' && gameGrid[0][1]=='b' && gameGrid[0][2]=='c' && gameGrid[0][3]=='d' &&
gameGrid[1][0]=='a' && gameGrid[1][1]=='b' && gameGrid[1][2]=='c' && gameGrid[1][3]=='d' &&
gameGrid[2][0]=='a' && gameGrid[2][1]=='b' && gameGrid[2][2]=='c' && gameGrid[2][3]=='d' &&
gameGrid[3][0]=='a' && gameGrid[3][1]=='b' && gameGrid[3][2]=='c' && gameGrid[3][3]=='d' && win==0)
{win=1;
printf("You win!!! Want to play level 1? Y/N\n");
level++;}
if(win==1)
{
YorN=getchar();
}
if(YorN=='n' || YorN=='N')
{return(0);}
if(YorN=='y' || YorN=='Y')
{
if(level%2==0)
{
gameGrid[0][0]='2';gameGrid[1][0]='1';gameGrid[2][0]='2';gameGrid[3][0]='4';
gameGrid[0][1]='1';gameGrid[1][1]='2';gameGrid[2][1]='3';gameGrid[3][1]='2';
gameGrid[0][2]='3';gameGrid[1][2]='4';gameGrid[2][2]='4';gameGrid[3][2]='3';
gameGrid[0][3]='4';gameGrid[1][3]='3';gameGrid[2][3]='1';gameGrid[3][3]='1';
}
if(level%2==1)
{
gameGrid[0][0]='a';gameGrid[1][0]='c';gameGrid[2][0]='b';gameGrid[3][0]='a';
gameGrid[0][1]='b';gameGrid[1][1]='d';gameGrid[2][1]='d';gameGrid[3][1]='c';
gameGrid[0][2]='d';gameGrid[1][2]='c';gameGrid[2][2]='a';gameGrid[3][2]='d';
gameGrid[0][3]='a';gameGrid[1][3]='b';gameGrid[2][3]='c';gameGrid[3][3]='b';
}
win=0;
YorN='0';
control=2;
turnNum=0;
printf("\n Sort the characters!\n\n");
printf("\n Game %d...\n" ,level);
printf("\n a b c d\n");
printf("\n 1 %c %c %c %c\n",gameGrid[0][0],gameGrid[0][1],gameGrid[0][2],gameGrid[0][3]);
printf(" 2 %c %c %c %c\n",gameGrid[1][0],gameGrid[1][1],gameGrid[1][2],gameGrid[1][3]);
printf(" 3 %c %c %c %c\n",gameGrid[2][0],gameGrid[2][1],gameGrid[2][2],gameGrid[2][3]);
printf(" 4 %c %c %c %c\n\n\n",gameGrid[3][0],gameGrid[3][1],gameGrid[3][2],gameGrid[3][3]);
printf(" So far, you spent \"%d\" turns.\n" ,turnNum);
printf("\n\n Please select from numbers \"1-4\" or letters \"a-d\" to move the the grid.\n\n\n\n\n\n");
}
}
return(0);
}
used dev-c++ by the way
Very nice first project! I do recommend you tidy your code up a bit.. My OCD is making me want to tidy it up already..