I want to code some basic stuff for a little minesweeper-game, that we do in university. Now I have the problem that my code .
public class Minesweeper1 {
public static int[][] makeRandomBoard(int s, int z, int n){
//creating the field and fill with 0
int feld[][] = new int [s][z];
for(int i = 0; i < s; i++){
for(int j = 0; j < z; j++){
feld[i][j] = 0;
}
}
//n-times to fill the field
for( int i = 0; i < n; i++){
selectRandomPosition(s, z);
//want to get them from selectRandomPosition
feld[randomHeight][randomWidth] = 1;
}
}
}
So it starts the selectRandomPosition code:
public static int[] selectRandomPosition(int maxWidth, int maxHeight) {
int randomHeight = StdRandom.uniform(0, maxHeight);
int randomWidth = StdRandom.uniform(0, maxWidth);
return new int[]{randomHeight, randomWidth};
}
Here I'm not allowed to change anything, but it returns a new array. Now is my question how can I use the new array in my makeRandomBoard method, since I do not know any name of the array. When I use feld[randomHeight][randomWidth] = 1;, it says that it doesn't know these variables.
how I can use the new array in my makeRandomBoard method, since I do not know any name of the array?
Call the method, and assign its return value to a variable. Now you have a name for the array:
// Make a call
int[] randomArray = selectRandomPosition(maxW, maxH);
// Access the width
int randomW = randomArray[0];
// Access the height
int randomH = randomArray[1];
Related
I am making a grid with the amounts determined by a scanner. I keep getting an error and I am not sure why. Here is the code for the grid that I am trying to make, I will also include the object and class for the maze/grid below.
public static void mazeSetup() {
System.out.println("How many rows and columns do you want? I would\n"
+ "suggest 10 minimum and 20 maximum.");
boolean mazeselect = true;
while(mazeselect) {
maze.columns = sc.nextInt();
maze.rows = maze.columns;
if (maze.rows > 30 || maze.rows < 10) {
System.out.println("Make sure that you make it within 10-30 rows.");
} else {
mazeselect = false;
}
}
mazeBuild();
}
public static void mazeBuild() {
for(int x = 0; x < maze.rows; x++) {
for(int y = 0; y < maze.columns; y++) {
maze.maze[x][y]= ".";
System.out.print(maze.maze[x][y]);
}
System.out.println();
}
characterPlacement();
}
I also have the object here:
static Maze maze = new Maze(null,0,0,0,0);
and the class with construtors for the maze/grid.
public class Maze {
String maze[][];
int rows;
int columns;
int xStart;
int yStart;
public Maze(String xMaze[][], int xRows, int xColumns, int xxStart, int xyStart) {
maze = xMaze;
rows = xRows;
columns = xColumns;
xStart = xxStart;
yStart = xyStart;
}
public String[][] maze() {
return maze;
}
public int rows() {
return rows;
}
public int columns() {
return columns;
}
public int xStart() {
return xStart;
}
public int yStart() {
return yStart;
}
}
Any help would be greatly appreciated. Thanks a lot! :D
Note: No errors occur until ran in console.
your String maze[][] is null because of this:
static Maze maze = new Maze(null,0,0,0,0); // notice that null
And you're trying to put values in it upon calling mazeBuild(). You should initialize it or pass an array instead of null. You can do this at the start of mazeBuild()
public static void mazeBuild() {
maze.maze = new String[maze.rows][maze.columns]; // <-- this one!
for(int x = 0; x < maze.rows; x++) { // <-- this loop tries to
for(int y = 0; y < maze.columns; y++) { // put values in your
maze.maze[x][y]= "."; // maze.maze (2D String array)
System.out.print(maze.maze[x][y]);
}
System.out.println();
}
You can also do this in exchange to the line of code I've added.
String[][] mazeArray = new String[maze.rows][maze.columns];
maze = new Maze(mazeArray, maze.rows, maze.columns, 0, 0);
I have the same problem as some guy who asked this.
I want to code some basic stuff for a little minesweeper game.
Now I have the problem that my code
public class Minesweeper1 {
public static int[][] makeRandomBoard(int s, int z, int n){
int feld[][] = new int [s][z];
for(int i = 0; i < s; i++){
for(int j = 0; j < z; j++){
feld[i][j] = 0;
}
}
for(int i = 0; i < n; i++){
selectRandomPosition(s, z);
feld[randomHeight][randomWidth] = 1;
}
}
so it starts the selectRandomPosition code:
public static int[] selectRandomPosition(int maxWidth, int maxHeight) {
int randomHeight = StdRandom.uniform(0, maxHeight);
int randomWidth = StdRandom.uniform(0, maxWidth);
return new int[]{randomHeight, randomWidth};
}
Here I am not allowed to change anything, but it returns a new array. Now my question is how can I use the new array in makeRandomBoard?, since I do not know any name of the array. When I use feld[randomHeight][randomWidth] = 1; it says that it does not know these variables.
And he did get the answer:
Call the method, and assign its return value to a variable. Now you have a name for the array:
// Make a call
int[] randomArray = selectRandomPosition(maxW, maxH);
// Access the width
int randomW = randomArray[0];
// Access the height
int randomH = randomArray[1];
but what do I have to write now?
I tried it with:
feld[randomW][randomH] = 1;
but it seems like it doesn't work. I also need a return statement.
Can anyone help me?
I believe this is the solution you are looking for:
Solution
public class Minesweeper1 {
// Note:
// s == Height
// z == Width
public static int[][] makeRandomBoard(int s, int z, int n) {
// Note: Misspelling of "field"?
int feld[][] = new int [s][z];
// Initialize the game board with no mines (value of 0)
for(int i = 0; i < s; i++) {
for(int j = 0; j < z; j++) {
feld[i][j] = 0;
}
}
// Iterate through n times to place "mines" (value of 1)
for(int i = 0; i < n; i++){
// selectRandomPosition returns an array of length 2
// the first index (0) = randomHeight
// the second index (1) = randomWidth
// Notice that z and s is flipped
// because the first parameter is for width, which is z
// and the second parameter is for height, which is s
int[] position = selectRandomPosition(z, s);
int positionX = position[1];
int positionY = position[0];
// The order of positionY/positionX is key!
// If it's in the wrong order you will get an
// IndexOutOfBoundsException!
feld[positionY][positionX] = 1;
}
// Return the newly created array
return feld;
}
public static int[] selectRandomPosition(int maxWidth, int maxHeight) {
int randomHeight = StdRandom.uniform(0, maxHeight);
int randomWidth = StdRandom.uniform(0, maxWidth);
// Notice that this is returning a fixed array of two elements
// the first being the Y component, and the second being the X
// component.
return new int[]{randomHeight, randomWidth};
}
}
Problems
Passing the wrong values
You were calling selectRandomPosition wrong. Remember that s is the Height, and z is the Width. So you were passing into selectRandomPosition the Height for the first parameter, and the Width for the second parameter. Look at the method declaration:
public static int[] selectRandomPosition(int maxWidth, int maxHeight)
That meant you passed the Height into the maxWidth, and the Width into the maxHeight. My solution flips it for you. This can be confusing because s and z don't really give you hints to what they are (Height and Width) - consider renaming these variables to help you.
Accessing in the wrong order
You have in your question:
feld[randomW][randomH] = 1;
This is wrong, it should be:
feld[randomH][randomW] = 1;
This image shows a visual representaion of what a two dimensional array looks like:
So the access would be:
feld[0][0] == 1
feld[0][1] == 1
feld[0][2] == 1
feld[1][0] == 1
feld[1][1] == 2
feld[1][2] == 4
feld[2][0] == 1
feld[2][1] == 3
feld[2][2] == 9
Nit-Pick: Misspellings
Other than having weird variable names like s and z that do not help you at all in remembering what means what, your main variable that you will eventually return is misspelled. Did you mean "field"?
Variable naming is important and can help you, and other readers, to easily understand your code. Also, comments in places where you think you need them also help!
I am calling one array and returning another array that is sorted based on the other array.
I think my method is proper but when I try to print out from the new array in the main method i'm getting an error that reads,
"Cannot resolve B to a variable."
B is the name of the new array that my method should return.
Thanks in advance.
package sort;
public class SortByFinal {
public static int [][] sortByFinal(int n, int [][] A)
{
int [][]B = new int [n][3];
int max = 0;
for(int i =0; i<n; i++)
{
for(int j = i; j<n; j++)
{
max = Math.max(A[i][1], max);
}
for(int k = i; k<n; k++)
{
if(A[k][1] == max)
{
B[k][0] = A[k][0];
B[k][1] = A[k][1];
B[k][2] = A[k][2];
}
}
}
return B;
}
public static void main(String []args)
{
int num = 5;
int[][] now = new int [num][3];
now[0][0] = 2342;
now[0][1] = 88;
now[0][2] = 98;
now[1][0] = 3901;
now[1][1] = 75;
now[1][2] = 71;
now[2][0] = 1444;
now[2][1] = 60;
now[2][2] = 85;
now[3][0] = 5327;
now[3][1] = 95;
now[3][2] = 80;
now[4][0] = 4888;
now[4][1] = 83;
now[4][2] = 100;
//int [][] B = new int[num][3];
sortByFinal(num, now);
for(int i = 0; i<num; i++)
{
for(int j = 0; j<3; j++)
{
System.out.print(B[i][j]);//this gives me the "B cannot be resolved to a variable."
System.out.print(" ");
}
System.out.println();
}
}
}
B is defined in sortByFinal(), but not in main(). To fix this, you could change your main to the follow:
int[][] B = sortByFinal(num, now);
Since sortByFinal(num, now) returns a int[][], this will work.
In your code, you're calling the function, but not doing anything with the return value, and you do not have access to the local variables of other methods; hence, the message.
My code compiles fine, but when I run it I get the following error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Sudoku.(Sudoku.java:20) at Test.main(Test.java:7)
The code is as follows:
Main Test class:
public class Test {
public static void main(String[] args) {
Sudoku puzzle;
int[][] entries = {{1,0,0,0},{0,0,0,3},{0,0,4,0},{0,4,0,0}};
puzzle = new Sudoku(2,2,entries);
boolean somethingChanged=true;
while(somethingChanged) {
somethingChanged=false;
System.out.println(puzzle);
for (int i=0; i<puzzle.size; i++)
for(int j=0; j<puzzle.size; j++)
if(puzzle.oneOption(i, j)!=puzzle.EMPTY) {
// exactly one value can be filled in at location i,j
// do this now, and record that something has changed
// compared to the previous iteration of the while loop
puzzle.setValue(i,j,puzzle.oneOption(i,j));
somethingChanged=true;
}
}
// if oneOption is implemented correctly, the puzzle is now solved!
System.out.println(puzzle);
}
}
and my uncommented Sudoku Class:
class Sudoku {
int cellHeight, cellWidth, size,EMPTY = 0;
int [][] sudGrid = new int[cellHeight * cellWidth][cellHeight * cellWidth];
int [][] cellGrid = new int [cellHeight][cellWidth];
public Sudoku(int a, int b){
cellHeight = a;
cellWidth = b;
size = cellHeight*cellWidth;
}
public Sudoku(int a, int b, int array[][]){
cellHeight = a;
cellWidth = b;
size = cellHeight*cellWidth;
for(int i = 0; i<size; i++){
for(int j = 0; j<size; j++){
int temp = array[i][j];
sudGrid[i][j] = temp;
}
}
}
public Sudoku(){
cellHeight = 3;
cellWidth = 3;
size = cellHeight*cellWidth;
for(int i = 0; i<size; i++)
for(int j = 0; j<size; j++)
sudGrid[i][j] = 0;
}
public void setValue(int r,int c, int v){
sudGrid[r][c] = v;
}
public int getValue(int r, int c){
return sudGrid[r][c];
}
public void clear(int r, int c){
sudGrid[r][c] = EMPTY;
}
public String toString(){
String message = "";
for(int i = 0; i<size; i++){
for(int j = 0; j<size; j++){
message = message + sudGrid[i][j];
if (j == cellWidth){
message = message + " ";
}
}
message = message + "\n";
}
return message;
}
public int oneOption(int r, int c){
return 1;
}
}
Sorry, I know this is a lot of code to splat on the screen, but I haven't done a lot on arrays, let alone two dimensional. I know my oneOption() method at the moment does nothing, I just needed it to compile, but where the errors are, is
int temp = array[i][j];
sudGrid[i][j] = temp;
and
int[][] entries = {{1,0,0,0},{0,0,0,3},{0,0,4,0},{0,4,0,0}};
puzzle = new Sudoku(2,2,entries);
Now I assumed where the entries array is declared is correct as this is code that my lecturer has set up for us, and we are only designing the Sudoku class. and I was trying to make the values of the entries array, go into the sudGrid array, I assumed I did it correctly, but im getting the exception error,
any ideas?
Your subgrid is defined as a zero length two dimensional array, so you can't fit anything in there.
See here:
int cellHeight, cellWidth, size,EMPTY = 0;
int [][] sudGrid = new int[cellHeight * cellWidth][cellHeight * cellWidth];
cellHeight and cellWidth are initialized to zero by default, then you create a new int[0*0][0*0] array.
Change your constructor to the following:
public Sudoku(int a, int b, int array[][]){
cellHeight = a; // Changing these two instance variables will *NOT*
cellWidth = b; // retrofit your subGrid to a new size.
size = cellHeight*cellWidth;
subGrid = new int[size][size];
for(int i = 0; i<size; i++){
for(int j = 0; j<size; j++){
int temp = array[i][j];
sudGrid[i][j] = temp;
}
}
}
You're setting your Sudoku.size to be the entire number of cells in the puzzle, and then trying to read that many rows and columns. You need to iterate just to the number of columns and rows.
I know this question was answered before, but I cant still handle it with my code.
Please can someone pointing out how can I fix it on this particular code. Id like to call trace(); but dont know where to call new trace. I tried different stuff from here but it does not work for me. Thank you!
package matr;
import java.util.Scanner;
final public class Matrix {
private final int M;
private final int N;
private double[][] data;
public Matrix(int M, int N) {
this.M = M;
this.N = N;
data = new double[M][N];
}
public Matrix(double[][] data) {
M = data.length;
N = data[0].length;
this.data = new double[M][N];
for (int i = 0; i < M; i++)
for (int j = 0; j < N; j++)
this.data[i][j] = data[i][j];
}
private Matrix(Matrix A) {
this(A.data);
}
public static Matrix random(int M, int N, int r) {
Matrix A = new Matrix(M, N);
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
A.data[i][j] = (Math.random() * r);
}
}
return A;
}
public double trace() {
// trace a = new trace();
double t = 0;
for (int i = 0; i < Math.min(M, N); i++) {
t += data[i][i];
}
return t;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("rows: ");
try {
int x = Math.abs(scan.nextInt());
System.out.println("columns: ");
int y = Math.abs(scan.nextInt());
System.out
.println("generate: ");
int r = scan.nextInt();
Matrix A = Matrix.random(x, y, r);
System.out.println("random A");
trace();
} catch (java.util.InputMismatchException e) {
System.out.println("Please enter a valid int");
}
}
}
call A.trace()
use lower-case names for variables and fields
your main method is static. and your trace() method is a non-static method of the Matrix class. That means you have to call trace() on an instance of Matrix.
You are trying to call a non-static method trace() from a static method main(). Since 'main' is static it can only refer to static variables and static methods within the class. You will need to use an instance of Matrix to call trace. for example:
Matrix A = Matrix.random(x,y,r);
A.trace();
You need to call the trace() method on an instance of the type Matrix.
You could do this simply by:
A.trace();