## 과제 : TicTacToe 게임 구현 목표 : CLI 환경에서 TicTacToe 게임을 구현한다. 요구사항 : 다음 조건을 만족하는 단어 맞추기 게임을 구현한다. 1. 2명의 플레이어가 번갈아 가며 플레이할 수 있는 콘솔 기반의 틱택토 게임이어야 한다. 2. 게임 보드는 3x3 크기의 격자로 표현되며, 각 셀에는 1부터 9까지의 숫자가 할당된다. 3. 플레이어는 1부터 9까지의 숫자를 입력하여 해당 셀에 자신의 기호(X 또는 O)를 표시한다. 4. 한 플레이어가 가로, 세로 또는 대각선 방향으로 자신의 기호를 3개 연속으로 놓으면 승리한다. 5. 모든 셀이 채워지고 승자가 없으면 무승부가 된다. 6. 게임 종료 후 다시 플레이할지 여부를 묻는다. --- ## 과제 : TicTacToe 게임 구현 모범답안 ```java import java.util.Scanner; public class TicTacToe { private static char[][] board = new char[3][3]; private static char currentPlayer = 'X'; private static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { initializeBoard(); playGame(); } private static void initializeBoard() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) {; board[i][j] = ' '; } } } private static void playGame() { boolean gameOver = false; while (!gameOver) { printBoard(); int move = getPlayerMove(); makeMove(move); gameOver = checkGameOver(); switchPlayer(); } printBoard(); announceResult(); playAgain(); } private static void printBoard() { System.out.println("-------------"); for (int i = 0; i < 3; i++) { System.out.print("| "); for (int j = 0; j < 3; j++) { System.out.print(board[i][j] + " | "); } System.out.println("\n-------------"); } } private static int getPlayerMove() { System.out.print("플레이어 " + currentPlayer + "의 차례입니다. 1-9 사이의 숫자를 입력하세요: "); int move = scanner.nextInt(); while (move < 1 || move > 9 || board[(move - 1) / 3][(move - 1) % 3] != ' ') { System.out.println("잘못된 입력입니다. 다시 입력해주세요."); move = scanner.nextInt(); } return move; } private static void makeMove(int move) { int row = (move - 1) / 3; int col = (move - 1) % 3; board[row][col] = currentPlayer; } private static boolean checkGameOver() { // 가로 확인 for (int i = 0; i < 3; i++) { if (board[i][0] != ' ' && board[i][0] == board[i][1] && board[i][1] == board[i][2]) { return true; } } // 세로 확인 for (int j = 0; j < 3; j++) { if (board[0][j] != ' ' && board[0][j] == board[1][j] && board[1][j] == board[2][j]) { return true; } } // 대각선 확인 if (board[0][0] != ' ' && board[0][0] == board[1][1] && board[1][1] == board[2][2]) { return true; } if (board[0][2] != ' ' && board[0][2] == board[1][1] && board[1][1] == board[2][0]) { return true; } // 무승부 확인 boolean isFull = true; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (board[i][j] == ' ') { isFull = false; break; } } } return isFull; } private static void switchPlayer() { currentPlayer = (currentPlayer == 'X') ? 'O' : 'X'; } private static void announceResult() { if (checkGameOver()) { System.out.println("플레이어 " + ((currentPlayer == 'X') ? 'O' : 'X') + "가 승리했습니다!"); } else { System.out.println("무승부입니다."); } } private static void playAgain() { System.out.print("다시 플레이하시겠습니까? (y/n): "); String playAgain = scanner.next(); if (playAgain.equalsIgnoreCase("y")) { initializeBoard(); playGame(); } else { System.out.println("게임을 종료합니다."); } } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up