Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Help debugging a program that runs a game of Nim

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help debugging a program that runs a game of Nim

    G'day, I've spent most of today trying to figure out why this doesn't run as it should - when I hit start game the program runs through but doesn't display any squares - I have a problem with the gameOver method also.

    Your welcome to have a look - the only edited classes are grid, symbol, square and location.

    Any help getting this to run correctly would be most welcome - I cannot see my logical error(s)

    Cheers see attached

    Provisions.zip


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help debugging a program that runs a game of Nim

    Please post the code you are having problems with on the forum. No links.
    Also post the contents of the console from when you execute the program.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help debugging a program that runs a game of Nim

    /**
    *	Grid ADT
    *	@author J.R.Dermoudy and <<INSERT YOUR NAME HERE>>
    *	@version December 2005
     
    	This file holds the Grid ADT which represents
    	the Nim board.  The Grid consists of an	array
    	of the (linear) squares in the board.
     
    	YOU NEED TO MAKE CHANGES TO THIS FILE!
    */
     
    import java.awt.*;
     
     
    public class Grid implements GridInterface
    {
    	//finals
    	private final int DIMENSION=21;						// number of squares in grid
    	private final int MAXIMUM_ROW=6;					// number of rows in grid
    	private final boolean TRACING=true;				// do we want to see trace output?
     
    	// properties
    	private int dimension;			// the number of rows in the grid
    	private Square board[];			// all the Squares within the grid
     
     
    	public Grid()
    	/*
    		Constructor method.
    		Pre-condition: none
    		Post-condition: a 21 element grid is created in
    						which all squares are occupied
    		Informally: create a full 6 row triangular grid
    	*/
    	{
    		int i,r,c;
    		Location l;
     
    		trace("Grid: constructor begins");
     
    		dimension=MAXIMUM_ROW;
    		board=new Square[DIMENSION];
     
    		trace("Grid: building squares");
    		i=0;
    		for (r=0; r<MAXIMUM_ROW; r++)
    		{
    			for (c=0; c<=r; c++)
    			{
    				l=new Location(r+1,c+1);
    				board[i]=new Square(l);
    				i++;
    			}
    		}
     
    		trace("Grid: constructor ends");
    	}
     
    	private int findIndex(Location l)
    	/*
    		Locate index in grid which corresponds to square at
    		given location
    		Pre-condition: location is within the current grid
    		Post-condition: the index of the element with the given
    						location is returned
    		Informally: identify which array element is at the given
    					location
    	*/
    	{
    		int i,r,c;
     
    		trace("findIndex: findIndex starts");
     
    		i=0;
    		for (r=1;r<l.getRow();r++)
    		{
    			for (c=1;c<=r;c++)
    			{
    				i++;
    			}
    		}
     
    		i+=(l.getColumn()-1);
     
    		trace("findIndex: findIndex ends");
    		return i;
    	}
     
     
    	public void setSquare(Location l, Square s) throws IllegalGridException
    	/*
    		Set method for an element of the "board" instance variable.
    		Pre-condition: the given Square object and the board array
    					   are defined
    		Post-condition: the given square is assigned to an element
    						of the Grid object selected according to
    						the given location within the grid
    		Informally: insert the given square into the grid at the
    					appropriate location, an exception is thrown if
    					the location is not within the grid
    	*/
    	{
    		int i;
     
          	trace("setSquare: setSquare starts");
     
    		//COMPLETE ME!!!
    		assert(s!=null);
     
    		if(!validMove(l))
    		{
    			throw new IllegalGridException();
    		}
    		i=findIndex(l);
    		board[i]=s;
          	trace("setSquare: setSquare ends");
    	}
     
     
    	public Square getSquare(Location l) throws IllegalGridException
    	/*
    		Get method for an element of the "board" instance variable.
    		Pre-condition: the board array is defined
    		Post-condition: the Square object at the appropriate
    						element of the "board" selected according
    						to the given Location value is returned
    		Informally: return the square of the grid at the given
    					location, an exception is thrown if the
    					location is not within the grid
    	*/
    	{
    		int i;
     
          	trace("getSquare: getSquare starts");
     
    		//COMPLETE ME!!!
    		assert(board!=null);
     
    		if (!validMove(l))
    		{
    			throw new IllegalGridException();
    		}
    		i=findIndex(l);
    		return board[i];
    	}
     
     
    	public void setDimension(int d)
    	/*
    		Set method for the "dimension" instance variable.
    		Pre-condition: the given Dimension value is defined and
    					   valid
    		Post-condition: the instance variable "dimension" is
    						assigned the given Dimension value
    		Informally: assign the given dimension to the Grid object
    	*/
    	{
          	trace("setDimension: setDimension starts");
     
    		//COMPLETE ME!!!
    		dimension=d; //assigns d to the dimension int variable
     
          	trace("setDimension: setDimension ends");
    	}
     
     
    	public int getDimension()
    	/*
    		Get method for "dimension" instance variable.
    		Pre-condition: none
    		Post-condition: the value of the Grid object's dimension
    						field is returned
    		Informally: return the current grid's dimension
    	*/
    	{
          	trace("getDimension: getDimension starts and ends");
     
    		//COMPLETE ME!!!
    		return dimension; //returns the int dimension of the grid
    	}
     
     
    	public void emptySquare(Location l)
    	/*
    		Pre-condition: the given Location value is within
    					   the bounds of the current Grid
    					   object
    		Post-condition: the square at the position in the
    						grid indicated by the given Location
    						value is altered to be empty
    		Informally: update the square at the nominated location
    					of the grid with the empty symbol
    	*/
    	{
    		Square q;
    		int i;
     
          	trace("emptySquare: emptySquare starts");
     
    		//COMPLETE ME!!!
     
    		assert(!validMove(l));
     
    		i=findIndex(l);
    		q=new Square(l);
    		board[i]=q;
     
          	trace("emptySquare: emptySquare ends");
    	}
     
     
    	public boolean isEmpty(Location l)
    	/*
    		Pre-condition: the given Location value is within
    					   the bounds of the current grid
    		Post-condition: a Boolean value is returned which
    						represents whether the symbol of
    						the square of the current Grid
    						object with the given Location
    						value is empty
    		Informally: return whether or not the square at
    					the given location is empty
    	*/
    	{
          	trace("isEmpty: isEmpty starts and ends");
    		//COMPLETE ME!!!
     
    		int i;
    		assert(!validMove(l));
     
    		i=findIndex(l);
    		return (board[i].isEmpty());
     
    	}
     
     
    	public boolean validMove(Location l)
    	/*
    		Pre-condition: none
    		Post-condition: true is returned if the given
    						Location is within the bounds of
    						the current Grid object, false is
    						returned if it is not
    		Informally: return whether or not the given
    					location lies within the current grid
    	*/
    	{
    		//int r;
    		//int c;
     
          	trace("validMove: validMove starts and ends");
     
    		//COMPLETE ME!!!
    		boolean validRow=false;
    		boolean validColumn=false;
    		if (l.getRow()>0 && l.getRow()<=dimension)
    		{
    			validRow=true;
    		}
    		if (l.getColumn()>0 && l.getColumn()<=dimension)
    		{
    			validColumn=true;
    		}
    		return (validRow && validColumn);
    	}
     
     
    	public boolean gameOver()
    	/*
    		Pre-condition: none
    		Post-condition: true is returned if the game is
    						over because the grid is empty
    		Informally: return whether or not the game is over
    	*/
     
    	{
    		int r,c;
    		Location l;
     
          	trace("gameOver: gameOver starts");
     
    		//COMPLETE ME!!!
    		for (r=1; r<=getDimension(); r++)
      		{
        		for (c=1; c<=getDimension(); c++)
        		{
        			l=new Location(r,c);
          			if (isEmpty(l))
            			return true;
    			}
    		}
     
      		return false;
     
    //      	trace("gameOver: gameOver ends");
        }
     
     
    	public String toString()
    	/*
    		Pre-condition: none
    		Post-condition: a String representation of the grid
    						is returned
    		Informally: find a String representation of the grid
    	*/
    	{
    		String s="[";
    		int r,c;
    		Location l;
     
     
          	trace("toString: toString starts");
     
    		for (r=1;r<=dimension;r++)
    		{
    			for (c=1;c<=r;c++)
    			{
    				l=new Location(r,c);
    				if (isEmpty(l))
    				{
    					s+=" ";
    				}
    				else
    				{
    					s+="X";
    				}
    			}
    			if (r != dimension)
    			{
    				s+=",";
    			}
    		}
     
    		s+="]";
     
          	trace("toString: toString ends");
    		return s;
    	}
     
     
    	public void showGrid(Display s)
    	/*
    		Pre-condition: the Screen parameter is correctly defined
    		Post-condition: the screen representation of the Grid
    						object is displayed on the given Screen
    		Informally: display the current grid
    	*/
    	{
    		int i;
     
          	trace("showGrid: showGrid starts");
     
    		for (i=0; i<DIMENSION; i++)
    		{
    			board[i].showSquare(s,getDimension());
    		}
     
    		trace("showGrid: grid is " + toString());
     
          	trace("showGrid: showGrid ends");
     	}
     
    	public void trace(String s)
    	/*
    		Provide trace output.
    		Pre-condition: none
    		Post-condition: if trace output is desired then the given String
    						parameter is shown on the console
    		Informally: show the given message for tracing purposes
    	*/
    	{
    		if (TRACING)
    		{
    			System.out.println("Grid: " + s);
    		}
    	}
    }

    --------------------Configuration: Assignment2 - JDK version 1.7.0_10 <Default> - <Default>--------------------
    Nim: Nim: constructor begins
    Display: Display: constructor begins
    Display: setGraphics: setGraphics starts
    Display: setGraphics: setGraphics ends
    Display: setObserver: setObserver starts
    Display: setObserver: setObserver ends
    Display: Display: constructor ends
    Nim: Nim: initialising frame
    Nim: paint: paint starts
    Display: setGraphics: setGraphics starts
    Display: setGraphics: setGraphics ends
    Nim: paint: not yet playing a game
    Nim: paint: paint ends
    Nim: Nim: adding GUI widgets
    Nim: Nim: initialising game controls
    Nim: Nim: display it all and wait!
    Display: setGraphics: setGraphics starts
    Display: setGraphics: setGraphics ends
    Display: setObserver: setObserver starts
    Display: setObserver: setObserver ends
    Nim: Nim: contructor ends
    Nim: paint: paint starts
    Display: setGraphics: setGraphics starts
    Display: setGraphics: setGraphics ends
    Nim: paint: not yet playing a game
    Nim: paint: paint ends
    Nim: paint: paint starts
    Display: setGraphics: setGraphics starts
    Display: setGraphics: setGraphics ends
    Nim: paint: not yet playing a game
    Nim: paint: paint ends
    Nim: paint: paint starts
    Display: setGraphics: setGraphics starts
    Display: setGraphics: setGraphics ends
    Nim: paint: not yet playing a game
    Nim: paint: paint ends

    My problem is I get no squares being displayed and the buttons are somewhat unresponsive

    Thanks any help would be great

    --- Update ---

    /**
    *	Grid ADT
    *	@author J.R.Dermoudy and <<INSERT YOUR NAME HERE>>
    *	@version December 2005
     
    	This file holds the Grid ADT which represents
    	the Nim board.  The Grid consists of an	array
    	of the (linear) squares in the board.
     
    	YOU NEED TO MAKE CHANGES TO THIS FILE!
    */
     
    import java.awt.*;
     
     
    public class Grid implements GridInterface
    {
    	//finals
    	private final int DIMENSION=21;						// number of squares in grid
    	private final int MAXIMUM_ROW=6;					// number of rows in grid
    	private final boolean TRACING=true;				// do we want to see trace output?
     
    	// properties
    	private int dimension;			// the number of rows in the grid
    	private Square board[];			// all the Squares within the grid
     
     
    	public Grid()
    	/*
    		Constructor method.
    		Pre-condition: none
    		Post-condition: a 21 element grid is created in
    						which all squares are occupied
    		Informally: create a full 6 row triangular grid
    	*/
    	{
    		int i,r,c;
    		Location l;
     
    		trace("Grid: constructor begins");
     
    		dimension=MAXIMUM_ROW;
    		board=new Square[DIMENSION];
     
    		trace("Grid: building squares");
    		i=0;
    		for (r=0; r<MAXIMUM_ROW; r++)
    		{
    			for (c=0; c<=r; c++)
    			{
    				l=new Location(r+1,c+1);
    				board[i]=new Square(l);
    				i++;
    			}
    		}
     
    		trace("Grid: constructor ends");
    	}
     
    	private int findIndex(Location l)
    	/*
    		Locate index in grid which corresponds to square at
    		given location
    		Pre-condition: location is within the current grid
    		Post-condition: the index of the element with the given
    						location is returned
    		Informally: identify which array element is at the given
    					location
    	*/
    	{
    		int i,r,c;
     
    		trace("findIndex: findIndex starts");
     
    		i=0;
    		for (r=1;r<l.getRow();r++)
    		{
    			for (c=1;c<=r;c++)
    			{
    				i++;
    			}
    		}
     
    		i+=(l.getColumn()-1);
     
    		trace("findIndex: findIndex ends");
    		return i;
    	}
     
     
    	public void setSquare(Location l, Square s) throws IllegalGridException
    	/*
    		Set method for an element of the "board" instance variable.
    		Pre-condition: the given Square object and the board array
    					   are defined
    		Post-condition: the given square is assigned to an element
    						of the Grid object selected according to
    						the given location within the grid
    		Informally: insert the given square into the grid at the
    					appropriate location, an exception is thrown if
    					the location is not within the grid
    	*/
    	{
    		int i;
     
          	trace("setSquare: setSquare starts");
     
    		//COMPLETE ME!!!
    		assert(s!=null);
     
    		if(!validMove(l))
    		{
    			throw new IllegalGridException();
    		}
    		i=findIndex(l);
    		board[i]=s;
          	trace("setSquare: setSquare ends");
    	}
     
     
    	public Square getSquare(Location l) throws IllegalGridException
    	/*
    		Get method for an element of the "board" instance variable.
    		Pre-condition: the board array is defined
    		Post-condition: the Square object at the appropriate
    						element of the "board" selected according
    						to the given Location value is returned
    		Informally: return the square of the grid at the given
    					location, an exception is thrown if the
    					location is not within the grid
    	*/
    	{
    		int i;
     
          	trace("getSquare: getSquare starts");
     
    		//COMPLETE ME!!!
    		assert(board!=null);
     
    		if (!validMove(l))
    		{
    			throw new IllegalGridException();
    		}
    		i=findIndex(l);
    		return board[i];
    	}
     
     
    	public void setDimension(int d)
    	/*
    		Set method for the "dimension" instance variable.
    		Pre-condition: the given Dimension value is defined and
    					   valid
    		Post-condition: the instance variable "dimension" is
    						assigned the given Dimension value
    		Informally: assign the given dimension to the Grid object
    	*/
    	{
          	trace("setDimension: setDimension starts");
     
    		//COMPLETE ME!!!
    		dimension=d; //assigns d to the dimension int variable
     
          	trace("setDimension: setDimension ends");
    	}
     
     
    	public int getDimension()
    	/*
    		Get method for "dimension" instance variable.
    		Pre-condition: none
    		Post-condition: the value of the Grid object's dimension
    						field is returned
    		Informally: return the current grid's dimension
    	*/
    	{
          	trace("getDimension: getDimension starts and ends");
     
    		//COMPLETE ME!!!
    		return dimension; //returns the int dimension of the grid
    	}
     
     
    	public void emptySquare(Location l)
    	/*
    		Pre-condition: the given Location value is within
    					   the bounds of the current Grid
    					   object
    		Post-condition: the square at the position in the
    						grid indicated by the given Location
    						value is altered to be empty
    		Informally: update the square at the nominated location
    					of the grid with the empty symbol
    	*/
    	{
    		Square q;
    		int i;
     
          	trace("emptySquare: emptySquare starts");
     
    		//COMPLETE ME!!!
     
    		assert(!validMove(l));
     
    		i=findIndex(l);
    		q=new Square(l);
    		board[i]=q;
     
          	trace("emptySquare: emptySquare ends");
    	}
     
     
    	public boolean isEmpty(Location l)
    	/*
    		Pre-condition: the given Location value is within
    					   the bounds of the current grid
    		Post-condition: a Boolean value is returned which
    						represents whether the symbol of
    						the square of the current Grid
    						object with the given Location
    						value is empty
    		Informally: return whether or not the square at
    					the given location is empty
    	*/
    	{
          	trace("isEmpty: isEmpty starts and ends");
    		//COMPLETE ME!!!
     
    		int i;
    		assert(!validMove(l));
     
    		i=findIndex(l);
    		return (board[i].isEmpty());
     
    	}
     
     
    	public boolean validMove(Location l)
    	/*
    		Pre-condition: none
    		Post-condition: true is returned if the given
    						Location is within the bounds of
    						the current Grid object, false is
    						returned if it is not
    		Informally: return whether or not the given
    					location lies within the current grid
    	*/
    	{
    		//int r;
    		//int c;
     
          	trace("validMove: validMove starts and ends");
     
    		//COMPLETE ME!!!
    		boolean validRow=false;
    		boolean validColumn=false;
    		if (l.getRow()>0 && l.getRow()<=dimension)
    		{
    			validRow=true;
    		}
    		if (l.getColumn()>0 && l.getColumn()<=dimension)
    		{
    			validColumn=true;
    		}
    		return (validRow && validColumn);
    	}
     
     
    	public boolean gameOver()
    	/*
    		Pre-condition: none
    		Post-condition: true is returned if the game is
    						over because the grid is empty
    		Informally: return whether or not the game is over
    	*/
     
    	{
    		int r,c;
    		Location l;
     
          	trace("gameOver: gameOver starts");
     
    		//COMPLETE ME!!!
    		for (r=1; r<=getDimension(); r++)
      		{
        		for (c=1; c<=getDimension(); c++)
        		{
        			l=new Location(r,c);
          			if (isEmpty(l))
            			return true;
    			}
    		}
     
      		return false;
     
    //      	trace("gameOver: gameOver ends");
        }
     
     
    	public String toString()
    	/*
    		Pre-condition: none
    		Post-condition: a String representation of the grid
    						is returned
    		Informally: find a String representation of the grid
    	*/
    	{
    		String s="[";
    		int r,c;
    		Location l;
     
     
          	trace("toString: toString starts");
     
    		for (r=1;r<=dimension;r++)
    		{
    			for (c=1;c<=r;c++)
    			{
    				l=new Location(r,c);
    				if (isEmpty(l))
    				{
    					s+=" ";
    				}
    				else
    				{
    					s+="X";
    				}
    			}
    			if (r != dimension)
    			{
    				s+=",";
    			}
    		}
     
    		s+="]";
     
          	trace("toString: toString ends");
    		return s;
    	}
     
     
    	public void showGrid(Display s)
    	/*
    		Pre-condition: the Screen parameter is correctly defined
    		Post-condition: the screen representation of the Grid
    						object is displayed on the given Screen
    		Informally: display the current grid
    	*/
    	{
    		int i;
     
          	trace("showGrid: showGrid starts");
     
    		for (i=0; i<DIMENSION; i++)
    		{
    			board[i].showSquare(s,getDimension());
    		}
     
    		trace("showGrid: grid is " + toString());
     
          	trace("showGrid: showGrid ends");
     	}
     
    	public void trace(String s)
    	/*
    		Provide trace output.
    		Pre-condition: none
    		Post-condition: if trace output is desired then the given String
    						parameter is shown on the console
    		Informally: show the given message for tracing purposes
    	*/
    	{
    		if (TRACING)
    		{
    			System.out.println("Grid: " + s);
    		}
    	}
    }

    --------------------Configuration: Assignment2 - JDK version 1.7.0_10 <Default> - <Default>--------------------
    Nim: Nim: constructor begins
    Display: Display: constructor begins
    Display: setGraphics: setGraphics starts
    Display: setGraphics: setGraphics ends
    Display: setObserver: setObserver starts
    Display: setObserver: setObserver ends
    Display: Display: constructor ends
    Nim: Nim: initialising frame
    Nim: paint: paint starts
    Display: setGraphics: setGraphics starts
    Display: setGraphics: setGraphics ends
    Nim: paint: not yet playing a game
    Nim: paint: paint ends
    Nim: Nim: adding GUI widgets
    Nim: Nim: initialising game controls
    Nim: Nim: display it all and wait!
    Display: setGraphics: setGraphics starts
    Display: setGraphics: setGraphics ends
    Display: setObserver: setObserver starts
    Display: setObserver: setObserver ends
    Nim: Nim: contructor ends
    Nim: paint: paint starts
    Display: setGraphics: setGraphics starts
    Display: setGraphics: setGraphics ends
    Nim: paint: not yet playing a game
    Nim: paint: paint ends
    Nim: paint: paint starts
    Display: setGraphics: setGraphics starts
    Display: setGraphics: setGraphics ends
    Nim: paint: not yet playing a game
    Nim: paint: paint ends
    Nim: paint: paint starts
    Display: setGraphics: setGraphics starts
    Display: setGraphics: setGraphics ends
    Nim: paint: not yet playing a game
    Nim: paint: paint ends

    My problem is I get no squares being displayed and the buttons are somewhat unresponsive

    Thanks any help would be great

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help debugging a program that runs a game of Nim

    The posted code does not compile without errors: like missing class definitions.


    Try debugging the code by adding lots of calls to the println method to show the execution flow and the values of variables as the code executes.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. no error when compiled but when the program runs .. help this !!!
    By izzahmed in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 8th, 2011, 08:55 AM
  2. Java Program that Runs in Background
    By 1bun100 in forum Java Theory & Questions
    Replies: 7
    Last Post: September 15th, 2011, 02:10 PM
  3. Program Runs in Eclipse but Not When Exported
    By Pantheon8 in forum Java IDEs
    Replies: 2
    Last Post: August 17th, 2011, 05:57 PM
  4. Debugging your program
    By helloworld922 in forum Debugging Tutorials
    Replies: 0
    Last Post: November 18th, 2009, 03:14 AM
  5. Debugging your program
    By helloworld922 in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: November 18th, 2009, 03:14 AM