Learning - 2D array and method question.
Hi Im new to Java and enjoying it so far. I have come to my first complex bit of code and am having some trouble understanding how methods work within a class. Below is my code thus far (and detailed description of what each method is to do), it is to based around creating a GoMoku game with a 2D array. The one I am looking at is the method stone, has this been done right? Also should I use a different variable for the method board int row, col? Thanks in advance.
Code:
import java.util.List;
import java.util.LinkedList;
import java.util.Collections;
/**
* Board class is Public
*
* @russo
* @1.0
*/
public class Board {
public final int SIZE = 15; // number of lines in board
public final int BLACK = 1;
public final int NONE = 0;
public final int WHITE = -1;
private int[][] board;
/* Creates a new, empty, SIZE*SIZE board */
public Board () {
//Declare Variables (ArrayRow, ArrayColumn)
int row, col;
//Create board based on SIZE
board = new int[SIZE][SIZE];
//- Initialization (set all to variable NONE 0)
for (row = 0; row < board.length; row++)
for (col = 0; col < board[row].length; col++)
board[row][col] = NONE;
}
/*
* Returns whether (row, col) is a valid board position,
* for 0 <= row, col < SIZE.
*/
public boolean valid (int row, int col, boolean valid) {
//Declare variable - assume true
valid = true;
//Is the value a less than or equal to the board size? Return State
if (row < SIZE && row >= 0 && col < SIZE && col >= 0){
valid = true;
}
else
valid = false;
return valid;
}
/* Returns whether position (row, col) is occupied, for 0 <= row, col < SIZE. */
public boolean occupied (int row, int col, boolean occupied) {
occupied = false;
if(board[row][col] != NONE){
occupied = true;}
else{
occupied = false;
}
return occupied;
}
/* Returns colour of stone at (row, col), for 0 <= row, col < SIZE */
public int stone (int row, int col, int value) {
// Implement this method.
if (occupied(row, col, true)){
value = board[col][row];
}
return value;
}
/* Clears the board */
public void clear () {
// Implement this method.
}
/* Places n random stones of given colour on the board, for n >= 0 */
public void addStones (int n, int colour) {
// Implement this method.
}
/*
* Place 1 stone of given colour at position (row, col),
* for valid (row, col).
*/
public void addStone(int row, int col, int colour) {
// Implement this method.
}
/* Removes stone from position (row, col) for valid (row, col) */
public void removeStone(int row, int col) {
// Implement this method.
}
/* Prints board, with column and row headings */
public void print () {
// Display array
Board ();
for (row = 0; row < board.length; row++)
{
for (col = 0; col < board[row].length; col++)
System.out.print(" " + board[row][col]);
System.out.println();
}
}
/*
* Returns greatest length of any line through (row, col)
*/
public int maxLength (int row, int col) {
// Implement this method.
}
/**
* Reads n >= 1, places n black stones and n white stones on the board,
* then reads a sequence of board positions, and displays the length of
* the longest line of stones of the same colour through that position.
*/
private void doit () {
// Implement this method.
}
/* Creates the app and calls doit() */
public static void main (String[] args) {
// Implement this method.
}
/**
* Returns list of 5 or more stones through (row, col)
* This method is used by the GUI version of Go Moku for
* implementing the undo functionality.
*
* DO NOT MODIFY THIS METHOD
*
* @param row an int representing the row position
* @param col an int representing the column position
* @return a list of Move objects.
*/
public List<Move> line (int row, int col) {
List<Move> seq = line1 (row, col, +1, +1); // down-right
if (seq.size() >= 5)
return seq;
seq = line1 (row, col, +1, 0); // down
if (seq.size() >= 5)
return seq;
seq = line1 (row, col, +1, -1); // down-left
if (seq.size() >= 5)
return seq;
seq = line1 (row, col, 0, -1); // left
if (seq.size() >= 5)
return seq;
return null;
}
/**
* Returns greatest length of line through (row,col)
* in general direction (rowInc,colInc)
*
* DO NOT MODIFY THIS METHOD
*
* @param row an int representing the row position
* @param col an int representing the column position
* @param rowInc an int representing a row direction indicator (-1, 0, +1)
* @param colInc an int representing a column direction indicator (-1, 0, +1)
* @return a list of Move objects.
*/
private List<Move> line1 (int row, int col, int rowInc, int colInc) {
List<Move> seq1 = line2 (row, col, rowInc, colInc);
List<Move> seq2 = line2 (row, col, -rowInc, -colInc);
Collections.reverse(seq1);
seq2.remove(0);
seq1.addAll(seq2);
return seq1;
}
/**
* Returns greatest length of line through (row, col)
* in direction (rowInc,colInc)
*
* DO NOT MODIFY THIS METHOD
*
* @param row an int representing the row position
* @param col an int representing the column position
* @param rowInc an int representing a row direction indicator (-1, 0, +1)
* @param colInc an int representing a column direction indicator (-1, 0, +1)
* @return a list of Move objects.
*/
private List<Move> line2 (int row, int col, int rowInc, int colInc) {
List<Move> seq = new LinkedList<Move>();
int colour = board[row][col];
seq.add(new Move(row, col));
row += rowInc;
col += colInc;
while (valid(row, col) && board[row][col] == colour) {
seq.add(new Move(row, col));
row += rowInc;
col += colInc;
}
return seq;
}
} // end Board
· public Board() – creates a new SIZE x SIZE board array, and set all elements in board to
NONE.
· public boolean valid(int row, int col) - Returns true if row, col is a valid board
position, otherwise false. row, col is a valid position if 0 <= row < SIZE and 0 <= col < SIZE.
· public boolean occupied(int row, int col) - Returns true if the value in position
row, col of board is not NONE. Precondition: row, col is a valid board position.
· public int stone(int row, int col) – Returns the value stored in position row, col of
board. Precondition: row, col is a valid board position.
· public void clear() – Sets all elements in board to NONE.
· public void addStones(int n, int colour) – Place n stones of the given colour in
random positions on the board. Precondition: n >= 0, and colour is either 1 or -1.
The following pseudocode describes how to place n random stones of the same colour col on the
board:
Set all positions in the board to be vacant;
while (n > 0)
Let r be a random integer in the range 0 to 14;
Let c be a random integer in the range 0 to 14;
if ((r,c) is a possible board position and
position (r, c) is vacant)
Place a stone of colour col at position (r, c);
Decrement n;
To generate a random integer in the range 0 to m, simply evaluate the expression
(int)(Math.random()*m).
· public void addStone(int row, int col, int colour) - Place 1 stone of given
colour at position row, col of board. Precondition: row, col is a valid board position.
· public void removeStone(int row, int col) – Removes a stone from position row,
col of board. Precondition: row, col is a valid board position.
· public void print() - Prints board, with column and row headings (as shown above in the
sample output).
· public int maxLength(int row, int col) – Returns the greatest length of any line of
adjacent stones of the same colour through row, col. Precondition: row, col is a valid board
position.
The following pseudocode describes how to compute the length of the longest line through a given
position in a given direction. Lines may go in each of eight directions by incrementing two
parameters rinc and cinc appropriately. E.g, setting rinc to -1 and cinc to 0 generates a
vertical line upwards.
int length (int row, int col, int rinc, int cinc) {
int colour = board[row][col];
if (colour represents an empty position) return 0;
int len = 1;
row += rinc; col += cinc;
while ((row,col) is on the board and board[row][col] == colour)
{
len++;
row += rinc; col += cinc;
}
return len;
}
To compute the actual length of the longest line through a given point in a given direction it is
necessary to look both "backwards" and "forwards", and evaluate something like
length(row,col,rinc,cinc) + length(row,col,-rinc,-cinc) - 1 (but note that lengths must always be
nonnegative).
· private void doit() – Provides console based user interface and call on the methods above
to fulfil all the requirements (generate random tokens, print board, print the longest line through a
position etc.) this Go Moku project.
· public static void main(String[] args) – Creates a new Board object, and call
doit()to start the application.