Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Tyler Dotson
Courses Plus Student 1,740 PointsHey everyone, I need to make a grid for a game I am creating that just uses x and y values
I am creating a game that needs a 100 x 100 grid (Note it will not be a visible grid just something that will give me coordinates.) Without having to write a array with every possible coordinate how do I create this grid.
3 Answers
james south
Front End Web Development Techdegree Graduate 33,271 Pointsnested for loop. for x....with a for y.... inside. for each x going across, make 100 y going down, 100x100.
james south
Front End Web Development Techdegree Graduate 33,271 Pointsactually if you need arrays you might switch x and y, put x inside of y, that might be more intuitive. then you would have 100 arrays stacked on top of each other, each 100 coordinate pairs long. the first y is 1, then inside you run x 1 to 100, left to right, then next row, y is 2, x 1 to 100 again, and so on. so a row might look like [(1,1),(2,1),(3,1),(4,1).....] and the next row [(1,2),(2,2),(3,2),(4,2).....].
Tyler Dotson
Courses Plus Student 1,740 Pointsso maybe something like this?
var gameArea: [String] = []
for y in 1...100 {
for x in 1...100{
let coordinate = "(x, y)"
gameArea.append(coordinate)
}
}
james south
Front End Web Development Techdegree Graduate 33,271 Pointswell i think that would just give you an array filled with strings of '(x,y)', not actual numbers. i would think you need an array of arrays, filled not with strings but tuples of x,y pairs. start with smaller numbers like up to 10 instead of 100 and print it out.
Tyler Dotson
Courses Plus Student 1,740 PointsI was thinking that too but how do I get the tuples in an array so when i do this in a play ground it gives me vlaues of like,
(.0 1, .1 1), (.0 1, .1 2)
Is that right? what does the .0 and the .1 mean?
ar gameArea: [(Int, Int)] = []
for y in 1...10 {
for x in 1...10 {
gameArea.append(x, y)
}
}
gameArea
Tyler Dotson
Courses Plus Student 1,740 PointsTyler Dotson
Courses Plus Student 1,740 Pointslike this