private variable, x and y cannot change as intended

Asked 2 years ago, Updated 2 years ago, 30 views

When running this program, the x and y in the decide_direction() do not change as intended.Could you tell me?
I would like the value of y to be 2,4,6,8... when I execute this code, but the value is the same as 2,2,2,2,...

#include<iostream>
# include <ctime>
/*
 * bar-down method
 */

classmaze {
  int wall_info[77][77] = {{0}};
  int x, y;
public:
  maze();
  void make_wall();
  intdecide_direction();
  intwall_check();
  void show();
};

maze::maze()
{
  // make wall
  for(y=0;y<=76;y++){
    for(x=0;x<=76;x++){
      if(y==0||x==0||y==76||x==76){
        wall_info[y][x] = 1;
      }if(y%2==0&x%2==0){
        wall_info[y][x] = 1;
      }
    }
  }
  y = 2;
  x = 2;
}

voidmaze::make_wall()
{
  while(y<76){
    while(x<76){
      if(decide_direction()==4){
        wall_info[y-1][x] = 1;
      }
      else if(decide_direction()==3){
        wall_info[y][x-1] = 1;
      }
      else if(decide_direction()==2){
        wall_info[y+1][x] = 1;
      }
      else{
        wall_info[y][x+1] = 1;
      }
      x + = 2;
    }
    y + = 2;
    // std::cout<<y<<std::endl;
  }
}

intmaze::decide_direction()
{
  int direction = 0;
  int check;

  std::cout<<y<<std::endl;

  if(y<=2){
    check=wall_check();
    if(wall_check()==-1){
      direction=land()%3+1;
    }
    else{
      direction=land()%4+1;
    }
  }
  else{
    if(wall_check()==-1){
      direction=land()%2+1;
    }
    else{
      direction=land()%3+1;
    }
  }

  return direction;
}

int maze::wall_check()
{
  if(wall_info[y][x-1]==1){
    return-1;
  }
  else{
    return 0;
  }
}

voidmaze::show()
{
  for(inti=0;i<=76;i++){
    for(int j=0;j<=76;j++){
      if(wall_info[i][j]==1){
        std::cout<<"■";
      }
      else{
        std::cout<<"□";
      }
    }
    std::cout<<std::endl;
  }
}

int main()
{
  sland((unsigned) time(NULL);
  maze maze;

  maze.make_wall();
  maze.show();

  return 0;
}

c++

2022-09-30 19:10

1 Answers

 void maze::make_wall()
{
  while(y<76){
    while(x<76){
      // omission
      x + = 2;
    }
    y + = 2;
    // std::cout<<y<<std::endl;
  }
}

This method is a double loop, calculated from x first.The expected output is 2,2,2...4,4,...6,6,6... because the value of y remains the same while calculating x in the inner loop.However, I think it will actually be 2,2,....This is because x has not been reinitialized since the inner roof was finished at the first y==2, so the inner loop will not run while x==76 remains since y==4.Reinitializing x just before the inner loop should work.

 void maze::make_wall()
{
  while(y<76){
    x = 2; // Add here
    while(x<76){

By the way, this is the part of the loop that determines the direction.

 if(decide_direction()==4){
    wall_info[y-1][x] = 1;
  }
  else if(decide_direction()==3){
    wall_info[y][x-1] = 1;
  }
  else if(decide_direction()==2){
    wall_info[y+1][x] = 1;
  }
  else{
    wall_info[y][x+1] = 1;
  }

I think the probability that the last else part will run is quite high because I have called decide_direction() many times, but the value returned varies from time to time.For example, if decide_direction() returns 3, then the next else if runs, then the next else if runs, and then the next else if runs, and then the 4 returns, then the last runs.You should use switch here.

switch(decide_direction()){
case4:
    wall_info[y-1][x] = 1;
    break;
case3:
    wall_info[y][x-1] = 1;
    break;
case2:
    wall_info[y+1][x] = 1;
    break;
default:
    wall_info[y][x+1] = 1;
    break;
}


2022-09-30 19:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.