Jump to content

[Help] Isolating data in an Array

TheRedViper

Hello there, I'm trying to figure out how to isolate the 8 solutions to a 3x3 tictactoe using an array (not 2d) with loops in C++ coding. Right now, I only have a chain of if statements, but its dull and I was wondering if you guys have any idea how to do it with a single dimension array (iGrid[8] and not the usual iGrid[2][2]). I know how to do it with a 2d array because the fact its 2d already isolates the rows and columns, but how can you make a series of loop do the same thing for a single dimension one is the question. Here's an exemple of a winning conditon:

 

(if statements):

if(tiGrille[2] == 1 && tiGrille[4] == 1 && tiGrille[6] == 1)
{
	return 1;
} 

 

 

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

My first thought is to literally map all the indexes of the 3x3 grid to a single array. To visualize my idea...

 

0 1 2

3 4 5      ->     [0,1,2,3,4,5,6,7,8]

6 7 8

Intel® Core™ i7-12700 | GIGABYTE B660 AORUS MASTER DDR4 | Gigabyte Radeon™ RX 6650 XT Gaming OC | 32GB Corsair Vengeance® RGB Pro SL DDR4 | Samsung 990 Pro 1TB | WD Green 1.5TB | Windows 11 Pro | NZXT H510 Flow White
Sony MDR-V250 | GNT-500 | Logitech G610 Orion Brown | Logitech G402 | Samsung C27JG5 | ASUS ProArt PA238QR
iPhone 12 Mini (iOS 17.2.1) | iPhone XR (iOS 17.2.1) | iPad Mini (iOS 9.3.5) | KZ AZ09 Pro x KZ ZSN Pro X | Sennheiser HD450bt
Intel® Core™ i7-1265U | Kioxia KBG50ZNV512G | 16GB DDR4 | Windows 11 Enterprise | HP EliteBook 650 G9
Intel® Core™ i5-8520U | WD Blue M.2 250GB | 1TB Seagate FireCuda | 16GB DDR4 | Windows 11 Home | ASUS Vivobook 15 
Intel® Core™ i7-3520M | GT 630M | 16 GB Corsair Vengeance® DDR3 |
Samsung 850 EVO 250GB | macOS Catalina | Lenovo IdeaPad P580

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, BlueChinchillaEatingDorito said:

My first thought is to literally map all the indexes of the 3x3 grid to a single array. To visualize my idea...

 

0 1 2

3 4 5      ->     [0,1,2,3,4,5,6,7,8]

6 7 8

Yeah I already guessed so, but the problem is how will the loop know that it changes rows? When its a 2d array, the loop knows its going to another row because its goes from iGrid[1] to iGrid[2] for exemple so if player 1 has all cases in first row so all of iGrid[1] = 1 then player 1 wins. However in a single dimension array if the loop goes through all 9 cases and lets say (iGrid[8] -> [0]= 1, [1]= 1, [2]= 1, [3]=0, [4]= 0, [5]= 0, [6]= 0, [7]= 0, [8]= 0) even though cases 0 to 2 (1,2 and 3) are picked by player 1 so he wins because theres 3 cases connected by the same player, the loop doesn't know its in a 3x3 because it never hits a ''wall''.

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

I think this will work, but I haven't tested it yet.

int tiGrille[3][3] = {2};
int scores[4];

// 0 is O
// 1 is X
// 2 is blank

int ThreeInARow(void) {
	int i;
	// Horizontal and vertical
	for( i = 0; i < 3; i++) {
		scores[0] = tiGrille[i][0] + tiGrille[i][1] + tiGrille[i][2];
		scores[1] = tiGrille[0][i] + tiGrille[1][i] + tiGrille[2][i];
		if ((scores[0] == 0) || (scores[1] = 0)) return 0;
		if ((scores[0] == 3) || (scores[1] = 3)) return 1;
	}
	// Diagonal
	scores[2] = tiGrille[0][0] + tiGrille[1][1] + tiGrille[2][2];
	scores[3] = tiGrille[0][2] + tiGrille[1][1] + tiGrille[2][0];
	if ((scores[2] == 0) || (scores[3] = 0)) return 0;
	if ((scores[2] == 3) || (scores[3] = 3)) return 1;
	return 2; // No winner
}

I don't know C++, just C, so there may be a simpler way to do it.

Make sure to quote or tag me (@JoostinOnline) or I won't see your response!

PSU Tier List  |  The Real Reason Delidding Improves Temperatures"2K" does not mean 2560×1440 

Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, JoostinOnline said:

I think this will work, but I haven't tested it yet.


int tiGrille[3][3] = {2};
int scores[4];

// 0 is O
// 1 is X
// 2 is blank

int ThreeInARow(void) {
	int i;
	// Horizontal and vertical
	for( i = 0; i < 3; i++) {
		scores[0] = tiGrille[i][0] + tiGrille[i][1] + tiGrille[i][2];
		scores[1] = tiGrille[0][i] + tiGrille[1][i] + tiGrille[2][i];
		if ((scores[0] == 0) || (scores[1] = 0)) return 0;
		if ((scores[0] == 3) || (scores[1] = 3)) return 1;
	}
	// Diagonal
	scores[2] = tiGrille[0][0] + tiGrille[1][1] + tiGrille[2][2];
	scores[3] = tiGrille[0][2] + tiGrille[1][1] + tiGrille[2][0];
	if ((scores[2] == 0) || (scores[3] = 0)) return 0;
	if ((scores[2] == 3) || (scores[3] = 3)) return 1;
	return 2; // No winner
}

