Jump to content

Isolate data in 1D array

TheRedViper

Hello there, so I'm currently working on a school project based on the classic game tic-tac-toe. Everything is working, except that I'm stuck at isolating the columns and the rows in my array to determine the winner. I know a solution for a 2d array, but we must use a 1d array.

 

With a 2d array, the rows and columns are already separated like: grid[0], grid[1], grid[2], but in a regular array grid[8] they're not.

CPU: Intel Core i9-10900K  MOBO: ROG MAXIMUS XII FORMULA GPU: 2080ti Hall of Fame 10th anniversary limited edition  PSU: Asus ROG THOR 1200W  COOLER: Optimus foundation black acetal RADS: 3x EKWB CoolStream PE 360  LOOP: EKWB torque HDC fittings / EKWB ZMT 15,9/9,5mm / EKWB CryoFuel Clear MONITOR: Acer predator X34

Link to comment
Share on other sites

Link to post
Share on other sites

to convert a 2d array to 1d array is pretty simple maths

so lets take array[x][y] and convert it to array[z]

 

position x=0 y= 0 in the new array will be z = 0

position x=2, y=2 will be z = 8

 

the formula is

z = x + (y * row length)

So you can use your x and y positions as you would in your 2d array but shove this in when you need to convert x and y to a 1d array.

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, SCHISCHKA said:

to convert a 2d array to 1d array is pretty simple maths

so lets take array[x][y] and convert it to array[z]

 

position x=0 y= 0 in the new array will be z = 0

position x=2, y=2 will be z = 8

 

the formula is

z = x + (y * row length)

So you can use your x and y positions as you would in your 2d array but shove this in when you need to convert x and y to a 1d array.

yeah but i dont think it helps in scanning specific bunchs of datas like in the tictactoe case, scanning rows then columns

CPU: Intel Core i9-10900K  MOBO: ROG MAXIMUS XII FORMULA GPU: 2080ti Hall of Fame 10th anniversary limited edition  PSU: Asus ROG THOR 1200W  COOLER: Optimus foundation black acetal RADS: 3x EKWB CoolStream PE 360  LOOP: EKWB torque HDC fittings / EKWB ZMT 15,9/9,5mm / EKWB CryoFuel Clear MONITOR: Acer predator X34

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, TheRedViper said:

yeah but i dont think it helps in scanning specific bunchs of datas like in the tictactoe case, scanning rows then columns

you have your x and y to play with. y are your rows and x are your columns.

index = column_number + (row_number * row_length)

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

On 4/3/2017 at 9:10 PM, TheRedViper said:

Hello there, so I'm currently working on a school project based on the classic game tic-tac-toe. Everything is working, except that I'm stuck at isolating the columns and the rows in my array to determine the winner. I know a solution for a 2d array, but we must use a 1d array.

 

With a 2d array, the rows and columns are already separated like: grid[0], grid[1], grid[2], but in a regular array grid[8] they're not.

You could group your columns or rows into single items within the array:

So [B, B, B, 0, X, 0, 0, X, X] would be turned into this

["BBB", "0X0", "0XX"]

 

To isolate specific tiles just treat the objects as if you were grabbing individual characters in a string.

 

so tile 1 in column 2 would be "0"

 

 

EDIT: 

 

I actually ran into a situation where a 1 dimensional array was necessary yesterday while working on my isometric rendering engine.

 

You can actually great an array with 9 entries and then, using math you can grab the position of any tile within the array relative to it's physical coordinates.

 

The method I'm describing uses the top furthermost left tile as the origin making it tile 0, 0

 

To calculate the position of any tile within your grid you must first multiply the width of your grid by the row that the tile is located on, then add on to your result the column the tile is located on

 

[0, 0, 0, 0, 0, 0, 0, 0, 0]

 

          0     2     3

      /=========

0    |    B    B     X

1    |    X    O     B

2    |    B    X     O

 

Where B is blank

 

if we're given the coordinates (3, 0) we first multiply the X value in our coordinate by the width of the grid producing 0. then we add on the y value to produce a result of 2 making the resulting tile X

 

My procrastination is the bane of my existence.

I make games and stuff in my spare time.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×