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:

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

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

public static int enter_move{

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 //
}
}
```

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

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

)

```
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?
```

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

nd which would drive the process

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

## Premium Content

You need an Expert Office subscription to comment.Start Free Trial