Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm new to Java and programming in general and embarking on an interesting problem. its a question has ive been tasked with to make my initials with asterisks. as mine is "CH" I've decided to start by trying to simplify by thinking of it as three columns and three rows.
so far I've created a two-dimensional array; to use as a blank grid. it's 5 by 10 (arbitrarily).
Breaking the task down I think there are three columns top to bottom, occupying the 0, 6, and 9th indexes of the y array. Also three rows: top and bottom first thirds; and the central last third. further to this I've decided on two spaces between the characters.
So my next thought has been it's probably best to consider the spaces? I believe I can probably effectively do this by slicing the arrays rather than doing iterating through or similar. to keep the code as tight as possible. i'm thinking I want to split x index and tell it to place a space in the mid-value..at some point (x/2) is it possible to give this index a name to do this and how do I do this in java?
What i'm trying to achieve is:
XXXX x x
x x x
x xxxx
x x x
xxxx x x
// so far i have just the base:
public class MyClass {
public static void main (string args[]){
int[x][y] myArray = new int[5][10];
brief summary of what i'd like to achieve so far:
*'s on Y "columns" 0,6,9
*'s on X 0-4 y 0;
6-9 y 2;
0-4 y 4
apologies if this seems stupidly simple but I'm unsure how to do this programmatically, and I'm on my own!
apologies for any errors any suggestions or pointers appreciated!
I would make each letter as array of bytes like:
byte[][] c = {{1, 1, 1, 1}, {1, 0, 0, 0}, {1, 0, 0, 0}, {1, 0, 0, 0}, {1, 1, 1, 1}};
byte[][] h = {{1, 0, 0, 1}, {1, 0, 0, 1}, {1, 1, 1, 1}, {1, 0, 0, 1}, {1, 0, 0, 1}};
so, 1 marks the X, and 0 marks the zero, ie. a "raster alphabet".
Then you can do a row by row iteration of the arrays and print it out?
public class Test
{
public static void main(String[] args)
{
byte[][] c = {{1, 1, 1, 1}, {1, 0, 0, 0}, {1, 0, 0, 0}, {1, 0, 0, 0}, {1, 1, 1, 1}};
byte[][] h = {{1, 0, 0, 1}, {1, 0, 0, 1}, {1, 1, 1, 1}, {1, 0, 0, 1}, {1, 0, 0, 1}};
for (int i = 0; i < 5; i++)
{
for (int j=0; j < 4; j++)
{
byte l = c[i][j];
System.out.print(l == 1 ? "x":" ");
}
System.out.print(" ");
for (int j=0; j < 4; j++)
{
byte l = h[i][j];
System.out.print(l == 1 ? "x":" ");
}
System.out.println("");
}
}
}
I am looking to set a random value to a certain element in a 2d array.
In my assignment I have to assign a random number 1-5 for the first element in the 2d array and then go the the next row and do the same thing. This is what I have so far but it does not look right.
double CURRENT_BOARD [5][5] = {{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0}};
public double shuffleBoard (double currentBoard) {
Random rand = new Random();
shuffledBoard [][] = new double [5][5];
for (i=0 ;i<5 ;i++) {
j = rand.nextInt(5) + 1;
shuffledBoard = shuffledBoard [j][0];
}
return shuffleBoard;
}//shuffleBoard
My end goal is that the elements of the array will look something like
{5, 0, 0, 0, 0}
{3, 0, 0, 0, 0}
{4, 0, 0, 0, 0} and so on as long as the first element of the array is selected at random. Can anyone offer any help to make this happen?
Your declaration of a 2D double array is not correct.
Take a look at this StackOverflow answer
Declaration should be like
double[][] CURRENT_BOARD = new double[][]{{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0}};
For random number part, you can try this
public double[][] shuffleBoard(double[][] currentBoard) {
double[][] shuffledBoard = currentBoard;
Random rand = new Random();
double rangeMin = 1, rangeMax = 5;
for (int i = 0; i < 5; i++) {
double j = rangeMin + (rangeMax - rangeMin) * rand.nextDouble();
shuffledBoard[i][0] = j;
}
return shuffledBoard;
}
I'm trying to solve a 4x4 linear equation system (4 variables, 4 equations) using Jama. I have tried the following code, but it doesn't work. I would appreciate if someone can help me, using Jama or any other method.
import Jama.Matrix;
public class OvaWork {
public OvaWork()
{
//Creating Arrays Representing Equations
double[][] lhsArray = {{-3, 1, -1}, {5, -2, 1}, {-1, 1, 3}, {2, 5, 7}};
double[] rhsArray = {-4, 6, 0, 8};
//Creating Matrix Objects with arrays
Matrix lhs = new Matrix(lhsArray);
Matrix rhs = new Matrix(rhsArray, 4);
//Calculate Solved Matrix
Matrix ans = lhs.solve(rhs);
//Printing Answers
System.out.println("w = " + Math.round(ans.get(0, 0)));
System.out.println("x = " + Math.round(ans.get(1, 0)));
System.out.println("y = " + Math.round(ans.get(2, 0)));
System.out.println("z = " + Math.round(ans.get(3, 0)));
}
public static void main(String[] args)
{
OvaWork o = new OvaWork();
}
}
You have to try with easier examples like this 2x2 equation
double[][] lhsArray = {{1,1},{2, 0}};
double[] rhsArray = {10,2};
Matrix lhs = new Matrix(lhsArray);
Matrix rhs = new Matrix(rhsArray, 2);
Matrix ans = lhs.solve(rhs);
It works and the output is a matrix {1,9}
The problem with your code is that your matrix is not square it is 3x4
double[][] lhsArray = {{-3, 1, -1}, {5, -2, 1}, {-1, 1, 3}, {2, 5, 7}};
Change your matrix to a square one.
Test this trivial equation:
double[][] lhsArray = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
double[] rhsArray = {1, 2, 3, 4};
Matrix lhs = new Matrix(lhsArray);
Matrix rhs = new Matrix(rhsArray, 4);
Matrix ans = lhs.solve(rhs);
The ans is {1,2,3,4} as expected.
I'm working on a solution for the game Peg Solitaire in Java. It appears however, that my solution is unable to solve the game.
I'm working with the 'standard' version, so the board looks like :
{2, 2, 1, 1, 1, 2, 2}
{2, 2, 1, 1, 1, 2, 2}
{1, 1, 1, 1, 1, 1, 1}
{1, 1, 1, 0, 1, 1, 1}
{1, 1, 1, 1, 1, 1, 1}
{2, 2, 1, 1, 1, 2, 2}
{2, 2, 1, 1, 1, 2, 2}
0 is empty, 1 is a peg, 2 is nothing
The desired state of the board is
{2, 2, 0, 0, 0, 2, 2}
{2, 2, 0, 0, 0, 2, 2}
{0, 0, 0, 0, 0, 0, 0}
{0, 0, 0, 1, 0, 0, 0}
{0, 0, 0, 0, 0, 0, 0}
{2, 2, 0, 0, 0, 2, 2}
{2, 2, 0, 0, 0, 2, 2}
My solve() method works like this:
move peg on location x,y in a direction (up, down, left or right)
check if the board is in the desired state, if so, print board and moves taken and exit the program
Check if there is a still a peg that can be moved, if there isn't one exit the function
Find the first possible peg that can move in 1 or more directions; call solve() for that peg and it's direction.
(if we get here step 4 produced a undesired board state) undo the move made in step 4 and recall step 4 with the next possible move that peg could make, if any.
return to step 4 for the next possible peg that comes after the peg found in step 4.
I've implemented the above instructions like this:
private void play(int xx, int yy, Direction direction) {
board.move(xx, yy, direction);
if (board.finished()) {
board.printBoard();
System.out.println(moves);
System.exit(0);
}
if (board.noMoreMoves()) {
return;
}
for (int y = 0; y < board.board.length; y++) {
for (int x = 0; x < board.board[y].length; x++) {
if (board.containsPeg(x, y)) {
Direction[] moves = board.getMovesFor(x, y);
for (Direction move : moves) {
if (move != null) {
this.moves++;
play(x, y, move);
if (move.equals(Direction.UP)) {
board.undoMove(x, y-2, move);
} else if (move.equals(Direction.DOWN)) {
board.undoMove(x, y+2, move);
} else if (move.equals(Direction.LEFT)) {
board.undoMove(x-2, y, move);
} else if (move.equals(Direction.RIGHT)) {
board.undoMove(x+2, y, move);
}
}
}
}
}
}
return;
}
However, the above solution does not produce the desired board state. I have this version running in eclipse for over an hour now, without any results.
I've tested the methods board.move(x, y, direction), board.finished(), board.noMoreMoves(), board.containsPeg(x, y), board.getMovesFor(x, y) and board.undoMove(x, y, direction) in isolation and they all appear to function as desired.
Is there something I'm missing here, either in my implementation or in recursion / backtracking in general? I'm pretty sure the solution doesn't require over 200 million moves to be made.
What you need is a debugging strategy.
Use a method that will output the state of your board. Also write one that will save a representation of your board, and one that will compare two states to see if they're the same.
Output after the first method has failed, then at different points after backtracking to ensure that the backtrack has backtracked back to the right track.
Also make sure that you aren't tracking the same move more than once.
As for the possible number of moves, don't be too sure. One wouldn't think, a priori, that there were billions of possible moves in the first 10-20 moves of chess, but there are.
I'm running into problems trying to assign a 2d array to a 3d array, so I thought i'd ask a question about 3d and 2d arrays.
Say I have a masterArray[][][] and wanted to put childArray1[][] and childArray2[][] into it.
This is how I have done it and was wondering if that is the correct way of applying it:
private int[][][] masterArray;
private int[][] childArray1 = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 0, 0, 1, 0, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 1, 1, 1, 1, 0, 1, 0, 1},
{1, 0, 1, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 1, 0, 1, 1, 1, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 1, 1, 0, 1, 1, 1, 0, 1},
{1, 0, 1, 1, 0, 1, 8, 1, 0, 1},
{1, 0, 7, 1, 1, 1, 0, 1, 0, 1},
{1, 0, 1, 1, 1, 1, 0, 1, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 9, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
private int[][] childArray2 = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 1, 1},
{1, 1, 1, 1, 7, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 0, 0, 1, 1, 1},
{1, 1, 1, 9, 1, 1, 8, 0, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};
Ok, so in my init method I use these some methods to set the child arrays into the master array. What I was curious about was how this exactly works. I assumed the following:
masterLevel = new int[MAX_LEVELS][MAP_WIDTH][MAP_HEIGHT];
for (int x = 0; x < MAP_WIDTH; x++) {
for (int y = 0; y < MAP_HEIGHT; y++) {
masterArray[currentLevel][x][y] = childArray1[x][y];
}
}
Would that work?
In my application things aren't working so I picking out code that I am not 100% sure on.
It doesn't really matter how you organize a 3d array as long as you put things in the same way as you take them out.
From your comment on another answer it seems that you are having problem with element order ([currentLevel][x][y] = childArray[y][x];)
It seems you mixed MAP_HEIGHT and MAP_WIDTH. It should be:
masterLevel = new int[MAX_LEVELS][MAP_HEIGHT][MAP_WIDTH];
then you can use:
master[currentLevel][x][y] = childArray[x][y];
In Java multi-d arrays are actually arrays of arrays. So they can even be disjoint.
In the code you posted you refer to a variable called currentLevel that you did not define. I am sure that is defined in some code you did not post. Also don't forget that arrays are zero index. This code should work.
masterArray = new int[MAX_LEVELS][MAP_WIDTH][MAP_HEIGHT];
for (int currentLevel = 0; currentLevel < MAX_LEVELS; currentLevel++) {
for (int x = 0; x < MAP_WIDTH; x++) {
for (int y = 0; y < MAP_HEIGHT; y++) {
masterArray[currentLevel][x][y] = childArray1[x][y];
}
}
}
If you ever work with massive arrays and need speed then you could look at System.arrayCopy();
String[] arr1D;
String[][] arr2D;
String[][][] arr3D;
arr1D = new String[] { "1", "2", "3" };
//////////////////////////////////////////////////////////////////////////
//assign 1D array to element of 2D array
//////////////////////////////////////////////////////////////////////////
arr2D = new String[][] {
arr1D ,
arr1D ,
arr1D
};
/*
// OR
arr2D = new String[3][];
arr2D[0] = arr1D;
arr2D[1] = arr1D;
arr2D[2] = arr1D;
// OR
arr2D = new String[][] {
new String[] { "1", "2", "3" } ,
new String[] { "1", "2", "3" } ,
new String[] { "1", "2", "3" }
};
*/
//////////////////////////////////////////////////////////////////////////
//assign 2D array to element of 3D array
//////////////////////////////////////////////////////////////////////////
arr3D = new String[][][] {
arr2D ,
arr2D ,
arr2D
};
/*
// OR
arr3D = new String[3][][];
arr3D[0] = arr2D;
arr3D[1] = arr2D;
arr3D[2] = arr2D;
*/