Code:
#include <stdio.h>
#define N 9
int is_valid(const char input[],int leng);
void mainPlayGame(board);
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 input[40];
char input2[40];
char board[N][N];
mainPlayGame(board);
printf("bye, please press enter to exit!\n");
getchar();
return 0;
}
void mainPlayGame(board){
int opt,leng,size=-1;
int index,kndex;
char input[40];
char ch_cords[40];
char input2[40];
int tr;
int cords[8][2];
int tndex;
int i,k,j,ch,l;
int leng2;
do
{
mainMenu();
scanf("%d",&opt);
l=getchar();
if (opt==1){
printf("enter board size [1..9] (no input control is needed):\n");
scanf("%d",&size);
}
if (opt==2){
if (size==-1){
printf("you must choose board size first!\n");
}
else{
printf("enter string\n");
for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch != EOF; ++i)
{
input2[i] = ch;
}
input2[i] = '\0';
leng=i;
printf("%s\n", input2);
k=0;
for(index=0;index<i;index++){
if(input2[index]!=' '){
input[k]=input2[index];
k++;
}
}
if (is_valid(input,leng))
{
printf("%s is valid.\n", input);
}
else
{
printf("%s is invalid.\n", input);
}
//////////////////////////////////////////////input check
leng2=leng/5;
k=0;
for(index=0;index<i;index++){
if((input2[index]!='(')&&(input2[index]!=',')&&(input2[index]!=')')){
ch_cords[k]=input2[index];
k++;
}
}
j=0;
for(index=0;index<leng2;index++){
for(kndex=0;kndex<2;kndex++){
tr=ch_cords[j]-'0';
cords[index][kndex]=tr;
j++;
}
}
}
}//end if opt 2
if (opt==3){
}
if (opt==4){
}
if (opt==5){
}
if (opt==0){
}
}while(opt!=0);
}
int playGame(char board[N][N],int size){
}
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++){
for(kndex=0;kndex<size;kndex++){
printf("+-+");
}
printf("\n");
for(kndex=0;kndex<size;kndex++){
printf("|%c|",board[index][kndex]);
}
printf("\n");
}
for(kndex=0;kndex<size;kndex++){
printf("+-+");
}
}
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';
printBoard(board, size);
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;
printBoard(board, size);
return 1;
}
}
int is_valid(const char input[],int leng)
{
int flag=3;
int i=1;
/* Match "(<number>,<number>)" */
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=(int)NULL;
}
if (((input[1])<'0')||((input[1])>'9')){
flag=(int)NULL;
}
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=(int)NULL;
}
i++;
while((input[i]<='9')&&(input[i]>='0')){
input++; //increasing the address by1
}
if (input[i]==')'){
i++;
}
else
flag=(int)NULL;
}
else{
flag=(int)NULL;
}
//////////////////////////////////////////////////////////////
if (flag ==(int)NULL)
{
return 0;
}
/* Match zero or more " (<number>,<number>)" */
while (input[i]!= '\0')
{
if (input[i] != '(')
{
flag=(int)NULL;
}
if (((input[i+1])<'0')||((input[i+1])>'9')){
flag=(int)NULL;
}
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=(int)NULL;
}
i++;
while((input[i]<='9')&&(input[i]>='0')){
i++; //increasing the address by1
}
if (input[i]==')'){
i++;
}
else
flag=(int)NULL;
}
else{
flag=(int)NULL;
}
if (flag ==(int)NULL)
{
return 0;
}
}
///////////////////////////////////////
return 1;
//////////////////////////////////////
}