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
- //9)na tsekaro gia yes kai no
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <time.h>
- int main(void)
- {
- //----------------arxikes diastaseis tamplo -----------------
- int dimensions=10,yes,no,flag=1,max,length,*type,users=0, user, computer,sumi,sumj,obnum;
- char yesno[4],*nam,**name,**array,typ[9],*symbol,sym;
- int i,players,j;
- int sp, counter=0, player,**pcoord;
- int userflag = 1;
- int direction;
- char *wincon;
- // time_t t;
- //----------------epalitheusi gia to an thelei o xristis na paiksei me tis prokathorismens diastaseis------
- srand(time(NULL));
- 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("\nEnter 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);
- symbol=(char*)malloc(players);
- for (i=0;i<players;i++)
- {
- symbol[i]='0';
- type[i]=1;
- }
- 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++)
- {
- 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);
- printf("\nEnter the symbol of player number %d .You can choose between (#,*,&,@) ",i+1);
- flag=1;
- do
- {
- if (flag==2)
- {
- flag=1;
- }
- scanf("%s",&sym);
- if ((sym=='#')||(sym=='@')||(sym=='&')||(sym=='*'))
- {
- flag=0;
- for (j=0;j<players;j++)
- {
- if (sym==symbol[j])
- {
- flag=2;
- printf("Another player already has this symbol taken.Please choose another one (#,*,&,@) ");
- }
- }
- }
- if (flag==1)
- {
- printf("\nWrong input.Please choose one of your options (#,*,&,@) ");
- }
- if ((flag!=2)&&(flag!=1))
- {
- flag=0;
- symbol[i]=sym;
- }
- }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);
- }
- 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(" and his symbol is %c ",symbol[i]);
- 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]=' ';
- array[0][1]=' ';
- array[0][2]=' ';
- //direction becomes 0 (up), 1 (left) or 2 (right) through the use of rand(). Keep this comment in the code
- //it is later used to check a direction's corresponding starting point for availability to put a player on
- printf("1\n");
- sp = (dimensions/2) + 1;
- wincon=(char*)malloc(players*sizeof(char));
- //malloc an array the size of players called wincon. it will save the characters u, l, r and d
- //which will be the opposite of the player's direction when he is being given a starting point
- //malloc an array named pcoord which will be players x 2, where player coordinates will be saved
- //initialize all of pcoord to be dimensions + 1
- printf("2\n");
- pcoord=(int**)malloc(players*sizeof(int*));
- printf("3\n");
- for (i=0;i<players;i++)
- {
- pcoord[i]=(int*)malloc(2*sizeof(int));
- printf("4\n");
- }
- for (i=0;i<players;i++)
- {
- printf("5\n");
- wincon[i]='!';
- for (j=0;j<2;j++)
- {
- printf("6\n");
- pcoord[i][j]=dimensions+7;
- }
- }
- do
- {
- printf("7\n");
- player=rand()%4;
- printf("7.1\n");
- if(pcoord[player][0] == (dimensions + 7))
- { //this if checks if the player has already had his starting point set
- printf("8\n");
- if((type[player] == 0) && (userflag == 1))
- { //this if checks for the first randomly chosen player
- printf("9\n");
- pcoord[player][0] = sumi-1;
- printf("9.1\n");
- pcoord[player][1] = sp;
- printf("9.2\n");
- wincon[player] = 'u';
- printf("9.3\n");
- array[sumi-1][sp+1] = symbol[player];
- printf("9.4\n");
- userflag = 0;
- }
- else
- {
- if (players==2)
- {
- pcoord[player][0]=2;
- pcoord[player][1]=sp+1;
- wincon[player]='d';
- array[2][sp+1]=symbol[player];
- }
- if (players==4)
- {
- printf("10\n");
- direction = rand()%3;
- //randomly check a direction
- //check for direction's availability. we could potentially use a small array, but i did it the hard way
- //make the check in a loop. might need a new variable
- if ((direction == 0) && (array[2][sp] == '.'))
- { //0 is for starting position up (array[2][sp])
- printf("11\n");
- printf("12\n");
- pcoord[player][0] = 2;
- pcoord[player][1] = sp;
- wincon[player] = 'd';
- array[2][sp+1] = symbol[player];
- }
- else if((direction == 1) && (array[sp][3] == '.'))
- { //1 is for starting position left (array[sp][3])
- printf("14\n");
- printf("15\n");
- pcoord[player][0] = sp;
- pcoord[player][1] = 3;
- wincon[player] = 'r';
- array[sp][3] = symbol[player];
- }
- else if((direction == 2) && (array[sp][sumj-1] == '.'))
- {
- printf("17\n");
- //2 is for starting position right (array[sp][sumj])
- //could have gone for a simple else, but put the check in as a precaution
- pcoord[player][0] = sp;
- pcoord[player][1] = sumj-1;
- wincon[player] = 'l';
- array[sp][sumj-1] = symbol[player];
- }
- }
- }
- counter++; //now that a new player has had his starting point set, the counter can increment
- }
- }while (counter < players);
- 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);
- free(symbol);
- free(pcoord);
- free(wincon);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement