자바 틱 택 톡 게임을 수정하고 싶습니다.

ansdyd64의 이미지

밑에 코드는 자바 틱 택 톡 게임 코드 입니다.
현재는 하나씩 플레이 한 뒤에 승자가 정해지면 바로
승자가 출력되는 상태입니다.

현재 상태에서 9개의 칸을 모두 채운 뒤에
승자를 출력하는 걸로 수정 하고 싶습니다.

여기서 어떻게 바꿔야 할까요?

import java.util.Scanner;
 
public class TicTac {
 
   public static final int zero = 0;
   public static final int CROSS = 1;
   public static final int NOUGHT = 2;
 
   public static final int PLAYING = 0;
   public static final int DRAW = 1;
   public static final int CWON = 2;
   public static final int NWON = 3;
 
   public static final int ROWS = 3, COLS = 3;
   public static int[][] board = new int[ROWS][COLS]; 
   public static int currentState;                                      
   public static int currentPlayer; 
   public static int currntRow, currentCol; 
 
   public static Scanner in = new Scanner(System.in); 
 
   public static void main(String[] args) {
      initGame();
      do {
         playerMove(currentPlayer);
         updateGame(currentPlayer, currntRow, currentCol);
         printBoard();
         if (currentState == CWON) {
            System.out.println("'X' won! Bye!");
         } else if (currentState == NWON) {
            System.out.println("'O' won! Bye!");
         } else if (currentState == DRAW) {
            System.out.println("It's a Draw! Bye!");
         }
         currentPlayer = (currentPlayer == CROSS) ? NOUGHT : CROSS;
      } while (currentState == PLAYING); 
   }
 
   public static void initGame() {
      for (int row = 0; row < ROWS; ++row) {
         for (int col = 0; col < COLS; ++col) {
            board[row][col] = zero;  
         }
      }
      currentState = PLAYING;
      currentPlayer = CROSS; 
   }
 
   public static void playerMove(int theSeed) {
      boolean validInput = false;  
      do {
         if (theSeed == CROSS) {
            System.out.print("Player 'X', enter your move (row[1-3] column[1-3]): ");
         } else {
            System.out.print("Player 'O', enter your move (row[1-3] column[1-3]): ");
         }
         int row = in.nextInt() - 1; 
         int col = in.nextInt() - 1;
         if (row >= 0 && row < ROWS && col >= 0 && col < COLS && board[row][col] == zero) {
            currntRow = row;
            currentCol = col;
            board[currntRow][currentCol] = theSeed;  
            validInput = true;  
         } else {
            System.out.println("This move at (" + (row + 1) + "," + (col + 1)
                  + ") is not valid. Try again...");
         }
      } while (!validInput);  
   }
 
 
 
   public static void updateGame(int theSeed, int currentRow, int currentCol) {
      if (hasWon(theSeed, currentRow, currentCol)) {  // check if winning move
         currentState = (theSeed == CROSS) ? CWON : NWON;
      } else if (isDraw()) {  
         currentState = DRAW;
      }
   }
 
   public static boolean isDraw() {
      for (int row = 0; row < ROWS; ++row) {
         for (int col = 0; col < COLS; ++col) {
            if (board[row][col] == zero) {
               return false;  
            }
         }
      }
      return true;  
   }
 
   public static boolean hasWon(int theSeed, int currentRow, int currentCol) {
      return (board[currentRow][0] == theSeed         
                   && board[currentRow][1] == theSeed
                   && board[currentRow][2] == theSeed
              || board[0][currentCol] == theSeed      
                   && board[1][currentCol] == theSeed
                   && board[2][currentCol] == theSeed
              || currentRow == currentCol            
                   && board[0][0] == theSeed
                   && board[1][1] == theSeed
                   && board[2][2] == theSeed
              || currentRow + currentCol == 2 
                   && board[0][2] == theSeed
                   && board[1][1] == theSeed
                   && board[2][0] == theSeed);
   }
 
   public static void printBoard() {
      for (int row = 0; row < ROWS; ++row) {
         for (int col = 0; col < COLS; ++col) {
            printCell(board[row][col]); 
            if (col != COLS - 1) {
               System.out.print("|");   
            }
         }
         System.out.println();
         if (row != ROWS - 1) {
            System.out.println("-----------"); 
         }
      }
      System.out.println();
   }
 
   public static void printCell(int content) {
      switch (content) {
         case zero:  System.out.print("   "); break;
         case NOUGHT: System.out.print(" O "); break;
         case CROSS:  System.out.print(" X "); break;
      }
   }
}

