2017-05-28 5 views
1

私はEnumを使用する解決プロジェクトを受け取りました。これは、3x3のグリッドテーブルにセルの位置を格納する列挙型と、セルの状態(空、X、またはO)を格納する列挙型を持つtic tac toeゲームです。 getCellState (CellLocation cellLocation);enumを返すインターフェイスからデータを取得する

マイタスクのみConsultantインタフェースを記述することであり、私が私に与えられたコードには何も変更することはできません:プラス私はCellState返しGameBoardためのインタフェースを持っています。 私は各ステップの前にBoardのステータスをチェックするのに苦労しています。

私の考えは、Xがゲームを開始し、私はステップのためにforループを使用し、各ステップで、私はif nrOfSteps%2==0をチェックしました。 の場合、私はcellStateをプレーヤーOに設定しました。勝った場合、または引き分けがある場合は、勝利位置にいるかどうかを確認してください。もしそうでなければ、私は動きを提案するでしょう。 など。

if (nrOfSteps%2!=0){ 
    cState =CellState.OCCUPIED_BY_X; 
     if (isWon(cState, board)){ 
     throw new IllegalStateException("Player X won!"); 
     } else if (draw(board, locations)){ 
     throw new IllegalStateException("No more empty fields!"); 
     } else 
     for (int remainingCells = 0; remainingCells <9; remainingCells++) { 
      if (board.equals(locations[remainingCells]) && 
      cState==CellState.EMPTY){ 
      availableCells[index] = locations[remainingCells]; 
      index++; 
     } 
     CellLocation cellToTake = availableCells[(int)(Math.random()* 
     (availableCells.length-1))]; 
     return cellToTake; 
} 

さて、私の問題は、私はisWon方法(今の最初の行だけを検証する部分コード)については、以下を試してみましたということである。

private boolean isWon(CellState player, GameBoard board){ 
if (board.equals(CellLocation.TOP_LEFT) && cState==player && 
    board.equals(CellLocation.TOP_CENTRE) && cState==player && 
    board.equals(CellLocation.TOP_RIGHT) && cState==player){ 
    return true; 
} 

return false; 
} 

しかし、私は、現在の状況と認識しています私は上記のコードをチェックしているので、ボードは1つのセルと同等ではありませんでした。そして、明らかに、3つの異なる「ただ1つの細胞」-sに等しいことはできません。

board.equals (CellLocation.TOP_LEFT) && cState==player 

誰かが私は1つにCellStateCellLocationの両方を組み込むことができますどのようにヒントを与えてもらえ:ボードは、このことにより、プレイヤーXによって占められる唯一つのセルがあった場合、私がチェックすることができればと私もわかりませんクエリ?配列を使うべきですか?例えばCellState[][]

答えて

2

行列内のセルの合計を計算することができます(各列、行および対角線ごとに)。

import java.io.IOException; 
import java.util.Arrays; 
import java.util.Random; 

public class CrossAndZeros { 
    private static CellState winner; 
    private static Enum[][] field = new Enum[3][3]; 

    public static void main(String[] args) throws IOException { 

     for (int i = 0; i < field.length; i++) { 
      for (int j = 0; j < field[i].length; j++) { 
       field[i][j] = CellState.values()[new Random().nextInt(3)]; 
      } 
     } 

     for (Enum[] enums : field) { 
      System.out.println(Arrays.toString(enums)); 
     } 
     System.out.println(); 

     System.out.println("Winner is found: " + isWinnerFound()); 

     System.out.println(winner == null ? "No winner, GAME OVER" : winner); 
    } 

    private static boolean isWinnerFound() { 
     int[] result = calculate(); 
     int count = 0; 

     for (int win : result) { 
      if (win == 3) { 
       winner = CellState.OCCUPIED_BY_X; 
       return true; 
      } else if (win == -12) { 
       winner = CellState.OCCUPIED_BY_O; 
       return true; 
      } else if (win == -9 || win == -2 || win == -3) { // This means that the line is spoilt 
       count++; 
      } 
     } 
     return count == 8; // If all the lines are spoilt, the game is over 
    } 

    private static int[] calculate() { 
     int[] result = new int[8]; 

     for (int i = 0; i < field.length; i++) { 
      for (int j = 0; j < field[i].length; j++) { 
      result[i] += getCellOwner(field[j][i]); // a column 
      result[i + 3] += getCellOwner(field[i][j]); // a row 
     } 
     result[field.length * 2] += getCellOwner(field[i][i]); // diagonal 
     result[field.length * 2 + 1] += getCellOwner(field[i][field.length - i - 1]); // diagonal 

     } 

     System.out.println(Arrays.toString(result)); 

     return result; 
    } 

    private static int getCellOwner(Enum cell) { 
     switch ((CellState) cell) { 
      case OCCUPIED_BY_O: 
       return -4; 
      case OCCUPIED_BY_X: 
       return 1; 
      case EMPTY: 
      default: 
       return 0; 
     } 
    } 

    public enum CellState { 
     /** 
     * this cell is occupied by player X 
     */ 
     OCCUPIED_BY_X, 

     /** 
     * this cell is occupied by player O 
     */ 
     OCCUPIED_BY_O, 

     /** 
     * this cell is Empty 
     */ 
     EMPTY 
    } 
} 
+0

おかげで、このように。 私はあなたのコードを試して、それを少し変更しました。なぜなら、私が書かなければならないインターフェースはnullを返す "Public CellLocation suggest(GameBoard gameBoard)"メソッドを持つからです。 それはmainメソッドを持っていないし、私のためにあらかじめ書かれたJUnitテストは、この提案メソッドを見ています。ここからタスクを委任する必要があります。 しかし、この方法では、forループ内のenumをcellLocationに変換して1つの場所だけを返すことはできません。 getCellOwnerメソッドが変数に解決できないというエラーが表示されています。 – Vero

+0

CellStateクラスとCellLocationクラスを表示できますか? –

+0

コメントに投稿できないので、私はaswerとして投稿しました。ありがとう。 – Vero

1

CellLocation列挙型:

public enum CellLocation { 
    /** The Cell in the top row and leftmost column */ 
    TOP_LEFT, 

    /** The Cell in the top row and centre column */ 
    TOP_CENTRE, 

    /** The Cell in the top row and rightmost column */ 
    TOP_RIGHT, 

    /** The Cell in the centre row and leftmost column */ 
    CENTRE_LEFT, 

    /** The Cell in the centre row and centre column */ 
    CENTRE_CENTRE, 

    /** The Cell in the centre row and rightmost column */ 
    CENTRE_RIGHT, 

    /** The Cell in the bottom row and leftmost column */ 
    BOTTOM_LEFT, 

    /** The Cell in the bottom row and centre column */ 
    BOTTOM_CENTRE, 

    /** The Cell in the bottom row and rightmost column */ 
    BOTTOM_RIGHT; 
} 

CellState列挙型:あなたの助けのための

public enum CellState { 
/** this cell is occupied by player X */ 
OCCUPIED_BY_X, 

/** this cell is occupied by player O */ 
OCCUPIED_BY_O, 

/** this cell is Empty */ 
EMPTY; 

}

関連する問題