extending noughts and crosses into N dimensions

im making N dimensional noughts and crosses (i was incredibly bored in class). for the sake of simplicity I've got it running with 4 dimensions for now. how would i extend it into N dimensions? obviously there'll be a finite amount of dimensions during builds. im just wondering how you'd do it for future use-cases too PS: this is probably way simpler than im describing it to be. im not very good at C# lol
No description
10 Replies
ـّّّ Jordan't ـّّّ
for (int x = 0; x <= GameArea.x + 1; x++)
{
for (int y = 0; y <= GameArea.y + 1; y++)
{
for (int z = 0; z <= GameArea.z + 1; z++)
{
//i dont know much about who wrote this. but praise be to you, sean bridges. genuinely saved me over 2000 lines of code with this one small snippet
for (int w = 0; w <= GameArea.w + 1; w++)
{
//if we have at least 1 x
if ((w == GameArea.w) | (z == GameArea.z) | (y == GameArea.y) | (x == GameArea.x))
{
//if all -x's occur after at least 1 x
if (
(x == GameArea.x) ||
((y == GameArea.y) & (x != GameArea.x + 1)) ||
((z == GameArea.z) & (x != GameArea.x + 1) & (y != GameArea.y + 1)) ||
((w == GameArea.w) & (x != GameArea.x + 1) & (y != GameArea.y + 1) & (z != GameArea.z + 1))
)
{ //we have a valid line.
PossibleWins.Add(TranslateWin(new Vector4Int(x, y, z, w)));
Debug.Log(x + " " + y + " " + z + " " + w);
}
}
}
}
}
for (int x = 0; x <= GameArea.x + 1; x++)
{
for (int y = 0; y <= GameArea.y + 1; y++)
{
for (int z = 0; z <= GameArea.z + 1; z++)
{
//i dont know much about who wrote this. but praise be to you, sean bridges. genuinely saved me over 2000 lines of code with this one small snippet
for (int w = 0; w <= GameArea.w + 1; w++)
{
//if we have at least 1 x
if ((w == GameArea.w) | (z == GameArea.z) | (y == GameArea.y) | (x == GameArea.x))
{
//if all -x's occur after at least 1 x
if (
(x == GameArea.x) ||
((y == GameArea.y) & (x != GameArea.x + 1)) ||
((z == GameArea.z) & (x != GameArea.x + 1) & (y != GameArea.y + 1)) ||
((w == GameArea.w) & (x != GameArea.x + 1) & (y != GameArea.y + 1) & (z != GameArea.z + 1))
)
{ //we have a valid line.
PossibleWins.Add(TranslateWin(new Vector4Int(x, y, z, w)));
Debug.Log(x + " " + y + " " + z + " " + w);
}
}
}
}
}
ـّّّ Jordan't ـّّّ
nvm. i think chatGPT got the answer (still gotta test it) nope, chatGPT didn't work
Anton
Anton14h ago
&&, not & & is bitwise and && is logical and have you looked up the algorithm?
ـّّّ Jordan't ـّّّ
Many times. Only even found the 4D algorithm after about a year of searching lol
Anton
Anton10h ago
have you tried implementing it by the algorithm? do you understand the algorithm?
ـّّّ Jordan't ـّّّ
i do. the main problem is this line here
if (
(x == GameArea.x) ||
((y == GameArea.y) & (x != GameArea.x + 1)) ||
((z == GameArea.z) & (x != GameArea.x + 1) & (y != GameArea.y + 1)) ||
((w == GameArea.w) & (x != GameArea.x + 1) & (y != GameArea.y + 1) & (z != GameArea.z + 1))
)
if (
(x == GameArea.x) ||
((y == GameArea.y) & (x != GameArea.x + 1)) ||
((z == GameArea.z) & (x != GameArea.x + 1) & (y != GameArea.y + 1)) ||
((w == GameArea.w) & (x != GameArea.x + 1) & (y != GameArea.y + 1) & (z != GameArea.z + 1))
)
im not sure how to recursively do this check as N increases
Anton
Anton7h ago
I see it's a problem that comes up pretty often
Anton
Anton7h ago
Stack Overflow
Iterate over m^n combinations with nested loops
I have an array y of dimension n, every dimension has m entries. How do I iterate over all combinations of indices using nested for loops? I'd like to get all possible combinations of sums like sho...
Anton
Anton7h ago
this might give you some ideas

Did you find this page helpful?