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("");
}
}
}
Related
In python, you can use the structure for x in [a, b, c, d] for loops. This can be replicated using a foreach loop in java.
What about if I wanted to replicate a for x, y in z loop, such as the one below, in java?
for x_off, y_off in ( (1, 2), (-1, 2), (1, -2), (-1, -2), (2, 1), (-2, 1), (2, -1), (-2, -1) ):
#do something
I ended up just using variables for each index. But this won't be useful for 2d arrays with large counts for each inner array.
int[][] offsets = new int[][] { {0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1} };
for(int[] offset: offsets) {
int x = offset[0], y = offset[1];
// do something with x and y
You should create a class for storing the three values:
final class Point3D {
private final int x, y, z;
// constructor, getters, and equals/hashCode/toString here
}
Then you can use an array initializer with an enhanced for loop:
for (Point3D point : new Point3D[] { new Point3D(1, 1, 1), new Point3D(-1, 1, 1),
new Point3D(-1, -1, 1), new Point3D(1, -1, 1) }) {
// code here
}
It reads better if you create the array separately, especially if there are many points:
Point3D[] points = {
new Point3D( 1, 1, 1), new Point3D(-1, 1, 1),
new Point3D(-1, -1, 1), new Point3D( 1, -1, 1),
new Point3D( 1, 1, -1), new Point3D(-1, 1, -1),
new Point3D(-1, -1, -1), new Point3D( 1, -1, -1)
};
for (Point3D point : points) {
// code here
}
This question already has answers here:
Problem with assigning an array to other array in Java
(4 answers)
Closed 4 years ago.
I have 2D array:
int[] zero = {
1, 1, 1, 1, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 1};
int[][] tab = {zero, zero};
I want to change this:
tab[0][0] = 0;
But when I did that It also change tab[1][0]. Can you tell me how can I disable that?
By making it so the two arrays are different objects rather than the same object.
One way to achieve that would be:
int[][] tab = {zero.clone(), zero.clone()};
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.