'Modeling a 4x4 Grid for Simple Game Using Looped ArrayLists in Java
I am meant to create a game in Java that specifically uses looping Arraylists to create a 4x4 grid. I have a Grid class, made up of an ArrayList of 4 Rows, a Row Class made up of an ArrayList of Squares, and Squares Class. As well as other classes of objects, such as a hero and various enemies that will move to a random adjacent spot on the grid or "board" on each new turn.
I have methods in place to randomly assign starting positions, to find all possible adjacent positions and then select a random position out of those possible positions, etc.
The end product should look similar to this:
Is a GUI necessary or is there a much more primitive, simple way to model to the user the 'Grid' with the objects in their current containers?
Solution 1:[1]
You can use System.out.println("...") to print out the info to the terminal. You can just put everything in a String and print it out. You'll be able to see each time you make a move and see previous moves and add in more verbose info if you want. Assuming your grid is in an Arraylist grid, the following code should help you see how it can work.
for (Row row: grid){
String str = "";
for(Square square:row){
str += square;
}
System.out.println(str);
}
You can use the Java Scanner to grab input from the user instead of buttons.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