I don't know C++, just C, so there may be a simpler way to do it.

that would work for a 2d array but you cant scan a single dimension array that way (because iGrid[8] and not iGrid[2][2])

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

1 minute ago, TheRedViper said:

that would work for a 2d array but you cant scan a single dimension array that way (because iGrid[8] and not iGrid[2][2])

Sorry, I read it backwards.  I thought you already had a one dimensional array.

 

Using multi-dimensional arrays makes it neater.  You'll have way more conditionals if you do single dimensional.  I think I cut the code down pretty small.

Make sure to quote or tag me (@JoostinOnline) or I won't see your response!

PSU Tier List  |  The Real Reason Delidding Improves Temperatures"2K" does not mean 2560×1440 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, JoostinOnline said:

Sorry, I read it backwards.  I thought you already had a one dimensional array.

 

Using multi-dimensional arrays makes it neater.  You'll have way more conditionals if you do single dimensional.  I think I cut the code down pretty small.

I know that multi-dimensional is easier to use, which is why I need to use the single one.

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

Just now, TheRedViper said:

I know that multi-dimensional is easier to use, which is why I need to use the single one.

That doesn't make any sense xD

Make sure to quote or tag me (@JoostinOnline) or I won't see your response!

PSU Tier List  |  The Real Reason Delidding Improves Temperatures"2K" does not mean 2560×1440 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, JoostinOnline said:

That doesn't make any sense xD

its a school project, therefore we are meant to use the hardest version of any controls.

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

Just now, TheRedViper said:

its a school project, therefore we are meant to use the hardest version of any controls.

Then hard-code the searches.  Basically what I did with the diagonals.

Make sure to quote or tag me (@JoostinOnline) or I won't see your response!

PSU Tier List  |  The Real Reason Delidding Improves Temperatures"2K" does not mean 2560×1440 

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, JoostinOnline said:

Then hard-code the searches.  Basically what I did with the diagonals.

thats what i did at first, but youre supposed to be able to do it with a loop
if(tiGrille[2] == 1 && tiGrille[4] == 1 && tiGrille[6] == 1)
{
	return 1;
} 

 

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

7 minutes ago, TheRedViper said:

thats what i did at first, but youre supposed to be able to do it with a loop

if(tiGrille[2] == 1 && tiGrille[4] == 1 && tiGrille[6] == 1)
{
	return 1;
} 

 

I don't feel like typing up a whole new set of code, but you could try using:

tiGrille[i*1]

tiGrille[i*2]

tiGrille[i*3]

in a loop.  You'd only have to modify my code a bit.

Make sure to quote or tag me (@JoostinOnline) or I won't see your response!

PSU Tier List  |  The Real Reason Delidding Improves Temperatures"2K" does not mean 2560×1440 

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, TheRedViper said:

Hello there, I'm trying to figure out how to isolate the 8 solutions to a 3x3 tictactoe using an array (not 2d) with loops in C++ coding. Right now, I only have a chain of if statements, but its dull and I was wondering if you guys have any idea how to do it with a single dimension array (iGrid[8] and not the usual iGrid[2][2]). I know how to do it with a 2d array because the fact its 2d already isolates the rows and columns, but how can you make a series of loop do the same thing for a single dimension one is the question. Here's an exemple of a winning conditon:

 

(if statements):


if(tiGrille[2] == 1 && tiGrille[4] == 1 && tiGrille[6] == 1)
{
	return 1;
} 

 

 

The normal approach is to write a accessor function that does the x, y decoding for you:

constexpr int gridWidth = 3;
constexpr int gridHeight = 3;

int& GetTile(int* grid, int x, int y) //x, y indexes start at 0.
{
	return grid[(y * gridWidth) + x];
}

//Now you can simply access your array asif it was a 2D array:
int grid[gridWidth * gridHeight];

//For example, fill the grid with values 1 - 9.
int value = 1;
for (int y = 0; y < gridHeight; ++y)
{
	for (int x = 0; x < gridWidth; ++x)
	{
		GetTile(grid, x, y) = value++;	
	}
}

Now, off course, in a true C++ program, a grid would be a class. It would be able to remember it's own width and height, allowing grids of different sizes and removing the need for those ugly global constants, and the accessor would be a member function with both const and non-const overloads.

Link to comment
Share on other sites

Link to post
Share on other sites

Tic Tac Toe is such a fun game! 

 

https://github.com/lpshanley/NodeJS-Demo/blob/master/public/js/chat.js

 

There is a function in there called Game Scan that will have all the logic you should need. Essentially You create a multi dimensional array and in the array you run some for loops. The logic in that file is a touch crude I didn't really refine anything so change and tweak to your hearts content. Its in javascript but the verbiage is close and the same logical patterns will get you to your end goal. Shoot me a message if you run into problems!

"Talk is cheap. Show me the code."

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

×