Java overwrite 2D array with the next step [duplicate] - java

This question already has answers here:
How to clear the console?
(14 answers)
Closed 1 year ago.
The following code is Conway's Game of Life in a terminal. What I would like to do here is to overwrite the current step into the next step. I have a temporary 2D array in the animateChanges() method. Also in the main method I am printing the animateChanges() three times, that's what I would like to change. Basically, I would like to overwrite the board instead of printing it three times because visually its quite ugly while looping.
public class GameOfLifeTerminal {
private int WIDTH;
private int HEIGHT;
private int[][] board;
private int aliveNeighbours = 0;
public GameOfLifeTerminal(int width, int height) {
this.WIDTH = width;
this.HEIGHT = height;
this.board = new int[width][height];
}
/*Getters & Setters*/
public void setAlive(int x, int y) {
this.board[x][y] = 1;
} //true
public void setDead(int x, int y) {
this.board[x][y] = 0;
} // false, currently not being used
//get the rows in the y axis
public int getRows(){ return board.length; }
//get the columns in the x axis
public int getColumns(){ return board[0].length; }
/*Methods and functions*/
public void printBoard() { //prints the board
for (int y = 0; y < HEIGHT; y++) {
String line = "";
for (int x = 0; x < WIDTH; x++) {
if (this.board[x][y] == 0) {
line += "ยท";
} else {
line += "O";
}
}
System.out.println(line);
}
System.out.println();
}
public int countAliveNeighbours(int x, int y) { //count the number of alive neighbours
int count = 0;
/*
for(int i = x-1; i < x+1; i++) {
for (int j = y - 1; i < y + 1; j++) {
if (getState(i, j) == 1) count++;
}
}
if(getState(x, y) == 1) count--;
*/
//checks each position one by one and calls the getState() method to be inside the bounds
count += getState(x - 1, y - 1);
count += getState(x, y - 1);
count += getState(x + 1, y - 1);
count += getState(x - 1, y);
count += getState(x + 1, y);
count += getState(x - 1, y + 1);
count += getState(x, y + 1);
count += getState(x + 1, y + 1);
//System.out.println(count);
return count;
}
public int getState(int x, int y) { //get state neighbours in case is out of bounds
if (x < 0 || x >= WIDTH) return 0;
if (y < 0 || y >= HEIGHT) return 0;
return this.board[x][y];
}
public void animateChanges() { //applies the rules and then prints the board with current state of the cells
//temporary board
int[][] newBoard = new int[WIDTH][HEIGHT];
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
aliveNeighbours = countAliveNeighbours(x, y);
//rules
if (getState(x, y) == 1) { //if true
if (aliveNeighbours < 2) { // le ded
newBoard[x][y] = 0;
} else if (aliveNeighbours == 2 || aliveNeighbours == 3) { //alive
newBoard[x][y] = 1;
} else if (aliveNeighbours > 3) { // le ded
newBoard[x][y] = 0;
}
} else {
if (aliveNeighbours == 3) { //create new cells
newBoard[x][y] = 1;
}
}
}
}
this.board = newBoard;
//System.out.println("Alive cells " + aliveNeighbours);
printBoard();
}
//main method
public static void main(String[] args) {
GameOfLifeTerminal simulation = new GameOfLifeTerminal(8, 5);
simulation.setAlive(2, 2);
simulation.setAlive(3, 2);
simulation.setAlive(4, 2);
simulation.animateChanges();
simulation.animateChanges();
simulation.animateChanges();
}
}

You appear to already be "overwriting" the board inside your animateChanges() method with this call: this.board = newBoard; Why not simply remove the call to printBoard(); from the same animateChanges() method? Then you can call printBoard() when and how you want to do it.

Related

problems occured when solving mazes using DFS

