2014-11-05 46 views
0

我正在爲類和一個類創建一個tic-tac-toe遊戲,我創建一個包含String數組的Board對象,然後將其傳遞給玩家類。然而,我無法弄清楚如何讓我在新課堂中使用這些信息。這裏有人能給我一些指點嗎?將數組鏈接到Java中的另一個類

public static void main(String[] args) 
    { 
     //new tic-tac-toe board 
     Board board = new Board(); 

     //two new players (computer and human) 
     Player computer = new Player(board, "X"); //Give computer player access to board and  assign as X. 
     Player human = new Player(board, "O");  //Give human player access to board and assign as O. 

package outlab5; 
import java.util.Scanner; 
public class Player { 
    private String[][] currentBoard; 
    private String move; 
    Scanner input = new Scanner(System.in); 


    public Player(Board inBoard, String inMove){ 
     move = inMove; 
    } 
    public void computerMove(){ 
     boolean valid = false; 
    while(!valid){ 
     int moveCols = (int)(Math.random()*4); 
     int moveRows = (int)(Math.random()*4); 

     System.out.print(currentBoard[0][0]); 

     } 
    } 
+0

也許你'的String []'應該是某種類的。也許'董事會'? – 2014-11-05 00:57:52

回答

0

我試圖使用它的類,我認爲你的板子類具有代表的String [] []數組,你正在尋找一個領域。 在你的播放器類中,正確地存儲Borad對象。

public class Player { 
private String[][] currentBoard; 
private String move; 
private Board board; //define a variable 
Scanner input = new Scanner(System.in); 


public Player(Board inBoard, String inMove){ 
    board = inBoard; 
    move = inMove; 
} 

你不顯示板類的代碼,所以我猜你會如何訪問字符串[] [],可能是主板類提供了一些獲取的方法,以訪問字符串數組。

String[][] currentBoard = board.get....(); //this call must be placed in a method 
0

下面是一個例子,說明如何做你的應用程序。

板類

public class Board { 
    // TODO : Stuff and Stuff (Where your 3x3 Matrix may be) 
} 

球員抽象類

abstract class Player { 

    private final Board board; 
    private final String move; 

    public Player(Board _board, String _move) { 
     this.board = _board; 
     this.move = _move; 
    } 

    public void playerMove() { 
     // TODO : Default Movement Actions 
    } 

    public void playerWin() { 
     // TODO : Default Event on Player Win 
    } 
} 

計算機類

public class Computer extends Player { 
    public Computer(Board _board, String _move) { 
     super(_board, _move); 
    } 

    @Override 
    public void playerMove() { 
     // TODO : Computer Related Movements (Like AI) 
     super.playerMove(); 
    } 

    @Override 
    public void playerWin() { 
     // TODO : Computer Related Events for Computer (Like Increase Dif) 
     super.playerWin(); 
    } 
} 

人力類

public class Human extends Player { 
    public Human(Board _board, String _move) { 
     super(_board, _move); 
    } 

    @Override 
    public void playerMove() { 
     // TODO : Human Related Movements (Like I/O) 
     super.playerMove(); 
    } 

    @Override 
    public void playerWin() { 
     // TODO : Human Related Events on Win (Like Score) 
     super.playerWin(); 
    } 
} 
相關問題