Othello board's difficulty during production

Asked 2 years ago, Updated 2 years ago, 27 views

I'm NU'EST and I started producing Othello programs yesterday. It's nothing else f = EMPRY; Although all arrays were initialized to " "" with the code, the output results showed that all arrays It is nullified. Where did it go wrong?

package project;
import java.util.Arrays;


public class Main{
    private static final int BOARD_SIZE = 8;
    private static final String WHITE = "●";
    private static final String BLACK = "○";
    private static final String EMPRY = " º "; 

    public static void main(String[] args){
        String[][] board = new String[BOARD_SIZE][BOARD_SIZE];
        startGame(board);
    }
    private static void startGame(String[][] input){
        for(String e[] : input){
            for(String f : e){
                f = EMPRY;
            }
        }
        for(String e[] : input){
            for(String f : e){
                System.out.print(f);
            }
            System.out.println();
        }
    }
}

java

2022-09-21 23:15

2 Answers

The string is a constant. It means that it is not changeable.

The moment you make it "", it is created in the constant pool in jvm.

When you do f = EMPRY , it points to a different address (EMPRY string address).

To do what you want, you must substitute f in the array after f = EMPRY.


2022-09-21 23:15

It should be like this

jshell> int BOARD_SIZE = 8
jshell> String[][] board = new String[BOARD_SIZE][BOARD_SIZE]

jshell> for(String[] arr : board) for(int i = 0; i < BOARD_SIZE; i++) arr[i] = "0"

jshell> board
board ==> String[8][] { String[8] { "0", "0", "0", "0", "0", "0", "0", "0" }, String[8] { "0", "0", "0", "0", "0", "0", "0", "0" }, String[8] { "0", "0", "0", "0", "0", "0", "0", "0" }, String[8] { "0", "0", "0", "0", "0", "0", "0", "0" }, String[8] { "0", "0", "0", "0", "0", "0", "0", "0" }, String[8] { "0", "0", "0", "0", "0", "0", "0", "0" }, String[8] { "0", "0", "0", "0", "0", "0", "0", "0" }, String[8] { "0", "0", "0", "0", "0", "0", "0", "0" } }

jshell> for(String[] arr : board) for(int i = 0; i < BOARD_SIZE; i++) arr[i] = "1"

jshell> board
board ==> String[8][] { String[8] { "1", "1", "1", "1", "1", "1", "1", "1" }, String[8] { "1", "1", "1", "1", "1", "1", "1", "1" }, String[8] { "1", "1", "1", "1", "1", "1", "1", "1" }, String[8] { "1", "1", "1", "1", "1", "1", "1", "1" }, String[8] { "1", "1", "1", "1", "1", "1", "1", "1" }, String[8] { "1", "1", "1", "1", "1", "1", "1", "1" }, String[8] { "1", "1", "1", "1", "1", "1", "1", "1" }, String[8] { "1", "1", "1", "1", "1", "1", "1", "1" } }


2022-09-21 23:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.