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,k,newflag,co1,co2,co3,co4,spot,sum,blanks=0,obcounter,p;
- char yesno[4],*nam,**name,**array,**possible,typ[9],*symbol,sym,set;
- int i,players,j,l;
- int sp, counter=0, player,**pcoord;
- int userflag = 1;
- int direction,match,*wincon;
- int a1=0,exit,target,help;
- char move[6];
- // 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 and <=26");
- }
- }while((dimensions<5)||(dimensions>26));
- }
- //--------------------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);
- //-----------------arxikopoisi enos pinaka nam,ston opoio tha perasoume ola ta onomata----------
- 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);
- //---------arxikopoioume ton pinaka symbol,vazontas '0' se kathe thesi
- //----------arxikopoioume ton pinaka type me 1,theorontas oti arxika einai oloi computers
- for (i=0;i<players;i++)
- {
- symbol[i]='0';
- 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("\nWrong 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;
- }
- }
- //-------------elegxos gia enan toulaxiston paixti--------------
- 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++)
- {
- user=1;
- computer=1;
- printf("\nEnter 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("\nWrong input.Please choose one of your options (user,computer) ");
- }
- }while (flag!=0);
- if(user==0)
- {
- users++;
- type[i]=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 (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(" 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*));
- possible=(char**)malloc((sumi)*sizeof(char*));
- for (i=0;i<sumi;i++)
- {
- array[i]=(char*)malloc((sumj)*sizeof(char));
- possible[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]=' ';
- obnum = ((dimensions - 1)/2);
- //after do{}while (counter < players);
- if (yes == 0){
- do{
- newflag=1;//used to confirm that the spot for the first piece of the obstacle is surrounded by at least one more dot
- do{
- do{
- co1 = (rand() % dimensions) + 2;
- co2 = (rand() % dimensions) + 3;
- }while (array[co1][co2] != '.');
- spot = rand() % 4;
- if((spot == 0) && (array[co1+1][co2] == '.')){
- co3=co1+1;
- co4=co2;
- newflag=0;
- }
- else if((spot == 1) && (array[co1-1][co2] == '.')){
- co3=co1-1;
- co4=co2;
- newflag=0;
- }
- else if((spot == 2) && (array[co1][co2+1] == '.')){
- co3=co1;
- co4=co2+1;
- newflag=0;
- }
- else if((spot == 3) && (array[co1][co2-1] == '.')){
- co3=co1;
- co4=co2-1;
- newflag=0;
- }
- }while(newflag!=0);
- array[co1][co2] = 'X';//there was a random } here. keep this in mind...
- array[co3][co4] = 'X';
- obnum--;
- }while(obnum > 0);
- }
- //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
- sp = (dimensions/2) + 1;
- wincon=(int*)malloc(players*sizeof(int));
- //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
- pcoord=(int**)malloc(players*sizeof(int*));
- for (i=0;i<players;i++)
- {
- pcoord[i]=(int*)malloc(2*sizeof(int));
- }
- for (i=0;i<players;i++)
- {
- wincon[i]=5;
- for (j=0;j<2;j++)
- {
- pcoord[i][j]=(dimensions+7);
- }
- }
- do
- {
- player=rand()%players;
- if(pcoord[player][0] == (dimensions + 7))
- { //this if checks if the player has already had his starting point set
- if((type[player] == 0) && (userflag == 1))
- { //this if checks for the first randomly chosen player
- pcoord[player][0] = sumi-1;
- pcoord[player][1] = sp+1;
- wincon[player] = 1;
- array[sumi-1][sp+1] = symbol[player];
- userflag = 0;
- counter++; //now that a new player has had his starting point set, the counter can increment
- }
- else
- {
- if (players==2)
- {
- pcoord[player][0]=2;
- pcoord[player][1]=sp+1;
- wincon[player]=3;
- array[2][sp+1]=symbol[player];
- counter++;
- }
- if (players==4)
- {
- 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])
- pcoord[player][0] = 2;
- pcoord[player][1] = sp;
- wincon[player] = 3;
- array[2][sp+1] = symbol[player];
- counter++; //now that a new player has had his starting point set, the counter can increment
- }
- else if((direction == 1) && (array[sp][3] == '.'))
- { //1 is for starting position left (array[sp][3])
- pcoord[player][0] = sp;
- pcoord[player][1] = 3;
- wincon[player] = 2;
- array[sp][3] = symbol[player];
- counter++; //now that a new player has had his starting point set, the counter can increment
- }
- else if((direction == 2) && (array[sp][sumj-1] == '.'))
- {
- //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] = 4;
- 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]);
- }
- }
- // for(i=0;i<players;i++)
- // {
- // exit=1;
- // help=1;
- // if (players==2)
- // {
- // if ((wincon[i]==1)||(wincon[i]==3))
- // {
- // j=0;
- // while((name[i][j]!='\0')&&(j<max))
- // {
- // printf("\n%d",wincon[i]);
- // j++;
- // }
- // do
- // {
- // printf(" is playing.Enter the coordinates of the square you want to move to,or the coordinates of the squares you want to place an obstacle.You can also enter 'help' or 'exit')");
- // scanf("%s", move);
- // exit=strcmp(move,"exit");
- // help=strcmp(move,"help");
- // flag=1;
- // if (!(exit==0)||(help==0))
- // {
- // target=strlen(move);
- // match=0;
- // j=0;
- // while((j<target)&&(flag==1))
- // {
- // if (j==0)
- // {
- // for (k = 3; k < sumj; k++)
- // {
- // if (array[0][k]!=move[j])
- // {
- // match++;
- // }
- // else
- // {
- // a1=0;
- // }
- // }
- //
- // }
- // else
- // {
- // //an o proigoumenos einai arithmos
- // if(a1==1)
- // {
- // for (k = 3; k < sumj; k++)
- // {
- // if (array[0][k]!=move[j])
- // {
- // match++;
- // }
- // else
- // {
- // a1=0;
- // }
- // }
- // for (k=2;k<sumi;k++)
- // {
- // if(array[k][1]!=move[j])
- // {
- // match++;
- // }
- // else
- // {
- // a1=1;
- // }
- // }
- // }
- // else
- // {
- // for(k=2;k<sumi;k++)
- // {
- // if(array[k][0]!=move[j])
- // {
- // match++;
- // }
- // else
- // {
- // a1=1;
- // }
- // }
- // }
- // }
- // j++;
- // }
- //
- // }
- // if (match!=0)
- // {
- // printf("\n Wrong input.");
- // }
- // }while (match!=0);
- // }
- // }
- // if (players==4)
- // {
- // if (wincon[i]==i+1)
- // {
- // j=0;
- // while((name[i][j]!='\0')&&(j<max))
- // {
- // printf("\n%d",wincon[i]);
- // j++;
- // }
- // do
- // {
- // printf(" is playing.Enter the coordinates of the square you want to move to,or the coordinates of the squares you want to place an obstacle.You can also enter 'help' or 'exit')");
- // scanf("%s", move);
- // exit=strcmp(move,"exit");
- // help=strcmp(move,"help");
- //// flag=1;
- // if (!(exit==0)||(help==0))
- // {
- // target=strlen(move);
- // match=0;
- // j=0;
- // while((j<target)&&(flag==1))
- // {
- // if (j==0)
- // {
- // for (k = 3; k < sumj; k++)
- // {
- // if (array[0][k]!=move[j])
- // {
- // match++;
- // }
- // else
- // {
- // a1=0;
- // }
- // }
- //
- // }
- // else
- // {
- // //an o proigoumenos einai arithmos
- // if(a1==1)
- // {
- // for (k = 3; k < sumj; k++)
- // {
- // if (array[0][k]!=move[j])
- // {
- // match++;
- // }
- // else
- // {
- // a1=0;
- // }
- // }
- // for (k=2;k<sumi;k++)
- // {
- // if(array[k][1]!=move[j])
- // {
- // match++;
- // }
- // else
- // {
- // a1=1;
- // }
- // }
- // }
- // else
- // {
- // for(k=2;k<sumi;k++)
- // {
- // if(possible[k][0]!=move[j])
- // {
- // match++;
- // }
- // else
- // {
- // a1=1;
- // }
- // }
- // }
- // }
- // j++;
- // }
- //
- // }
- // if (match!=0)
- // {
- // printf("\n Wrong input.");
- // }
- // }while (match!=0);
- // }
- // }
- // }
- blanks=0;
- for (j=2;i<sumj;j++)
- {
- for (l=0;l<sumi;l++)
- {
- possible[l][j]=array[l][j];
- }
- i=3;
- if (possible[i][j]=='.')
- {
- k=j;
- sum=0;
- blanks++;
- do
- {
- if (possible[i][k+1]=='.')
- {
- sum++;
- obcounter=0;
- if (possible[i][k+1]!='.')
- {
- obcounter++;
- }
- if (possible[i+1][k]!='.')
- {
- obcounter++;
- }
- if (possible[i-1][k]!='.')
- {
- obcounter++;
- }
- if (obcounter==3)
- {
- possible[i][k+1]='X';
- sum--;
- }
- else
- {
- set=possible[i][k+1];
- possible[i][k+1]=possible[i][k];
- possible[i][k]=set;
- k=k+1;
- }
- }
- else
- {
- if (possible[i+1][k]=='.')
- {
- sum++;
- obcounter=0;
- if (possible[i+1][k+1]!='.')
- {
- obcounter++;
- }
- if (possible[i+2][k+1]!='.')
- {
- obcounter++;
- }
- if (possible[i+1][k-1]!='.')
- {
- obcounter++;
- }
- if (obcounter==3)
- {
- sum--;
- possible[i+1][k]='X';
- }
- else
- {
- set=possible[i+1][k];
- possible[i+1][k]=possible[i][k];
- possible[i][k]=set;
- i=i+1;
- }
- }
- else
- {
- if (possible[i-1][k]=='.')
- {
- sum++;
- obcounter=0;
- if (possible[i-1][k-1]!='.')
- {
- obcounter++;
- }
- if (possible[i-2][k-1]!='.')
- {
- obcounter++;
- }
- if (possible[i-1][k+1]!='.')
- {
- obcounter++;
- }
- if (obcounter==3)
- {
- sum--;
- possible[i-1][k]='X';
- }
- else
- {
- set=possible[i-1][k];
- possible[i-1][k]=possible[i][k];
- possible[i][k]=set;
- i=i-1;
- }
- }
- else
- {
- if (possible[i][k-1]=='.')
- {
- obcounter=0;
- sum++;
- if (possible[i][k-2] !='.')
- {
- obcounter++;
- }
- if (possible[i+1][k-1]!='.')
- {
- obcounter++;
- }
- if (possible[i-1][k-1] !='.')
- {
- obcounter++;
- }
- if (obcounter==3)
- {
- sum--;
- possible[i][k-1]='X';
- }
- else
- {
- set=possible[i][k-1];
- possible[i][k-1]=possible[i][k];
- possible[i][k]=set;
- k=k-1;
- }
- }
- }
- }
- }
- }
- while(i!=sumi-1);
- printf("\n%d",sum);
- }
- }
- for (i=0;i<players;i++)
- {
- free (name[i]);
- free(pcoord[i]);
- }
- for (i = 0; i < sumi; i++)
- {
- free (array[i]);
- }
- free(nam);
- free(type);
- free(symbol);
- free(wincon);
- free(array);
- free(name);
- free(pcoord);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement