2012-06-26 4 views
-1

4 가지 방법을 완료해야하지만이를 이해하는 데 어려움이 있습니다. whitemove()을 완료하려고합니다. 나는 이사회를 통해 반복해야한다는 것을 알고 있지만 어떻게해야하는지 완전히 이해할 수는 없습니다!Java의 HexapawnBoard 클래스

import java.util.List; 
import java.util.ArrayList; 

public class HexapawnBoard { 
private static final int WHITE_PIECE = 1; 
private static final int BLACK_PIECE = 2; 
private static final int EMPTY = 0; 

private static final int BOARD_SIZE = 3; 

private int[][] board; 

/** 
* Create a board position from a string of length BOARD_SIZE*BOARD_SIZE (9). 
* The first BOARD_SIZE positions in the string correspond to the black player's home 
* position. The last BOARD_SIZE positions in the string correspond ot the white player's 
* home position. So, the string "BBB WWW" corresponds to a starting position. 
* 
* @param pos the string encoding the position of the board 
*/ 
public HexapawnBoard(String pos) 
{ 
    if(pos.length() != BOARD_SIZE * BOARD_SIZE) 
     throw new RuntimeException("HexapawnBoard string must be of length BOARD_SIZExBOARD_SIZE"); 

    board = new int[BOARD_SIZE][BOARD_SIZE]; 
    for(int row=0;row<BOARD_SIZE;row++) 
    { 
     for(int col=0;col<BOARD_SIZE;col++) 
     { 
      switch(pos.charAt(row*BOARD_SIZE+col)) 
      { 
      case 'B': 
      case 'b': 
       board[row][col] = BLACK_PIECE; 
       break; 
      case 'W': 
      case 'w': 
       board[row][col] = WHITE_PIECE; 
       break; 
      case ' ': 
       board[row][col] = EMPTY; 
       break; 
      default: 
       throw new RuntimeException("Invalid Hexapawn board pattern " + pos); 
      } 
     } 
    } 
} 

/** 
* A copy constructor of HexapawnBoard 
* 
* @param other the other instance of HexapawnBoard to copy 
*/ 
public HexapawnBoard(HexapawnBoard other) 
{ 
    board = new int[BOARD_SIZE][BOARD_SIZE]; 
    for(int i=0;i<BOARD_SIZE;i++) 
     for(int j=0;j<BOARD_SIZE;j++) 
      this.board[i][j] = other.board[i][j]; 
} 


/** 
* Return a string version of the board. This uses the same format as the 
* constructor (above) 
* 
* @return a string representation of the board. 
*/ 
public String toString() 
{ 
    StringBuffer sb = new StringBuffer(); 
    for(int row=0;row<BOARD_SIZE;row++) 
    { 
     for(int col=0;col<BOARD_SIZE;col++) 
     { 
      switch(board[row][col]) 
      { 
      case BLACK_PIECE: 
       sb.append('B'); 
       break; 
      case WHITE_PIECE: 
       sb.append('W'); 
       break; 
      case EMPTY: 
       sb.append(' '); 
       break; 
      } 
     } 
    } 
    return sb.toString(); 
} 

/** 
* Determine if this board position corresponds to a winning state. 
* 
* @return true iff black has won. 
*/ 
public boolean blackWins() 
{ 
    for(int col=0;col<BOARD_SIZE;col++) 
     if(board[BOARD_SIZE-1][col] == BLACK_PIECE) 
      return true; 
    return false; 
} 

/** 
* Determine if this board position corresponds to a winning state 
* 
* @return true iff white has won 
*/ 
public boolean whiteWins() 
{ 
    for(int col=0;col<BOARD_SIZE;col++) 
     if(board[0][col] == WHITE_PIECE) 
      return true; 
    return false; 
} 

/** 
* Determine if black has a move 
* 
* @ return truee iff black has a move 
*/ 
public boolean blackCanMove() 
{ 

    return false; 
} 

/** 
* Return a List of valid moves of white. Moves are represented as valid 
* Hexapawn board positions. If white cannot move then the list is empty. 
* 
* @return A list of possible next moves for white 
*/ 
public List<HexapawnBoard> whiteMoves() 
{ 
    return null 
} 

/** 
* Determine if two board positions are equal 
* @param other the other board position 
* @return true iff they are equivalent board positions 
*/ 
public boolean equals(HexapawnBoard other) 
{ 

    return true; 
} 

/** 
* Determine if two board positions are reflections of each other 
* @param other the other board position 
* @return true iff they are equivalent board positions 
*/ 
public boolean equalsReflection(HexapawnBoard other) 
{ 

    return true; 
} 

} 
+0

이 코드는 무엇입니까? 그것은 무엇을할까요? 어떤 오류가 있습니까? –

+0

숙제 인 경우 숙제 태그를 추가하십시오. – Leigh

답변

0

먼저 코드가 올바른 것으로 보입니다.

네 가지 남은 방법에 대해,이 hexapawn 게임에서 움직이는 규칙이 무엇인지 알지 못하기 때문에 blackCanMove() 또는 whiteMoves() 방법을 사용하면 도움이되지 않습니다. 규칙을 설명하면 도움이 될 것입니다.

equals()equalsReflection() 메서드에 대해서는 기본적으로 복사 생성자에서 수행 한 작업을 수행해야하지만 복사하는 대신 조건 테스트를 수행해야합니다. 두 순위가 예상 한 것과 일치하지 않으면 false을 반환하십시오. 그렇지 않으면 각 사각형을 확인하고 모두 맞으면 true을 반환하십시오.

whiteMoves() 방법까지 보드를 반복하여 흰색 조각을 모두 찾아야합니다. 각 흰색 조각에는 몇 가지 (최대 3 개) 유효한 이동이있을 수 있습니다. 각 위치를 점검하여 가능한지 확인하십시오. 조각은 그 지점에 검은 색 조각이 있으면 대각선으로 이동할 수 있으며, 그 지점에 조각이 전혀 없으면 앞으로 움직일 수 있습니다. 각각의 유효한 이동에 대해 보드가 그 움직임에 뒤 따르는 모습을 나타내는 새로운 HexapawnBoard을 작성하십시오. 메서드가 끝나면 작성한 HexapawnBoard의 모든 목록을 반환합니다.

+0

다니엘 감사합니다. – Moe

+0

@MoeAlShaiban : 나는 학교에서 학생이 아니기 때문에 pdf에 접속할 수 없다. 규칙을 설명해 주시겠습니까? – Daniel

+0

죄송합니다. 가능한 모든 동작에 대한 그림입니다. http://www.flickr.com/photos/moe0/7449939014/in/photostream – Moe