英文:
Boolean board game in java
问题
以下是翻译好的部分:
import java.util.Arrays;
public class uppg10 {
public static void main(String[] args) {
int[][] array1 = {{1, 1, 2, 3, 3},
{2, 1, 1, 2, 3},
{3, 2, 2, 1, 2},
{3, 3, 3, 3, 3}};
boolean[][] array2 = new boolean[4][5];
int rows = array1.length;
int cols = array1[0].length;
int totalavGrannar = 0;
array2 = new boolean[rows][cols];
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
System.out.print(String.format("%4d", array1[row][col]));
if (((col + 1) % cols == 0) && (col > 0))
System.out.println();
}
}
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
boolean trueorFalse;
if (array1[row][col] < 3) {
trueorFalse = true;
array2[row][col] = trueorFalse;
} else {
trueorFalse = false;
}
row = 1;
col = 1;
for (int offsetRow = row - 1; offsetRow <= row + 1; offsetRow++) {
for (int offsetCol = col - 1; offsetCol <= col + 1; offsetCol++) {
totalavGrannar += array1[offsetRow][offsetCol];
boolean trueorFalse2;
if (totalavGrannar < 15) {
trueorFalse2 = true;
array2[row][col] = trueorFalse2;
} else {
trueorFalse2 = false;
}
}
}
}
}
for (int row = 0; row < array2.length; row++) {
for (int col = 0; col < array2[row].length; col++) {
System.out.print(String.format("%8s", array2[row][col] ? "T" : "F"));
if (((col + 1) % cols == 0) && (col > 0))
System.out.println();
}
}
}
}
英文:
Program should print out the given values in array1 and print the second array as true or false.
Array1 represents a boardgame where characters can stand at the different positions. The integers indicate how dangerous it is to stand at every position.
If the character finds itself at int 3, it is to be declared dead.
If you add the values of each neighbor(not including the current position) and the total value equals 15 or more, it is also to be declared dead.
dead = false (F)
alive = true (T)
So if the current position is 2 or 1 and the total value of every neighbor is less then 15, the character lives. The neighbors missing in the edge of the array is to be counted as 0.
How do i print this the same way as array1 but with boolean values of T or F?
totalavGrannar is the variable that should add all the neighbors total value.
My code so far:
import java.util.Arrays;
public class uppg10{
public static void main(String[] args){
int [][] array1 = {{1,1,2,3,3},
{2,1,1,2,3},
{3,2,2,1,2},
{3,3,3,3,3}};
boolean [][] array2 = new boolean [4][5];
int rows = array1.length;
int cols = array1[0].length;
int totalavGrannar = 0;
array2 = new boolean[rows][cols];
for (int row=0; row<rows; row++) {
for (int col=0; col<cols; col++) {
System.out.print(String.format("%4d", array1[row][col]));
if ( ( (col+1) % cols ==0) && (col > 0))
System.out.println();
}
}
for (int row=0; row<rows; row++) {
for (int col=0; col<cols; col++) {
boolean trueorFalse;
if (array1[row][col]<3) {
trueorFalse = true;
array2[row][col] = trueorFalse;
}
else{
trueorFalse = false;
}
row = 1;
col = 1;
for(int offsetRow=row-1; offsetRow<=row+1; offsetRow++){
for(int offsetCol=col-1; offsetCol<=col+1; offsetCol++){
totalavGrannar += array1[offsetRow][offsetCol];
boolean trueorFalse2;
if (totalavGrannar<15) {
trueorFalse2 = true;
array2[row][col] = trueorFalse2;
}
else{
trueorFalse2 = false;
}
}
}
}
}
for (int row=0; row<array2.length; row++) {
for (int col=0; col<array2[row].length; col++) {
System.out.print(String.format("%8s" , array2[row][col] ? "T" : "F"));
if ( ( (col+1) % cols ==0) && (col > 0))
System.out.println();
}
}
}
}
答案1
得分: 0
import java.util.Arrays;
public class uppg10 {
public static void main(String[] args) {
int[][] array1 = {{1, 1, 2, 3, 3},
{2, 1, 1, 2, 3},
{3, 2, 2, 1, 2},
{3, 3, 3, 3, 3}};
int rows = array1.length;
int cols = array1[0].length;
boolean[][] array2 = new boolean[rows][cols];
int totalavGrannar = 0;
//Print array1
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
System.out.print(String.format("%4d", array1[row][col]));
if (((col + 1) % cols == 0) && (col > 0))
System.out.println();
}
}
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
if (array1[row][col] < 3)
array2[row][col] = true;
//Iterate neighbors to sum values
for (int offsetRow = row - 1; offsetRow <= row + 1; offsetRow++) {
for (int offsetCol = col - 1; offsetCol <= col + 1; offsetCol++) {
if (offsetRow >= 0 && offsetCol >= 0) //0-bounds check
/*if (offsetRow < array1.length && offsetCol < array1.length) //edges-bounds check
if (offsetRow != row && offsetCol != col) { //exclude position from sum
totalavGrannar += array1[offsetRow][offsetCol];
}*/
if (offsetRow < array1.length && offsetCol < array1[0].length) //edges-bounds check
if (offsetRow != row || offsetCol != col) { //exclude position from sum
totalavGrannar += array1[offsetRow][offsetCol];
}
}
}
if (totalavGrannar >= 15)
array2[row][col] = false;
totalavGrannar = 0;
}
}
for (int row = 0; row < array2.length; row++) {
for (int col = 0; col < array2[row].length; col++) {
System.out.print(String.format("%8s", array2[row][col] ? "T" : "F"));
if (((col + 1) % cols == 0) && (col > 0))
System.out.println();
}
}
}
}
英文:
> How do i print this the same way as array1 but with boolean values of T or F?
For your question you did it correct in your code.
Your code not work because you have few others problems:
-
In the second loop you override array2 values even if false(dead).
you make him alive (set to true)if (totalavGrannar<15) { trueorFalse2 = true; array2[row][col] = trueorFalse2; }
Also, you sum the position with all neighbors (total 9 cells instead of only 8 neighbors)
--> In solution I set condition to exclude the position itself.
-
Another problem is that when you sum neighbors you check equality to 15 per addition for updat (T/F). This is wasteful.
--> You need to sum all and only then update value (T/F). -
In the second loop you reset you loop condition to '1' so you have endless loop.
--> I removed those lines. -
You forgot to reset totalavGrannar
A short fix I suggest:
(I also removed some redundant lines)import java.util.Arrays; public class uppg10 { public static void main(String[] args) { int[][] array1 = {{1, 1, 2, 3, 3}, {2, 1, 1, 2, 3}, {3, 2, 2, 1, 2}, {3, 3, 3, 3, 3}}; int rows = array1.length; int cols = array1[0].length; boolean[][] array2 = new boolean[rows][cols]; int totalavGrannar = 0; //Print array1 for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { System.out.print(String.format("%4d", array1[row][col])); if (((col + 1) % cols == 0) && (col > 0)) System.out.println(); } } for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { if (array1[row][col] < 3) array2[row][col] = true; //Iterate neighbors to sum values for (int offsetRow = row - 1; offsetRow <= row + 1; offsetRow++) { for (int offsetCol = col - 1; offsetCol <= col + 1; offsetCol++) { if (offsetRow >= 0 && offsetCol >= 0) //0-bounds check /*if (offsetRow < array1.length && offsetCol < array1.length) //edges-bounds check if (offsetRow != row && offsetCol != col) { //exclude position from sum totalavGrannar += array1[offsetRow][offsetCol]; }*/ if (offsetRow < array1.length && offsetCol < array1[0].length) //edges-bounds check if (offsetRow != row || offsetCol != col) { //exclude position from sum totalavGrannar += array1[offsetRow][offsetCol]; } } } if (totalavGrannar >= 15) array2[row][col] = false; totalavGrannar = 0; } } for (int row = 0; row < array2.length; row++) { for (int col = 0; col < array2[row].length; col++) { System.out.print(String.format("%8s", array2[row][col] ? "T" : "F")); if (((col + 1) % cols == 0) && (col > 0)) System.out.println(); } }
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论