<div><span style="line-height:14.3999996185303px;font-size:9pt;">아직 프로그래밍에 대해 많이 이해를 못 해서 질문이 좀 횡설수설하고 장황할 수 있는 점 죄송합니다.</span></div> <div><br></div>이번에 숙제로 자바로 플레이어가 콘솔창을 통해 할 수 있는 체스게임을 만드는 것에도전해야합니다. 그런데 잘 모르는 부분이 있네요. <div><br></div> <div>특히 장기말들의 움직임에 대해 코드 짜는 게 힘듭니다.</div> <div><br></div> <div>1) 처음 숙제를 받았을 때, 먼저, Pawn, 보드, Display, Player, Piece, Pieces 등등의 클래스가 이미 주어져 있습니다.</div> <div><br></div> <div>예시로 주어진 Pawn 클래스의 코드가 </div> <div><br></div> <div> <div>public class Pawn extends Piece {</div> <div><br></div> <div> public Pawn (int ix, int iy, int c, Board b) {</div> <div> super(PieceCode.PAWN, ix, iy, c, b);</div> <div> }</div> <div> </div> <div> // method implements abstract method in Piece class</div> <div> public ArrayList<Move> availableMoves() {</div> <div> if (getColour()==PieceCode.WHITE) return whitePawn();</div> <div> else return blackPawn();</div> <div> }</div> <div><br></div> <div> // method to return Vector of legal moves for a white pawn</div> <div> private ArrayList<<u>Move</u>> whitePawn() {</div> <div> int x = getX();</div> <div> int y = getY();</div> <div> </div> <div> // return null if the pawn is at the edge of the board, or if the</div> <div> // next move takes it out of range</div> <div> if (y==7) return null;</div> <div> if (getBoard().outOfRange(x,y+1)) return null;</div> <div> </div> <div> // otherwise create a new vector to store legal moves</div> <div> ArrayList<Move> v = new ArrayList<Move>();</div> <div> </div> <div> // set up m to refer to a Move object </div> <div> Move m = null;</div> <div> </div> <div> // first legal move is to go from x,y to x,y+1 if x,y+1 is unoccupied </div> <div> if (!getBoard().occupied(x,y+1)) {</div> <div> m = new Move(<u>this, x,y,x,y+1,false</u>);</div> <div> v.add(m);</div> <div> }</div> <div> </div> <div> // second legal move is to go from x,y to x+1,y+1 if x+1,y+1 is occupied </div> <div> // by a black piece</div> <div> if (!getBoard().outOfRange(x+1, y+1)</div> <div> && getBoard().occupied(x+1, y+1)</div> <div> && (getBoard().getPiece(x+1, y+1).getColour()</div> <div> !=this.getColour())) {</div> <div> m = new Move(<u>this, x,getY(),x+1,y+1,true</u>);</div> <div> v.add(m);</div> <div> }</div> <div> </div> <div> // third legal move is to go from x,y to x-1,y+1 if x-1,y+1 is occupied </div> <div> // by a black piece </div> <div> if (!getBoard().outOfRange(x-1, y+1)</div> <div> && getBoard().occupied(x-1, y+1)</div> <div> && (getBoard().getPiece(x-1, y+1).getColour()</div> <div> !=this.getColour())) {</div> <div> m = new Move(<u>this, x,y,x-1,y+1,true</u>);</div> <div> v.add(m);</div> <div> }</div> <div> </div> <div> if (v.isEmpty()) return null;</div> <div> return v;</div> <div> }</div></div> <div><br></div> <div>이런 식으로 주어져 있고, 저는 이 형식을 빌려서 나머지 기사, 승려, 룩, 킹, 퀸 클래스 등등을 작성해야 합니다.</div> <div><br></div> <div>여기서 모르는 부분은 줄 쳐져 있는 부분입니다.</div> <div><br></div> <div>Move에 대한 클래스가 없어서 에러가 난다는 것은 알겠는데, 클래스의 constructor를 어떻게 작성해야하는 지 잘 모르겠습니다. (정확히는 주어진 contructor에 대해 해석이 안되요)</div> <div><br></div> <div><span style="font-size:9pt;line-height:14.3999996185303px;">Move(</span><u style="font-size:9pt;line-height:14.3999996185303px;">this, x,y,x-1,y+1,true</u><span style="font-size:9pt;line-height:14.3999996185303px;">);, </span><span style="font-size:9pt;line-height:14.3999996185303px;">Move(</span><u style="font-size:9pt;line-height:14.3999996185303px;">this, x,y,x,y+1,false</u><span style="font-size:9pt;line-height:14.3999996185303px;">); 등등 이렇게 Move 클래스가 (String, int, int, int, int, boolean)으로 되어있지만 작성할 때마다 에러가 나서 어떻게 작성해야 하는 지 잘 모르겠습니다 (특히 저 false와 true의 의미는 아무리 생각해도 모르겠고요).</span></div> <div><span style="font-size:9pt;line-height:14.3999996185303px;"><br></span></div> <div><span style="font-size:9pt;line-height:14.3999996185303px;"><br></span></div> <div><span style="font-size:9pt;line-height:14.3999996185303px;">2) 글에는 없지만 예시로 주어진 Pawn 클래스 내에 whitePawn 과 코드는 완전히 같지만 이름만 다른 blackPawn 도 있습니다.</span></div> <div><span style="font-size:9pt;line-height:14.3999996185303px;"><br></span></div> <div><span style="font-size:9pt;line-height:14.3999996185303px;">그런데 나머지 기사, 승려, 룩, 킹, 퀸 도 흑,백으로 나눠서 코드를 짤 필요가 있나요? 제 생각에는 Pawn은 한 방향으로 밖에 못 움직이니까 흑,백으로각각 나눠서 코드를 짰지만, 다른 말들은 흑이던 백이던 갈 수 있는 방향은 같으니까 차라리 하나로 통합해서 만드는 게 코드 길이도 줄일 수 있다고 생각하는 데 어떤가요?</span></div> <div><span style="font-size:9pt;line-height:14.3999996185303px;"><br></span></div> <div><span style="font-size:9pt;line-height:14.3999996185303px;"><br></span></div> <div><span style="font-size:9pt;line-height:14.3999996185303px;">3) Pawn이야 앞으로 한 칸씩 밖에 전진을 못 해서 </span><span style="line-height:14.3999996185303px;font-size:9pt;">x,y+1 식으로</span><span style="font-size:9pt;line-height:14.3999996185303px;"> 움직이게 할 수 있지만, 나머지 장기말들은 다 방향으로, 어떤 말들은 한칸 이상씩 전진할 수 있잖아요?</span></div> <div><span style="font-size:9pt;line-height:14.3999996185303px;"><br></span></div> <div><span style="font-size:9pt;line-height:14.3999996185303px;">그러면 예를들어 퀸 같은 경우 x축, y축, 대각선으로 모두 움직일 수 있는데, 설마 무식하게 </span><span style="line-height:14.3999996185303px;font-size:9pt;">x,y+1, </span><span style="line-height:14.3999996185303px;font-size:9pt;">x,y+2, </span><span style="line-height:14.3999996185303px;font-size:9pt;">x,y+3 이런 식으로 다 짜야 하는 건 아니겠죠? 제 생각으로는 각 방향으로 while 루프를 사용해서 다른 말에 막힐때까지 이동할 수 있다 라는 식으로 해볼까 하는 데 어떤가요?</span></div> <div><span style="font-size:9pt;line-height:14.3999996185303px;"><br></span></div> <div><span style="font-size:9pt;line-height:14.3999996185303px;"><br></span></div> <div><span style="font-size:9pt;line-height:14.3999996185303px;">구글신에도 여러번 검색을 하고 조사를 했으나, 다른 사람이 짠 코드를 이해하고 응용해서 제것을 만들어야 하다보니 도움 될만한 정보를 못 찾았네요.</span></div> <div><br></div> <div>질문에 대한 관련 웹사이트나 정보가 있으시다면 알려주시면 감사하겠습니다 (특히 첫번째 질문인 contructor관련)</div> <div><span style="font-size:9pt;line-height:14.3999996185303px;"><br></span></div>
댓글 분란 또는 분쟁 때문에 전체 댓글이 블라인드 처리되었습니다.