Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //1)lathos me ti malloc,de diavazo sosta onoma kai typo
- //2)elegxos gia athroisma xriston(prepei na einai toylaxiston 1)
- //3)olo to 3
- //4)check gia 26 grammata
- //5)apodesmeusi name kai type
- //6)den afinoyme tipota ellinika
- //7)backslash n stylish changes
- //8)na tsekaro an uparxei xoros se kathe malloc
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- int main(void)
- {
- //----------------arxikes diastaseis tamplo -----------------
- int dimensions=10,yes,no,flag=1,max,length,*type,users=0, user, computer,sumi,sumj;
- char yesno[4],*nam,**name,**array,typ[9];
- int i,players,j;
- //----------------epalitheusi gia to an thelei o xristis na paiksei me tis prokathorismens diastaseis------
- do
- {
- printf("Do you want to play by defaut dimensions(10X10)?(yes/no) ");
- scanf("%s", yesno);
- yes=strcmp(yesno,"yes");
- no=strcmp(yesno,"no");
- if ((yes==0)||(no==0))
- {
- flag=0;
- }
- if (flag==1)
- {
- printf("\nWrong input.Please choose between default dimensions(yes) and custom dimensions(no) ");
- }
- }while (flag!=0);
- //---------------epalitheusi gia to oti oi custom diastaseis tha einai megaliteris i ises tou 5------
- if (no==0)
- {
- do
- {
- printf("\nChoose your own dimensions ");
- scanf("%d", &dimensions);
- if (dimensions<5)
- {
- printf("\nWrong input.Please choose a number >=5 ");
- }
- }while(dimensions<5);
- }
- //--------------------paixtes-------------------------
- //--------------------arithmos paixton-----------------
- do
- {
- printf("\nChoose the number of players (2 or 4) ");
- scanf("%d",&players);
- if (!((players==2)||(players==4)))
- {
- printf("\nWrong input.Please choose between 2 or 4 ");
- }
- }while(!((players==2)||(players==4)));
- //-------------------onoma kai typos paixton-----------
- nam=(char*)malloc(players*101);
- for (i=0;i<players;i++)
- {
- nam[i]='\0';
- }
- printf("\n Enter the name and the type(user or computer) of each player.Keep in mind that there should be at least one user ");
- type=(int*)malloc(players);
- for (i=0;i<players;i++)
- {
- type[i]=1;
- }
- for (i=0;i<players;i++)
- {
- printf("\nEnter the name of player number %d ",i+1);
- scanf("%s",&nam[i*101]);
- printf("\nEnter the type of player number %d ",i+1);
- flag=1;
- do
- {
- scanf("%s", typ);
- user=strcmp(typ,"user");
- computer=strcmp(typ,"computer");
- if ((user==0)||(computer==0))
- {
- flag=0;
- }
- if (flag==1)
- {
- printf("\n Wrong input.Please choose one of your options (user,computer) ");
- }
- }while (flag!=0);
- if (user==0)
- {
- users++;
- type[i]=0;
- }
- }
- if (users==0)
- {
- do
- {
- printf("There must be at least on user.Please re-enter the type of each player,making sure that there is one user! ");
- flag=1;
- for (i=0;i<players;i++)
- {
- printf("\n Enter the type of player number %d ",i+1);
- do
- {
- scanf("%s", typ);
- user=strcmp(typ,"user");
- computer=strcmp(typ,"computer");
- if ((user==0)||(computer==0))
- {
- flag=0;
- }
- else
- {
- printf("\n Wrong input.Please choose one of your options (user,computer) ");
- }
- }while (flag!=0);
- }
- }while(users==0);
- }
- max=0;
- i=0;
- while((nam[i]!='\0')&&(i<101))
- {
- max++;
- i++;
- }
- length=0;
- for (i=101;i<(players*101);i=i+101)
- {
- while(nam[i]!='\0')
- {
- length++;
- i++;
- }
- if (length>max)
- {
- max=length;
- }
- i=i-length;
- length=0;
- }
- max=max+1;
- name=(char**)malloc(players*sizeof(char*));
- for (i=0;i<players;i++)
- {
- name[i]=(char*)malloc(max*sizeof(char));
- }
- for (i=0;i<players;i++)
- {
- for (j=0;j<max;j++)
- {
- name[i][j]=nam[j+(i*101)];
- }
- }
- for (i=0;i<players;i++)
- {
- j=0;
- printf("\nThe player with the name ");
- while((name[i][j]!='\0')&&(j<max))
- {
- printf("%c",name[i][j]);
- j++;
- }
- printf(" is a ");
- if (type[i]==0)
- {
- printf("user");
- }
- if (type[i]==1)
- {
- printf("computer");
- }
- printf("\n");
- }
- flag=1;
- do
- {
- printf("\nDo you want the map to be populated by obstacles?(yes/no) ");
- scanf("%s", yesno);
- yes=strcmp(yesno,"yes");
- no=strcmp(yesno,"no");
- if ((yes==0)||(no==0))
- {
- flag=0;
- }
- if (flag==1)
- {
- printf("\nWrong input.Choose between (yes) and (no) ");
- }
- }while (flag!=0);
- sumi=dimensions+2;
- sumj=dimensions+3;
- array=(char**)malloc((sumi)*sizeof(char*));
- for (i=0;i<sumi;i++)
- {
- array[i]=(char*)malloc((sumj)*sizeof(char));
- }
- for (i = 0; i < sumi; i++)
- {
- if ((i > 1) && (i < 11))
- {
- array[i][0] = i - 1 + '0';
- array[i][1] = ' ';
- array[i][2] = '|';
- for (j = 3; j < sumj; j++)
- {
- array[i][j] = '.';
- }
- }
- else
- {
- array[i][0] = ((i -1)/10) + '0';
- array[i][1] = ((i -1)%10) + '0';
- array[i][2] = '|';
- for (j = 3; j < sumj; j++)
- {
- array[i][j] = '.';
- }
- }
- if (i == 0)
- {
- array[i][2] = '|';
- for (j = 3; j < sumj; j++)
- {
- array[i][j] = j - 3 + 'a';
- }
- }
- if (i == 1)
- {
- for (j = 0; j < sumj; j++)
- {
- array[i][j] = '-';
- }
- }
- }
- array[0][0]=NULL;
- array[0][1]=NULL;
- array[0][2]=NULL;
- // for (i = 0; i < dimensions + 2 ; i++)
- // {
- // for (j = 0; j < dimensions + 2; j++)
- // {
- // array[i][j]='0' ;
- // }
- // }
- for (i = 0; i < sumi; i++)
- {
- printf("\n");
- for (j=0;j<sumj;j++)
- {
- printf("%c ",array[i][j]);
- }
- }
- free(name);
- free(nam);
- free(type);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement