Reputation: 35
So before i go any further in coding this, I want tot make sure it prints out properly. Unfortunately I can not seem to find the code to print out the entire array on the screen. In my GameBoard class, mines[][] and tileCount[][] should have some sort of value in each apot in the array, so they should be able to output these values correct? In my main method i have a few different ways I have tried, and the only one close deeptoString but that only outputted nulls (so my thoughts are that the array is still not being initialized?) Im looking for and out out of just char as a matrice (user input can change it to whatever though) and if mines[][] was called to out put the values as a matrice as well. Overall, i am looking for a way to output the 2d array onto the screen.
import java.util.Random;
import java.util.Scanner;
public class GameBoard
{
private int[][] mines;
private char[][] tileCount;
private int Row, Column;
Random random = new Random();
Scanner input = new Scanner(System.in);
public GameBoard(int Row, int Column)
{
Row = Row;
Column = Column;
mines = new int[Row][Column];
tileCount = new char[Row][Column];
startMines();
randomMines();
fillTips();
startBoard();
}
public void startMines(){
for(int i=0 ; i<mines.length ; i++)
for(int j=0 ; j<mines[0].length ; j++)
mines[i][j]=0;
}
public void randomMines()
{
boolean raffled;
int x = Row * Column;
int tempRow, tempColumn;
for(int i=0 ; i<x ; i++)
{
tempRow = (int)(Math.random() + 1.0);
tempColumn = (int)(Math.random() + 1.0);
if(mines[tempRow][tempColumn] == 0)
{
mines[tempRow][tempColumn] = 11;
}
}
}
public void fillTips(){
for(int i=1 ; i < Row ; i++)
for(int j=1 ; j < Column ; j++){
for(int x=-1 ; x<=1 ; x++)
for(int y=-1 ; y<=1 ; y++)
if(mines[i][j] != -1)
if(mines[i+x][j+y] == -1)
mines[i][j]++;
}
}
public void startBoard(){
for(int i=1 ; i<mines.length ; i++)
for(int j=1 ; j<mines.length ; j++)
tileCount[i][j]= '_';
}
public String toString()
{
System.out.println("\n Row");
System.out.print("Row: " + Row + "\nColumn: " + Column);
for(int i = 1 ; i < Row ; i++){
System.out.print(" "+Row + " ");
for(int j = 1 ; j < Column ; j++){
return " "+ tileCount[i][j];
}
System.out.println();
}
return "\n 1 2 3 4 5 6 7 8\n Columns";
}
}
import java.util.Scanner;
import java.util.Arrays;
public class GameClient {
int grid;
public static void main(String args[]) {
System.out.println("Welcome to the game minesweeper, I hope you enjoy your stay.");
System.out.println("We will begin by asking how large you would like the game.");
System.out.println("---------------------------------------------------------------");
System.out.println("Input two values please: ");
Scanner grid = new Scanner(System.in);
int grid1 = grid.nextInt();
int grid2 = grid.nextInt();
double mineCount = ((grid1*grid2)*.25);
System.out.println("Enter a number of mines, please 25% less than the total tiles which is " + mineCount);
Scanner mineCounts = new Scanner(System.in);
mineCount = mineCounts.nextInt();
GameBoard[][] tileSize = new GameBoard[grid1][grid2];
tileSize[0][0] = new GameBoard(grid1, grid2);
System.out.println(tileSize.toString());
System.out.println(tileSize[0][0]);
System.out.println(Arrays.deepToString(tileSize));
}
}
Upvotes: 0
Views: 58
Reputation: 1879
Because exception occurred when tileSize[0][0] is initialized.
see my log:
Welcome to the game minesweeper, I hope you enjoy your stay.
We will begin by asking how large you would like the game.
---------------------------------------------------------------
Input two values please:
100
10
Enter a number of mines, please 25% less than the total tiles which is 250.0
3
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at GameBoard.startBoard(GameBoard.java:67)
at GameBoard.<init>(GameBoard.java:21)
at GameClient.main(GameClient.java:19)
There is a bug in your code:
public GameBoard(int Row, int Column)
{
Row = Row;
Column = Column;
.......
}
Which should be:
public GameBoard(int Row, int Column)
{
this.Row = Row;
this.Column = Column;
.....
}
Another bug in method fillTips
:
when i has a number (Row - 1) at the last loop,mines[i+x] maybe mines[Row -1 + 1] which is mines[Row] and an exception(java.lang.ArrayIndexOutOfBoundsException
) will be thrown.
Another bug...:
for(int j = 1 ; j < Column ; j++){
return " "+ tileCount[i][j];
}
should be:
for(int j = 1 ; j < Column ; j++){
System.out.print( " "+ tileCount[i][j]);
}
Upvotes: 1