코딩도장

코딩도장

변경이력

돌아가기
3 208개 문자 추가 5개 문자 삭제

2022年06月15日 11:25

유로

자바로 풀어봤습니다. ```{.java} import java.util.Scanner; public class RealTest { // 판단 public static int whoIsWinner(int count, String check, int winner) { if(count==3&check.equals("X")) { winner = 1; }else if(count==3&check.equals("O")) { winner = 2; } return winner; } // 이긴 사람이 있는가? public static int answerResult(String[][] ticTacToe, int player) { int winner = 0; String[] checkList = {"X", "O"}; // 행 판단 for(String check : checkList) { for(int i=0; i<3; i++) { int count = 0; for(int j=0; j<3; j++) { if(ticTacToe[i][j].equals(check)){ count++; } } winner = whoIsWinner(count, check, winner); } } // 열 판단 for(String check : checkList) { for(int j=0; j<3; j++) { int count = 0; for(int i=0; i<3; i++) { if(ticTacToe[i][j].equals(check)){ count++; } } winner = whoIsWinner(count, check, winner); } } // 대각선 판단 for(String check : checkList) { int countLeft = 0, countRight = 0; for(int i=0; i<3; i++) { if(ticTacToe[i][i].equals(check)) { countLeft++; } if(ticTacToe[i][2-i].equals(check)) { countRight++; } } winner = whoIsWinner(countLeft, check, winner); winner = whoIsWinner(countRight, check, winner); } return winner; } // 해당 원소가 들어있는가? & 있으면 표시 public static boolean checkContain(String[][] ticTacToe, String selectedElement, int player) { boolean result = false; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(ticTacToe[i][j].equals(selectedElement)) { result = true; if(player==1) { ticTacToe[i][j] = "X"; }else { ticTacToe[i][j] = "O"; } } } } return result; } // 남은 원소 출력 public static void printRemaining (String[][] ticTacToe, int player) { String remaining = ""; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if((ticTacToe[i][j].equals("X")==false)&(ticTacToe[i][j].equals("O")==false)) { remaining += " "+ticTacToe[i][j];+","; } } } remaining = remaining.substring(0, remaining.length()-1); System.out.printf("Player %d's turn\n", player); System.out.printlnf("please type a position:" (available position(s) are %s):", remaining); } // 결과 출력 public static void painting(String[][] ticTacToe) { for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(j!=2) { System.out.print(" *"); }else { System.out.print(" "); } } System.out.println(); for(int j=0; j<3; j++) { if(j!=2) { System.out.printf(" %s *",ticTacToe[i][j]); }else { System.out.printf(" %s ",ticTacToe[i][j]); } } System.out.println(); for(int j=0; j<3; j++) { if(j!=2) { System.out.print(" *"); }else { System.out.print(" "); } } System.out.println(); } System.out.println(); } // 메인 public static void main(String[] args) { Scanner scan = new Scanner(System.in); // 게임 시작 전 세팅 String[][] ticTacToe = {{"1", "2", "3"}, {"4", "5", "6"}, {"7" ,"8" ,"9"}}; System.out.println("Game Start!"); painting(ticTacToe); // 시작 int player = 1; for(int i=1; i<=9; i++) { while(true) { printRemaining(ticTacToe, player); String selectedElement = scan.next(); boolean isContain = checkContain(ticTacToe, selectedElement, player); if(isContain==false) { System.out.println("The element does not exist. Please re-enter.\n"); continue; }else { break; } } System.out.println(); painting(ticTacToe); int winner = answerResult(ticTacToe, player); if(winner!=0) { System.out.printf("Game over! Winner is player %d\n", winner); break; }else if(i==9&&winner==0) { System.out.println("Game over! draw! \n"); } // 플레이어 차례 선정 if(player==1) { player=2; }else { player=1; } } } } ```
자바로 풀어봤습니다. ```{.java} import java.util.Scanner; public class RealTest { // 판단 public static int whoIsWinner(int count, String check, int winner) { if(count==3&check.equals("X")) { winner = 1; }else if(count==3&check.equals("O")) { winner = 2; } return winner; } // 이긴 사람이 있는가? public static int answerResult(String[][] ticTacToe, int player) { int winner = 0; String[] checkList = {"X", "O"}; // 행 판단 for(String check : checkList) { for(int i=0; i<3; i++) { int count = 0; for(int j=0; j<3; j++) { if(ticTacToe[i][j].equals(check)){ count++; } } winner = whoIsWinner(count, check, winner); } } // 열 판단 for(String check : checkList) { for(int j=0; j<3; j++) { int count = 0; for(int i=0; i<3; i++) { if(ticTacToe[i][j].equals(check)){ count++; } } winner = whoIsWinner(count, check, winner); } } // 대각선 판단 for(String check : checkList) { int countLeft = 0, countRight = 0; for(int i=0; i<3; i++) { if(ticTacToe[i][i].equals(check)) { countLeft++; } if(ticTacToe[i][2-i].equals(check)) { countRight++; } } winner = whoIsWinner(countLeft, check, winner); winner = whoIsWinner(countRight, check, winner); } return winner; } // 해당 원소가 들어있는가? & 있으면 표시 public static boolean checkContain(String[][] ticTacToe, String selectedElement, int player) { boolean result = false; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(ticTacToe[i][j].equals(selectedElement)) { result = true; if(player==1) { ticTacToe[i][j] = "X"; }else { ticTacToe[i][j] = "O"; } } } } return result; } // 남은 원소 출력 public static void printRemaining (String[][] ticTacToe, int player) { String remaining = ""; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if((ticTacToe[i][j].equals("X")==false)&(ticTacToe[i][j].equals("O")==false)) { remaining += " "+ticTacToe[i][j];+","; } } } remaining = remaining.substring(0, remaining.length()-1); System.out.printf("Player %d's turn\n", player); System.out.printlnf("please type a position:" (available position(s) are %s):", remaining); } // 결과 출력 public static void painting(String[][] ticTacToe) { for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(j!=2) { System.out.print(" *"); }else { System.out.print(" "); } } System.out.println(); for(int j=0; j<3; j++) { if(j!=2) { System.out.printf(" %s *",ticTacToe[i][j]); }else { System.out.printf(" %s ",ticTacToe[i][j]); } } System.out.println(); for(int j=0; j<3; j++) { if(j!=2) { System.out.print(" *"); }else { System.out.print(" "); } } System.out.println(); } System.out.println(); } // 메인 public static void main(String[] args) { Scanner scan = new Scanner(System.in); // 게임 시작 전 세팅 String[][] ticTacToe = {{"1", "2", "3"}, {"4", "5", "6"}, {"7" ,"8" ,"9"}}; System.out.println("Game Start!"); painting(ticTacToe); // 시작 int player = 1; for(int i=1; i<=9; i++) { while(true) { printRemaining(ticTacToe, player); String selectedElement = scan.next(); boolean isContain = checkContain(ticTacToe, selectedElement, player); if(isContain==false) { System.out.println("The element does not exist. Please re-enter.\n"); continue; }else { break; } } System.out.println(); painting(ticTacToe); int winner = answerResult(ticTacToe, player); if(winner!=0) { System.out.printf("Game over! Winner is player %d\n", winner); break; }else if(i==9&&winner==0) { System.out.println("Game over! draw! \n"); } // 플레이어 차례 선정 if(player==1) { player=2; }else { player=1; } } } } ```
자바로 풀어봤습니다. ```{.java} import java.util.Scanner; public class RealTest { // 판단 public static int whoIsWinner(int count, String check, int winner) { if(count==3&check.equals("X")) { winner = 1; }else if(count==3&check.equals("O")) { winner = 2; } return winner; } // 이긴 사람이 있는가? public static int answerResult(String[][] ticTacToe, int player) { int winner = 0; String[] checkList = {"X", "O"}; // 행 판단 for(String check : checkList) { for(int i=0; i<3; i++) { int count = 0; for(int j=0; j<3; j++) { if(ticTacToe[i][j].equals(check)){ count++; } } winner = whoIsWinner(count, check, winner); } } // 열 판단 for(String check : checkList) { for(int j=0; j<3; j++) { int count = 0; for(int i=0; i<3; i++) { if(ticTacToe[i][j].equals(check)){ count++; } } winner = whoIsWinner(count, check, winner); } } // 대각선 판단 for(String check : checkList) { int countLeft = 0, countRight = 0; for(int i=0; i<3; i++) { if(ticTacToe[i][i].equals(check)) { countLeft++; } if(ticTacToe[i][2-i].equals(check)) { countRight++; } } winner = whoIsWinner(countLeft, check, winner); winner = whoIsWinner(countRight, check, winner); } return winner; } // 해당 원소가 들어있는가? & 있으면 표시 public static boolean checkContain(String[][] ticTacToe, String selectedElement, int player) { boolean result = false; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(ticTacToe[i][j].equals(selectedElement)) { result = true; if(player==1) { ticTacToe[i][j] = "X"; }else { ticTacToe[i][j] = "O"; } } } } return result; } // 남은 원소 출력 public static void printRemaining (String[][] ticTacToe, int player) { String remaining = ""; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if((ticTacToe[i][j].equals("X")==false)&(ticTacToe[i][j].equals("O")==false)) { remaining += " "+ticTacToe[i][j];+","; } } } remaining = remaining.substring(0, remaining.length()-1); System.out.printf("Player %d's turn\n", player); System.out.printlnf("please type a position:" (available position(s) are %s):", remaining); } // 결과 출력 public static void painting(String[][] ticTacToe) { for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(j!=2) { System.out.print(" *"); }else { System.out.print(" "); } } System.out.println(); for(int j=0; j<3; j++) { if(j!=2) { System.out.printf(" %s *",ticTacToe[i][j]); }else { System.out.printf(" %s ",ticTacToe[i][j]); } } System.out.println(); for(int j=0; j<3; j++) { if(j!=2) { System.out.print(" *"); }else { System.out.print(" "); } } System.out.println(); } System.out.println(); } // 메인 public static void main(String[] args) { Scanner scan = new Scanner(System.in); // 게임 시작 전 세팅 String[][] ticTacToe = {{"1", "2", "3"}, {"4", "5", "6"}, {"7" ,"8" ,"9"}}; System.out.println("Game Start!"); painting(ticTacToe); // 시작 int player = 1; for(int i=1; i<=9; i++) { while(true) { printRemaining(ticTacToe, player); String selectedElement = scan.next(); boolean isContain = checkContain(ticTacToe, selectedElement, player); if(isContain==false) { System.out.println("The element does not exist. Please re-enter.\n"); continue; }else { break; } } System.out.println(); painting(ticTacToe); int winner = answerResult(ticTacToe, player); if(winner!=0) { System.out.printf("Game over! Winner is player %d\n", winner); break; }else if(i==9&&winner==0) { System.out.println("Game over! draw! \n"); } // 플레이어 차례 선정 if(player==1) { player=2; }else { player=1; } } } } ```
2 4개 문자 추가 45개 문자 삭제

