2014-04-22 73 views
0

我正在嘗試完成這個Tic Tac Toe遊戲的任務。除了方法AddPiece和GetPieceAt之外,我完成了所有其他要求。我已經搜索了幾乎所有關於如何將其實現到ArrayList以及如何在ArrayList的(x,y)中設置它的一切。我覺得我可能會錯誤地理解這個任務,但是現在我不知道該怎麼做。我有一些想法寫下來,但我已經刪除了大部分我認爲會用這兩種方法去做的事情。在位置x,y的ArrayList中添加一個元素

爲了減少在這裏添加所有其他文件的麻煩,這是發佈任務的鏈接。 http://go.code-check.org/codecheck/files/1404121614cuepj4pvhuprowa1awz8s0642

任何幫助和指導將非常感激。

這是我對文件名TicTacToeBoard.java

代碼
import java.util.ArrayList; 


public class TicTacToeBoard extends GameBoard 
{ 
/** 
* The pieces in this game. 
*/ 
ArrayList<TicTacToePiece> GamePieces; 

/** 
* Constructor. Instantiate the GamePieces ArrayList. 
*/ 
public TicTacToeBoard() 
{ 
    // YOUR CODE HERE 
    super(0, 0); 
    GamePieces = new ArrayList<TicTacToePiece>(); 
} 

/** 
* empty out the GamePieces ArrayList 
*/ 
public void Reset() 
{ 
    // YOUR CODE HERE 
    GamePieces.clear(); 
} 


/** 
* Fill a space with the newPiece, IF THAT SPACE IS EMPTY. 
* 
* @param x the first, horizontal coordinate for the next move 
* @param y the second, vertical coordinate for the next move 
* @newPiece the piece to place at the location 
*/ 
public void AddPiece(int x, int y, TicTacToePiece newPiece) 
{ 
    // YOUR CODE HERE 
    GamePiece gp = new GamePiece(x,y); 


    gp.GetPosition(); 


//  GamePieces.add((int) gp.GetPosition(), newPiece); 


} 

/** 
* Get a GamePiece at a specific position. 
* 
* @param x the first, horizontal coordinate for the next move 
* @param y the second, vertical coordinate for the next move 
* @return the game piece at position x, y. or null if there is none 
*/ 
public TicTacToePiece GetPieceAt(int x, int y) 
{ 
    // YOUR CODE HERE 


    return null; 

} 

/** 
* Checks the board for win or draw conditions and update the GameState property appropriately. 
* 
* @return the GameStatus of this game 
*/ 
public GameStatus CheckForWin() 
{ 
    TicTacToeGame t = new TicTacToeGame(); 


    if(t.GetGameState() == GameStatus.ON) 
     return GameStatus.ON; 
    else if(t.GetGameState() == GameStatus.WIN_PLAYER_1) 
     return GameStatus.WIN_PLAYER_1; 
    else if(t.GetGameState() == GameStatus.WIN_PLAYER_2) 
     return GameStatus.WIN_PLAYER_2; 
    else 
     return GameStatus.DRAW; 


    // YOUR CODE HERE 

} 

/** 
* Create a Board[][] array. This is a helper function that I used so that I could reuse code from Assignment 1. You do not have to implement this method. 
* 
* @return a two dimensional array of Strings 
*/ 
private String[][] GetGameBoard() 
{ 
    // YOUR CODE HERE 
    String[][] Board = new String[3][3]; 

    for(int i = 0; i < 3; i++) 
     for(int j = 0; j < 3; j++) 
      Board[i][j] = "-"; 

    return Board; 
} 

// /** 
// * Checks a string for win conditions. If three in a row occur, then it returns the proper GameState. 
// * This is a helper function that I used, but is not required for you to implement. 
// * 
// * @param Input a representation of a row, column, or diagonal in the game. 
// * 
// * @return the proper GameStatus for a row, column, or diagonal represented by the Input String 
// *   "---" would indicate an entirely free row, column or diagonal, in which case it should return GameStatus.ON. 
// *   "000" indicates a row, column, or diagonal that has been won by player  1. 
// *   "111" indicates a row, column, or diagonal that has been won by player 2. 
// */ 
// private GameStatus CheckStringForThree(String Input) 
// { 
//  // YOUR CODE HERE 
// } 

/** 
* Print the game board to stdout. 
* 0 should be used to represent moves by player 1. 
* 1 should be used to represent moves by player 2. 
* - should be used to represent a free space. 
* One blank space should occur between each space. 
* So an empty game board would be 
* - - - 
* - - - 
* - - - 
* And a game might look like 
* 0 - 1 
* 0 - - 
* 1 - 0 
* WARNING: If you are storing the game board as Board[x][y], then the traditional nested loops won't 
* print the board properly. x should be the horizontal coordinate. y should be the vertical coordinate. 
*/ 
public void Print() 
{ 
    // YOUR CODE HERE 

    for(int r = 0; r < 3; r++) 
    { 
     for(int c = 0; c < 3; c++) 
     { 
       System.out.print(GetGameBoard()[r][c]); 
     } 
     System.out.println(); 
    } 
    System.out.println(GamePieces); 
} 

} 

