Get last made move from game boards - java

I have two 2D int arrays that represent game boards. (The initial board and the new board)
Content of the initial board:
013 | 6 0 0 0 4 4 4 4 4 0 0 0 6
012 | 0 0 0 0 0 0 4 0 0 0 0 0 0
011 | 0 0 0 0 0 0 0 0 0 0 0 0 0
010 | 0 0 0 0 0 0 2 0 0 0 0 0 0
009 | 4 0 0 0 0 0 2 0 0 0 0 0 4
008 | 4 0 0 0 0 0 2 0 0 0 0 0 4
007 | 4 4 0 2 2 2 5 2 2 2 0 4 4
006 | 4 0 0 0 0 0 2 0 0 0 0 0 4
005 | 4 0 0 0 0 0 2 0 0 0 0 0 4
004 | 0 0 0 0 0 0 2 0 0 0 0 0 0
003 | 0 0 0 0 0 0 0 0 0 0 0 0 0
002 | 0 0 0 0 0 0 4 0 0 0 0 0 0
001 | 6 0 0 0 4 4 4 4 4 0 0 0 6
A B C D E F G H I J K L M
Content of the new board (after applying a move):
013 | 6 0 0 0 4 4 4 4 4 0 0 0 6
012 | 0 0 0 0 0 0 4 0 0 0 0 0 0
011 | 0 0 0 0 0 0 0 0 0 0 0 0 0
010 | 0 0 0 0 0 0 2 0 0 0 0 0 0
009 | 4 0 0 0 0 0 2 0 0 0 0 0 4
008 | 4 0 0 0 0 0 2 0 0 0 0 0 4
007 | 4 4 0 2 2 2 5 2 2 2 0 4 4
006 | 4 0 0 0 0 0 2 0 0 0 0 0 4
005 | 4 0 0 0 0 0 2 0 0 0 0 0 4
004 | 0 0 0 0 0 0 2 0 0 0 0 0 0
003 | 0 0 0 0 0 0 0 0 0 0 0 0 0
002 | 0 0 0 0 4 0 4 0 0 0 0 0 0
001 | 6 0 0 0 0 4 4 4 4 0 0 0 6
A B C D E F G H I J K L M
I wanna compare the two game boards to get the last fetched move. How can I determine the last made move on this board?
As you can see the last made move is: "E1 - E2" -> The pawn "E1" was put in "E2"
Is there a fast way to compare the two to determine this last made move "E1 - E2"
What I have:
for (int y = 0; y < Board.DIMENSION; y++) {
for (int x = 0; x < Board.DIMENSION; x++) {
// Check where not equal
if (initialBoard.grid[x][y] != newBoard.grid[x][y]) {
// What to add??
}
}
}

Assuming that the user can only make a move over the empty places(0).
You can do something like this:
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
// Check where not equal
if (initialBoard.grid[x][y] != newBoard.grid[x][y] && newBoard.grid[x][y] == 0) {
// From move
String from = x +""+ (char)y+65;
}
if(initialBoard.grid[x][y] != newBoard.grid[x][y] && newBoard.grid[x][y] != 0) {
// To move
}
}
}

You will need to find two spots where the boards differ. You already seem to have the logic down to find a spot where the game board has changed. Now you need to decide what direction the move occurs in.
To me, it looks like the board going from 0 to 4 means a piece has moved there. So, if the new int is > the old int... we know this is the end location.
Since you've gotten this far, I think you can figure it out with the follow logic: (maybe there are cases in this game I am not aware of - so you will have to check for those cases)
String startPos, endPos;
for (int y = 0; y < Board.DIMENSION; y++) {
for (int x = 0; x < Board.DIMENSION; x++) {
// Check where not equal
if (initialBoard.grid[x][y] != newBoard.grid[x][y]) {
// if we go from a 0 to a 4, this is the starting position
if (initialBoard.grid[x][y] < newBoard.grid[x][y]) {
// translate the location into a position and store it as startPos
} else {
// translate the location into a position and store it as endPos
}
}
}
}
System.out.println(startPos + " -> " + endPos); // something like that

Related

Creating matrices Java

I have trouble creating a matrice for a game map design.
void prepareMatrix(int width, int height)
{
room = new int[height][width];
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
if(i < height/4)
{
room[i][j] = 2;
}
else if(j == 0 || j == --width)
{
room[i][j] = 1;
}
else if(i == --height)
{
room[i][j] = 1;
}
else
{
room[i][j] = 0;
}
}
}
}
I want to create something like this: (1- Wall1, 2- wall2, 0-floor)
2 2 2 2 2 2
2 2 2 2 2 2
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
And I get this:
2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2
1 0 0 0 0 1 0 0 0 0
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 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
The matrice would be a blueprint for the map.
You are using --width and --height. It appears from the expected result that you want the 1's to go in the first and last columns and in the last row. As a commenter implied, --width does not just return the width minus one, it also reduces width by 1. You may want width - 1 and height - 1 instead.
Like M. Aroosi said, try to change the --width to width-1 and --height to height-1. You don't want to modify the value of a parameter. I think what is happening is that every time it goes through the loop, the values change for width and height.

