본문 바로가기

알고리즘

혼자하는 틱택토

 

 


https://school.programmers.co.kr/learn/courses/30/lessons/160585

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 


틱택토 게임의 규칙을 이해했어야 했다.

처음 짠 코드는 틱택토 규칙을 모르는 상태로 해서

초반에 주어진 테스트는 통과했지만, 게임 자체에 대한 이해도가 없어서 다른 테스트에서 실패했다.

 

졸리니까 문제 집중력이 낮아진다..

테스트 10번

하나가 계속 안 잡혀서 끄지도 못하고..

졸리고...눈은 감기고.. 머리는 안 돌아가고..

 

에라 모르겠다.

https://hoonsb.tistory.com/44

 

[프로그래머스] 혼자서 하는 틱택토 Java

https://school.programmers.co.kr/learn/courses/30/lessons/160585 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞

hoonsb.tistory.com

 

힌트 얻으러 검색 함..

//test 10
if (OCount - XCount > 1) { return 0; }

 

이거 넣으니까 해결했다... 난 자러 간다 👋


틱택토 게임 이해도 없을 때

 

 

 

public class 혼자서하는틱택토 {

//	https://school.programmers.co.kr/learn/courses/30/lessons/160585

	public static void main(String[] args) {
		Solution solution = new Solution();
		String[] arr = {".O.", ".X.", ".O."};
		int result = solution.solution(arr);
		System.out.println(result);

	}

	static class Solution {
		public int solution(String[] board) {

			String[][] newBoard = new String[3][3];
			int XCount = 0;
			int OCount = 0;
			int dotCount = 0;

			for (int i = 0; i < board.length; i++) {
				String[] temp = board[i].split("");
				for (String t : temp) {
					switch (t) {
						case "O":
							OCount++;
							break;
						case "X":
							XCount++;
							break;
						case ".":
							dotCount++;
					}

					newBoard[i] = temp;
				}
			}

			System.out.println(newBoard);

			// 초기화
			if (dotCount >= 9) {
				return 1;// true
			}

			// O가 선공
			if (dotCount == 8 && XCount == 1) {
				return 0;// false
			}

			// 체크0) O 존재 하는가?
			if (OCount < 1) {
				return 0;// false
			}

			// 체크1) O와 X의 숫자가 같은가? O는 X보다 1개 많아야한다
			if (OCount - XCount >= 1) {
				return 0;// false
			}

			int result = 0;
			result += checkRow(newBoard);
			result += checkColumn(newBoard);
			result += checkDiagonal(newBoard);

			// 체크3) 게임이 끝났는데도 계속 진행하지 않는가?
			if (result > 1) {
				return 0;// false
			}

			return 1;
		}

		private int checkRow(String[][] newBoard) {
			int result = 0;
			for (int i = 0; i < 3; i++) {
				if (!newBoard[i][0].equals(".") && newBoard[i][0].equals(newBoard[i][1]) && newBoard[i][1].equals(newBoard[i][2])) {
					result++;
				}
			}

			return result;
		}

		private int checkColumn(String[][] newBoard) {
			int result = 0;
			for (int i = 0; i < 3; i++) {
				if (!newBoard[0][i].equals(".") && newBoard[0][i].equals(newBoard[1][i]) && newBoard[1][i].equals(newBoard[2][i])) {
					result++;
				}
			}

			return result;
		}

		private int checkDiagonal(String[][] newBoard) {
			int result = 0;
			if (!newBoard[0][0].equals(".") && newBoard[0][0].equals(newBoard[1][1]) && newBoard[1][1].equals(newBoard[2][2])) {
				result++;
			}

			if (!newBoard[0][2].equals(".") && newBoard[0][2].equals(newBoard[1][1]) && newBoard[1][1].equals(newBoard[2][0])) {
				result++;
			}

			return result;
		}
	}// Solution class end

}// one class end

 

 


해결 코드

알 고 리 즘-6.pdf
0.54MB

public class 혼자서하는틱택토_3 {

//	https://school.programmers.co.kr/learn/courses/30/lessons/160585

	public static void main(String[] args) {
		Solution solution = new Solution();
		String[] arr = {".O.", ".X.", ".O."};
		int result = solution.solution(arr);
		System.out.println(result);

	}

	static class Solution {
		public int solution(String[] board) {
			String[][] newBoard = new String[3][3];
			int XCount = 0;
			int OCount = 0;
			int dotCount = 0;

			// 보드를 2차원 배열로 변환하고 각 기호의 개수를 카운트
			for (int i = 0; i < board.length; i++) {
				String[] temp = board[i].split("");
				for (String t : temp) {
					if (t.equals("O")) {
						OCount++;
					} else if (t.equals("X")) {
						XCount++;
					} else {
						dotCount++;
					}
				}
				newBoard[i] = temp;
			}

			// 초기화
			if (dotCount >= 9) {
				return 1;// true
			}

			// O가 선공
			if (dotCount == 8 && XCount == 1) {
				return 0;// false
			}

			// 체크0) O 존재 하는가?
			if (OCount < 1) {
				return 0;// false
			}

			// X는 O보다 많을 수 없다.
			if (XCount > OCount) {
				return 0;
			}

			//test 10
			if (OCount - XCount > 1) {
				return 0;
			}

			// O가 이긴 경우와 X가 이긴 경우를 각각 확인
			boolean OWin = checkWin(newBoard, "O");
			boolean XWin = checkWin(newBoard, "X");

			// O와 X가 모두 이겼다면 잘못된 상태
			if (OWin && XWin) {
				return 0;
			}

			// O가 이겼다면 O가 X보다 1개 더 많아야 함
			if (OWin && OCount != XCount + 1) {
				return 0;
			}

			// X가 이겼다면 O와 X의 개수가 같아야 함
			if (XWin && OCount != XCount) {
				return 0;
			}

			// 그 외의 경우는 정상적인 상태
			return 1;
		}

		// 가로, 세로, 대각선에서 승리 조건을 체크
		private boolean checkWin(String[][] board, String player) {
			for (int i = 0; i < 3; i++) {
				// 가로와 세로를 체크
				if ((board[i][0].equals(player) && board[i][1].equals(player) && board[i][2].equals(player)) ||
						(board[0][i].equals(player) && board[1][i].equals(player) && board[2][i].equals(player))) {
					return true;
				}
			}
			// 대각선을 체크
			if ((board[0][0].equals(player) && board[1][1].equals(player) && board[2][2].equals(player)) ||
					(board[0][2].equals(player) && board[1][1].equals(player) && board[2][0].equals(player))) {
				return true;
			}
			return false;
		}
	}// Solution class end

}// one class end