+1

想知道爲什麼要將它設置爲一個數組列表而不僅僅是一個2D數組? –

+0

教授給了我們所有這些代碼,無論它在哪裏//你的代碼都是我們所做的。就我個人而言,我會用二維數組完成它,並完成這件事,但他希望我們使用該數組列表。 – thatonechin

回答

0

你TicTacToePiece包含x和y的值

public TicTacToePiece(int newX, int newY, int newPlayerNumber, String newSymbol) 

和你GamePieces是包括TicTacToePieces

GamePieces = new ArrayList<TicTacToePiece>(); 

而且,由於你的AddPiece函數有X,Y,newPiece作爲輸入,你可能不得不

  1. 檢查,如果在電路板上的位置(X,Y)被佔用,您可以通過迭代做到這一點陣列列表
  2. 如果位置未被佔用,在必要時適當設置newPiece sx,y值後,將新的棋子添加到棋盤(arraylist)。

編輯:::

for (TicTacToePiece tttp : GamePieces) { 
/* Note that this 'tttp' is each element in the Gameieces arraylist 
    You are just iterating through all elements in the array checking if condition meets */ 
    if (tttp.x == x && tttp.y == y) { 
     //x and y value match, do something 
    } 
} 

以上等同於下面的代碼

for (int i = 0; i < GamePieces.size(); i++) { 
    if (GamePieces.get(i).x == x && GamePieces.get(i).y == y) 
      //dosomething 
    } 
} 
+0

我不確定如何完成的部分是如何查看給定位置處的數組列表,因爲它不是2D數組列表或2D數組。有關如何檢查_GamePieces_ arraylist的位置(x,y)的任何建議? – thatonechin

+0

您可以遍歷arraylist,檢查每個元素。否則,你將需要另一個數組結構而不是數組列表。另外請注意,除非不同的線程正在修改列表,否則上面的代碼使用'tttp'是首選的方法。 – Ryan

0

井字遊戲位置也可以在ArrayList中來表示。在這裏,我們不必使用二維數據結構來製作電路板。

例如TicTacToe遊戲有9個職位,您可以將職位存儲在大小爲9的數組列表中,並存儲0到8個索引的職位。

如果你要映射的X和Y位置到ArrayList,使用公共的HashMap與ArrayList的索引X和Y位置映射如下(此映射首先應該做)

HashMap<Integer, Integer> hMap=new HashMap<Integer, Integer>(); 
     hMap.put(00, 0); 
     hMap.put(01, 1); 
     hMap.put(02, 2); 
     hMap.put(10, 3); 
     hMap.put(11, 4); 
     hMap.put(12, 5); 
     hMap.put(20, 6); 
     hMap.put(21, 7); 
     hMap.put(22, 8); 

在hMap 00映射到ArrayList中的第0個索引,01映射到第1個索引並繼續。

因此,當你從功能參數x和y的值,構建「XY」字符串,並從如下的ArrayList中的相應位置,

 int pos=0; 
     int x=1; 
     int y=2; 

     pos=Integer.parseInt(String.valueOf(x)+String.valueOf(y)); 
     int listPos=hMap.get(pos); 

而得到X和Y位置,遍歷所有ArrayList對象,獲取索引並使用索引從HashMap中獲取X和Y值。

相關問題