Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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, it is also to be declared dead.

So if the current position is 2 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?

What I have tried:

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;

   int rows = array1.length;
   int cols = array1[0].length;

   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();
     }

   }

 }
}
Posted
Updated 5-Oct-20 0:21am
v2
Comments
Richard MacCutchan 5-Oct-20 5:00am    
What are the criteria for declaring something true or false?
Helin1 5-Oct-20 5:13am    
For True: integer should be less then 3, and the added value of all the neighbors in the array should be less then 15
Richard MacCutchan 5-Oct-20 5:20am    
Then the issue is the same as for alive or dead. Calculate the values for array 1, and set True or False in array2 as appropriate.

Try
Java
  int sum_adjacent_values(int r, int c)
  {
    int sum = - position_array[r][c];

    for (int i = -1; i <= 1; ++i)
    {
      for (int j = -1; j <= 1; ++j)
      {
        int row = r+i;
        int col = c+j;

        if ( (row >= 0 && row < Rows) && ( col >= 0 && col < Cols))
        {
          sum += position_array[row][col];
        }
      }
    }
    return sum;
  }
  void compute_state_array()
  {
    for (int row = 0; row < Rows; ++row)
    {
      for (int col = 0; col < Cols; ++col)
      {
        if ( position_array[row][col] == 3)
        {
          state_array[row][col] = false;
        }
        else
        {
          int sum_of_adjacent_values = sum_adjacent_values(row, col);

          if ( sum_of_adjacent_values == 15)
            state_array[row][col] = false;
          else
            state_array[row][col] = true;
        }
      }
    }
  }

  public final boolean [][] get_state_array(){ return state_array; }

  public Bg(int [][] a)
  {
    Rows = a.length;
    Cols = a[0].length;

    position_array = new int[Rows][Cols];
    state_array = new boolean[Rows][Cols];

    for (int row = 0; row < Rows; ++row)
      for (int col = 0; col < Cols; ++col)
        position_array[row][col] = a[row][col];

    compute_state_array();
  }


  public static void show( int [][] a)
  {
    for (int r = 0; r < a.length; ++r)
    {
      for (int c = 0; c < a[0].length; ++c)
        System.out.print(String.format("%6d", a[r][c]));
      System.out.println();
    }
  }

  public static void show( boolean [][] a)
  {
    for (int r = 0; r < a.length; ++r)
    {
      for (int c = 0; c < a[0].length; ++c)
        System.out.print(String.format("%6b", a[r][c]));
      System.out.println();
    }
  }

  public static void main(String[] args)
  {
    int [][] position_array =
    {
      {1,1,2,3,3},
      {2,1,1,2,3},
      {3,2,2,1,2},
      {3,3,3,3,3}
    };

    Bg bg = new Bg( position_array);
    boolean [][] state_array = bg.get_state_array();

    show( position_array);
    show( state_array);
  }

}
 
Share this answer
 
Comments
Patrice T 7-Oct-20 2:39am    
5
Use nested loops, just as you do for array1, but instead of formatting a number as a string, use the java conditional (or ternary) operator: What is the conditional operator ?: in Java?[^]
Java
System.out.print(array2[row][col] ? "T" : "F");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900