0

So I have got to this stage on my Battleships game where i have created an array and filled a 10x10 grid I now want to make it so that the user can input co-ordinates x,y,z and the grid update to S instead of O, what is the best way of going around this im very new to java.

GRID

public class Grid1 {
    public void BattleshipsGrid() {

        System.out.println ("Players Board");


        char [][] grid = new char [10][10];
        //FILL GRID//
        for(int outerLoopValue = 0; outerLoopValue<10;outerLoopValue++)
        {


            for(int innerLoopValue = 0; innerLoopValue<10;innerLoopValue++)
            {
                grid[outerLoopValue][innerLoopValue]='O';
            }

        }
        //END OF FILL GRID//
        //DRAW GRID//
        for(int outerLoopValue = 0; outerLoopValue<10;outerLoopValue++)
        {

            System.out.println("");
            for(int innerLoopValue = 0; innerLoopValue<10;innerLoopValue++)
            {
                System.out.print(grid[outerLoopValue][innerLoopValue]+"  ");
            }
        }
    }
}

MAIN GAME

public class Game {
    public static void main (String args[]) {

        //Calling Player grid
        Grid1 CPUGrid = new Grid1();
        Grid1 PGrid = new Grid1();

        System.out.println("Welcome to Battleships");
        System.out.println("Please choose the co-ordinates for your ships");
        System.out.println("");
        System.out.println("");
        CPUGrid.BattleshipsGrid();
    }
}
Matt
  • 3,052
  • 1
  • 17
  • 30
Chop
  • 5
  • 6

2 Answers2

1

You would have a much easier time if you made your grid into an instance variable. Then you can edit it directly from Game. Have a look at this question for more information about instance variables.

As a note, while public instance variables can be okay for learning the mechanics of Java, they aren't practical for larger projects; more information here.

public class Grid1 {
    public char [][] grid = new char [10][10];
    public Grid1() {
        //initialize grid
        for(int outerLoopValue = 0; outerLoopValue<10;outerLoopValue++)
        {
            for(int innerLoopValue = 0; innerLoopValue<10;innerLoopValue++)
            {
                grid[outerLoopValue][innerLoopValue]='O';
            }
        }
    }
    public void PrintGrid() {
        for(int outerLoopValue = 0; outerLoopValue<10;outerLoopValue++)
        {

            System.out.println("");
            for(int innerLoopValue = 0; innerLoopValue<10;innerLoopValue++)
            {
                System.out.print(grid[outerLoopValue][innerLoopValue]+"  ");
            }
        }
    }
}

public class Game {
    public static void main (String args[]) {

        //Calling Player grid
        Grid1 CPUGrid = new Grid1();
        Grid1 PGrid = new Grid1();

        System.out.println("Welcome to Battleships");
        System.out.println("Please choose the co-ordinates for your ships");


        Scanner s = new Scanner(System.in);
        System.out.println("X coord: ");
        int x = System.out.println(s.nextInt());
        System.out.println("Y coord: ");
        int y = System.out.println(s.nextInt());
        PGrid.grid[x][y] = 'S'
    }
}
Athena
  • 3,200
  • 3
  • 27
  • 35
0

I believe you are only asking for coordinates x,y, as you only have a 10x10 grid.

Continuing, on that assumption.

Take input from the user, then change O to S as follows

System.out.println("Please enter the X location [1-10]");
int x = Integer.parseInt(System.console().readLine()) - 1;

System.out.println("Please enter the Y location [1-10]");
int y = Integer.parseInt(System.console().readLine()) - 1;

//Assuming the user enters an integer only.
//You may want to implement a check to ensure this

grid[x][y] = 'S';
Jesse Fogel
  • 75
  • 2
  • 10
  • 1
    Hi thankyou for your answer, sorry to trouble you but when this is put into my code to test i am given this error 'The method writeLine(String) is undefined for the type Console' – Chop Nov 20 '17 at 19:37