0

I am creating a text game in Java that uses a 2D array for a board. I want to make this so when I enter the "scan" command it will print the same 8x8 board each time. The program compiled fine I just need to know how to print the same 8x8 board I generate each time I run the "scan" command.

import java.util.*;
import java.math.*;
import java.util.Random;
import java.util.Scanner;
import java.text.*;
import java.io.*;
public class Main {
    static Scanner in = new Scanner(System.in);
    
        public static void Start() {
        System.out.println("Would you like a Short, Medium, or Long game?");
        String x = in.nextLine();
        System.out.println("Are you a Novice, Fair, Good, Expert, or Emeritus player?");
        String y = in.nextLine();
        System.out.println("Good Luck!");
        System.out.println("What would you like to do?");
        }
        
       public static void main() {
                //create the grid
       String x;
       String y;
       String z;

       z = in.nextLine();
        if (z.equals("scan")) {
        final int rowWidth = 8;
        final int colHeight = 8;
        Random rand = new Random();
        boolean a = true;
        boolean b = true;
        boolean c = true;
        boolean d = true;
        String [][] board = new String[rowWidth][colHeight];
        
        //fill the grid
        for (char row = 0; row < board.length; row++) {
            
            for (char col = 0; col < board[row].length; col++) {
                double r = Math.random();

                
                
                if(a == true && r <= .02) {
                  board[row][col] = "P";
                  a = false; }
                

                 else if(b == true && r <= .04 && r > .02){
                  board[row][col] = "K"; 
                  b = false; }
                

                else if(c == true && r <= .06 && r > .04){
                  board[row][col] = "B";  
                  c = false; }
                  
                 else if(d == true && r <= .08 && r > .06){
                  board[row][col] = " ";  
                  d = false; }
                  
                  
                  else 
                    board[row][col] = "*";
                

                }
            }
       //display output
        for(int i = 0; i < board.length; i++) {

            for(int j = 0; j < board[i].length; j++) {
                    
                System.out.print(board[i][j] + " ");
                //System.out.println();
            }
            System.out.println();
        }
        System.out.println("");
        z = in.nextLine();
        main();
    }else if( z.equals("exit")) {
    System.exit(0);
}else {
           System.out.println("Parton");
            main();
       }   

        }
        
        public static void run() {
            Start();
            main();
        }
        
    }//end of main

//end of class Main

2
  • 1) I don't see a scan command? Commented Apr 18, 2016 at 13:50
  • @blahfunk after I declare the three strings I get input. If that input equals "scan" then the 8x8 is generated. Commented Apr 18, 2016 at 13:53

1 Answer 1

1

If this is a simple program, you can make simple changes to do this. Just widen the scope of your array to the class level and then create a scan method.

public class Main {
    static Scanner in = new Scanner(System.in);
    String[][] board;

    ...//Your other code

    public static scan() {
        //Printing/Drawing logic
    }
}

Then you can just add a select statement for "scan" to your current input check, which would run the method.

However, when it comes to designs like this, I would prefer to have some kind of Board class backed by the 2D array which could contain many useful functions for the program, including Board.scan(), and perhaps even a helper class to resolve input strings.

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

Comments

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.