<div>안녕하세요. 이제 막 자바에 대해 알아가는 대학생 1학년입니다.</div> <div><br></div>저번 질문글에 답변이 많은 도움이 되었습니다. 덕분에 막힘없이 코드를 쓰다가, 테스트를 하는 도중에 문제가 발생했는데, 도움을 주시면 감사하겠습니다. <div><br></div> <div>제가 가진 문제는 체스의 퀸의 움직임을 짜는 코드에 대한 것입니다.</div> <div><br></div> <div>스택 오버플로우나, 구글에 검색하여 도움이 될 만한 글들을 참조하였으나 (예를 들어 <a target="_blank" href="http://codereview.stackexchange.com/questions/53875/generating-possible-chess-moves">http://codereview.stackexchange.com/questions/53875/generating-possible-chess-moves</a> )</div> <div><br></div> <div>제가 쓴 코드와는 다른 방식으로 짜여져 있는 경우가 많아 큰 도움이 되질 않았습니다.</div> <div><br></div> <div>물론 해당 글들의 코드와 비슷하게 다시 짤 수도 있었지만, 제가 스스로 코드를 쓰기 위해 들인 시간과 노력이 아까워 이렇게 질문합니다.</div> <div><br></div> <div>우선은 해당 코드입니다.</div> <div><br></div> <div> <div class="colorscripter-code" style="color:#010101;overflow:auto;font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> <table class="colorscripter-code-table" style="margin:0px;padding:0px;border:none;background-color:#fafafa;" cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:6px;border-right-width:2px;border-right-style:solid;border-right-color:#e5e5e5;"> <div style="margin:0px;padding:0px;text-align:right;color:#666666;line-height:120%;"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> <div>10</div> <div>11</div> <div>12</div> <div>13</div> <div>14</div> <div>15</div> <div>16</div> <div>17</div> <div>18</div> <div>19</div> <div>20</div> <div>21</div> <div>22</div> <div>23</div> <div>24</div> <div>25</div> <div>26</div> <div>27</div> <div>28</div> <div>29</div> <div>30</div> <div>31</div> <div>32</div> <div>33</div> <div>34</div> <div>35</div> <div>36</div> <div>37</div> <div>38</div> <div>39</div> <div>40</div> <div>41</div> <div>42</div> <div>43</div> <div>44</div> <div>45</div> <div>46</div> <div>47</div> <div>48</div> <div>49</div> <div>50</div> <div>51</div> <div>52</div> <div>53</div> <div>54</div> <div>55</div> <div>56</div> <div>57</div> <div>58</div> <div>59</div> <div>60</div> <div>61</div> <div>62</div> <div>63</div> <div>64</div> <div>65</div> <div>66</div> <div>67</div> <div>68</div> <div>69</div> <div>70</div> <div>71</div> <div>72</div> <div>73</div></div></td> <td style="padding:6px 0px;"> <div style="margin:0px;padding:0px;color:#010101;"> <div style="padding:0px 6px;white-space:pre;line-height:120%;"><span style="line-height:120%;font-size:9pt;">import java.util.ArrayList;</span></div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> </div> <div style="padding:0px 6px;white-space:pre;line-height:120%;">public class Queen extends Piece{</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> </div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> public Queen(int ix, int iy, int c, Board b) {</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> super(PieceCode.QUEEN, ix, iy, c, b);</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> }</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> </div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> // method implements abstract method in Piece class</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> public ArrayList<Move> availableMoves() {</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> return queen();</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> }</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> </div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> // method to return Vector of legal moves for a queen</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> private ArrayList<Move> queen() {</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> int x = getX();</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> int y = getY();</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> </div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> </div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> // otherwise create a new vector to store legal moves</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> ArrayList<Move> v = new ArrayList<Move>();</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> </div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> // set up m to refer to a Move object </div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> Move m = null;</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> </div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> </div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> // legal moves to go from x,y to unoccupied places</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> </div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> int[][] avaiableMoves = {</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> {x+1,y}, {x+2,y}, {x+3,y}, {x+4,y}, {x+5,y}, {x+6,y}, {x+7,y}, //Positive x axis</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> {x-1,y}, {x-2,y}, {x-3,y}, {x-4,y}, {x-5,y}, {x-6,y}, {x-7,y}, //Negative x axis</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> </div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> {x,y+1}, {x,y+2}, {x,y+3}, {x,y+4}, {x,y+5}, {x,y+6}, {x,y+7}, //Positive y axis</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> {x,y-1}, {x,y-2}, {x,y-3}, {x,y-4}, {x,y-5}, {x,y-6}, {x,y-7}, //Negative y axis</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> </div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> {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</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> {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</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> </div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> {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</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> {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</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> </div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> };</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> </div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> </div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> //using the array of moves, find all the possible moves in the array that piece can from its position</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> </div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> for (int i = 0, j = avaiableMoves.length; i<j; i++){</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> int posX = avaiableMoves[i][0];</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> int posY = avaiableMoves[i][1];</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> </div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> //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 </div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> if (!getBoard().outOfRange(posX,posY) && getBoard().occupied(posX,posY) && (getBoard().getPiece(posX,posY).getColour() !=this.getColour())) {</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> v.add(new Move(this, x,y,posX,posY,true));</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> }</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> //if occupied, then return false</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> else if (!getBoard().outOfRange(posX,posY) && !getBoard().occupied(posX,posY)) {</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> v.add(new Move(this, x,y,posX,posY,false));</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> }</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> }</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> </div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> if (v.isEmpty()) return null;</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> return v;</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;">}</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> </div> <div style="padding:0px 6px;white-space:pre;line-height:120%;">}</div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> </div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> </div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> </div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> </div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> </div> <div style="padding:0px 6px;white-space:pre;line-height:120%;"> </div></div> <div style="text-align:right;margin-top:-13px;margin-right:5px;font-size:9px;font-style:italic;"><a target="_blank" href="http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5;text-decoration:none;">Colored by Color Scripter</a></div></td> <td style="vertical-align:bottom;padding:0px 2px 4px 0px;"><a target="_blank" href="http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:#FFFFFF;"><span style="font-size:9px;padding:1px;background-color:#e5e5e5;">cs</span></a></td></tr></tbody></table><br></div></div> <div class="colorscripter-code" style="color:#010101;overflow:auto;font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"><br></div> <div class="colorscripter-code" style="color:#010101;overflow:auto;font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">맨 처음에 계획한 것은 퀸의 모든 가능한 움직임들을 availableMove 라는 array에 넣어, for 루프와 if 조건문을 통해 해당 칸이 퀸이 움직일 수 있는 칸인지 아닌지를 확인하게 하였습니다.</div> <div class="colorscripter-code" style="color:#010101;overflow:auto;font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"><br></div> <div class="colorscripter-code" style="color:#010101;overflow:auto;font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">그런데 막상 테스트를 돌려보니 만약 퀸의 x축 이동경로에 다른 말이 있다면 그곳에서 멈춰야 하는데, 그 이후로 빈공간이있다면 다른 말이 있는 칸을 뛰어넘고 움직이는 게 가능했습니다. (마치 장기의 포와 같이요)</div> <div class="colorscripter-code" style="color:#010101;overflow:auto;font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"><br></div> <div class="colorscripter-code" style="color:#010101;overflow:auto;font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">제가 원하는 데로 퀸을 움직이게 할려면, 어떤 부분을 수정해야 하는지, 혹은 도움이 될 만한 링크가 있으면 알려주시길 바랍니다.</div> <div class="colorscripter-code" style="color:#010101;overflow:auto;font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"><br></div> <div class="colorscripter-code" style="color:#010101;overflow:auto;font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">또한 제가 미처 얄려드리지 못한 부분은 댓글을 다시면 수시로 확인하여 업데이트 하겠습니다. (외국이라 시차가 있습니다)</div> <div class="colorscripter-code" style="color:#010101;overflow:auto;font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"><br></div> <div class="colorscripter-code" style="color:#010101;overflow:auto;font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">감사합니다.</div>
댓글 분란 또는 분쟁 때문에 전체 댓글이 블라인드 처리되었습니다.