2

This is a pretty niche problem, so please hear me out.

The Situation

I'm coding an Ultimate Tic Tac Toe game for the command line. I have a 2d char[9][9] array called currentState where each sub-array, which holds 'X', 'O', or ' ', represents a small 3x3 grid within the large 3x3 grid (index 0 is top left, 1 is top middle, 2 is top right, 3 is middle left, etc., for both the small grids and the large grid).

The Problem

If I were to try to print the array by iterating through it normally with two nested for loops, then the elements meant to display in each small grid would be displayed across a line. I want to print currentState so it will display in an "array of arrays" like the board is actually meant to look, as shown in the link above.

What I've Tried

I have created another int[9][9] called renderedState, with the idea being that it could be printed using nested for as described above; every element would correspond to the correct element in currentState (for example, currentState[3][7] == renderedState[5][1] -- the element in board 3 at position 7 should be printed on line 5, column 1 (both zero-based)).

The difficulty with this method is in finding a way to update this array given the indices of the square from currentState being updated (if currentState1[0] is being changed, renderedState[0][3] should also be changed). It took many failed attempts to come up with the following, which updates renderedState by looping through currentState:

for (int row = 0, board = 0; row < 9; row += 3)
    for (int col = 0; col < 9; col += 3, board++)
        for (int pos = 0; pos < 9; pos++)
            renderedState[row + (pos / 3)][col + (pos % 3)] = currentState[board][pos];

This works properly, but I plan to develop some sort of engine for the user to play against, so looping through all 81 elements to change a single value takes too much time for something that seems like it could be reduced to a single statement, like

renderedState[someRow][someColumn] = currentState[grid][pos];

...but how can I find the values for someRow and someColumn?

Sorry for the lengthy explanation. I've been trying to figure this out for so long that I'm getting confused myself. Please ask questions if something was unclear.

Jackson H
  • 171
  • 10
  • can you just show some sample here. – Negi Rox Jan 16 '18 at 06:30
  • Why do you need the auxiliary array? Just use the mapping from "print coordinates" to "storage coordinates" to print the array directly with two nested for loops. – Henry Jan 16 '18 at 06:33
  • There is no need to introduce new array, just override toString() method, following the simple logic. You can find that logic using the debugger, pencil, etc. (hint - is some int divisible by 3?). :) – zlakad Jan 16 '18 at 06:35
  • @Henry This would be ideal, but how would I go about doing this? Unless I am mistaken, this would require the "inverse" mapping from what @p.p. suggested below---you are given `row` and `col` and must find `grid` and `pos` instead of vice versa. – Jackson H Jan 17 '18 at 03:35
  • @JacksonH no, it is exactly what p.p. answered. – Henry Jan 17 '18 at 04:56
  • @Henry If you were to loop through two nested for loops with `i` and `j` as indices, then `i` and `j` would be the row and col which are calculated using grid and pos. P.p.'s solution calculates row and col from grid and pos instead of grid and pos from row and col. To print, in two nested for loops, I would need to calculate grid and pos from `i` and `j`. This is not what p.p.'s answer does. – Jackson H Jan 26 '18 at 01:25

1 Answers1

1

You can just calculate the values of someRow and someColumn out of the values grid and pos:

someRow = Math.floor(grid / 3) * 3 + Math.floor(pos / 3); //each third grid-value the row increases by 3, each third pos-value it increases by 1
someColumn = (grid % 3) * 3 + pos % 3; //The column is thrice the rest of grid divided by 3 plus the position divided by three

This code works if, as you said, you are using a zero-based numbering system. Otherwise you need to use accordingly decremented values of grid and pos.