0 Replies - 240 Views - Last Post: 05 June 2014 - 04:25 PM Rate Topic: -----

#1 sse345  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-June 14

Can't change GridWorld Grid during Runtime

Posted 05 June 2014 - 04:25 PM

Hello,
I have a question about updating a grid during runtime in GridWorld. I'm making a game called Flood-It (basically, you click on squares to change their color and attempt to get all of the squares the same color in the grid) and I'm having trouble with changing the grid size. I made my own world class, called CellWorld.
import info.gridworld.actor.*;
import java.util.ArrayList;
import info.gridworld.grid.Location;
import java.awt.Color;
import javax.swing.JOptionPane;

public class CellWorld extends ActorWorld
{
	private static String gridSize;
	private static int length;
	private static int width;
	private int steps;
	private static final int[] maxStepList = {3,10,17,25,32,39};
	private static int maxSteps;
	private static final int[] possibilitiesInt = {2,6,10,14,18,22};
	private static String[] winLoseOptions = {"Yes, same size","Yes, change size","No"};
	private static Object[] possibilities= {"2", "6", "10 (Default)","14","18","22"};
	private boolean replay;
	
	public CellWorld()
	{	
		replay = true;
	}
	
	/*Loads the grid by placing a cell in each location of the grid*/	   
    public void maxStepCalc(){
    	for(int c = 0; c < possibilitiesInt.length; c++)
		{
			if(length == possibilitiesInt[c])
				maxSteps = maxStepList[c];
		}
    	System.out.println("maxcalc");
    	System.out.println(maxSteps);
    }
    
    /*Accessors & mutators, nothing special*/
	public boolean getReplay()
	{
		return replay;
	}
		
	public int getLength()
	{
		return length;
	}
	
	
	public int getWidth()
	{
		return width;
	}
	
	public int getMaxSteps()
	{
		return maxSteps;
	}
	
	public int getSteps()
	{
		return steps;
	}
	/* Changes the color of the flood cells by getting the cell's color at location loc and changing the color, and then calls processCells*/
    public boolean locationclicked(Location loc)
    {
    	Color oldColor = getGrid().get(new Location(0,0)).getColor();
    	Color newColor = getGrid().get(loc).getColor();
    	
    	//Updates steps if the user clicked a new color
    	if(!oldColor.equals(newColor))
    		steps++;
    	
    	//If the player has not lost yet, then the game continues to flood.
    	if (steps<=maxSteps){
    	flood(new Location(0,0), oldColor, newColor);
    	}    	    	
        return true;
    }
    
    /*Precondition: should only be called once when the game first starts (first line in runner's main method)*/
    public void promptDimensions()
    {
    	gridSize = (String)JOptionPane.showInputDialog(
                null,
                "What size grid would you like? (Default is 10). " +
                "\nThe larger the grid, the more difficult the game is.",
                "Flood-It: Grid Select",
                JOptionPane.PLAIN_MESSAGE,
                null,
                possibilities,
                "2");

    	try{length = Integer.parseInt(gridSize);}
    	catch(NumberFormatException e)
    	{
    		length = 10;
    	}
    	
    	try{width = Integer.parseInt(gridSize);}
    	catch(NumberFormatException e)
    	{
    		width = 10;
    	}
    }
    
    /*If the user loses, displays a message asking if user wants to play again. Resets grid if yes, exits program if no.*/
    public void promptLoss()
    {   	
    		setMessage("Yikes! You lost at Flood-It. Better luck next time.");
    		int reply = JOptionPane.showOptionDialog(null, "Oh no, you lost at Flood-It! Do you want to play again?", "Sorry, you lost!",
    				JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
    				null, winLoseOptions, winLoseOptions[0]);
    		if(reply == JOptionPane.YES_OPTION)
    		{
    			steps = 0;
    		}
    		else if(reply == JOptionPane.OK_CANCEL_OPTION)
    		{
    			replay = false;
    		}
    		else
    		{
    			this.promptDimensions();
    			steps = 0;
    		}
    	
    }
    
    /*If the user wins, displays a message asking if user wants to play again. Resets grid if yes, exits program if no.*/
    public void promptWin(){    	
    		this.setMessage("Flood-It: You won!");
    		int reply = JOptionPane.showOptionDialog(null, "You beat Flood-It! Do you want to play again?", "You won!",
    		        JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
    		        null, winLoseOptions, winLoseOptions[0]);
    		if(reply == JOptionPane.YES_OPTION)
    		{
    			steps = 0;
    			this.setMessage("Flood-It: Moves left: "+steps+ "/"+maxSteps);
    		}
    			else if(reply == JOptionPane.OK_CANCEL_OPTION)
    			{
    				replay = false;
    			}
    			
    			else
    			{
    				this.promptDimensions();
    				steps = 0;
    			}    			
    	
    }
    