I am doing an assignment which creates a maze and then solves it,
I simulate the maze as an array,
using 1 to represent the wall, 0 -- road, 5 -- start, 9 -- destination
if the road has been used, change its value to 2;
it works well for creating mazes,
In most cases, it can find the path to the destination, but sometimes it still pops error stating "empty stack error", like the picture shows...
[]
I can't find the problem(s), may you help me out?
package MazeDFS;
import java.util.Random;
import java.util.Stack;
public class maze {
public static void main(String[] args) {
//create maze
//and solve it
//define the size as 9*9
int r,c;
r = 9;
c = 9;
int[][] newMaze = mazeGenerator(r,c);
newMaze[1][1] = 5;
mazePrinter(newMaze,r,c);
Stack<Step> path = new Stack<Step>();
Step s0 = new Step(1,1);
path.push(s0);
long startTime = System.nanoTime();
newMaze = solve(newMaze, 1, 1, path);
long endTime = System.nanoTime();
System.out.println("Time length:" + (endTime - startTime));
}
public static int[][] mazeGenerator(int x, int y){
Random randNum = new Random();
/*I simulated the maze as a x*y array
Values of the array represent elements of the maze
0 -- road, 1 -- wall, 5 -- start, 9 -- destination
*/
int[][] maze = new int[x][y];
for(int i = 0; i < x; i++) {
for(int j = 0; j < y; j++) {
//bound of the maze
if(i==0 || j==0 || i==x-1 || j==y-1) {
maze[i][j] = 1;
}
//build walls
if(j%2 == 0) {
maze[i][j] = 1;
}
}
}
//randomly choose a row of every wall and make it a road
for(int i = 1; i < y-1; i++) {
int row = randNum.nextInt(x-2)+1;
maze[row][i] = 0;
}
maze[x-2][y-2] = 9;
return maze;
}
public static void mazePrinter(int[][] maze,int x, int y) {
for(int i=0; i<x; i++) {
for(int j=0; j<y; j++) {
System.out.print(maze[i][j]);
}
System.out.println();
}
}
public static int[][] solve(int[][] newMaze, int x, int y, Stack<Step> path) {
mazePrinter(newMaze, 9,9);
System.out.println();
if(newMaze[x][y] == 9) {
Step s = new Step(x,y);
newMaze[x][y] = 2;
path.push(s);
return newMaze;
}
//going down
int dx = 1;
int dy = 0;
if(newMaze[x+dx][y+dy] == 0 || newMaze[x+dx][y+dy] == 9) {
//if visited mark as 2
if(newMaze[x+dx][y+dy] != 9) {
newMaze[x+dx][y+dy] = 2;
}
Step ss = new Step(x+dx,y+dy);
path.push(ss);
System.out.println("going down");
return solve(newMaze, x+dx, y+dy, path);
}
//going up
dx = -1;
dy = 0;
if(newMaze[x+dx][y+dy] == 0 || newMaze[x+dx][y+dy] == 9) {
//if visited mark as 2
if(newMaze[x+dx][y+dy] != 9) {
newMaze[x+dx][y+dy] = 2;
}
Step ss = new Step(x+dx,y+dy);
path.push(ss);
System.out.println("going up");
return solve(newMaze, x+dx, y+dy, path);
}
//turn right
dx = 0;
dy = 1;
if(newMaze[x+dx][y+dy] == 0 || newMaze[x+dx][y+dy] == 9) {
//if visited mark as 2
if(newMaze[x+dx][y+dy] != 9) {
newMaze[x+dx][y+dy] = 2;
}
Step ss = new Step(x+dx,y+dy);
path.push(ss);
System.out.println("turn right");
return solve(newMaze, x+dx, y+dy, path);
}
//turn left
dx = 0;
dy = -1;
if(newMaze[x+dx][y+dy] == 0 || newMaze[x+dx][y+dy] == 9) {
//if visited mark as 2
if(newMaze[x+dx][y+dy] != 9) {
newMaze[x+dx][y+dy] = 2;
}
Step ss = new Step(x+dx,y+dy);
path.push(ss);
System.out.println("turn left");
return solve(newMaze, x+dx, y+dy, path);
}
//if their is no way to go then turn back to the last step
Step sss = path.pop();
int x1 = sss.getX();
int y1 = sss.getY();
System.out.println("return to:"+"x1:"+x +", y1:"+ y);
return solve(newMaze, x1, y1, path);
}
}
class Step{
private int x;
private int y;
Step(int x, int y){
this.x = x;
this.y = y;
}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
}