2022年06月15日 11:16

유로

자바로 풀어봤습니다. ```{.java} import java.util.Scanner; public class RealTest { // 판단 public static int whoIsWinner(int count, String check, int winner) { if(count==3&check.equals("X")) { winner = 1; }else if(count==3&check.equals("O")) { winner = 2; } return winner; } // 이긴 사람이 있는가? public static int answerResult(String[][] ticTacToe, int player) { int winner = 0; String[] checkList = {"X", "O"}; // 행 판단 for(String check : checkList) { for(int i=0; i<3; i++) { int count = 0; for(int j=0; j<3; j++) { if(ticTacToe[i][j].equals(check)){ count++; } } winner = whoIsWinner(count, check, winner); } } // 열 판단 for(String check : checkList) { for(int j=0; j<3; j++) { int count = 0; for(int i=0; i<3; i++) { if(ticTacToe[i][j].equals(check)){ count++; } } winner = whoIsWinner(count, check, winner); } } // 대각선 판단 for(String check : checkList) { int countLeft = 0, countRight = 0; for(int i=0; i<3; i++) { if(ticTacToe[i][i].equals(check)) { countLeft++; } if(ticTacToe[i][2-i].equals(check)) { countRight++; } } winner = whoIsWinner(countLeft, check, winner); winner = whoIsWinner(countRight, check, winner); } return winner; } // 해당 원소가 들어있는가? & 있으면 표시 public static boolean checkContain(String[][] ticTacToe, String selectedElement, int player) { boolean result = false; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(ticTacToe[i][j].equals(selectedElement)) { result = true; if(player==1) { ticTacToe[i][j] = "X"; }else { ticTacToe[i][j] = "O"; } } } } return result; } // 남은 원소 출력 public static void printRemaining (String[][] ticTacToe, int player) { String remaining = ""; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { remaining += ticTacToe[i][j]; } } System.out.printf("Player %d's turn\n", player); System.out.printfln("please type a position (available position(s) are %s):", remaining:"); } // 결과 출력 public static void painting(String[][] ticTacToe) { for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(j!=2) { System.out.print(" *"); }else { System.out.print(" "); } } System.out.println(); for(int j=0; j<3; j++) { if(j!=2) { System.out.printf(" %s *",ticTacToe[i][j]); }else { System.out.printf(" %s ",ticTacToe[i][j]); } } System.out.println(); for(int j=0; j<3; j++) { if(j!=2) { System.out.print(" *"); }else { System.out.print(" "); } } System.out.println(); } System.out.println(); } // 메인 public static void main(String[] args) { Scanner scan = new Scanner(System.in); // 게임 시작 전 세팅 String[][] ticTacToe = {{"1", "2", "3"}, {"4", "5", "6"}, {"7" ,"8" ,"9"}}; System.out.println("Game Start!"); painting(ticTacToe); // 시작 int player = 1; for(int i=1; i<=9; i++) { while(true) { printRemaining(ticTacToe, player); String selectedElement = scan.next(); boolean isContain = checkContain(ticTacToe, selectedElement, player); if(isContain==false) { System.out.println("The element does not exist. Please re-enter.\n"); continue; }else { break; } } System.out.println(); painting(ticTacToe); int winner = answerResult(ticTacToe, player); if(winner!=0) { System.out.printf("Game over! Winner is player %d\n", winner); break; }else if(i==9&&winner==0) { System.out.println("Game over! draw! \n"); } // 플레이어 차례 선정 if(player==1) { player=2; }else { player=1; } } } } ```
자바로 풀어봤습니다. ```{.java} import java.util.Scanner; public class RealTest { // 판단 public static int whoIsWinner(int count, String check, int winner) { if(count==3&check.equals("X")) { winner = 1; }else if(count==3&check.equals("O")) { winner = 2; } return winner; } // 이긴 사람이 있는가? public static int answerResult(String[][] ticTacToe, int player) { int winner = 0; String[] checkList = {"X", "O"}; // 행 판단 for(String check : checkList) { for(int i=0; i<3; i++) { int count = 0; for(int j=0; j<3; j++) { if(ticTacToe[i][j].equals(check)){ count++; } } winner = whoIsWinner(count, check, winner); } } // 열 판단 for(String check : checkList) { for(int j=0; j<3; j++) { int count = 0; for(int i=0; i<3; i++) { if(ticTacToe[i][j].equals(check)){ count++; } } winner = whoIsWinner(count, check, winner); } } // 대각선 판단 for(String check : checkList) { int countLeft = 0, countRight = 0; for(int i=0; i<3; i++) { if(ticTacToe[i][i].equals(check)) { countLeft++; } if(ticTacToe[i][2-i].equals(check)) { countRight++; } } winner = whoIsWinner(countLeft, check, winner); winner = whoIsWinner(countRight, check, winner); } return winner; } // 해당 원소가 들어있는가? & 있으면 표시 public static boolean checkContain(String[][] ticTacToe, String selectedElement, int player) { boolean result = false; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(ticTacToe[i][j].equals(selectedElement)) { result = true; if(player==1) { ticTacToe[i][j] = "X"; }else { ticTacToe[i][j] = "O"; } } } } return result; } // 남은 원소 출력 public static void printRemaining (String[][] ticTacToe, int player) { String remaining = ""; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { remaining += ticTacToe[i][j]; } } System.out.printf("Player %d's turn\n", player); System.out.printfln("please type a position (available position(s) are %s):", remaining:"); } // 결과 출력 public static void painting(String[][] ticTacToe) { for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(j!=2) { System.out.print(" *"); }else { System.out.print(" "); } } System.out.println(); for(int j=0; j<3; j++) { if(j!=2) { System.out.printf(" %s *",ticTacToe[i][j]); }else { System.out.printf(" %s ",ticTacToe[i][j]); } } System.out.println(); for(int j=0; j<3; j++) { if(j!=2) { System.out.print(" *"); }else { System.out.print(" "); } } System.out.println(); } System.out.println(); } // 메인 public static void main(String[] args) { Scanner scan = new Scanner(System.in); // 게임 시작 전 세팅 String[][] ticTacToe = {{"1", "2", "3"}, {"4", "5", "6"}, {"7" ,"8" ,"9"}}; System.out.println("Game Start!"); painting(ticTacToe); // 시작 int player = 1; for(int i=1; i<=9; i++) { while(true) { printRemaining(ticTacToe, player); String selectedElement = scan.next(); boolean isContain = checkContain(ticTacToe, selectedElement, player); if(isContain==false) { System.out.println("The element does not exist. Please re-enter.\n"); continue; }else { break; } } System.out.println(); painting(ticTacToe); int winner = answerResult(ticTacToe, player); if(winner!=0) { System.out.printf("Game over! Winner is player %d\n", winner); break; }else if(i==9&&winner==0) { System.out.println("Game over! draw! \n"); } // 플레이어 차례 선정 if(player==1) { player=2; }else { player=1; } } } } ```
자바로 풀어봤습니다. ```{.java} import java.util.Scanner; public class RealTest { // 판단 public static int whoIsWinner(int count, String check, int winner) { if(count==3&check.equals("X")) { winner = 1; }else if(count==3&check.equals("O")) { winner = 2; } return winner; } // 이긴 사람이 있는가? public static int answerResult(String[][] ticTacToe, int player) { int winner = 0; String[] checkList = {"X", "O"}; // 행 판단 for(String check : checkList) { for(int i=0; i<3; i++) { int count = 0; for(int j=0; j<3; j++) { if(ticTacToe[i][j].equals(check)){ count++; } } winner = whoIsWinner(count, check, winner); } } // 열 판단 for(String check : checkList) { for(int j=0; j<3; j++) { int count = 0; for(int i=0; i<3; i++) { if(ticTacToe[i][j].equals(check)){ count++; } } winner = whoIsWinner(count, check, winner); } } // 대각선 판단 for(String check : checkList) { int countLeft = 0, countRight = 0; for(int i=0; i<3; i++) { if(ticTacToe[i][i].equals(check)) { countLeft++; } if(ticTacToe[i][2-i].equals(check)) { countRight++; } } winner = whoIsWinner(countLeft, check, winner); winner = whoIsWinner(countRight, check, winner); } return winner; } // 해당 원소가 들어있는가? & 있으면 표시 public static boolean checkContain(String[][] ticTacToe, String selectedElement, int player) { boolean result = false; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(ticTacToe[i][j].equals(selectedElement)) { result = true; if(player==1) { ticTacToe[i][j] = "X"; }else { ticTacToe[i][j] = "O"; } } } } return result; } // 남은 원소 출력 public static void printRemaining (String[][] ticTacToe, int player) { String remaining = ""; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { remaining += ticTacToe[i][j]; } } System.out.printf("Player %d's turn\n", player); System.out.printfln("please type a position (available position(s) are %s):", remaining:"); } // 결과 출력 public static void painting(String[][] ticTacToe) { for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(j!=2) { System.out.print(" *"); }else { System.out.print(" "); } } System.out.println(); for(int j=0; j<3; j++) { if(j!=2) { System.out.printf(" %s *",ticTacToe[i][j]); }else { System.out.printf(" %s ",ticTacToe[i][j]); } } System.out.println(); for(int j=0; j<3; j++) { if(j!=2) { System.out.print(" *"); }else { System.out.print(" "); } } System.out.println(); } System.out.println(); } // 메인 public static void main(String[] args) { Scanner scan = new Scanner(System.in); // 게임 시작 전 세팅 String[][] ticTacToe = {{"1", "2", "3"}, {"4", "5", "6"}, {"7" ,"8" ,"9"}}; System.out.println("Game Start!"); painting(ticTacToe); // 시작 int player = 1; for(int i=1; i<=9; i++) { while(true) { printRemaining(ticTacToe, player); String selectedElement = scan.next(); boolean isContain = checkContain(ticTacToe, selectedElement, player); if(isContain==false) { System.out.println("The element does not exist. Please re-enter.\n"); continue; }else { break; } } System.out.println(); painting(ticTacToe); int winner = answerResult(ticTacToe, player); if(winner!=0) { System.out.printf("Game over! Winner is player %d\n", winner); break; }else if(i==9&&winner==0) { System.out.println("Game over! draw! \n"); } // 플레이어 차례 선정 if(player==1) { player=2; }else { player=1; } } } } ```
1 Original

