I'm studying the Conway's Game of Life to implement it on my own, and came across the following implementation with the rules:
Given a board with m by n cells, each cell has an initial state live (1) or dead (0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):
Any live cell with fewer than two live neighbors dies, as if caused by under-population.
Any live cell with two or three live neighbors lives on to the next generation.
Any live cell with more than three live neighbors dies, as if by over-population..
Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
And implementation (https://discuss.leetcode.com/topic/29054/easiest-java-solution-with-explanation):
public void gameOfLife(int[][] board) {
if (board == null || board.length == 0) return;
int m = board.length, n = board[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int lives = liveNeighbors(board, m, n, i, j);
// In the beginning, every 2nd bit is 0;
// So we only need to care about when will the 2nd bit become 1.
if (board[i][j] == 1 && lives >= 2 && lives <= 3) {
board[i][j] = 3; // Make the 2nd bit 1: 01 ---> 11
}
if (board[i][j] == 0 && lives == 3) {
board[i][j] = 2; // Make the 2nd bit 1: 00 ---> 10
}
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
board[i][j] >>= 1; // Get the 2nd state.
}
}
}
public int liveNeighbors(int[][] board, int m, int n, int i, int j) {
int lives = 0;
for (int x = Math.max(i - 1, 0); x <= Math.min(i + 1, m - 1); x++) {
for (int y = Math.max(j - 1, 0); y <= Math.min(j + 1, n - 1); y++) {
lives += board[x][y] & 1;
}
}
lives -= board[i][j] & 1;
return lives;
}
And driver:
public static void main(String args[]) {
GameOfLife gl = new GameOfLife();
int[][] board = {
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0, 0},
{0, 1, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 1, 1, 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, 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}
};
gl.gameOfLife(board);
}
And my question is, what do the x and y in liveNeighbors() represent? Do not understand why the need for Math.min() and Math.max(). And also, does lives represent the amount of initialized lives on the board?
The given code is using the min and max functions to limit the search to valid entries in the array. If this is not done, the code will return an ArrayOutOfBoundsException when trying to use -1, m, or n as array indexes. (The loop doesn't "know" that given a square at the right edge of the map, it shouldn't search for living neighbors further to the right; these functions encode that fact.) x and y are simply the loop control variables which are used to iterate over valid squares surrounding the target square.
As for the lives variable, that's the placeholder to keep count of how many live neighbors have been found by the loops below. You might have guessed this by the fact that it's the return value of the liveNeighbors function.
Let's do an example. We'll call liveNeighbors(board,9,9,0,2), where board is the board given in the driver. Your board has dimensions 9x9, so those are the m and n we pass, and for our example we're investigating the square at 0,2, which is the first entry in the third row (which has a 1 to its right). Great, let's begin.
i=0, so x = Math.max(i - 1, 0) = Math.max(-1, 0) = 0 (this shows the reason for the max function: if we just said int x=i-1, we would end up with x = -1 which is out of the bounds of the array. Next we evaluate x <= Math.min(i + 1, m - 1) = Math.min(1, 8) = 1. If we were investigating a cell in the final column, this condition would have enforced the right edge of the array.
I'll leave the similar logic involving y and j to you.
The loop simplifies to:
for (int x = 0; x <= 1; x++) {
for (int y = 1; y <= 3; y++) {
lives += board[x][y] & 1;
}
}
The inner loop will run six times, with the following (x,y) pairs: (0,1),(0,2),(0,3),(1,1),(1,2),(1,3). Convince yourself that these are the neighbors of the square we're investigating, as well as the square itself.
Five of these six squares will return 0, with the one at (1,2) returning 1, so at the end of this loop, lives will equal 1. The final thing to do is lives -= board[i][j] & 1;, which reduces lives by 1 if the square we're investigating has a 1 in it. In our case it doesn't (board[i][j] = 0) so we subtract 0, leaving us with 1, which we return. liveNeighbors(board,9,9,0,2) = 1
I may have gotten x and y backwards once or twice, but hopefully that's enough so you can understand what's going on.
Related
I need to take a 2D array and move everything as far left as possible. It is a 4x4 array and I have tried to do it but either only move certain items or the index goes out of bounds.
The gameBoard array looks like this:
{0 2 4 2}
{0 0 2 0}
{2 2 0 0}
{0 4 0 2}
and after you call the swipeLeft() method it should look like this:
{2 4 2 0}
{2 0 0 0}
{2 2 0 0}
{4 2 0 0}
There is also the issue of placing a zero into the previous index that you moved it from.
I created a double for loop to just loop through the array and tried to code something that would move it over but it hasn't worked.
Here was the code I had so far
public void swipeLeft() {
for ( int r = 0; r < gameBoard.length; r++ ) {
for ( int c = 0; c < gameBoard[r].length; c++ ) {
gameBoard[r][c] = gameBoard[r][ (c+1) %
gameBoard.length];
}
}
}
Based on your desired OUTPUT, it looks like swipeLeft() is supposed to push all non-zero values to the very left of their row, displacing the zeroes to the right of all non-zero values.
If that's correct, this is similar to Old Dog Programmer's approach, except all shifting is done "in place" without creating any new arrays:
import java.util.*;
class Main {
private static int[][] gameBoard;
public static void main(String[] args) {
gameBoard = new int[][] {
{0, 2, 4, 2},
{0, 0, 2, 0},
{2, 2, 0, 0},
{0, 4, 0, 2}
};
System.out.println("Before:");
displayBoard();
swipeLeft();
System.out.println("\nAfter:");
displayBoard();
}
public static void displayBoard() {
for(int[] row : gameBoard) {
System.out.println(Arrays.toString(row));
}
}
public static void swipeLeft() {
for(int[] row : gameBoard) {
// find the first blank (zero) spot
int nextIndex = 0;
while(nextIndex < row.length && row[nextIndex] != 0) {
nextIndex++;
}
// start with the first blank, and shift any non-zero
// values afterwards to the left
for(int col=nextIndex; col < row.length; col++) {
if (row[col] != 0) {
row[nextIndex] = row[col];
row[col] = 0;
nextIndex++;
}
}
}
}
}
Output:
Before:
[0, 2, 4, 2]
[0, 0, 2, 0]
[2, 2, 0, 0]
[0, 4, 0, 2]
After:
[2, 4, 2, 0]
[2, 0, 0, 0]
[2, 2, 0, 0]
[4, 2, 0, 0]
From the example in the question, it appears to me that what is wanted is to shift all non-zero elements to the left, and zero elements are shifted to the right. The order of the non-zero elements is to be retained.
Note that each row is independent of other rows.
One way to approach this is to create a method that works on a 1D array. This method takes a 1D array as a parameter, and returns another 1D array with the elements shifted:
public static int [] zeroShift (int [] arr) {
int [] left = new int [arr.length];
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] != 0) {
left [count++] = arr [i];
}
}
return left;
}
This copies each non-zero element to a new array of the same size, keeping track (count) of how many have been copied so far. Note this relies on left being initialized to all-zeros.
Once that method is working, it can be used for gameBoard on a row-by-row basis:
public void swipeLeft() {
for (int r = 0; r < gameBoard.length; r++) {
gameBoard [r] = zeroShift (gameBoard [r]);
}
// output for testing
for (int i = 0; i < gameBoard.length; ++i) {
System.out.println(Arrays.toString(gameBoard[i]));
}
}
To rotate the array in place, you should roteate the array 3 times:
123456 -> 654312
654321
3456..
....12
public static void shiftLeft(int[] arr, int offs) {
if (offs <= 0)
return;
offs = arr.length - offs % arr.length - 1;
for (int i = 0, j = arr.length - 1; i < j; i++, j--)
swap(arr, i, j);
for (int i = 0, j = offs; i < j; i++, j--)
swap(arr, i, j);
for (int i = offs + 1, j = arr.length - 1; i < j; i++, j--)
swap(arr, i, j);
}
private static void swap(int[] arr, int i, int j) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
So your code intends to rotate the board one column to the left. Rotate? Well, the numbers you push out on the left might come back on the end, right?
Probably the line
gameBoard[r][c] = gameBoard[r][ (c+1) % gameBoard.length];
should be
gameBoard[r][c] = gameBoard[r][ (c+1) % gameBoard[r].length];
But try to do this stuff with pen & paper, and you should notice that you are going to loose one column/copy the values from the second column into the first, then copy that into the last column again.
You will need to change two items:
store the value from the first column somewhere if you still need it so you can push it into the last one.
only rotate the column data if it needs to be rotated. Or in other words, rotate the remainder of the row if you find a zero. In this case you do not need to remember the first column, as you will overwrite a zero and push a zero into the last column. And then it would not be called rotate but shift.
Exercise this with pen & paper until you can write down instructions for someone else to perform the same operation. Then you are ready to also write it in Java.
I need the sum of the variables around 2,2 and then to print it out.
I have no idea on how to do this. Please do help!
This is my code so far:
import java.util.*;
import java.io.*;
public class MatrixSumming
{
private int[][] m = {{5,6},{7,8},{3,4}}; //load in the matrix values
public int sum( int r, int c )
{
return 0;
}
public String toString()
{
return "";
}
}
Here is my runner
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import static java.lang.System.*;
public class MatrixSummingRunner
{
public static void main( String args[] ) throws IOException
{
//Scanner file = new Scanner (new File("matsum.dat"));
int[][] mat = {{0, 0, 0, 0, 0, 0, 0},
{0, 1, 2, 3, 4, 5, 0},
{0, 6, 7, 8, 9, 0, 0},
{0, 6, 7, 1, 2, 5, 0},
{0, 6, 7, 8, 9, 0, 0},
{0, 5, 4, 3, 2, 1, 0},
{0, 0, 0, 0, 0, 0, 0}};
}
}
I tried finding but couldn't find anything similar to this in matrix.
Do a regular search for the required number and let the index vvalues be "curi" and "curj"
Use the following logic
Let maxi and maxj be the m and n for (mxn) matrix
sum = 0;
for(int i=curi-1;i<curi+3;i++){
for(int j=curj-1;j<curj+3;j++){
if(i>-1 && j>-1 && i<maxi && j<maxj){ // boundary conditions
if(i!=curi && j!=curj){
continue; // skip the current search key to add
}
sum += array[i][j];
}
}
}
You have this method public int sum( int r, int c ) where you pass in two ints.
These are the row and column index, and correspond to a position in the matrix. (Or you have to substract 1, if you take row 1 as the first row, because of the zero-indexed arrays in Java)
So if you have a matrix and the values r and c, you will be at position matrix[r][c] (or the other way round).
Imagine standing on a chess board. Now you have 8 fields around you, which you can reach by taking one step. (Or two, if you cannot move diagonally in one step). You might not have fields on one or more sides. You have to check this before computing a sum.
Now, taking a step means adding or substracting 1 to either r or c. Thus you get other indexes around the field you're standing at by addressing matrix[r-1][c] or matrix[r][c+1] etc. To move diagonally, you have to change both r and c (take two steps. e.g. first move one to the left, then one up. you moved to the field diagonally at the upper left of the original field)
You can then, using this knowledge, access the fields around the current field and sum them up.
This method will get all the surrounding cells values and sum them.
private static int neighboursSum(int[][] grid, int r, int c) {
int sum = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
// Make sure that we don't sum the Original Value, we only want it's neighbours
if (i == 0 && j == 0) {
continue;
} else {
int newX = r + i;
int newY = c + j;
// Make sure that the new Coordinates do not point outside the range of the Array
if (newX >= 0 && newX <= grid.length && newY >= 0 && newY <= grid.length) {
sum += grid[newX][newY];
}
}
}
}
return sum;
}
I am trying to not load my entire tile based map into memory to save RAM client side. The map will be huge and already is requriring 1GB client side (multi-layered map).
I have gotten some perspective on Game Dev SO. I am trying to Load zones/chunks of my game map into memory (i.e. 300x300) and then when the player moves 100 steps shift the array and load 100 new tiles depending on direction. I have tried to work on a scaled version of this and now have a generic question.
I need help when the playerX/Y coordinates are on the perimeter of the map (which causes the chunk to be outside of the map)
Here is what I have come up with so far (note: player is in center of chunk & chunk always odd number sized)... It has the following issues(when the character is on the edge of the map):
change characterX/Y to 0,0 and the bottom left(0,2) coordinate will incorrectly be 7
0, 0, 0
0, 1, 1
7, 1, 8
change characterX/Y to 8,8 and the top right(2,0) coordinate of the chunk will incorrectly be 6
1, 1, 6
1, 1, 0
0, 0, 0
Here is the SSCCE:
public class MapChunkLoad {
public static void main(String[] args) {
short[] groundLayer;
int mapWidth = 9;
int mapHeight = 9;
int chunkWidth = mapWidth / 3; //3
int chunkHeight = mapHeight / 3; //3
int characterX = 8;
int characterY = 8;
String map = "1, 1, 1, 1, 1, 1, 1, 1, 7, " +
"1, 8, 8, 1, 1, 1, 1, 1, 1, " +
"1, 8, 9, 9, 1, 1, 1, 1, 1, " +
"1, 1, 9, 9, 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, " +
"6, 1, 1, 1, 1, 1, 1, 1, 1";
String[] strArr = map.split(", ");
groundLayer = new short[chunkWidth * chunkHeight];
//load chunk into groundLayer
int arrayIndex = 0;
int count = (characterX - (chunkWidth/2)) + ((characterY - (chunkHeight/2)) * mapWidth); //top left tile within chunk
for (int y = 0; y < chunkHeight; y++){
for (int x = 0; x < chunkWidth; x++){
if (count > -1 && count < strArr.length){
groundLayer[arrayIndex] = Short.parseShort(strArr[count]);
System.out.println("arrayIndex[" + arrayIndex + "] = " + strArr[count]);
} else {
groundLayer[arrayIndex] = 0;
System.out.println("arrayIndex[" + arrayIndex + "] = " + 0);
}
arrayIndex++;
count++;
}
count += (mapWidth - chunkWidth);
}
System.out.println("");
//print map grid
int printcount = 0;
for (int y = 0; y < chunkHeight; y++){
for (int x = 0; x < chunkWidth; x++){
if (x == chunkWidth - 1){
System.out.println(groundLayer[printcount]);
} else {
System.out.print(groundLayer[printcount] + ", ");
}
printcount++;
}
}
}
}
Thanks so much for any assistance.
So I think your logic for checking if count is outside the bounds is faulty.
I think it needs to be more complex to account for that your array represents a 2D figure. I suspect for chunks larger than 3x3 you're getting a lot more errors that are really tricky to describe. Consider this map where each square's value is its index.
0 1 2
3 4 5
6 7 8
when in the top right corner (2) your map should look like this
0 0 0
1 2 0
4 5 0
but this will fail because count will in some cases be a valid index when calculating xValue*width+yValue, but you want that to be invalid (mapped to 0). Instead you need to keep track of both the X and Y components of count and make your map display a zero when either of those are out of bounds.
int countX = characterX - (chunkWidth/2);
int countY = characterY - (chunkHeight/2);
int index = countX + (countY*mapWidth)
then later. Instead of checking:
if (count > -1 && count < strArr.length)
check:
if( countX + x >= mapWidth || countY + y >= mapHeight)
EDIT:
As you can imagine this also changes how you count. You will also need a way to break your loop. Something like
if(x == chunkWidth && y == chunkWidth) break;
I would be more specific, but I'm having trouble loading your original post to use as reference.
I think that fixes everything. Leave a comment if you have any questions. Good luck!
I'm not sure if this is what you are looking for, but commonly you'd check for bounds inside your nested for-loop that fills the groundLayer, rather than checking with a count variable. That way will be much more robust. Something like this:
for(int y = 0;y < chunkHeight;y++) {
for(int x = 0;x < chunkWidth;x++) {
//Get the absolute position of the cell.
int cellX = characterX + x - chunkWidth / 2; //Please check if this is correctly lined out.
int cellY = characterY + y - chunkHeight / 2;
if(cellX >= 0 && cellX < mapWidth && cellY >= 0 && cellY < mapHeight) { //Within bounds.
groundLayer[arrayIndex] = Short.parseShort(strArr[cellX + (cellY * mapWidth)]);
} else { //Out of bounds, put down a placeholder.
groundLayer[arrayIndex] = 0;
}
arrayIndex++;
}
}
Let me know if this was what you were looking for, and whether it works!
I am really stuck at this problem.
In this problem, you are given a 2xN board. You need to fill in non-negative numbers in this board in such a way, that:
The sum of all the numbers filled = N
Each of the 2 rows
consist of numbers in non-increasing order
Each of the N
columns consist of numbers in non-increasing order.
In how many ways can this be done, given the number N?
Two ways are considered different if there is a cell in the board which has different numbers.
The output should be the number of ways the matrix can be formed.
The matrix can have repetitive numbers and zero can be used. The matrix should not have increasing numbers but equal numbers can be filled along side each other.
Example:
input-> 5
output->16
From your example (input=5, output=16) I suppose only integer numbers are allowed.
One naive (brute force) solution is to use backtracing algorithm:
http://en.wikipedia.org/wiki/Backtracking
On this site you can see example with sudoku board being filled until solution is found.
==
For example:
You have array of integers with size 2N.
For position 0 you take first free number.
If solution is not broken yet you go to position 1 of array.
If solution is broken - stop as cannot back anymore
For position 1 you take next free number.
If solution is not broken you you go to position 2 of array.
If solution is broken you back to previous sten and take next free number.
For position 2...
This is typically done with recursion.
I think, on each position (recursion level) numbers can be taken from pool 0..N.
Try - good luck.
EDIT:
Here is valid solution (using backtracking algo):
private final int N = 5;
// 2 rows in one array [0..N-1, N..2N-1]
private int[] board = new int[2 * N];
// found solution counter
int found = 0;
/*
* this method set next number to current position
* and recursively go to next position.
*/
public void check(int position) {
// if board is complete - check if valid
if (position == 2 * N) {
if (isValid()) {
System.out.println("foun : " + Arrays.toString(board));
found++;
}
return;
}
// if board is not complete - put all numbers (0..N) into current position
// and recursively go to next position
for (int v = 0; v <= N; v++) {
board[position] = v;
// if solution is already broken - step backwards
// see: backtracking algorithms
if (isBroken(position)) {
return;
}
check(position + 1);
}
}
public boolean isValid() {
// condition 1
int sum = 0;
for (int i = 0; i < board.length; i++) {
sum += board[i];
}
if (sum != N) {
return false;
}
// conditin 2
int prev = board[0];
for (int i = 1; i < N; i++) {
if (board[i] > prev) {
return false;
}
prev = board[i];
}
prev = board[N];
for (int i = N + 1; i < 2 * N; i++) {
if (board[i] > prev) {
return false;
}
prev = board[i];
}
// condition 3
for (int i = 0; i < N; i++) {
int top = board[i];
int bottom = board[i + N];
if (top < bottom) {
return false;
}
}
// valid
return true;
}
// simplified version of this method - but correct
public boolean isBroken(int current) {
int sum = 0;
for (int i = 0; i <= current; i++) {
sum += board[i];
}
return sum > N;
}
public void start() {
check(0);
System.out.println("found: " + found);
}
And program output for N = 5:
found : [1, 1, 1, 0, 0, 1, 1, 0, 0, 0]
found : [1, 1, 1, 1, 0, 1, 0, 0, 0, 0]
found : [1, 1, 1, 1, 1, 0, 0, 0, 0, 0]
found : [2, 1, 0, 0, 0, 1, 1, 0, 0, 0]
found : [2, 1, 0, 0, 0, 2, 0, 0, 0, 0]
found : [2, 1, 1, 0, 0, 1, 0, 0, 0, 0]
found : [2, 1, 1, 1, 0, 0, 0, 0, 0, 0]
found : [2, 2, 0, 0, 0, 1, 0, 0, 0, 0]
found : [2, 2, 1, 0, 0, 0, 0, 0, 0, 0]
found : [3, 0, 0, 0, 0, 2, 0, 0, 0, 0]
found : [3, 1, 0, 0, 0, 1, 0, 0, 0, 0]
found : [3, 1, 1, 0, 0, 0, 0, 0, 0, 0]
found : [3, 2, 0, 0, 0, 0, 0, 0, 0, 0]
found : [4, 0, 0, 0, 0, 1, 0, 0, 0, 0]
found : [4, 1, 0, 0, 0, 0, 0, 0, 0, 0]
found : [5, 0, 0, 0, 0, 0, 0, 0, 0, 0]
found: 16
I'm almost positive this has a solution in closed form, or its similar to another problem with a solution in closed form. I would do it programmatically with recursion.
So suppose you know a solution for N. You want N+1. So what you should do is take all the solutions for N and see where you can stick an extra 1 in there without breaking any constraints. That is, super impose the N solution(s) on the N+1 board then try in all 2N places to add 1 without breaking constraints. Then store all of them in a set so they will be deduped.
In any case, its similar to http://en.wikipedia.org/wiki/Partition_(number_theory)
Here's a brute force way: since this is a 2xN matrix and the sum of all numbers must be N, the simplest solution is to fill the first row with 1's, and the 2nd row with 0's. Now you will need a recursive algorithm that takes a valid board, and removes 1 from any "free" position and add it to any legal position. That board is also a solution. By "free" I mean a number n at position [i, j] where [i+1, j] <= n - 1 and [i, j + 1] <= n - 1. You then recursively invoke the algorithm on the new boards, and save everything.
All that's left is to deduplicate solutions.
Example of the algorithm on input 5:
Initial solution:
11111
00000
The only "free" number is [0, 4]. Remove 1, the only legal positions are [0, 0] and [0, 1]. This gives you 2 new solutions
21110
00000
and
11110
10000
Now apply same algorithm again on both these solutions. Notice that the 2nd board now has 2 "free" numbers. Repeat until you get to
50000
00000
EDIT: Just had a lot of fun coding this example. Didn't test it, but that's where my head is going:
public void TwoRowBoard()
{
var board = new int[2, N];
//Create initial, simplest solution.
for (int i = 0; i < N; i++)
{
board[0, i] = 1;
}
var solutions = new List<int[,]>();
RecursiveSolve(board, solutions);
}
private void RecursiveSolve(int[,] board, List<int[,]> solutions)
{
var freeNumbers = GetFreeNumbers(board);
foreach (var freeNumber in freeNumbers)
{
board[freeNumber.i, freeNumber.j] -= 1;
var legalPositions = GetLegalPositions(board);
foreach (var legalPosition in legalPositions)
{
var newBoard = Copy(board);
newBoard[legalPosition.i, legalPosition.j] += 1;
solutions.Add(newBoard);
RecursiveSolve(newBoard, solutions);
}
}
}
private List<Coordinates> GetLegalPositions(int[,] board)
{
//Position 0, 0 is always legal.
var results = new List<Coordinates> {new Coordinates {i = 0, j = 0}};
//Row 0
for (int j = 1; j < N; j++)
{
if (board[0, j - 1] > board[0, j])
{
results.Add(new Coordinates{i = 0, j = j});
}
}
//Row 1. Board[1, higher than N/2] are never legal positions.
for (int j = 0; j <= N /2; j++)
{
if (board[1, j - 1] > board[1, j]
&& board[0, j] > board[1, j])
{
results.Add(new Coordinates{i = 1, j = j});
}
}
return results;
}
private List<Coordinates> GetFreeNumbers(int[,] board)
{
var results = new List<Coordinates>();
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < N; j++)
{
if (i == 0 && j == 0)
{
continue;
}
if (i == 0)
{
if (j == N - 1 && board[0, j] > 0)
{
results.Add(new Coordinates {i = 0, j = j});
}
else if (board[0, j] > board[1, j]
&& board[0, j] > board[0, j + 1])
{
results.Add(new Coordinates {i = 0, j = j});
}
}
else
{
if (j > N/2 && board[1, j] > 0)
{
throw new Exception("Don't see how it's possible for board[1, N/2 or higher] to not be 0");
}
if (board[1, j] > board[1, j + 1])
{
results.Add(new Coordinates{i = 1, j = j});
}
}
}
}
return results;
}
public class Coordinates
{
public int i { get; set; }
public int j { get; set; }
}
I have 2D array but if I change x coordinate, everytime I get wrong result.
int[][] arr = {{0, 2, 0, 0, 1},{0, 2, 0, 0, 1},{0, 2, 0, 0, 1},{0, 2, 0, 0, 1},{0, 2, 0, 0, 1}};
int now, previous;
for (int i = 1; i < 5; i++) {
for (int j = 0; j < 5; j++) {
now = arr[i][j];
previous = arr[i-1][j];
}
}
The result of variable now is 0, 2, 0, 0, 1... Why I want to have only 2, 0, 0, 1
If I change i coordinate of variable for Example i = 1 the output is still 0, 2, 0, 0, 1...
Do you know where is problem?
Thanks
because i corresponds to the outer array in your example. what you have will print 0, 2, 0, 0, 1 four times. I gather what you want is to show 2, 0, 0, 1 five times... for that you should be doing
for (int i = 0; i < 5; i++) {
for (int j = 1; j < 5; j++) {
now = arr[i][j];
previous = arr[i-1][j];
}
}
You need to put j = 1 instead of j = 0.
The i variable is iterating over the vectors that compose your array, while the j variable is iterating over each element of one of those vectors. You want to skip the first element of each vector, so you should change j to start at 1 instead of 0.
In any case, you are repeating the attribution inside a loop, and only the last value assigned to the variables will be kept. So assuming this is the original code you're using, you should remove the "for" loops and do the attributions directly, assuming your arr array will not change.