布尔棋盘游戏在Java中

huangapple go评论80阅读模式
英文:

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&lt;rows; row++) {
for (int col=0; col&lt;cols; col++) {
System.out.print(String.format(&quot;%4d&quot;, array1[row][col]));
if ( ( (col+1) % cols ==0) &amp;&amp; (col &gt; 0))
System.out.println();
}
}
for (int row=0; row&lt;rows; row++) {
for (int col=0; col&lt;cols; col++) {
boolean trueorFalse;
if (array1[row][col]&lt;3) {
trueorFalse = true;
array2[row][col] = trueorFalse;
}
else{
trueorFalse = false;
}
row = 1;
col = 1;
for(int offsetRow=row-1; offsetRow&lt;=row+1; offsetRow++){
for(int offsetCol=col-1; offsetCol&lt;=col+1; offsetCol++){
totalavGrannar += array1[offsetRow][offsetCol];
boolean trueorFalse2;
if (totalavGrannar&lt;15) {
trueorFalse2 = true;
array2[row][col] = trueorFalse2;
}
else{
trueorFalse2 = false;
}
}
}
}
}
for (int row=0; row&lt;array2.length; row++) {
for (int col=0; col&lt;array2[row].length; col++) {
System.out.print(String.format(&quot;%8s&quot; , array2[row][col] ? &quot;T&quot; : &quot;F&quot;));
if ( ( (col+1) % cols ==0) &amp;&amp; (col &gt; 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:

  1. In the second loop you override array2 values even if false(dead).
    you make him alive (set to true)

    if (totalavGrannar&lt;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.

  1. 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).

  2. In the second loop you reset you loop condition to '1' so you have endless loop.
    --> I removed those lines.

  3. 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 &lt; rows; row++) {
    for (int col = 0; col &lt; cols; col++) {
    System.out.print(String.format(&quot;%4d&quot;, array1[row][col]));
    if (((col + 1) % cols == 0) &amp;&amp; (col &gt; 0))
    System.out.println();
    }
    }
    for (int row = 0; row &lt; rows; row++) {
    for (int col = 0; col &lt; cols; col++) {
    if (array1[row][col] &lt; 3)
    array2[row][col] = true;
    //Iterate neighbors to sum values
    for (int offsetRow = row - 1; offsetRow &lt;= row + 1; offsetRow++) {
    for (int offsetCol = col - 1; offsetCol &lt;= col + 1; offsetCol++) {
    if (offsetRow &gt;= 0 &amp;&amp; offsetCol &gt;= 0)   //0-bounds check
    /*if (offsetRow &lt; array1.length &amp;&amp; offsetCol &lt; array1.length) //edges-bounds check
    if (offsetRow != row &amp;&amp; offsetCol != col) {      //exclude position from sum
    totalavGrannar += array1[offsetRow][offsetCol];
    }*/
    if (offsetRow &lt; array1.length &amp;&amp; offsetCol &lt; array1[0].length) //edges-bounds check
    if (offsetRow != row || offsetCol != col) {      //exclude position from sum
    totalavGrannar += array1[offsetRow][offsetCol];
    }
    }
    }
    if (totalavGrannar &gt;= 15)
    array2[row][col] = false;
    totalavGrannar = 0;
    }
    }
    for (int row = 0; row &lt; array2.length; row++) {
    for (int col = 0; col &lt; array2[row].length; col++) {
    System.out.print(String.format(&quot;%8s&quot;, array2[row][col] ? &quot;T&quot; : &quot;F&quot;));
    if (((col + 1) % cols == 0) &amp;&amp; (col &gt; 0))
    System.out.println();
    }
    }
    

    }
    }

huangapple
  • 本文由 发表于 2020年10月8日 05:42:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64252729.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定