0

I'm building out a user defined array as a game board. The characters used "O" and "." have to be randomized and the "O" has to appear more than once.

This is what I have thus far.

import java.util.Scanner;


public class PacMan {

    public static void main(String[] args) 
    {


        Scanner input = new Scanner(System.in);
        System.out.println("Input total rows:");
        int row = input.nextInt();
        System.out.println("Input total columns:");
        int column = input.nextInt();



        boolean[][] cookies = new boolean[row+2][column+2];
        for (int i = 1; i <= row; i++)
            for (int j = 1; j <= column; j++);
                cookies [row][column] = (Math.random() < 100);

        // print game
        for (int i = 1; i <= row; i++) 
        {
            for (int j = 1; j <= column; j++)
                if (cookies[i][j]) System.out.print(" O ");
                else             System.out.print(". ");
            System.out.println();
        }
    }
}

The output, for example, produces a 5 x 5 grid, but the "O" only appears once and is at the bottom right of the grid.

Assistance randomizing the "O" and "." and having the "O" appear in random fashion throughout the board which is initialized by the user input via Scanner.

Here is the updated code which is producing the output that I'm looking for and is user defined.

import java.util.*;
public class PacManTest
{
    public static void main(String[] args)
    {
        char O;
        Scanner input = new Scanner(System.in);
        System.out.println("Input total rows:");
        int row = input.nextInt();
        System.out.println("Input total columns:");
        int column = input.nextInt();

        char board[][] = new char[row][column];

        for(int x = 0; x < board.length; x++)
        {
            for(int i = 0; i < board.length; i++)
            {
                double random = Math.random();
                if(random >.01 && random <=.10)
                {
                    board[x][i] = 'O';
                }

                else {
                    board[x][i] = '.';
                }
                System.out.print(board[x][i] + " ");
            }
            System.out.println("");
        }
    }
}
1

1 Answer 1

2

The main issue is the typo in the first loop:

cookies [row][column] = (Math.random() < 100);

should be

cookies [i][j] = (Math.random() < 100);

Second, Math.random() returns a value greater than or equal to 0.0 and less than 1.0 (doc). So, (Math.random() < 100); will always be true. If you want a 50% chance of an O or . use:

cookies[i][j] = Math.random() < 0.5;

Also, not sure what your motivation is for using a starting index of 1 but array indexes start at 0.

Sign up to request clarification or add additional context in comments.

3 Comments

Johnny thanks. So I've actually managed to make implementations to create what I'm looking for.
Now that I've constructed the user defined game board with randomized "O" characters which act as cookies for the player defined as "<" to move across the board to "eat" those cookies. I need to figure out how to get the character on the board, initially open face facing left ">" and has the ability to move across the board using user defined movements. This will create symbols ">", "V", "<", and "^" depending on the user move.
@JoshuaPeterson Create a class for the player that stores its current row, column, direction. Then you need a loop to read the keyboard and move the player based on user input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.