Izabella Wnuk
Izabella Wnuk

Reputation: 19

1024 Java Game: adding a number to a random tile

I'm a beginner at java trying to implement the code for the 1024 game for a college assignment. I'm really stuck on trying to add the number 1 to two random cells in the grid. The game is a little bit different. Instead of printing a grid 4x4, the grid is printed based on the user's preference. My code below, adds the number 1 to every tile because they are all empty but how do I add the number just to 2 random tiles?

Every subsequent turn a ‘1’ tile is added to an unoccupied cell in the grid, for this reason at the end of every turn I need to make a list of all free cells and randomly select a free cell using that list.

How do I do this? In my code, I tried to count every empty cell but it doesn't work for adding '1' to 2 random tiles.

int count = 0;

for (int i = 0; i < board.length; i++) {

  for (int j = 0; j < board.length; j++) {

    if (board[i][j] == 0) {
      ++count;
    }

    if (count == 0) {
      return;
    }
    
    int location = r.nextInt(count);
    int newTile = 1;
    board[i][j] = newTile;
  }
}

Upvotes: 0

Views: 428

Answers (1)

lime
lime

Reputation: 809

Try the following:

public class Main
{
    public static void main(String[] args) {
        //Create random genertor
        Random randomGenerator = new Random();
        Scanner input = new Scanner(System.in); 
        
        //Get user input
        System.out.println("Enter a legth");
        int length = input.nextInt();
        System.out.println("Enter a width");
        int width = input.nextInt();
        
        //Initialize board (4x4)
        int[][] board = new int[length][width];
        
        //Segment to generate two different points on the board
        int randomCoordinateXOne = randomGenerator.nextInt(length);
        int randomCoordinateYOne = randomGenerator.nextInt(width);
        int randomCoordinateXTwo = randomGenerator.nextInt(length);
        int randomCoordinateYTwo = randomGenerator.nextInt(width);
        while (randomCoordinateXOne == randomCoordinateXTwo && randomCoordinateXTwo == randomCoordinateYTwo) {
          randomCoordinateXTwo = randomGenerator.nextInt(length);
          randomCoordinateYTwo = randomGenerator.nextInt(width);
        }
        
        //Iterate through the board
        for (int i = 0; i < length; i++) {
          for (int j = 0; j < width; j++) {
            //If the current space is one of the two selected above, put a 1 in that spot
            if ((i == randomCoordinateXOne && j == randomCoordinateYOne) || (i == randomCoordinateXTwo && j == randomCoordinateYTwo)) {
              board[i][j] = 1;
            }
            //Otherwise, put a 0
            else {
              board[i][j] = 0;
            }
            //Used to visualize result
            System.out.print(board[i][j]);
          }
          //Used to visualize result
          System.out.println();
        }
    }
}

Make sure you include the following at the top of your java file:

import java.util.Scanner;
import java.util.Random;

Let me know if this helps. Please mark the answer correct if so. Comments are in the code, but if you have any questions, let me know!

Upvotes: 1

Related Questions