Code:
#include <stdio.h>
#define N 9
int checkEndOfGame(char board[N][N],int size);
void sprint(char board[N][N],int size);
int same_digit(char ch_cords[40],int k);
int is_valid(const char input[40],int leng);
void mainPlayGame(char board[N][N]);
int playGame(char board[N][N],int size);
void printBoard(char board[N][N],int size);
int removeMine(char board[N][N],int size,int x,int y);
void mainMenu();
int unCover(char board[N][N],int size,int x,int y);
int main()
{
char board[N][N];
mainPlayGame(board);
printf("bye, please press enter to exit!\n");
getchar();
return 0;
}
void mainPlayGame(char board[N][N])
{
int x_cord,y_cord,wins=0,loses=0;
char input[40];
char input2[40];
int flag2=0;
int opt,leng,size=-1;
int index,kndex;
char ch_cords[40];
int tr;
int cords[8][2];
int tndex,result;
int i,k,j,ch,l,x_rem,y_rem;
for(index=0;index<N;index++)
{
for(kndex=0;kndex<N;kndex++)
{
board[index][kndex]=' ';
}
}
do
{///
mainMenu();
scanf("%d",&opt);
while((l= getchar()) != '\n');
if ((opt<0)||(opt>9))
{
}
else
{
if (opt==1)
{
for(index=0;index<N;index++)
{
for(kndex=0;kndex<N;kndex++)
{
board[index][kndex]=' ';
}
}
printf("enter board size [1..9] (no input control is needed):\n");
scanf("%d",&size);
while((l= getchar()) != '\n');
}
if (opt==2)
{
flag2=0;
if (size==-1)
{
printf("you must choose board size first!\n");
}
else
{
printf("enter x,y of the new mines (x1,y1)(x2,y2)...(xn,yn)\n");
for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch >=0; ++i)
{
input2[i] = ch;
}
input2[i] = '\0';
k=0;
for(index=0;index<=i;index++)
{
if(input2[index]!=' ')
{
input[k]=input2[index];
k++;
}
}
leng=k;//same digit input check
if (is_valid(input,leng))
{
for(index=0;index<leng;index++)
{
if (((input[index])>='0')&&((input[index])<='9')&&((input[index+1])>='0')&&((input[index+1])<='9'))
{
flag2=1;
}
}
k=0;
if (flag2!=1){
for(index=0;index<leng;index++)
{
if((input[index]!='(')&&(input[index]!=',')&&(input[index]!=')'))
{
ch_cords[k]=input[index];
k++;
}
}
k--;
tndex=0;
for(index=0;index<=leng;index++)
{
if((input[index]==')'))
{
tndex++;
}
}
j=0;
for(index=0;index<tndex;index++)
{
for(kndex=0;kndex<2;kndex++)
{
tr=ch_cords[j]-'0';
cords[index][kndex]=tr;
j++;
}
}
result=same_digit(ch_cords,tndex); //i call the function here
if (result==0)
{
for(index=0;index<tndex;index++)
{
x_cord=cords[index][0];
y_cord=cords[index][1];
if((x_cord>size)||(y_cord>size)||(y_cord<0)||(x_cord<0))
{
//out of bound
printf("(%d,%d) is out of range\n",x_cord,y_cord);
}
else
{ //excepted
if (board[y_cord-1][x_cord-1]==' ')
{
board[y_cord-1][x_cord-1]='*';
printf("(%d,%d) mine inserted\n",x_cord,y_cord);
}
else
{
printf("(%d,%d) already full\n",x_cord,y_cord);
}
}
}
}
}
else
{
printf("wrong input\n");
}
}
else
{
printf("wrong input\n");
}
}
}//end if opt 2///
if (opt==3)
{
if (size==-1)
{
printf("you must choose board size first!\n");
}
else
{
printf("enter x,y of the mine to remove (no input control is needed):\n");
for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch >=0; ++i)
{
input2[i] = ch;
}
input2[i] = '\0';
ch_cords[0]=input2[0];
ch_cords[2]=input2[2];
x_rem=ch_cords[0]-'0';
y_rem=ch_cords[2]-'0';
if (board[y_rem-1][x_rem-1]!='*')
{
printf("wrong input or no mine\n");
}
else
{
printf("mine removed\n");
board[y_rem-1][x_rem-1]=' ';
}
}
}
if (opt==4)
{
if (size==-1)
{
printf("you must choose board size first!\n");
}
else
{
printBoard(board,size);
}
}
if (opt==5)
{
tr=playGame(board,size);
if (tr==1){
wins++;
}
if (tr==0){
loses++;
}
}
if (opt==0)
{
printf("you played %d games(s):\n",wins+loses);
printf("you won %d\n",wins);
printf("you lost %d\n",loses);
printf("bye!");
}
}
}while(opt!=0);
}
int same_digit(char ch_cords[40],int k)
{
int index;
char ch;
ch=ch_cords[0];
for (index=0;index<=k;index++)
{
if (ch_cords[index]!=ch)
{
return 0;
}
}
if(k>2)
{
return 1;
}
else
{
return 0;
}
}//end same digit
void mainMenu()
{
printf("--------------------------\n");
printf("welcome to the game\n");
printf("1. choose board size\n");
printf("2. place mines\n");
printf("3. remove mines\n");
printf("4. show mines\n");
printf("5. start the game\n");
printf("0. exit\n");
printf("please enter your choice (input control is needed): \n");
}
void printBoard(char board[N][N],int size)
{
int index,kndex;
for(index=0;index<size;index++)
{
printf("+");
for(kndex=0;kndex<size;kndex++)
{
printf("-+");
}
printf("\n");
printf("|");
for(kndex=0;kndex<size;kndex++)
{
printf("%c|",board[index][kndex]);
}
printf("\n");
}
printf("+");
for(kndex=0;kndex<size;kndex++)
{
printf("-+");
}
printf("\n");
}
int removeMine(char board[N][N],int size,int x,int y)
{
if (board[y-1][x-1]=='*')
{
board[y-1][x-1]=' ';
printf("mine removed");
return 1;
}
else
{
printf("wrong input or no mine");
return 0;
}
}
int unCover(char board[N][N],int size,int x,int y){
int count=0;
if (board[y-1][x-1]=='*'){
printf("you lost\n");
board[y-1][x-1]='X';
return 0;
}
else
{
if (((y-2)>=0) && ((x-2)>=0) && (board[y-2][x-2]=='*')){
count++;
}
if (((y-2)>=0) && ((x-1)>=0) && (board[y-2][x-1]=='*')){
count++;
}
if (((y-2)>=0) && ((x)<size) && (board[y-2][x]=='*')){
count++;
}
if (((y)<size) && ((x-2)>=0) && (board[y][x-2]=='*')){
count++;
}
if (((y)<size) && ((x-1)>=0) && (board[y][x-1]=='*')){
count++;
}
if (((y)<size) && ((x)<size) && (board[y][x]=='*')){
count++;
}
if (((y-1)>=0) && ((x-2)>=0) && (board[y-1][x-2]=='*')){
count++;
}
if (((y-1)>=0) && ((x)<size) && (board[y-1][x]=='*')){
count++;
}
board[y-1][x-1]='0' +count;
return 1;
}
}
int is_valid(const char input[40],int leng)
{
int flag=3;
int i=1;
int index;
for(index=0;index<leng;index++)
{
if((input[index]!='(')&&(input[index]!=',')&&(input[index]!=')'))
{
if (((input[1])<'0')||((input[1])>'9'))
{
return 0;
}
}
}
if (input[0] != '(')
{
flag=0;
}
if (((input[1])<'0')||((input[1])>'9'))
{
flag=0;
}
i++;
while((input[i]<='9')&&(input[i]>='0'))
{
i++; //increasing the address by1
} //it wiil get char '2'
if (input[i] == ',')
{ //'2' differs ','
if (((input[i+1])<'0')||((input[i+1])>'9'))
{
flag=0;
}
i++;
while((input[i]<='9')&&(input[i]>='0'))
{
input++; //increasing the address by1
}
if (input[i]==')')
{
i++;
}
else
flag=0;
}
else
{
flag=0;
}
if (flag ==0)
{
return 0;
}
while (input[i]!= '\0')
{
if (input[i] != '(')
{
flag=0;
}
if (((input[i+1])<'0')||((input[i+1])>'9'))
{
flag=0;
}
i++;
while((input[i]<='9')&&(input[i]>='0'))
{
i++; //increasing the address by1
} //it wiil get char '2'
if (input[i] == ',')
{ //'2' differs ','
if (((input[i+1])<'0')||((input[i+1])>'9'))
{
flag=0;
}
i++;
while((input[i]<='9')&&(input[i]>='0'))
{
i++; //increasing the address by1
}
if (input[i]==')')
{
i++;
}
else
flag=0;
}
else
{
flag=0;
}
if (flag ==0)
{
return 0;
}
}
return 1;
}
int checkEndOfGame(char board[N][N],int size){
int index,kndex;
for (index=0;index<size;index++){
for (kndex=0;kndex<size;kndex++){
if (board[index][kndex]==' '){
return 0;
}
}
}
return 1;
}
void sprint(char board[N][N],int size)
{
int index,kndex;
for(index=0;index<size;index++)
{
printf("+");
for(kndex=0;kndex<size;kndex++)
{
printf("-+");
}
printf("\n");
printf("|");
for(kndex=0;kndex<size;kndex++)
{
if ((board[index][kndex]=='*')||(board[index][kndex]==' '))
{
printf("?|");
}
else
{
printf("%c|",board[index][kndex]);
}
}
printf("\n");
}
printf("+");
for(kndex=0;kndex<size;kndex++)
{
printf("-+");
}
printf("\n");
}
int playGame(char board[N][N],int size){
int x_rem,y_rem,i,ch,kent,wins=0,loses=0,end,tr;
char input2[40];
if (size==-1)
{
printf("you must choose board size first!\n");
}
else
{
do
{
printf("enter a square x,y to uncover : (no input control is needed):\n");
for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch >=0; ++i)
{
input2[i] = ch;
}
input2[i] = '\0';
x_rem=input2[0]-'0';
y_rem=input2[2]-'0';
kent=unCover(board,size,x_rem,y_rem);
if (kent==0){
end=1;
loses++;
printBoard(board,size);
}
else{
tr=checkEndOfGame(board,size);
if (tr==0)
{
sprint(board,size);
}
else
{
printBoard(board,size);
end=1;
printf("all mines were uncovered - you won\n");
wins++;
}
}
}while(end!=1);
}
if (wins==1){
return 1;
}
{
return 0;
}
}