1 Attachment(s)
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
Attachment 1732
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.
Re: Help debugging a program that runs a game of Nim
Code java:
/**
* 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 ---
Code java:
/**
* 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
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.