2014-04-22 16 views
0

Tic Tac Toe 게임의 할당을 완료하려고합니다. AddPiece 및 GetPieceAt 메서드를 제외하고는 다른 모든 요구 사항을 완료했습니다. 나는 이것을 ArrayList로 구현하는 방법과 ArrayList의 (x, y)에서 설정하는 방법에 대해 모든 것을 살펴 보았습니다. 나는 내가 잘못 할당을 이해하고 있을지도 모르는 것처럼 느낀다. 그러나이 점에서 나는 무엇을해야하는지 전혀 모른다. 나는 몇 가지 아이디어를 적어 놓았지만 그 두 가지 방법으로 생각할 것들을 대부분 삭제했다.ArrayList의 x, y 위치에 요소를 추가하십시오.

다른 모든 파일을 여기에 추가하는 번거 로움을 덜기 위해 할당이 게시되는 링크입니다. 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 배열이 아닌 arraylist로 설정하려고하는지 궁금하십니까? –

+0

교수님이이 코드를 우리에게 모두 주셨습니다. // 여기에 귀하의 코드가 있습니다. 개인적으로 2D 배열로이 작업을 수행했지만이 작업은 끝났지 만 그는 그 arraylist를 사용하기를 원합니다. – 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는 입력으로, 당신은 아마 보드의 위치 (X, Y)가 점유하는 경우, 당신은을 반복하여이 작업을 수행 할 수 있습니다

  1. 체크 할 것 arraylist
  2. 위치가 없으면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 
    } 
} 

상기 위치하여 ticTacToe도 ArrayList를 표현할 수

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

내가 수행하는 방법을 모르는 부분은 2D arraylist 또는 2D 배열이 아니기 때문에 주어진 위치에서 배열 목록을 보는 방법입니다. _GamePieces_ arraylist의 위치 (x, y)를 확인하는 방법에 대한 조언이 있으십니까? – thatonechin

+0

arraylist를 통해 반복하여 각 요소를 확인할 수 있습니다. 그렇지 않으면 배열 목록 이외의 다른 데이터 구조가 필요합니다. 또한 위의 코드는 다른 스레드가리스트를 수정하지 않는 한'tttp'가있는 메소드가 맨 아래 메소드보다 선호된다. – Ryan

0

코드 이하 동일하다. 여기서 우리는 2 차원 데이터 구조를 사용하여 보드를 만들 필요가 없습니다.

예를 들어 TicTacToe 게임은 9 개의 위치를 ​​가지고 있으며, 크기 9의 arraylist에 위치를 저장하고 0에서 8 개의 색인 위치를 저장할 수 있습니다.

당신이 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); 

(이 매핑을 처음 수행해야합니다) 다음과 같이 ArrayList의 인덱스 X 및 Y 위치를 매핑하는 공공의 HashMap을 사용 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 값을 가져옵니다.

관련 문제