Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*int k;
- //we already know turn
- k=1;
- do{
- if((wincon[fastest]==k) && (k!=wincon[turn])){
- }
- k++;
- }while(k!=4;)
- */
- //your way ^, my way v
- int bloc=0;
- int uflag=dflag=rflag=lflag=aflag1=aflag2=1;//flags used to see if a direction has already been checked for an obstacle. aflag stops us from checking further directions
- newflag=1;
- do{
- co1=pcoord[fastest][0];
- co2=pcoord[fastest][1];
- if(fastest == 0){
- if(array[pcoord[fastest][0]-1][pcoord[fastest][1]]=='.'){
- co1=pcoord[fastest][0]-1;
- aflag1=0;
- }
- else{
- bloc++;
- }
- spot = rand() % 4;
- do{
- else if((spot==1) && (rflag!=0)){
- if(array[co1][co2+1]=='.'){
- rflag=0;
- if(aflag1==0){//this if ensures that an open space for movement has already been found before setting a place to block
- aflag12=0;
- }
- else{
- aflag1=0;
- co2=co2+1;//a coordinate is set for an obstacle if there is no previously found spot for it. having placed one direction outside the loop gives that direction priority
- }
- }
- else{
- rflag=0;
- bloc++;
- }
- }
- else if((spot==2) && (dflag!=0)){
- if(array[co+1][co2]=='.'){
- dflag=0;
- if(aflag1==0){
- co1=co1+1;
- aflag12=0;
- }
- else{
- aflag1=0;
- }
- }
- else{
- dflag=0;
- bloc++;
- }
- }
- else if((spot==3) && (lflag!=0)){
- if(array[co1][co2-1]=='.'){
- lflag=0;
- if(aflag1==0){
- co2=co2-1;
- aflag12=0;
- }
- else{
- aflag1=0;
- }
- }
- else{
- lflag=0;
- bloc++;
- }
- }//with this we have checked all directions for an open space. If bloc becomes 3, we only have one open space, and thus cannot place further obstacles around the fastest
- //if, however, we can block him, co1 and co2 have become the coordinates of a free adjacent space. with this, we can look around that space for the next part of the obstacle
- //if we find that as well, we will set both locations on array (array[co1][co2] and array[co3][co4]) to become 'X'
- spot = rand() % 4;
- }while(!((aflag1==0) && (aflag2==0)) || (bloc==3));
- }
- }
- else if(turn == 1){
- if(array[pcoord[fastest][0]-1][pcoord[fastest][1]]=='.'){
- co2=pcoord[fastest][1]+1;
- }
- else{
- bloc++;
- spot = rand() % 4;
- do{
- }while((aflag!=0) && (bloc!=4));
- }
- }
- else if(turn == 2){
- if(array[pcoord[fastest][0]-1][pcoord[fastest][1]]=='.'){
- co1=pcoord[fastest][0]+1;
- }
- else{
- bloc++;
- spot = rand() % 4;
- do{
- }while((aflag!=0) && (bloc!=4));
- }
- }
- else if(turn == 3){
- if(array[pcoord[fastest][0]-1][pcoord[fastest][1]]=='.'){
- co2=pcoord[fastest][1]-1;
- }
- else{
- bloc++;
- spot = rand() % 4;
- do{
- }while((aflag!=0) && (bloc!=4));
- }
- }
- //once co1 and co2 have been found and an obstacle can be placed, we need co3 and co4 for the second piece
- //if an obstacle can be placed, we start by setting co3 and co4 to co1 and co2 respectively. if not, we set them to the coordinates of the fastest
- if((aflag1==0) && (aflag2==0)){
- co3=co1;
- co4=co2;
- spot = rand() % 4;
- if((spot == 0) && (array[co1+1][co2] == '.')){
- co3=co1+1;
- newflag=0;
- }
- else if((spot == 1) && (array[co1-1][co2] == '.')){
- co3=co1-1;
- newflag=0;
- }
- else if((spot == 2) && (array[co1][co2+1] == '.')){
- co4=co2+1;
- newflag=0;
- }
- else if((spot == 3) && (array[co1][co2-1] == '.')){
- co4=co2-1;
- newflag=0;
- }
- }
- //now we check their values. if no obstacle can be placed, we set newflag to 0 and call proceed instead
- if((co3!=co1) || (co4!=co2)){
- array[co1][co2]='X';
- array[co3][co4]='X';
- }
- else{
- aflag2=2;//outside the obstacles side of the code, check for this flag. if it is 2, call proceed instead
- }
- }while (newflag!=0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement