So what I have so far is a randomized board containing numbers 1, 2 and 3. The board is 6 * 10. (So 6 Columns and 10 rows). The plan is when the user gives an input, a new row of randomized numbers should be added to the board. The thing is that the 2D array will be 6 * 10 but only half of the board is going to be filled with randomized numbers and the other half will be empty. But if the user gives an input, the 2D array will be filled with one row, replacing an empty row with a new row.
So this is the original board:
3 1 1 2 2 2
3 3 3 3 2 3
1 1 1 3 3 2
2 3 2 3 1 2
3 3 1 2 2 2
What column do you want to shoot at?
and after the user gives the input (when the user shoots) the board should look like this, with a new row of randomized numbers as I mentioned before.
3 1 1 2 2 2
3 3 3 3 2 3
1 1 1 3 3 2
2 3 2 3 1 2
3 3 1 2 2 2
2 3 3 1 1 1
What column do you want to shoot at?
that is how it should go until the board reaches 10 rows.
My two main problems are how to make an array that is half empty and half full and then how to fill the empty half with new rows. Here's the code I have so far:
import java.util.Scanner;
public class Project {
public static void main( String[] args) {
Scanner scan = new Scanner(System.in);
int [][] grid = createBoard(6, /*Number of colums*/ 5, /*Number of rows*/ 3 /*Bubble types in the board (1, 2 or 3)*/);
printGrid( grid );
System.out.println("What column do you want to shoot at?");
shoot();
}
public static int[][] createBoard(int cols, int rows, int numBubbleTypes) {
int[][] board = new int[rows][cols];
for( int row = 0; row < rows; row ) {
for( int col = 0; col < cols; col ) {
board[row][col] = (int)(Math.random() * (double)numBubbleTypes) 1;
}
}
return board;
}
public static void printGrid(int[][] grid) {
for( int row = 0; row < grid.length; row ) {
for( int col = 0; col < grid[row].length; col ) {
System.out.print( grid[row][col] " " );
}
System.out.println("");
}
}
public static void fillBoard(int[] grid) {
}
public static void shoot() {
Scanner scanShoot = new Scanner(System.in);
String input = scanShoot.nextLine();
}
private static void clearConsole() {
System.out.print('\u000C'); //Clear terminal
}
}
CodePudding user response:
You can fill the "empty" part of your array with 0
s and then at the printGrid()
part check if there is a 0
in the grid if(grid[row][col] == 0)
and if there is one break
.
For the part where you want to fill in the new row just do the same. Check until you reach a row which has 0
s in it and overwrite it.