We help IT Professionals succeed at work.

# Completing the board in TTT

on
Again, I know there are lots of TTT programs out there but our program needs to be written as a 3-dim array specifically using some of the code already given to us and I am struggling to understand code out there which is why I'm posting this.

Anyway, I am trying to complete the board (field) class of my TTT game (or K in a row of an n x m grid). Could someone please help me complete and/or tell me where I have gone wrong:

``````class Field {
public static final int fe = 0; // cell empty
public static final int f1 = 1; // cell occupied by player 1
public static final int f2 = 2; // cell occupied by player 2

public static boolean valid_entry(final int e) {
return e == fe || e == f1 || e == f2;
}

public static int[][] empty_field(final int m, final int n) {
assert(m >= 1);
assert(n >= 1);
return new int[m][n];
}

public static boolean valid_field(int[][] field) {
if (field == null) return false;
final int M = field.length;
if (M == 0) return false;
if (field[0] == null) return false;
final int N = field[0].length;
if (N == 0) return false;
for (int i = 0; i < M; ++i) {
if (field[i] == null) return false;
if (field[i].length != N) return false;
for (int j = 0; j < N; ++j)
if (! valid_entry(field[i][j])) return false;
}
return true;
}

public static boolean valid_coordinates(final int[][] field, final int i, final int j) {
assert(valid_field(field));
return i >= 1 && j >= 1 && i <= field.length && j <= field[0].length;
}

public static boolean valid_move(final int[][] field, final int i, final int j) {
return valid_coordinates(field, i, j) && field[i-1][j-1] == fe;
}

public static int enter_move{
final int[][] field;
final int i, final int j;
final boolean first_player;
final int[][] move_list;
final int[] move_index;
final int[][][] occurrences;
final int[][] occupation) {
assert(valid_move(field, i, j));
final int player = (first_player) ? f1 : f2;
field[i-1][j-1] = player;
assert(move_index != null);
assert(move_index.length == 1);
assert(move_list != null);
assert(move_index[0] < move_list.length);
move_list[move_index[0]][0] = i;
move_list[move_index[0]][1] = j;
++move_index[0];
final int[] occ = occurrences[i-1][j-1];
assert(occ != null);
int max = Integer.MIN_VALUE;
for (int r = 0; r < occ.length; ++r) {
final int new_length = Occupation.update(occupation, occ[r], first_player);
if (new_length > max) max = new_length;
}
return max;
}

public static void output_field(final int[][] field) {
[b]for (int i = 0, i<field.length, i++)
{
for (int j = 0, j<field[0].length, j++)
{
field[m][n] = " . "
}
}
for (int i = 0, i<field.length, i++)
{
for (int j = 0, j<field[0].length, j++)
{
System.out.println(" " + field[i][j] + " ")
}
}[/b]
}
private static String cell(final int f) {
assert(f == fe || f == f1 || f == f2);
if (f == fe) return ".";
if (f == f1) return "X";
return "0";
}
public static void output_movelist(final int[][] move_list, final int number_moves) {
// NEED TO PRINT OUT A MOVE LIST //
}
}
``````

Could someone please help with particularly the end section which I don't know how to start/complete and the bold section which I believe may be wrong.

Any help would be much appreciated as I've been struggling a while with this and as you may tell I am a novice.

Thanks
Comment
Watch Question

## View Solution Only

Awarded 2011
Awarded 2011

Commented:
What is this enter_move with curly brace after it - is it supposed to be a  method - then no parentheses there ?