    /*Returns true if the user lost (steps == maxSteps), false if otherwise*/
    public boolean loseCheck(){
    	if(steps == maxSteps)
    		return true;
    	else
    		return false;
    }
   
    /*Returns true if the person won (changed all cells to one color), false otherwise*/
    public boolean winCheck(){
    	boolean win = false;
    	int dimensions = getGrid().getNumCols()*getGrid().getNumRows();
    	Color mainColor = getGrid().get(new Location(0,0)).getColor();
    	ArrayList<Actor> list = new ArrayList<Actor>();
    	for(int r = 0; r<length; r++){
    		for(int c = 0; c<width; c++){
    			if(mainColor.equals(getGrid().get(new Location(r,c)).getColor()))
    				list.add(getGrid().get(new Location(r,c)));   			
    		}
    	}
    	if(list.size() == dimensions)
    		win = true;
    	
    	return win;
    }
    
    /*Loads the grid by placing a cell in each location of the grid*/
    public void loadGrid(){
    	for(int r = 0; r< length; r++){
    		for(int c = 0; c< width; c++){
    			this.add(new Location(r,c), new Cell());
    		}
    	}
    	System.out.println("GridLoaded");
    }
      
    /* Floods the grid by changing the color of each cell touching a cell that is the same color as the 'base cell' (Location 0,0)*/
    private void flood(Location current, Color oldColor, Color newColor)
    {
    	if(!getGrid().isValid(current))
    		return;
    	
    	if(!getGrid().get(current).getColor().equals(oldColor))
    		return;
    	if(oldColor.equals(newColor))
    		return;
    	
    	getGrid().get(current).setColor(newColor);
    	
    	flood(current.getAdjacentLocation(Location.NORTH), oldColor, newColor);
    	flood(current.getAdjacentLocation(Location.SOUTH), oldColor, newColor);
    	flood(current.getAdjacentLocation(Location.EAST), oldColor, newColor);
    	flood(current.getAdjacentLocation(Location.WEST), oldColor, newColor);
    	
    }
}


and my runner class,
import info.gridworld.grid.*;
import info.gridworld.actor.*;
//import javax.swing.JOptionPane;
public class CellWorldRunner
{
	private static CellWorld world = new CellWorld();
    public static void main(String[] args)
    {
    	world.promptDimensions();
    	
    	//Set the title bar of the window
    	System.setProperty("info.gridworld.gui.frametitle", "Flood-It: Created By Anthony Ruzzo & Akhil Aerra");
    	//suppress the toString method from displaying
    	//System.setProperty("info.gridworld.gui.tooltips", "hide");
    	
    	world.setGrid(new BoundedGrid<Actor>(world.getLength(),world.getWidth()));
    	world.loadGrid();
		world.maxStepCalc();
		world.show();
		
		//Initially sets the title of the grid
		world.setMessage("Flood-It: Moves left: 0" + "/"+world.getMaxSteps());
    	do
    	{
    		if(world.winCheck())
    		{
    			int oldLength = world.getLength();
    			world.promptWin();
    			if(oldLength != world.getLength())
    			{
    				world.setGrid(new BoundedGrid<Actor>(world.getLength(),world.getWidth())); 
    				System.out.println("Changed grid size");
    			}
    			world.loadGrid();
    			world.maxStepCalc();
    			System.out.println("Showed grid");
    		}
    		
    		if(world.loseCheck())
    		{
    			int oldLength = world.getLength();
    			world.promptWin();
    			if(oldLength != world.getLength())
    			{
    				world.setGrid(new BoundedGrid<Actor>(world.getLength(),world.getWidth())); 
    				System.out.println("Changed grid size");
    			}   			
    			world.loadGrid();
    			world.maxStepCalc();
			
    			System.out.println("Showed grid");
    		}
    		
    		world.setMessage("Flood-It: Moves left: "+world.getSteps() + "/"+world.getMaxSteps());
    	}while(world.getReplay());
    	   	
    	System.exit(0);      
    }
}

Now, let me narrow down the issue. It is in the runner class:

world.setGrid(new BoundedGrid<Actor>(world.getLength(),world.getWidth()));

Whenever a user wants to change the grid size after playing the game, this is supposed to set the grid to the new updated size, but it never changes in the actual game, i.e. the user just won a 2x2 game, attempts to change the size to 10x10, but the grid stays 2x2. By debug testing, I can say for certain that everything else works, such as the maxStepCalc and loading the grid. The only issue is the new grid not showing up. If I could have some assistance, that'd be great. Thanks

Is This A Good Question/Topic? 0
  • +

Page 1 of 1