현재는 밑에 사진처럼 출력되고 있습니다.

File attachments: 
첨부파일 크기
Image icon 스크린샷 2018-10-25 오후 7.13.54.png77.04 KB
세벌의 이미지

승패가 결정나도 계속 하려는 까닭을 모르겠습니다만...

import java.util.Scanner;
 
public class TicTac {
 
   public static final int zero = 0;
   public static final int CROSS = 1;
   public static final int NOUGHT = 2;
 
   public static final int PLAYING = 0;
   public static final int DRAW = 1;
   public static final int CWON = 2;
   public static final int NWON = 3;
 
   public static final int ROWS = 3, COLS = 3;
   public static int[][] board = new int[ROWS][COLS]; 
   public static int currentState;                                      
   public static int currentPlayer; 
   public static int currntRow, currentCol; 
 
   public static Scanner in = new Scanner(System.in); 
 
   public static void main(String[] args) {
      initGame();
      do {
         playerMove(currentPlayer);
         updateGame(currentPlayer, currntRow, currentCol);
         printBoard();
         if (currentState == CWON) {
            System.out.println("'X' won! Bye!");
         } else if (currentState == NWON) {
            System.out.println("'O' won! Bye!");
         } else if (currentState == DRAW) {
            System.out.println("It's a Draw! Bye!");
         }
         currentPlayer = (currentPlayer == CROSS) ? NOUGHT : CROSS;
      } while (currentState == PLAYING); 
   }
 
   public static void initGame() {
      for (int row = 0; row < ROWS; ++row) {
         for (int col = 0; col < COLS; ++col) {
            board[row][col] = zero;  
         }
      }
      currentState = PLAYING;
      currentPlayer = CROSS; 
   }
 
   public static void playerMove(int theSeed) {
      boolean validInput = false;  
      do {
         if (theSeed == CROSS) {
            System.out.print("Player 'X', enter your move (row[1-3] column[1-3]): ");
         } else {
            System.out.print("Player 'O', enter your move (row[1-3] column[1-3]): ");
         }
         int row = in.nextInt() - 1; 
         int col = in.nextInt() - 1;
         if (row >= 0 && row < ROWS && col >= 0 && col < COLS && board[row][col] == zero) {
            currntRow = row;
            currentCol = col;
            board[currntRow][currentCol] = theSeed;  
            validInput = true;  
         } else {
            System.out.println("This move at (" + (row + 1) + "," + (col + 1)
                  + ") is not valid. Try again...");
         }
      } while (!validInput);  
   }
 
   public static void updateGame(int theSeed, int currentRow, int currentCol) {
      if (hasWon(theSeed, currentRow, currentCol)) {  // check if winning move
         currentState = (theSeed == CROSS) ? CWON : NWON;
      } else if (isDraw()) {  
         currentState = DRAW;
      }
   }
 
   public static boolean isDraw() {
      for (int row = 0; row < ROWS; ++row) {
         for (int col = 0; col < COLS; ++col) {
            if (board[row][col] == zero) {
               return false;  
            }
         }
      }
      return true;  
   }
 
   public static boolean hasWon(int theSeed, int currentRow, int currentCol) {
      return (board[currentRow][0] == theSeed         
                   && board[currentRow][1] == theSeed
                   && board[currentRow][2] == theSeed
              || board[0][currentCol] == theSeed      
                   && board[1][currentCol] == theSeed
                   && board[2][currentCol] == theSeed
              || currentRow == currentCol            
                   && board[0][0] == theSeed
                   && board[1][1] == theSeed
                   && board[2][2] == theSeed
              || currentRow + currentCol == 2 
                   && board[0][2] == theSeed
                   && board[1][1] == theSeed
                   && board[2][0] == theSeed);
   }
 
   public static void printBoard() {
      for (int row = 0; row < ROWS; ++row) {
         for (int col = 0; col < COLS; ++col) {
            printCell(board[row][col]); 
            if (col != COLS - 1) {
               System.out.print("|");   
            }
         }
         System.out.println();
         if (row != ROWS - 1) {
            System.out.println("-----------"); 
         }
      }
      System.out.println();
   }
 
   public static void printCell(int content) {
      switch (content) {
         case zero:  System.out.print("   "); break;
         case NOUGHT: System.out.print(" O "); break;
         case CROSS:  System.out.print(" X "); break;
      }
   }
}

위 코드에서
} while (currentState == PLAYING); 

이 부분을 고쳐야 할 거 같은데요.

어떻게 고칠 지는 저도 모르겠...

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.