how to restricts the number of character and lines reads from a file before insertinting into 2dimentional array in java

i have go through the previous answers but nothing like what i am looking for, specifically in java. here is my code, the block of my code can read a singe character integer only which is not exactly what i intend to do, i intends to reads more than one char integer, it doesn't work. and i want reads only 16 lines and 16 integers in a line from the file even if the file contains more than 16 lines and more than 16 integers per line. can some one share an idea with me please?
Here is sample input data:
13 20 0 0 0 0 0 0 0 0 0 0 11 2
0 0 0 0 0 0 0 0 0 4 5 0 0 11 2
0 0 0 0 0 0 0 0 0 333 4 0 0 0 0
0 0 0 0 0 0 0 0 0 9 10 41 3 5 8
0 0 0 0 0 0 0 0 0 0 11 2 333 4
13 20 0 0 0 0 0 0 0 0 0 0 11 2
0 0 0 0 0 0 0 0 0 4 5 0 0 11 2
0 0 0 0 0 0 0 0 0 333 4 0 0 0 0
0 0 0 0 0 0 0 0 0 9 10 41 3 5 8
0 0 0 0 0 0 0 0 0 0 11 2 333 4
13 20 0 0 0 0 0 0 0 0 0 0 11 2
0 0 0 0 0 0 0 0 0 4 5 0 0 11 2
0 0 0 0 0 0 0 0 0 333 4 0 0 0 0
0 0 0 0 0 0 0 0 0 9 10 41 3 5 8
0 0 0 0 0 0 0 0 0 0 11 2 333 4
13 20 0 0 0 0 0 0 0 0 0 0 11 2
0 0 0 0 0 0 0 0 0 4 5 0 0 11 2
0 0 0 0 0 0 0 0 0 333 4 0 0 0 0
0 0 0 0 0 0 0 0 0 9 10 41 3 5 8
0 0 0 0 0 0 0 0 0 0 11 2 333 4
i just want insert this into the 2dimentional array as you can see in my code, but my array is 16X16 but the file may be more than 16x16 in size, but i just want reads just 16x16 even if the file contains more than that, and ignore the empty line even if it exist.
BufferedReader bufferedReader = new BufferedReader(new FileReader("text.txt"));
String line = null;
int[][] board = new int[16][16];
int k = 0;
while((line = bufferedReader.readLine())!=null) {
String[] newmatrix = line.split(" ");
for(int i=0; i<9; i++) {
board[k][i] = Integer.parseInt(newmatrix[i]);
}
k++;
}
This appears to be "a learning exercise". So, hints / advice only1:
To stop reading after 16 lines, use a counter.
Skip an empty line by testing for an empty line.
Use a Scanner (hasNextInt() and nextInt() to process each line.
It is a good idea to avoid hard-wiring literal constants into your code ... like 9 which you seem to have pulled out of the air.
Use board.length - 1 or board[i].length - 1 for your array bounds when "looping". (See previous)
Also ... your input file seems to only have 14 integers per line, not 16 as the question states.
1 ... because you will learn more by coding this yourself.
The code below should work.
BufferedReader bufferedReader = new BufferedReader(new FileReader("text.txt"));
String line = null;
int[][] board = new int[16][16];
int k = 0;
while((line = bufferedReader.readLine())!=null) {
String[] newmatrix = line.split(" ");
for(int i = 0; i < 16; i++) {
board[k][i] = Integer.parseInt(newmatrix[i]);
}
k++;
if (k == 16)
break;
}
bufferedReader.close();

Problems adding to two dimensional list in java

I don't understand why this cannot work, if anyone could help that would be great;
for(int i = 0; i < 10 ; i++){
lines = fileL[i];
for(int j = 0; j < lines.length(); j++){
enemySpawningL[i][j] = fileL[i].substring(j*2, 1);
}
}
where enemySpawning[][] has been set as a string and fileL is set as this;
private String[] fileL = {
"1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1",
"1 0 0 0 0 1 0 0 0 0 1 0 0 2 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 2 0 0 1 0 0 0 0 1 0 0 0 0 1",
"1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 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2 0 0 3 0 2 0 0 3 0 2 0 0 3 0 2 0 0 3 0 2 0 0 3 0 2 0 0 3 0 2 0 0 3 0 2 0 0 3 0 2 0 0 3 0 2 0 0 3 0 2 0 0 3 0 2 0 0 3",
"2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2",
};
(I tried using a textfile for it, but I didn't think there was a point cause it's not too important)
EDIT: I'm just trying to make the enemySpawning list be [line number][number in line]
EDIT2: Also the error I'm getting is java.lang.NullPointerException
The error is probably because you didn't initialize enemySpawningL.
String[][] enemySpawningL = new String[rowsNum][columnsNum];
And if I understand correctly what you are trying to do change your code to
for(int i = 0 ; i < 10 ; ++i) {
String[] digits = fileL[i].split(" ");
for(int j = 0 ; j < digits.length ; ++j) {
enemySpawningL[i][j] = digits[j];
}
}
That will give you one digit in every cell. Currently your substring() is wrong as the start point is bigger than the end point for j > 0 and if you change the order you insert string with several digits every time.
"1"
"1 0"
"1 0 0"
....
The problem is, as #guy mentioned, not initializing the array, but there is also another error in here:
enemySpawningL[i][j] = fileL[i].substring(j*2, 1);
Because 'j' can have maximal value of fileL[i] length, if you request index j*2, in half of the loop it will be out of bound. You are also using the substring method in incorrect way. How you want you loop to look like is:
for(int j = 0; j < lines.length(); j += 2){
enemySpawningL[i][j] = fileL[i].substring(j, j+1);
}
The j variable is now incremented by two every iteration (to skip spaces) and substring will return one character from position j of the string file[L]. Which is what you wanted.

Java counting connected regions in a grid

I am trying to locate all four-connected regions in the grid. A four-connected region consists of a set of marked cells (value 1) such that each cell in the region can be reached by moving up, down, left or right from another marked cell in the region. The assignment states that we should use recursion.
An example input would be:
10 20
0 1 1 0 0 0 1 0 1 1 0 0 0 0 1 0 0 0 1 0
0 1 1 1 0 1 1 0 0 1 0 0 1 1 1 0 0 0 1 1
0 0 1 0 0 0 1 1 0 1 0 0 0 1 0 0 0 1 1 1
1 0 0 0 0 0 0 0 1 1 0 0 1 0 0 0 1 1 1 0
1 1 0 1 0 0 0 1 1 1 0 0 0 1 1 0 1 1 0 0
1 1 1 1 0 0 0 0 0 0 1 0 1 1 1 0 0 0 0 0
0 1 1 1 0 0 0 1 1 1 1 0 0 1 1 0 1 0 0 0
0 1 1 1 1 1 0 0 1 1 1 0 0 0 1 1 1 1 1 0
0 0 0 1 1 0 0 0 0 1 0 0 0 1 1 0 0 1 1 0
1 0 1 1 1 1 1 0 0 0 0 0 0 0 1 1 0 1 0 0
And the output should be:
0 1 1 0 0 0 2 0 3 3 0 0 0 0 4 0 0 0 5 0
0 1 1 1 0 2 2 0 0 3 0 0 4 4 4 0 0 0 5 5
0 0 1 0 0 0 2 2 0 3 0 0 0 4 0 0 0 5 5 5
6 0 0 0 0 0 0 0 3 3 0 0 7 0 0 0 5 5 5 0
6 6 0 6 0 0 0 3 3 3 0 0 0 8 8 0 5 5 0 0
6 6 6 6 0 0 0 0 0 0 9 0 8 8 8 0 0 0 0 0
0 6 6 6 0 0 0 9 9 9 9 0 0 8 8 0 8 0 0 0
0 6 6 6 6 6 0 0 9 9 9 0 0 0 8 8 8 8 8 0
0 0 0 6 6 0 0 0 0 9 0 0 0 8 8 0 0 8 8 0
10 0 6 6 6 6 6 0 0 0 0 0 0 0 8 8 0 8 0 0
Right now when I run the code I have, I get this output:
0 2 2 0 0 0 2 0 2 2 0 0 0 0 2 0 0 0 2 0
0 2 2 2 0 2 2 0 0 2 0 0 2 2 2 0 0 0 2 2
0 0 2 0 0 0 2 2 0 2 0 0 0 2 0 0 0 2 2 2
2 0 0 0 0 0 0 0 2 2 0 0 2 0 0 0 2 2 2 0
2 2 0 2 0 0 0 2 2 2 0 0 0 2 2 0 2 2 0 0
2 2 2 2 0 0 0 0 0 0 2 0 2 2 2 0 0 0 0 0
0 2 2 2 0 0 0 2 2 2 2 0 0 2 2 0 2 0 0 0
0 2 2 2 2 2 0 0 2 2 2 0 0 0 2 2 2 2 2 0
0 0 0 2 2 0 0 0 0 2 0 0 0 2 2 0 0 2 2 0
2 0 2 2 2 2 2 0 0 0 0 0 0 0 2 2 0 2 0 0
My code looks like:
package project2;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
public class project2 {
private static int height;
private static int length;
public static void main(String[] args) {
String inputFile;
Scanner input = new Scanner(System.in);
System.out.print("Enter input file name: ");
inputFile = input.nextLine();
try {
Integer grid[][] = loadGrid(inputFile);
System.out.println("Before flood fill");
printGrid(grid);
findGroups(grid, 0, 0, 2, height, length);
System.out.println("After flood fill");
printGrid(grid);
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
public static void findGroups(Integer[][] array, int column, int row,
int counter, int height, int length) {
for (int i = 0; i < height; i++) {
for (int j = 0; j < length; j++) {
if (row < 0 || row >= length || column < 0 || column >= height) {
} else {
if (array[i][j] == 1) {
array[i][j] = counter;
findGroups(array, column, row + 1, counter, height, length);
findGroups(array, column, row - 1, counter, height, length);
findGroups(array, column - 1, row, counter, height, length);
findGroups(array, column + 1, row, counter, height, length);
counter++;
}
}
}
}
}
public static Integer[][] loadGrid(String fileName) throws IOException {
FileInputStream fin;
fin = new FileInputStream(fileName);
Scanner input = new Scanner(fin);
height = input.nextInt();
length = input.nextInt();
Integer grid[][] = new Integer[height][length];
for (int r = 0; r < height; r++) {
for (int c = 0; c < length; c++) {
grid[r][c] = input.nextInt();
}
}
fin.close();
return (grid);
}
public static void printGrid(Integer[][] grid) {
for (Integer[] grid1 : grid) {
for (int c = 0; c < grid[0].length; c++) {
System.out.printf("%3d", grid1[c]);
}
System.out.println();
}
}
}
I'm not sure what I am doing wrong, I believe I am moving the counter up after each time. Does anyone have a recommendation of what the issue might be?
Thanks in advance.

Arithmetic operation so that 0, 1, & 2 return 0 | 3, 4, & 5 return 1, etc

I'm trying to take 9x9, 12x12, 15x15, etc. arrays and have the program interpret them as multiple 3x3 squares.
For example:
0 0 1 0 0 0 0 0 0
0 0 0 0 0 2 0 0 0
0 0 0 0 0 0 0 3 0
0 0 0 0 0 0 6 0 0
0 0 4 0 0 0 0 0 0
0 0 0 0 0 5 0 0 0
0 0 0 0 0 0 0 0 0
0 7 0 0 0 0 0 0 0
0 0 0 0 8 0 0 0 9
Will be understood as:
0 0 1 | 0 0 0 | 0 0 0
0 0 0 | 0 0 2 | 0 0 0
0 0 0 | 0 0 0 | 0 3 0
------+-------+------
0 0 0 | 0 0 0 | 6 0 0
0 0 4 | 0 0 0 | 0 0 0
0 0 0 | 0 0 5 | 0 0 0
------+-------+------
0 0 0 | 0 0 0 | 0 0 0
0 7 0 | 0 0 0 | 0 0 0
0 0 0 | 0 8 0 | 0 0 9
Where:
"1" # [0][2] is in box "[0][0]"
"2" # [1][5] is in box "[0][1]"
...
"6" # [3][6] is in box "[1][2]"
...
"9" # [8][8] is in box "[2][2]"
.
I can use row % 3 and column % 3 to determine the row and column values within the box, but how can I determine which box a given value in the array is stored in?
This formula could be used in a method such as the one below.
public int[] determineCoordinatesOfBox(int rowInArray, int columnColumnInArray) {
// determine row value
// determine column value
// return new int[2] with coordinates
}
It seems possible and I've been beating my head over this. Perhaps I'm making a simple problem too difficult?
Many thanks for the help!
Justian
You're looking for the / operator:
box[0] = rowInArray / 3;
box[1] = columnInArray / 3;
If I understand correctly, it's just simple integer division.
Since you're coding Java (it would be the same in at least C, C++ and C#), it's simply / operator:
int rowInArray = 3;
int columnInArray = 7;
int boxY = rowInArray / 3; // will evaluate to 1
int boxX = columnInArray / 3; // will evaluate to 2
int rowInBox = rowInArray % 3; // will evaluate to 0
int columnInBox = columnInArray % 3; // will evaluate to 1
Just keep both the arguments of division integer - 7 / 3 is 2, but 7 / 3.0 or 7.0 / 3 will be 2.5.

Categories

Resources