C
C#15mo ago
Dami

❔ How would I implement a toroidal universe?

A while ago I had started a project to hopefully create John Conway's Game of Life. I have everything else figured out except how to implement a toroidal universe to where the boxes wrap to the other side of the console window. I genuinely don't understand the math behind it and I need a bit of help.
14 Replies
Thinker
Thinker15mo ago
Since cells in Game of Life only check their surroundings neighbors, you could just check whether a cell is on the edge and then pick cells from the other side a neighbors, if I'm understanding the problem right
230V
230V15mo ago
Sounds like a modulo usecase
Dami
Dami15mo ago
my brain hurts already would you like to see my code for the finite portion of the game of life?
int MainWindow::NeighborCount(int row, int column)
{
int total = 0;

if (_isFinite)
{
if (row - 1 >= 0)
{
if (gameBoard[row - 1][column])
{
total++;
}
if (column - 1 >= 0)
{
if (gameBoard[row - 1][column - 1])
{
total++;
}
}
if (column + 1 < settings.defaultGridSize)
{
if (gameBoard[row - 1][column + 1])
{
total++;
}
}
}

if (row + 1 < settings.defaultGridSize)
{
if (gameBoard[row + 1][column])
{
total++;
}
if (column + 1 < settings.defaultGridSize)
{
if (gameBoard[row + 1][column + 1])
{
total++;
}
}
if (column - 1 >= 0)
{
if (gameBoard[row + 1][column - 1])
{
total++;
}
}

}

if (column + 1 < settings.defaultGridSize)
{
if (gameBoard[row][column + 1])
{
total++;
}

}

if (column - 1 >= 0)
{
if (gameBoard[row][column - 1])
{
total++;
}
}
}
int MainWindow::NeighborCount(int row, int column)
{
int total = 0;

if (_isFinite)
{
if (row - 1 >= 0)
{
if (gameBoard[row - 1][column])
{
total++;
}
if (column - 1 >= 0)
{
if (gameBoard[row - 1][column - 1])
{
total++;
}
}
if (column + 1 < settings.defaultGridSize)
{
if (gameBoard[row - 1][column + 1])
{
total++;
}
}
}

if (row + 1 < settings.defaultGridSize)
{
if (gameBoard[row + 1][column])
{
total++;
}
if (column + 1 < settings.defaultGridSize)
{
if (gameBoard[row + 1][column + 1])
{
total++;
}
}
if (column - 1 >= 0)
{
if (gameBoard[row + 1][column - 1])
{
total++;
}
}

}

if (column + 1 < settings.defaultGridSize)
{
if (gameBoard[row][column + 1])
{
total++;
}

}

if (column - 1 >= 0)
{
if (gameBoard[row][column - 1])
{
total++;
}
}
}
230V
230V15mo ago
Arithmetic operators - C# reference
Learn about C# operators that perform multiplication, division, remainder, addition, and subtraction operations with numeric types.
Dami
Dami15mo ago
thats the finite universe.. thats how i implemented the math there.. now i am so unsure how to do the toroidal
230V
230V15mo ago
(btw it's a c# server and you're using c++, but there's barely anything c++ specific in your code and c++ has modulo too)
Dami
Dami15mo ago
Im.. oh wow I genuinely didnt notice its been so long since i opened this project Like since January. I have had a lot more projects lol. Ill close this then. Thank you!
etreit
etreit15mo ago
Did you get this (besides the C#/C++ divide) figured out? If you still have questions about the actual implementation, happy to talk about it (though maybe tomorrow lol)
Dami
Dami15mo ago
I did not get this figured out no. Im genuinely so confused
etreit
etreit15mo ago
Do you understand the basics of modular arithmetic? If not, that’s totally alright, we can go through it! (If you want)
etreit
etreit15mo ago
Tossing this out there while I remember
Dami
Dami15mo ago
We should probably do this tomorrow im sorrg im super tired
etreit
etreit15mo ago
Haha yeah same for me. But insomnia so I figured I’d at least take a look and jot down some stuff Basically, if I'm understand correctly, the main thing you are trying to do is get the count of all live squares near a coordinate, including wrapping around to the other side of the board. The concepts behind modular arithmetic are pretty simple, you just keep counting and reset to 0 every time you hit, very similar to if you were counting on a clock which is the same as mod 12. In your case, it looks like you have a grid of size settings.defaultGridSize and it is a square, yay! That means we have less numbers to keep track of. Honestly, most of the time working in this sort of space makes code a bit simpler. You can reduce a lot of your checks because you don't ever need to worry about a "surrounding square" being out of bounds. So, for example, taking
if (column + 1 < settings.defaultGridSize)
{
if (gameBoard[row - 1][column + 1])
{
total++;
}
}
if (column + 1 < settings.defaultGridSize)
{
if (gameBoard[row - 1][column + 1])
{
total++;
}
}
You could just check and see if gameboard[(row - 1) % settings.defaultGridSize][(column + 1) % settings.defaultGridSize] exists, and then increment the total (I think this is how you use modulo in c++, I'm not really a dev so please take any random code snippets with a grain of salt) Because you are taking the remainder, even if, say, column + 1 is outside the range of the board, it will "reset" the value, effectively "wrapping" it over to the other end. So if you were in a 10x10 grid (which means we will mod by 10), numbered 0-9, and your column value is 9, then (column + 1) mod 10 = 10 mod 10 = 0
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
❔ How would I have a compiled Class Library look for necessary reference in a folder other than ./I am creating a game mod via Melon Loader, and I want to have the compiled .dll look for its referen❔ asp.net MVC, can I store some data in a static variable to access in a future request??I might need to modify a current process where user uploads a shit ton of data via excel. the server❔ journal system?https://www.youtube.com/watch?v=OWvicJkXv94 is there an easier way of implementing a journal system ❔ Bug in blazor?Hello guys, i have encountered something strange in my blazor app. I'll try to explain it here. So i❔ Unescaping special charactersWorking on a little SaaS thumbnail preview tool I'm gonna sell, and special characters like apostrop❔ How could I make it so that every tabpage on a tabcontrol fades in when you open them?How could I make it so that every tabpage on a tabcontrol fades in when you open them?❔ How to take a native crash [{0x7f803bba7228} + 0x3bd] + PDB/DLL and get a line number?I have a native crash from a .net application on macOS that contains some managed code in the stack.❔ .NET MAUI Blazor Windows Application does not startHi I wanted to deploy my application, I followed this tutorial: https://learn.microsoft.com/de-de/do❔ Ef core-Odd BehaviorSo straight into it, The mapping: builder.Entity<LessonPhase>(b => { b.ToTable(l❔ [MAUI] Global Activity IndicatorI have a MAUI Shell application where I'd like to show a global ActivityIndicator or loading icon. U