Firas Abd El Gani
Firas Abd El Gani

Reputation: 113

Finding the indexes of an element of 9x9 array that is divided into nine 3x3 arrays

In order to create an ultimate tic-tac-toe game, which includes a 9x9 array that is divided into nine 3x3 sub-arrays.

Each sub-array is numbered in the following manner:

Enter image description here

And I want to create a function that if the user for example enters two parameters, it goes to the cell in the 9x9 matrix, (or gives back the indexes).

For example, if I enter 2,2: I go to the second 3x3 matrix, and there I go to the second cell, which means I end up in cell [0,4] in the 9x9 matrix.

I'm writing the code in the C language. All I ended up with is making a big function with many loops that takes me directly to the wanted cell, but its too much I think, I would be happy for a simpler way to do that.

Any ideas?

The following code is just the start of what I did. I divided the 9x9 matrix into three columns, and each time I deal with a column according to the number from 1-9 the user chosen for sub array, and from there I go to a series of functions for each sub array to find the proper place. It’s too complicated and long, and that’s the problem of my code. That’s why I’m searching for an alternative.

int Insert_Move(char player, char GameBoard[Lines][Columns], int chosen_pos,
                int next_subboard)

{
    if(next_subboard == 3 || next_subboard == 6 || next_subboard == 9)
        return First_Column_9x9(player, GameBoard, chosen_pos, next_subboard);

    if(next_subboard == 2 || next_subboard == 5 || next_subboard == 8)
        return  Second_Column_9x9(player, GameBoard, chosen_pos, next_subboard);

    if(next_subboard == 1 || next_subboard == 4 || next_subboard == 7)
        return Third_Column_9x9(player, GameBoard, chosen_pos, next_subboard);

    return 1; /* Success */
}

Upvotes: 0

Views: 786

Answers (2)

Naresh Teli
Naresh Teli

Reputation: 138

In the two-dimensional array as:

X coordinate = ((p1-1)℅3)*3 + (p2-1)℅3;

Y coordinate = ((p1-1)/3)*3 + (p2-1)/3;

Upvotes: 1

ad absurdum
ad absurdum

Reputation: 21364

Here is a function that takes an array of two size_t values to store the indices of the cell, and the two position arguments given by the user. It finds the indices for the given positions in a zero-indexed 3X3 array, and then combines these results to produce the indices in the 9X9 array.

I put it in a little program to test a couple of inputs:

#include <stdio.h>

void get_index(size_t indices[2], int pos, int sub_board);

int main(void)
{
    size_t indices[2];
    int pos, sub_board;

    pos = 2;
    sub_board = 2;

    get_index(indices, pos, sub_board);

    printf("Cell indices are: [%zu][%zu]\n", indices[0], indices[1]);

    pos = 6;
    sub_board = 9;

    get_index(indices, pos, sub_board);

    printf("Cell indices are: [%zu][%zu]\n", indices[0], indices[1]);    

    return 0;
}

void get_index(size_t indices[2], int pos, int sub_board)
{
    size_t row_pos, col_pos;
    size_t row_sub, col_sub;

    row_pos = (pos - 1) / 3;
    col_pos = (pos - 1) % 3;
    row_sub = (sub_board - 1) / 3;
    col_sub = (sub_board - 1) % 3;

    indices[0] = 3 * row_sub + row_pos;    
    indices[1] = 3 * col_sub + col_pos;

}

Output:

Cell indices are: [0][4]
Cell indices are: [7][8]

Upvotes: 1

Related Questions