Click here to Skip to main content
15,904,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello coders, I want to refactor the switch statement to a enum class. The result of the code: creates a box and put particles inside the box, then make particles move freely and if two particles collide a new particle will be added inside the box. Help us some hints to do. One more, I knew when I pasted my code in here (I asked my team and they agreed), it will be copied easily by another person. How can I prevent that? Thanks.

What I have tried:

Box class:
import java.util.Random;

public class box{
	private final int WIDTH = 25;
	private final int HEIGHT = 6;
	private final int PARTICLE_MAX = 300;
	private static box box;
	private particle[] particleList = new particle[PARTICLE_MAX];
	private Random r = new Random();
	
	private box(){
		for(int i = 0; i<3;i++){
			addNewParticle();
		}
	}
	
	public int getWidth(){
		return WIDTH;
	}
	
	public int getHeight(){
		return HEIGHT;
	}
	
	public int getParticleMax(){
		return PARTICLE_MAX;
	}
	
	public static box getInstance(){
		if(box == null)
			box = new box();
		return box;
	}
	
	public void makeParticlesMove(){
		int n;
		for(particle particle:particleList){
			if(particle != null){
				n = r.nextInt(8)+1;
				switch(n){
					case 1: if(particle.getY()-1 > 0){
								particle.north();
								
							}
							break;
					case 2: if(particle.getY()-1 > 0 && particle.getX()+1 < WIDTH-1){
								particle.northeast();
								
							}
							break;
					case 3: if(particle.getX()+1 < WIDTH-1){
								particle.east();
								
							}
							break;
					case 4: if(particle.getY()+1 < HEIGHT-1 && particle.getX()+1 < WIDTH-1){
								particle.southeast();
								
							}
							break;
					case 5: if(particle.getY()+1 < HEIGHT-1){
								particle.south();
								
							}
							break;
					case 6: if(particle.getY()+1 < HEIGHT-1 && particle.getX()-1 > 0){
								particle.southwest();
								
							}
							break;
					case 7: if(particle.getX()-1 > 0){
								particle.west();
								
							}
							break;
					case 8: if(particle.getY()-1 > 0 && particle.getX()-1 > 0){
								particle.northwest();
								
							}
							break;
				}
			}
		}
		collisionCheck();
	}
	
	public int particleCount(){
		int p = 0;
		for(particle particle:particleList){
			if(particle !=null){
				p++;
			}
		}
		return p;
	}
	
	public void showParticles(){
		char[][] map = new char[WIDTH][HEIGHT];
		for(particle p:particleList){
			if(p != null){
				int x = p.getX();
				int y = p.getY();
				map[x][y] = '*';
			}
		}
		for(int i = 0; i < WIDTH; i++)
			System.out.print("-");
		System.out.println();
		
		for(int j = 1; j < HEIGHT-1; j++){
			System.out.print("|");
			for(int c = 1; c < WIDTH-1; c++){
				if(map[c][j] == '*')
					System.out.print("*");
				else
					System.out.print(" ");
			}
			System.out.println("|");
		}
		for(int i = 0; i < WIDTH; i++)
			System.out.print("-");
		System.out.println();
	}
	
	public void collisionCheck(){
		boolean[][] map = new boolean[WIDTH][HEIGHT];
		for(int i =0; i<HEIGHT; i++){
			for(int j = 0; j<WIDTH; j++)
				map[j][i] = false;
		}
		for(particle p:particleList){
			if(p != null){
				int x = p.getX();
				int y = p.getY();
				if(map[x][y]) {
					System.out.println("Collided!!!!");
					addNewParticle();
				}
				else
					map[x][y] = true;
			}
		}
	}
	
	public void addNewParticle(){
		for(int i = 0; i < PARTICLE_MAX; i++){
			if(particleList[i] == null){
				particleList[i] = new particle(r.nextInt(WIDTH-2)+1, r.nextInt(HEIGHT-2)+1);
				break;
			}
		}
		
	}
}

particle class:
public class Particle{
	private int x;
	private int y;
		
	public Particle(int x, int y){
		this.x = x;
		this.y = y;
	}
	
	public void setX(int x){
		this.x = x;
	}
	
	public void setY(int y){
		this.y = y;
	}
	
	public int getX(){
		return x;
	}
	
	public int getY(){
		return y;
	}
	
	public void north(){
		y-=1;
	}
	
	public void northeast(){
		y-=1;
		x+=1;
	}
	
	public void east(){
		x+=1;
	}
	
	public void southeast(){
		y+=1;
		x+=1;
	}
	
	public void south(){
		y+=1;
	}
	
	public void southwest(){
		y+=1;
		x-=1;
	}
	
	public void west(){
		x-=1;
	}
	
	public void northwest(){
		y-=1;
		x-=1;
	}
}

test:
public class Main{	
	public static void main(String[] args){
		Box b = Box.getInstance();
		do{
			System.out.println("Particle count: " + b.particleCount());
			b.makeParticlesMove();
			b.showParticles();
			try{
				Thread.sleep(300);
			}catch (Exception e){
				System.out.println(e);
			}
		}while(b.particleCount() < b.getParticleMax());
	}
}
Posted
Updated 21-Oct-18 20:39pm
v2
Comments
Richard MacCutchan 21-Oct-18 12:13pm    
"I have a problem with the enum class in java. Here are three codes of my team but we don't know how to change the switch to the enum class."
What problem, and what do you need to change?

And, yes, anyone can copy that code if they want to, this is an open forum. If you do not want it copied then do not post the entire program, but just the part that has the error.
Member 14025358 21-Oct-18 12:28pm    
I want to refactor switch statement to enum

1 solution

You can define an enum something like:

C++
enum Direction {
   Invalid(-1),
   North(0),
   NorthEast(1),
   South(2),
   ...
   ...
   SouthEast(7);

  private int val;
  private static Map<int, Direction> intToTypeMap;
  private Direction(int v) {
     this.val = v;
     this.addToMap(v);
  }

  public int getValue() {
     return this.val;
  }

  private void addToMap(int v) {
      if (intToTypeMap == null) {
         intToTypeMap = new TreeMap<int, Direction>();
      }
      intToTypeMap.put(v, this);
   }

   public static Direction fromInt(int v) {
      Direction type = intToTypeMap.get(Integer.valueOf(v));
      if (type == null) {
        return Direction.Invalid;
      }
      return type;
   }
}


After above enum, you can change your switch statement something like as below:

. . .
. . .
		for (particle particle:particleList) {
			if(particle != null){
				switch(Direction.fromtInt(r.nextInt(8))){
					case North: if(particle.getY()-1 > 0){
								particle.north();
								
							}
							break;

. . .
. . .
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900