My recursive function for finding the number of paths in a grid runs infinitely

I'm working on a couple of Project Euler problems and want to test my solution. My recursive function never ends even with reachable base cases.
in a 20x20 grid I am using x and y coordinates to navigate up and left to find the number of paths from (19,19) to (0,0). My base case is to return 1 when we reach (0,0). Otherwise I add the current count to the recursive call.
Function:
private static int numPaths(int x, int y, int pathsFound)
{
if(x == 0 && y == 0)
return 1;
else
{
if(x > 0)
{
pathsFound += numPaths(x - 1, y, pathsFound);
}
if(y > 0)
{
pathsFound += numPaths(x, y - 1, pathsFound);
}
}
return pathsFound;
}
Main:
int x = 19;
int y = 19;
System.out.println("Answer: " + numPaths(x, y, 0));
Is there a flaw in my recursive logic, or is just taking a very long time to compute? If you know the solution to this Euler problem, please do not post it.
https://projecteuler.net/problem=15
So if anyone is interested, I looked into memoization and came up with an elegant solution without recursion.
Function:
private static BigInteger numberPaths(ArrayList<ArrayList<BigInteger>> grid)
{
for(int i = 0; i <= 20; ++i)
{
for(int j = 0; j <= 20; ++j)
{
int x = j;
int y = i;
if(x - 1 < 0 || y - 1 < 0)
{
grid.get(x).set(y, BigInteger.ONE);
}
else
{
BigInteger topVal = grid.get(x - 1).get(y);
BigInteger leftVal = grid.get(x).get(y - 1);
grid.get(x).set(y, topVal.add(leftVal));
}
}
}
return grid.get(20).get(20); //the solution
}
Main:
ArrayList<ArrayList<BigInteger>> grid = new ArrayList<>();
for(int i = 0; i <= 20; ++i)
{
ArrayList<BigInteger> column = new ArrayList<>();
for(int j = 0; j <= 20; ++j)
{
column.add(BigInteger.valueOf(0));
}
grid.add(column);
}
System.out.println("Answer: " + numberPaths(grid));

How to write a code recursively and pass the value dynamically in java as shown below picture?

public static void getSome(int x) {
int i, j, k, l, m, temp = (x - (x - 1));
for (i = 1; i <= x; i++) {
System.out.print("X");
}
System.out.println();
for (l = 1; l >= (x / 2); l--) {
for (j = 1; j <= l; j++) {
System.out.print(" ");
}
System.out.print("X");
}
for (m = (x - (x + 1)); m >= 1; m--) {
for (k = temp; k <= m; k--) {
System.out.print(" ");
}
System.out.print("X");
System.out.println();
}
}
The above code having issue, even its not recursive function, Could you please any one help me out.
To get this output
X X
X X
X X
X
X X
X X
X X
With this function to print one row
private static void printRow(int width, int level) {
StringBuilder sb = new StringBuilder();
if (width < 1) return;
for (int i = 0; i < level; i++) {
sb.append(" ");
}
for (int i = 0; i < width; i++) {
if (i == 0 || i == width - 1) {
sb.append("X");
}
else {
sb.append(" ");
}
}
System.out.println(sb.toString());
}
This is the hard-coded iterative approach
public static void main(String[] args) {
printRow(7, 0);
printRow(5, 1);
printRow(3, 2);
printRow(1, 3);
printRow(3, 2);
printRow(5, 1);
printRow(7, 0);
}
This is the recursive approach
private static void printX(int width, int level) {
if (width < 2) {
if (width == 1) {
printRow(width, level);
}
return;
}
printRow(width, level);
printX(width - 2, level+1);
printRow(width, level);
}
private static void printX(int width) {
printX(width, 0);
}
public static void main(String[] args) {
printX(7);
}

Trying to wrap my head around 3 nested if statments