2022年06月15日 10:51

유로

자바로 풀어봤습니다. ```{.java} import java.util.Scanner; public class RealTest { // 판단 public static int whoIsWinner(int count, String check, int winner) { if(count==3&check.equals("X")) { winner = 1; }else if(count==3&check.equals("O")) { winner = 2; } return winner; } // 이긴 사람이 있는가? public static int answerResult(String[][] ticTacToe, int player) { int winner = 0; String[] checkList = {"X", "O"}; // 행 판단 for(String check : checkList) { for(int i=0; i<3; i++) { int count = 0; for(int j=0; j<3; j++) { if(ticTacToe[i][j].equals(check)){ count++; } } winner = whoIsWinner(count, check, winner); } } // 열 판단 for(String check : checkList) { for(int j=0; j<3; j++) { int count = 0; for(int i=0; i<3; i++) { if(ticTacToe[i][j].equals(check)){ count++; } } winner = whoIsWinner(count, check, winner); } } // 대각선 판단 for(String check : checkList) { int countLeft = 0, countRight = 0; for(int i=0; i<3; i++) { if(ticTacToe[i][i].equals(check)) { countLeft++; } if(ticTacToe[i][2-i].equals(check)) { countRight++; } } winner = whoIsWinner(countLeft, check, winner); winner = whoIsWinner(countRight, check, winner); } return winner; } // 해당 원소가 들어있는가? & 있으면 표시 public static boolean checkContain(String[][] ticTacToe, String selectedElement, int player) { boolean result = false; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(ticTacToe[i][j].equals(selectedElement)) { result = true; if(player==1) { ticTacToe[i][j] = "X"; }else { ticTacToe[i][j] = "O"; } } } } return result; } // 남은 원소 출력 public static void printRemaining (String[][] ticTacToe, int player) { String remaining = ""; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { remaining += ticTacToe[i][j]; } } System.out.printf("Player %d's turn\n", player); System.out.printf("please type a position (available position(s) are %s):", remaining); } // 결과 출력 public static void painting(String[][] ticTacToe) { for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(j!=2) { System.out.print(" *"); }else { System.out.print(" "); } } System.out.println(); for(int j=0; j<3; j++) { if(j!=2) { System.out.printf(" %s *",ticTacToe[i][j]); }else { System.out.printf(" %s ",ticTacToe[i][j]); } } System.out.println(); for(int j=0; j<3; j++) { if(j!=2) { System.out.print(" *"); }else { System.out.print(" "); } } System.out.println(); } System.out.println(); } // 메인 public static void main(String[] args) { Scanner scan = new Scanner(System.in); // 게임 시작 전 세팅 String[][] ticTacToe = {{"1", "2", "3"}, {"4", "5", "6"}, {"7" ,"8" ,"9"}}; System.out.println("Game Start!"); painting(ticTacToe); // 시작 int player = 1; for(int i=1; i<=9; i++) { while(true) { printRemaining(ticTacToe, player); String selectedElement = scan.next(); boolean isContain = checkContain(ticTacToe, selectedElement, player); if(isContain==false) { System.out.println("The element does not exist. Please re-enter.\n"); continue; }else { break; } } System.out.println(); painting(ticTacToe); int winner = answerResult(ticTacToe, player); if(winner!=0) { System.out.printf("Game over! Winner is player %d\n", winner); break; }else if(i==9&&winner==0) { System.out.println("Game over! draw! \n"); } // 플레이어 차례 선정 if(player==1) { player=2; }else { player=1; } } } } ```
자바로 풀어봤습니다. ```{.java} import java.util.Scanner; public class RealTest { // 판단 public static int whoIsWinner(int count, String check, int winner) { if(count==3&check.equals("X")) { winner = 1; }else if(count==3&check.equals("O")) { winner = 2; } return winner; } // 이긴 사람이 있는가? public static int answerResult(String[][] ticTacToe, int player) { int winner = 0; String[] checkList = {"X", "O"}; // 행 판단 for(String check : checkList) { for(int i=0; i<3; i++) { int count = 0; for(int j=0; j<3; j++) { if(ticTacToe[i][j].equals(check)){ count++; } } winner = whoIsWinner(count, check, winner); } } // 열 판단 for(String check : checkList) { for(int j=0; j<3; j++) { int count = 0; for(int i=0; i<3; i++) { if(ticTacToe[i][j].equals(check)){ count++; } } winner = whoIsWinner(count, check, winner); } } // 대각선 판단 for(String check : checkList) { int countLeft = 0, countRight = 0; for(int i=0; i<3; i++) { if(ticTacToe[i][i].equals(check)) { countLeft++; } if(ticTacToe[i][2-i].equals(check)) { countRight++; } } winner = whoIsWinner(countLeft, check, winner); winner = whoIsWinner(countRight, check, winner); } return winner; } // 해당 원소가 들어있는가? & 있으면 표시 public static boolean checkContain(String[][] ticTacToe, String selectedElement, int player) { boolean result = false; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(ticTacToe[i][j].equals(selectedElement)) { result = true; if(player==1) { ticTacToe[i][j] = "X"; }else { ticTacToe[i][j] = "O"; } } } } return result; } // 남은 원소 출력 public static void printRemaining (String[][] ticTacToe, int player) { String remaining = ""; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { remaining += ticTacToe[i][j]; } } System.out.printf("Player %d's turn\n", player); System.out.printf("please type a position (available position(s) are %s):", remaining); } // 결과 출력 public static void painting(String[][] ticTacToe) { for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(j!=2) { System.out.print(" *"); }else { System.out.print(" "); } } System.out.println(); for(int j=0; j<3; j++) { if(j!=2) { System.out.printf(" %s *",ticTacToe[i][j]); }else { System.out.printf(" %s ",ticTacToe[i][j]); } } System.out.println(); for(int j=0; j<3; j++) { if(j!=2) { System.out.print(" *"); }else { System.out.print(" "); } } System.out.println(); } System.out.println(); } // 메인 public static void main(String[] args) { Scanner scan = new Scanner(System.in); // 게임 시작 전 세팅 String[][] ticTacToe = {{"1", "2", "3"}, {"4", "5", "6"}, {"7" ,"8" ,"9"}}; System.out.println("Game Start!"); painting(ticTacToe); // 시작 int player = 1; for(int i=1; i<=9; i++) { while(true) { printRemaining(ticTacToe, player); String selectedElement = scan.next(); boolean isContain = checkContain(ticTacToe, selectedElement, player); if(isContain==false) { System.out.println("The element does not exist. Please re-enter.\n"); continue; }else { break; } } System.out.println(); painting(ticTacToe); int winner = answerResult(ticTacToe, player); if(winner!=0) { System.out.printf("Game over! Winner is player %d\n", winner); break; }else if(i==9&&winner==0) { System.out.println("Game over! draw! \n"); } // 플레이어 차례 선정 if(player==1) { player=2; }else { player=1; } } } } ```
코딩도장

코딩도장은 프로그래밍 문제풀이를 통해서 코딩 실력을 수련(Practice)하는 곳입니다.

코딩도장 © 2014 · 문의 [email protected]
피드백 · 개인정보취급방침 · RSS

AltStyle によって変換されたページ (->オリジナル) /