게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
본삭금)전에 자바로 체스게임 만드는 거에 대해 질문한 사람입니다
게시물ID : programmer_9282짧은주소 복사하기
작성자 : KOTHAICHI
추천 : 0
조회수 : 694회
댓글수 : 9개
등록시간 : 2015/04/12 02:00:27
옵션
  • 본인삭제금지
안녕하세요. 이제 막 자바에 대해 알아가는 대학생 1학년입니다.

저번 질문글에 답변이 많은 도움이 되었습니다. 덕분에 막힘없이 코드를 쓰다가, 테스트를 하는 도중에 문제가 발생했는데, 도움을 주시면 감사하겠습니다.

제가 가진 문제는 체스의 퀸의 움직임을 짜는 코드에 대한 것입니다.

스택 오버플로우나, 구글에 검색하여 도움이 될 만한 글들을 참조하였으나 (예를 들어 http://codereview.stackexchange.com/questions/53875/generating-possible-chess-moves )

제가 쓴 코드와는 다른 방식으로 짜여져 있는 경우가 많아 큰 도움이 되질 않았습니다.

물론 해당 글들의 코드와 비슷하게 다시 짤 수도 있었지만, 제가 스스로 코드를 쓰기 위해 들인 시간과 노력이 아까워 이렇게 질문합니다.

우선은 해당 코드입니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.ArrayList;
 
public class Queen extends Piece{
    
  public Queen(int ix, int iy, int c, Board b) {
      super(PieceCode.QUEEN, ix, iy, c, b);
    }
 
    // method implements abstract method in Piece class
  public ArrayList<Move> availableMoves() {
    return queen();
  }
  
      // method to return Vector of legal moves for a queen
      private ArrayList<Move> queen() {
    int x = getX();
    int y = getY();
      
   
    // otherwise create a new vector to store legal moves
    ArrayList<Move> v = new ArrayList<Move>();
      
    // set up m to refer to a Move object  
    Move m = null;
     
    
    // legal moves to go from x,y to unoccupied places
   
    int[][] avaiableMoves = {
            {x+1,y}, {x+2,y}, {x+3,y}, {x+4,y}, {x+5,y}, {x+6,y}, {x+7,y}, //Positive x axis
            {x-1,y}, {x-2,y}, {x-3,y}, {x-4,y}, {x-5,y}, {x-6,y}, {x-7,y}, //Negative x axis
            
            {x,y+1}, {x,y+2}, {x,y+3}, {x,y+4}, {x,y+5}, {x,y+6}, {x,y+7}, //Positive y axis
            {x,y-1}, {x,y-2}, {x,y-3}, {x,y-4}, {x,y-5}, {x,y-6}, {x,y-7}, //Negative y axis
 
            {x+1,y+1}, {x+2,y+2}, {x+3,y+3}, {x+4,y+4}, {x+5,y+5}, {x+6,y+6}, {x+7,y+7}, //Positive right diagonal
            {x-1,y-1}, {x-2,y-2}, {x-3,y-3}, {x-4,y-4}, {x-5,y-5}, {x-6,y-6}, {x-7,y-7}, //Negative left diagonal
            
            {x-1,y+1}, {x-2,y+2}, {x-3,y+3}, {x-4,y+4}, {x-5,y+5}, {x-6,y+6}, {x-7,y+7}, //Positive left diagonal
            {x+1,y-1}, {x+2,y-2}, {x+3,y-3}, {x+4,y-4}, {x+5,y-5}, {x+6,y-6}, {x+7,y-7}, //Negative right diagonal
            
    };
    
      
    //using the array of moves, find all the possible moves in the array that piece can from its position
    
    for (int i = 0, j = avaiableMoves.length; i<j; i++){
        int posX = avaiableMoves[i][0];
        int posY = avaiableMoves[i][1];
         
    //using for loop to check whether any of available moves in the list are occupied by other pieces or not when it's not out of range         
        if (!getBoard().outOfRange(posX,posY) && getBoard().occupied(posX,posY) && (getBoard().getPiece(posX,posY).getColour() !=this.getColour())) {
            v.add(new Move(this, x,y,posX,posY,true));
              }
  //if occupied, then return false
        else if (!getBoard().outOfRange(posX,posY) && !getBoard().occupied(posX,posY)) {
          v.add(new Move(this, x,y,posX,posY,false));
        }
  }
    
    if (v.isEmpty()) return null;
    return v;
}
  
}
 
 
 
 
 
 
cs


맨 처음에 계획한 것은 퀸의 모든 가능한 움직임들을 availableMove 라는 array에 넣어, for 루프와 if 조건문을 통해 해당 칸이 퀸이 움직일 수 있는 칸인지 아닌지를 확인하게 하였습니다.

그런데 막상 테스트를 돌려보니 만약 퀸의 x축 이동경로에 다른 말이 있다면 그곳에서 멈춰야 하는데, 그 이후로 빈공간이있다면 다른 말이 있는 칸을 뛰어넘고 움직이는 게 가능했습니다. (마치 장기의 포와 같이요)

제가 원하는 데로 퀸을 움직이게 할려면, 어떤 부분을 수정해야 하는지, 혹은 도움이 될 만한 링크가 있으면 알려주시길 바랍니다.

또한 제가 미처 얄려드리지 못한 부분은 댓글을 다시면 수시로 확인하여 업데이트 하겠습니다. (외국이라 시차가 있습니다)

감사합니다.
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호