java 中的布尔棋盘游戏
Boolean board game in java
程序应打印出 array1 中的给定值,并将第二个数组打印为 true 或 false。
Array1 表示角色可以站在不同位置的棋盘游戏。整数表示站在每个位置有多危险。
如果角色发现自己处于 int 3,则将被宣布死亡。
如果将每个邻居的值(不包括当前位置)相加,总值等于或大于15,则也宣告死亡。
死 = 假 (F)
活着 = 真 (T)
所以如果当前位置是2或1,并且每个邻居的总值都小于15,则角色存活。数组边缘缺失的邻居算作0个
如何以与 array1 相同的方式打印它,但布尔值为 T 或 F?
totalavGrannar 是应该添加所有邻居总值的变量。
到目前为止我的代码:
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();
}
}
}
}
How do i print this the same way as array1 but with boolean values of T or F?
对于您的问题,您在代码中做出了更正。
您的代码不起作用,因为您几乎没有其他问题:
在第二个循环中,即使 false(dead) 也覆盖了 array2 值。
你让他活着(设为真)
if (totalavGrannar<15) {
trueorFalse2 = true;
array2[row][col] = trueorFalse2;
}
此外,您将位置与所有邻居相加(总共 9 个单元而不是只有 8 个邻居)
--> 在解决方案中,我设置了排除位置本身的条件。
另一个问题是,当您对邻居求和时,您检查每次添加更新时是否等于 15 (T/F)。这是浪费。
--> 您需要对所有内容求和,然后才更新值 (T/F)。
在第二个循环中,您将循环条件重置为“1”,这样您就有了无限循环。
--> 我删除了那些行。
您忘记重置 totalavGrannar
我建议一个简短的修复:
(我也删除了一些多余的行)
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();
}
}
}
}
程序应打印出 array1 中的给定值,并将第二个数组打印为 true 或 false。
Array1 表示角色可以站在不同位置的棋盘游戏。整数表示站在每个位置有多危险。
如果角色发现自己处于 int 3,则将被宣布死亡。 如果将每个邻居的值(不包括当前位置)相加,总值等于或大于15,则也宣告死亡。
死 = 假 (F) 活着 = 真 (T)
所以如果当前位置是2或1,并且每个邻居的总值都小于15,则角色存活。数组边缘缺失的邻居算作0个
如何以与 array1 相同的方式打印它,但布尔值为 T 或 F?
totalavGrannar 是应该添加所有邻居总值的变量。
到目前为止我的代码:
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();
}
}
} }
How do i print this the same way as array1 but with boolean values of T or F?
对于您的问题,您在代码中做出了更正。 您的代码不起作用,因为您几乎没有其他问题:
在第二个循环中,即使 false(dead) 也覆盖了 array2 值。 你让他活着(设为真)
if (totalavGrannar<15) { trueorFalse2 = true; array2[row][col] = trueorFalse2; }
此外,您将位置与所有邻居相加(总共 9 个单元而不是只有 8 个邻居)
--> 在解决方案中,我设置了排除位置本身的条件。
另一个问题是,当您对邻居求和时,您检查每次添加更新时是否等于 15 (T/F)。这是浪费。 --> 您需要对所有内容求和,然后才更新值 (T/F)。
在第二个循环中,您将循环条件重置为“1”,这样您就有了无限循环。 --> 我删除了那些行。
您忘记重置 totalavGrannar 我建议一个简短的修复: (我也删除了一些多余的行)
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(); } }
} }