public static int enter_move{
Awarded 2011
Awarded 2011

Commented:
>using some of the code already given to us

which code was already given to you?

There are obvuious syntax errors in this code - liek in the opening loop
statement using commas insted  of semicolon, and opening curly brace insted of
parentheses, etc.

This code almost compiles - though there is no class Occupation (?)
and there is erroneoud assuigment field[m][n] = " .";

field is int array - you cannot obviously assing strings, and ma nd is not defined, etc.

so if you wrote this part - then think again, as you cannot
assign string to int;

If that was given to you - check if you copied correctly

``````    class Field {
public static final int fe = 0; // cell empty
public static final int f1 = 1; // cell occupied by player 1
public static final int f2 = 2; // cell occupied by player 2

public static boolean valid_entry(final int e) {
return e == fe || e == f1 || e == f2;
}

public static int[][] empty_field(final int m, final int n) {
assert(m >= 1);
assert(n >= 1);
return new int[m][n];
}

public static boolean valid_field(int[][] field) {
if (field == null) return false;
final int M = field.length;
if (M == 0) return false;
if (field[0] == null) return false;
final int N = field[0].length;
if (N == 0) return false;
for (int i = 0; i < M; ++i) {
if (field[i] == null) return false;
if (field[i].length != N) return false;
for (int j = 0; j < N; ++j)
if (! valid_entry(field[i][j])) return false;
}
return true;
}

public static boolean valid_coordinates(final int[][] field, final int i, final int j) {
assert(valid_field(field));
return i >= 1 && j >= 1 && i <= field.length && j <= field[0].length;
}

public static boolean valid_move(final int[][] field, final int i, final int j) {
return valid_coordinates(field, i, j) && field[i-1][j-1] == fe;
}

public static int enter_move(
final int[][] field,
final int i,
final int j,
final boolean first_player,
final int[][] move_list,
final int[] move_index,
final int[][][] occurrences,
final int[][] occupation) {
assert(valid_move(field, i, j));
final int player = (first_player) ? f1 : f2;
field[i-1][j-1] = player;
assert(move_index != null);
assert(move_index.length == 1);
assert(move_list != null);
assert(move_index[0] < move_list.length);
move_list[move_index[0]][0] = i;
move_list[move_index[0]][1] = j;
++move_index[0];
final int[] occ = occurrences[i-1][j-1];
assert(occ != null);
int max = Integer.MIN_VALUE;
for (int r = 0; r < occ.length; ++r) {
final int new_length = Occupation.update(occupation, occ[r], first_player);  //this requires class Occupation
if (new_length > max) max = new_length;
}
return max;
}

public static void output_field(final int[][] field) {
for (int i = 0; i<field.length; i++)
{
for (int j = 0; j<field[0].length; j++)
{
field[m][n] = " . ";  // this line is not compuiling
}
}
for (int i = 0; i<field.length; i++)
{
for (int j = 0; j<field[0].length; j++)
{
System.out.println(" " + field[i][j] + " ");
}
}
}
private static String cell(final int f) {
assert(f == fe || f == f1 || f == f2);
if (f == fe) return ".";
if (f == f1) return "X";
return "0";
}
public static void output_movelist(final int[][] move_list, final int number_moves) {
// NEED TO PRINT OUT A MOVE LIST //
}
}

``````

Commented:
Hey, thanks for the comments. This is what was originally given to us along with the comments:

``````/* Library for handling the playing field ("board"), which provides the
* following functionality:
*
* - constants fe, f1, f2 as codes for cell-values;
* - valid_entry(e) checks whether e is one of these codes;
* - empty_field(M,N) returns the empty field of dimension M X N;
* - valid_field(field) checks whether field has been initialised, and
* all cells contain valid values;
* - valid_coordinates(field, i, j) checks whether i, j are valid
* (matrix-)coordinates (or matrix-indices) for field; these are
* the user-coordinates, which are 1-based, while the internal
* representations are all 0-based; the top-left corner thus has (here!)
* coordinates i=1, j=1 (internally i=0, j=0);
* - valid_move(field, i, j) checks whether occupying field i,j is valid
* for field;
* - enter_move(field, i, j, first_player, move_list, move_index, occurrences,
* occupation) enters the valid move given by i,j into field, where the
* boolean first_player states whether it is the first player's move or not;
* update of data-structures move_list, move_index, and occupation;
* - output_field(field) prints a simple representation of field to
* standard output;
* - output_movelist(move_list, number_moves) prints out a move-list.
*/

class Field {

public static final int fe = 0; // cell empty
public static final int f1 = 1; // cell occupied by player 1
public static final int f2 = 2; // cell occupied by player 2

public static boolean valid_entry(final int e) {
return e == fe || e == f1 || e == f2;
}

// creates the empty field of size m x n:
public static int[][] empty_field(final int m, final int n) {
assert(m >= 1);
assert(n >= 1);
return new int[m][n];
}

// checks whether field is a rectangular non-empty array with entries
// fe, f1, or f2:
public static boolean valid_field(int[][] field) {
if (field == null) return false;
final int M = field.length;
if (M == 0) return false;
if (field[0] == null) return false;
final int N = field[0].length;
if (N == 0) return false;
for (int i = 0; i < M; ++i) {
if (field[i] == null) return false;
if (field[i].length != N) return false;
for (int j = 0; j < N; ++j)
if (! valid_entry(field[i][j])) return false;
}
return true;
}

// these are the "user"-coordinates, and thus 1-based:
public static boolean valid_coordinates(final int[][] field, final int i, final int j) {
assert(valid_field(field));
return i >= 1 && j >= 1 && i <= field.length && j <= field[0].length;
}

// i, j are "user-coordinates", and thus 1-based:
public static boolean valid_move(final int[][] field, final int i, final int j) {
return valid_coordinates(field, i, j) && field[i-1][j-1] == fe;
}

// enters a valid move into the field and into the move-list, with update
// of move_index and occupation; returns the maximum of the number of fields
// occupied by the player for the involved rows:
public static int enter_move{
final int[][] field,
final int i,
final int j,
final boolean first_player,
final int[][] move_list,
final int[] move_index,
final int[][][] occurrences,
final int[][] occupation){
assert(valid_move(field, i, j));
final int player = (first_player) ? f1 : f2;
field[i-1][j-1] = player;
assert(move_index != null);
assert(move_index.length == 1);
assert(move_list != null);
assert(move_index[0] < move_list.length);
move_list[move_index[0]][0] = i;
move_list[move_index[0]][1] = j;
++move_index[0];
final int[] occ = occurrences[i-1][j-1];
assert(occ != null);
int max = Integer.MIN_VALUE;
for (int r = 0; r < occ.length; ++r) {
final int new_length = Occupation.update(occupation, occ[r], first_player);
if (new_length > max) max = new_length;
}
return max;
}

// printing out a field:
public static void output_field(final int[][] field) {
// XXX to be completed XXX
}
private static String cell(final int f) {
assert(f == fe || f == f1 || f == f2);
if (f == fe) return ".";
if (f == f1) return "X";
return "0";
}

// printing out a move-list:
public static void output_movelist(final int[][] move_list, final int number_moves) {
// XXX to be completed XXX
}
}
``````

Should the enter_move have the '{' ?
Also, instead of the " . " should I initialise it to 0??

Thanks
Awarded 2011
Awarded 2011

Commented:
Yes, the enter_move they gave you has a misprint:

public static int enter_move( // here they gave you with curly brace - in fact it should  be opnening parnthesis - the arguments of the mthod follow
final int[][] field,
final int i,
final int j,
final boolean first_player,
final int[][] move_list,
final int[] move_index,
final int[][][] occurrences,
final int[][] occupation){   // on this line the closing parenthesis for argument list and opening  brace for the method is OK
Awarded 2011
Awarded 2011

Commented:
yes, field[][] is int array, it cannot have a string value, it cannot be null either

I guess you shoulduse fe, f1 and f2
So empty cells should be initialized to fe (see the beginning line:
public static final int fe = 0; // cell empty
)

Commented:
Thanks, you're really helping.

The for loop on the public static void output_field(final int[][] field) that I posted in the original post, is this correct? I tried to initialise the board and then print the board for the respective movements.

How would I print out the final list of moves on the final method? For example:
The complete list of moves is:
1.I :  2  2
1.II:  2  1
2.I :  1  3
2.II:  3  1
3.I :  1  1
3.II:  3  3
4.I :  1  2
Awarded 2011
Awarded 2011
Commented:

``````public static void output_field(final int[][] field)
the way I saw it last time was not correct beacuse of this line

field[m][n] = " . ";  //

you probably meant

field[i][j] = fe;

instead, this would at least be compilable.

With that the issue left which is not compilabale is line

final int new_length = Occupation.update(occupation, occ[r], first_player);

in  public static int enter_move(..) method

Waht is Occupation - is there a separate class of this name?
``````

Commented:
Yes apologies, this is another class given to us. This should be now complete:

``````/* Provides the array "occupation", which for each "row" (given by its
* index) contains the current number of cells owned by the two players.
*/

class Occupation {
// the occupation-array, initialised with all counts zero:
public static int[][] occupation_vector(final int R) {
assert(R >= 1);
return new int[R][2];
}

// checking whether occupation has been fully allocated, and with each
// count non-negative:
public static boolean valid_occupation(final int[][] occupation) {
assert(occupation != null);
for (int i = 0; i < occupation.length; ++i) {
if (occupation[i] == null) return false;
if (occupation[i].length != 2) return false;
if (occupation[i][0] < 0) return false;
if (occupation[i][1] < 0) return false;
} return true;
}

// checks additionally also whether the sum of counts per row is never
// greater than the length of the row:
public static boolean valid_occupation(final int[][] occupation, final int K) {
if (! valid_occupation(occupation)) return false;
for (int i = 0; i < occupation.length; ++i)
if (occupation[i][0] + occupation[i][1] > K) return false; return true;
}

// function to support algorithmic playing: a row r is "owned" by a
// player if the other player does not own cells in that row:
public static boolean owns_row(final int[][] occupation, final int r, final boolean first_player) {
assert(valid_occupation(occupation));
assert(r < occupation.length);
if (first_player) return occupation[r][1] == 0;
return occupation[r][0] == 0;
}

// update the occupation-count for row r after a cell has been marked:
public static int update(final int[][] occupation, final int r, final boolean first_player) {
assert(valid_occupation(occupation));
assert(r < occupation.length);
if (first_player) return ++occupation[r][0];
return ++occupation[r][1];
}
}
``````
Awarded 2011
Awarded 2011

Commented:
OK, now they at least compile togeher.

Now I guess you need to write the class which would have main() method
nd which would drive the process

Commented:
I'm sorry I don't understand how I would start the output_movelist(...) to be able to print the actual final move list out as the example before shows
Awarded 2011
Awarded 2011

Commented:
What you have in your code is just a library of functions.

In order to make use of these functions you should devise the flow of the game itslef.
Do as afar as I can undrerstand, you should create another class with method main()
which should drive the process of the game - then you will accumulate inforamtion during the game,
in particular about the moves, and after you ran through the game or part of it then you can use
that function and print movelist.

Until you have this method which has the logic whicvh drives the game, I don't think you can print
ny move_list, as there were no moves and no move_list

Commented:
Unfortunately there are a lot of different classes (which are incomplete) such as:

the generalised game:

``````class GeneralisedTicTacToe {

public static final String program_name = "GeneralisedTicTacToe";
public static final String message_start = "Starting " + program_name + ".";
public static final String message_win_1 = "The first player wins.";
public static final String message_win_2 = "The second player wins.";
public static final String message_error_exit = "Exiting after input errors.";
public static final String message_not_implemented = "Functionality not implemented.";
public static final String message_win1_always = "The first player will always win after the first move.";
public static final String message_no_win = "Every game must end in a draw.";
public static final String message_output_field = "The final position is:";
public static final String message_output_movelist = "The complete list of moves is:";

public static final int mode_hh = 1;
public static final int mode_hc = 2;
public static final int mode_ch = 3;
public static final int mode_cc = 4;

public static void main(final String[] args) {
System.out.println(message_start);

final int[] parameters = Parameters.process_parameters(args);
if (parameters == null) {
System.err.println(message_error_exit);
System.exit(1);
}
assert(parameters.length == 4);
final int K = parameters[0];
assert(K >= 1);
final int M = parameters[1];
assert(M >= 1);
final int N = parameters[2];
assert(N >= 1);
final int mode = parameters[3];
assert(mode == mode_hh || mode == mode_hc || mode == mode_ch || mode == mode_cc);

// Handling of trivial cases

if (K == 1) {
System.out.println(message_win1_always);
return;
}
if (M < K && N < K) {
System.out.println(message_no_win);
return;
}

// Preparing fundamental data and data structures

final int number_cells = M*N;
final int R = Counting.number_rows(K,M,N);
assert(R >= 1);

final int[][] field = Field.empty_field(M,N);
assert(field.length == M);
assert(field[0].length == N);
assert(Field.valid_field(field));

final int[][] move_list = new int[number_cells][2];
final int[] move_index = new int[1];
move_index[0] = 0;
assert(move_list[move_index[0]][0] == 0);
assert(move_list[move_index[0]][1] == 0);

final int[][][] rows = Rows.list_rows(K,M,N,R);
assert(rows.length == R);
assert(Rows.check_list_rows(rows, field));

final int[][][] occurrences = Occurrences.occurrences_field(rows, field);
assert(occurrences.length == M);
assert(occurrences[0].length == N);
assert(Occurrences.consistency_check(rows, occurrences));

final int[][] occupation = Occupation.occupation_vector(R);
assert(Occupation.valid_occupation(occupation, K));
assert(occupation[0][0] == 0);
assert(occupation[0][1] == 0);

// Reading resp. computing the moves

// XXX to be completed XXX
System.out.println(message_not_implemented); // yet nothing implemented
boolean first_player_moves = true;
// XXX
{
// XXX to be completed
final int max = Field.enter_move(field, reading[0], reading[1], first_player_moves, move_list, move_index, occurrences, occupation);
// XXX to be completed
}
// XXX to be completed XXX

// Final output

System.out.println(message_output_field);
Field.output_field(field);
System.out.println(message_output_movelist);
Field.output_movelist(move_list);
}
}
``````