2017-05-28 104 views
1

我收到了一個項目來解決使用枚舉,我有點卡住了。這是一個tic tac腳趾遊戲,它具有將單元格的位置存儲在3x3網格表中的枚舉值,以及存儲單元格狀態的枚舉值(空,X或O)。另外,我有一個返回CellStateGameBoard接口:getCellState (CellLocation cellLocation);從接口返回數據檢索數據

我的任務是隻寫Consultant界面,我無法改變這是給我提供的代碼什麼。 我很努力地在每一步之前檢查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; 
} 

但我已經意識到,當前狀態因爲我正在檢查上面的代碼,所以電路板不能僅僅等於一個單元。它顯然不能等於3個不同的「只有一個細胞」--s。我甚至不知道我是否會被檢查,如果董事會有這個由玩家X只佔用一個單元:

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

可能有人請給我如何可以將前端兩個CellStateCellLocation成一個查詢?我應該使用數組嗎?例如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

感謝您的幫助。 我已經嘗試過你的代碼,並修改了一下,因爲我必須寫的接口將有一個「public CellLocation(GameBoard gameBoard)」方法返回null。 所以它沒有一個主要的方法,並且爲我預先編寫的JUnit測試着眼於這個建議的方法。我將不得不從這裏委託任務。 但是,這種方式我不能將for循環中的枚舉轉換爲cellLocation,只返回一個位置。 它顯示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; 

}