2

チェッカー ボード ゲームを作成する必要があり、4 つのクラスがあります。

  1. Cell(空のセルを表すため、toString "." を返します)
  2. White(白いセルを表し、toString "W" を返します)
  3. Black (黒のセルを表し、toString "B" を返します)
  4. チェッカー(ボード(Cell[8][8])をセットアップして初期化します。

文字で表される空の白と黒のセルの呼び出しに問題があり.,W,Bます。コードでやるべきことはまだありますが、それらのメソッドを呼び出す際に助けが必要です

cell、Black、および WHite クラスは次のようになります。

  public class Cell {
 public static final String EMPTY=".";

 int i;
 int j;
 String value;

 public Cell(int i, int j){
    this.i = i;
    this.j = j;
    value = EMPTY;
 }

 public String toString(){
    return value;
 }
}

Checkers クラスには、これらのメソッドがあります。他のクラスからメソッドを呼び出す方法がわからないので、Another char 配列を作成して、それらの値を入れました。

    /*********initialization******/
public void init(){
    board = new Cell[8][8];


    char[][] a =
        {
            getEmptyWhite("EW"),
            getEmptyWhite("WE"),
            getEmptyWhite("EW"),
            getEmptyWhite("EE"),
            getEmptyWhite("EE"),
            getEmptyWhite("BE"),
            getEmptyWhite("EB"),
            getEmptyWhite("BE")

        };
    for(int i = 0; i<8; i++){
        for(int j=0; j<8; j++){
            System.out.print(a[i][j]);
        }
        System.out.println();
    }
}


    public char[] getEmptyWhite(String a){
        Cell empty;
        Black black;
        White white;    
        if(a.equals(empty)){
            char[] emptyWhite = {'E','E','E','E','E','E','E','E'};
            return emptyWhite;
        }
        else if(a.equals("EW")){
            char[] emptyWhite = {'E','W','E','W','E','W','E','W'}; 
            return emptyWhite;
        }
        else if(a.equals("EB")){
            char[] emptyWhite = {'E','B','E','B','E','B','E','B'};
            return emptyWhite;
        } else if(a.equals("WE")){
            char[] emptyWhite = {'W','E','W','E','W','E','W','E'};
            return emptyWhite;          
        } else if(a.equals("BE")){
            char[] emptyWhite = {'B','E','B','E','B','E','B','E'};
            return emptyWhite;          
        } 
        return null;
    }



/*********initialization ended*******/
4

1 に答える 1

0

内部getEmptyWhiteでは、この行の代わりに次のようになります。

if(a.equals(empty)){

これを使用する必要があります:

if(a.equals("EE")){

投稿したコードでは、値emptyが の型の変数であることに注意してください。したがって、常に になります。また、ローカル変数、、およびは実際には必要ありません。Cellnulla.equals(empty)falseemptyblackwhite

于 2013-07-31T21:14:31.583 に答える