Say I have an array of 5 strings and that related to that array is 3 arrays of integers all of the same size. e.g.`
String a[] = new String[5]
int x[] = new int[5]
int y[] = new int[5]
int z[] = new int[5]
so that a[0],x[0],y[0],z[0] are all related to the same thing.
I want to find out which index(es) in x[] hold the highest number. If more than one those has the same highest number then which of those has the highest number in y[] and if there is more than one with the same highest number which one would have the highest in z[](Its safe to assume that none would have the same max value in z[]
I've tried to explain as best I can..
This is the best if got it only checks the first 2 conditions
for(int i=0;i<a.length;i++)
{
if(x[i]>=maximum){
if(x[i]==maximum)
{
if(y[i]>=maximum)
{
maximum=x[i];
winner=a[i];
maximum=y[i];
}
}
else
{
maximum=x[i];
winner=teams[i];
maximum=y[i];
}
}
So this is my new code
static int compareValues(){
for (int i=0; i<a.length; i++){
int max =0;
int diff = x[i] - x[max];
if (diff == 0){
diff = y[i] - y[max];
}
if (diff == 0){
diff = z[i] - z[max];
}
if (diff > 0){
max = i
}
}
return max;
}
If the String[n] is related to the int[n]s for each n then really you should compose them properly, make them Comparable and sort them.
class Thing implements Comparable<Thing> {
final String a;
final int x;
final int y;
final int z;
public Thing(String a, int x, int y, int z) {
this.a = a;
this.x = x;
this.y = y;
this.z = z;
}
#Override
public int compareTo(Thing o) {
int diff = o.x - x;
if (diff == 0) {
diff = o.y - y;
}
if (diff == 0) {
diff = o.z - z;
}
return diff;
}
#Override
public String toString() {
return "{" + a + "," + x + "," + y + "," + z + "}";
}
}
public static int max(Thing[] things) {
// NB - This should really call compareTo.
int max = 0;
for (int i = 1; i < things.length; i++) {
int diff = things[i].x - things[max].x;
if (diff == 0) {
diff = things[i].y - things[max].y;
}
if (diff == 0) {
diff = things[i].z - things[max].z;
}
if (diff > 0) {
// Higher
max = i;
}
}
return max;
}
public void test() {
Thing[] things = new Thing[6];
things[0] = new Thing("Hello", 1, 2, 3);
things[1] = new Thing("There", 1, 2, 4);
things[2] = new Thing("Everyone", 0, 2, 3);
things[3] = new Thing("How", 9, 0, 3);
things[4] = new Thing("Are", 8, 9, 3);
things[5] = new Thing("You", 7, 2, 3);
System.out.println("Before: " + Arrays.toString(things));
System.out.println("Max: " + things[max(things)]);
Arrays.sort(things);
System.out.println("Sorted: " + Arrays.toString(things));
}
This solution might be easier to understand:
static String compareValues() {
// find the maximum in x
int xMax = Integer.MIN_VALUE;
for (int i = 0; i < 5; i++) {
if (x[i] > xMax) {
xMax = x[i];
}
}
// find the maximum in y, but limited to the positions where x is maximal according to the calculation above
int yMax = Integer.MIN_VALUE;
for (int i = 0; i < 5; i++) {
if (x[i] == xMax && y[i] > yMax) {
yMax = y[i];
}
}
// find the maximum in z, but limited to the positions where y is maximal according to the calculation above
int zMax = Integer.MIN_VALUE;
int iMax = 0;
for (int i = 0; i < 5; i++) {
if (y[i] == yMax && z[i] > zMax) {
zMax = z[i];
iMax = i; // record the maximum position
}
}
return a[iMax];
}
I did not test the code, but I think that it should work.

Eight Queens Array out of bounds (Java)

I'm trying to make a program that does Eight Queens with recursion but I keep on getting an array out of bounds error. I've been having problems with this for awhile now and I can't really seem to pinpoint the problem. Here's my code:
public class Queens {
public int currColumn = 0;
public static final int BOARD_SIZE = 8;
public static final int EMPTY = 0;
public static final int QUEEN = 1;
private int board[][];
public Queens() {
board = new int[BOARD_SIZE][BOARD_SIZE];
}
public void clearBoard() {
for (int x = 0; x <= BOARD_SIZE; x++) {
for (int y = 0; y <= BOARD_SIZE; y++) {
board[x][y] = 0;
}
}
}
public void displayBoard() {
for (int x = 0; x < BOARD_SIZE; x++) {
System.out.print("\n");
for (int y = 0; y < BOARD_SIZE; y++) {
System.out.print(board[x][y]);
}
}
}
public boolean placeQueens(int column) {
if (column > BOARD_SIZE) {
return true;
} else {
boolean queenPlaced = false;
int row = 1;
while (!queenPlaced && (row <= BOARD_SIZE)) {
if (isUnderAttack(row, column)) {
++row;
} else {
setQueen(row, column);
queenPlaced = placeQueens(column + 1);
if (!queenPlaced) {
removeQueen(row, column);
++row;
}
}
}
return queenPlaced;
}
}
public void setQueen(int row, int column) //SET BACK TO PRIVATE
{
board[row][column] = 1;
}
private void removeQueen(int row, int column) {
board[row][column] = 0;
}
private boolean isUnderAttack(int row, int column) {
if (column == 0) {
return false;
}
int prevColumn = column - 1;
int prevRow = index(prevColumn);
while (prevColumn >= 0) {
prevRow = index(prevColumn);
for (int i = 0; i > BOARD_SIZE; i++) {
if (prevRow == row && prevColumn + i == column) //Going right
{
return true;
}
if (prevRow + i == row && prevColumn + i == column) //Going up/right
{
return true;
}
if (prevRow - i == row && prevColumn + i == column) //Going down/right
{
return true;
}
}
prevColumn--;
}
return false;
}
public int index(int column) //BACK TO PRIVATE
{
for (int i = 0; i < 8; i++) {
if (board[i][column] == 1) {
return i;
}
}
return 0;
}
public static void main(String[] args) {
Queens x = new Queens();
if (x.placeQueens(1) == true) {
x.displayBoard();
} else {
System.out.println("No solution found");
}
}
}
First Problem:
public void clearBoard()
{
for(int x = 0; x < BOARD_SIZE; x++) // changed from x <= BOARD_SIZE
{
for(int y = 0; y < BOARD_SIZE; y++) // changed from y <= BOARD_SIZE
{
board[x][y] = 0;
}
}
}
Array index counting starts from 0 upto size-1. There is nothing in board[8][8].
Another Problem
/* ... */
while(prevColumn >= 0)
{
prevRow = index(prevColumn);
for(int i = 0; i > BOARD_SIZE; i++) // i=0; i> BOARD_SIZE is always false, so no looping mechanism
{ /* ... */ }
Change it to
for(int i = 0; i < BOARD_SIZE; i++) // corrected
Another Problem ?
if(column > BOARD_SIZE) when `column = 8`
Make it
if(column >= BOARD_SIZE)
Another Problem:
while(!queenPlaced && (row <= BOARD_SIZE))
Make it
row < BOARD_SIZE
Yet Another ?
queenPlaced = placeQueens(column + 1); // What if column = 7
for(int x = 0; x <= BOARD_SIZE; x++)
Change that to:
for(int x = 0; x < BOARD_SIZE; x++)
Since arrays are 0-indexed.
You try to access more elements than the declared array actually has:
// this means you can access index in [0, BOARD_SIZE - 1].
board = new int[BOARD_SIZE][BOARD_SIZE];
// this means you access index in [0, BOARD_SIZE].
for(int x = 0; x <= BOARD_SIZE; x++)
So, a good practice is to always use < when referring to the declared size of the array.
In java array indexes starts from zero.
for(int x = 0; x <= BOARD_SIZE; x++)
should be
for(int x = 0; x < BOARD_SIZE; x++)
{
for(int y = 0; y < BOARD_SIZE; y++)
{
board[x][y] = 0;
}
Your BOARD_SIZE is 8 .so array initialized for 8 elelements. So avaliable indexes are 0 to 7.
in loop
for(int x = 0; x <= BOARD_SIZE; x++)
when x=8 ArrayIndexOutOfBound Exception throws.
Check this point in all your arrays,where you are looping.

Categories

Resources