반응형
오목 게임을 만들어 보겠다.
내가 짠 순서도는
오목판
1번째
중복된 값이 있나 확인.
바둑돌 배열에서 중복된 좌표값이 있으면 경고창 출력
2번째
순서대로 오목 알 출력 ------여기까지 완성
3번째
5개 연속적으로 그어졌을 때
그 돌의 주인이 승리.
4번째
게임 재시작 여부 묻기
5번째
아니요 - 종료
예 - 다시 오목판 출력
package javaExample1;
import java.util.Random;
import java.util.Scanner;
public class Ex13omokProgram2 {
public static void main(String[] args) {
int height = 0;
int width = 0;
int[] xs;
int[] ys;
int menu;
Scanner scan = new Scanner(System.in);
while (true) {
// 메뉴판 부분.
System.out.println("어떤 바둑판 크기를 원하십니까?");
System.out.println("1. (20x20)");
System.out.println("2. (40x20)");
System.out.println("3. (60x30)");
System.out.print(">");
String menu_ = scan.nextLine();
menu = Integer.parseInt(menu_);
if (menu < 1 || menu > 3)
System.out.println("1~3사이의 값을 입력하세요");
else
break;
} // while
switch (menu) {
case 2:
height = 40;
width = 20;
break;
case 3:
height = 60;
width = 30;
break;
case 1:
default:
height = 20;
width = 20;
}// swich
char[][] board = new char[height][width];
// 바둑돌 배열크기정함
xs = new int[width * height];
ys = new int[width * height];
// 바둑판 출력
for (int y = 1; y <= height; y++) {
for (int x = 1; x <= width; x++) {
board[y - 1][x - 1] = '┼';
}
}
// 상판
for (int x = 1; x < width; x++)
board[0][x - 1] = '┬';
// 우측벽
for (int y = 1; y <= height; y++)
board[y - 1][width - 1] = '┤';
// 밑판
for (int x = 1; x <= width; x++)
board[height - 1][x - 1] = '┴';
// 좌측벽
for (int y = 1; y <= height; y++)
board[y - 1][width - 20] = '├';
// 4개의 꼭지점.
board[0][0] = '┌'; // 왼쪽상단모서리
board[0][width - 1] = '┐'; // 오른쪽상단모서리
board[height - 1][width - 1] = '┘'; // 오른쪽하단 모서리
board[height - 1][0] = '└'; // 왼쪽하단모서리
int index = 0;
//반복적으로 바둑돌 출력하기위한 while문
while (true) {
int k = 0;
EXIT:while (k == 0) {
System.out.println("x좌표입력,y좌표입력");
int ox = scan.nextInt();
int oy = scan.nextInt();
System.out.println();
// 입력받은 값을 인덱스에 넣고
xs[index] = ox;
ys[index] = oy;
index++;
for (int i = 0; i < index; i++) {
for (int j = 0; j < i; j++) {
if (xs[i] == xs[j] && ys[i] == ys[j]) {
System.out.println("값이 중복됬습니다.");
i--;
index--;
continue EXIT;
}
}
}
k++;
}
// 인덱스값에 해당되는 배열에 값을 넣음(0,0)을기준
for (int i = 0; i < index; i++) {
int x = xs[i];
int y = ys[i];
if (i % 2 == 0) {
board[x][y] = '○'; // 홀수
} else if (i % 2 == 1)
board[x][y] = '●'; // 짝수
}
// 출력부분
for (int y = 1; y <= height; y++) {
for (int x = 1; x <= width; x++) {
System.out.printf("%c", board[y - 1][x - 1]);
}
System.out.println();
} // for
} // while
}// main
}// class
3번째 고민
바둑돌이
왼쪽 상단 모서리에 있을 때
오른쪽 상단 모서리 벽에 있을 때
왼쪽 하단 모서리에 있을 때
오른쪽 상단 모서리에 있을 때
왼쪽 벽에 있을 때
오른쪽 벽에 있을 때
상단 벽에 있을 때
하단 벽에 있을 때
중복되는 조건이 있는데 그럴땐 각 모서리가 우선순위가 되어야하므로 절차를 작성할때 상단에 작성해 먼저 출력되도록 하는방법이있다
반응형
'웹 프로그래밍 기초 > 자바기반의 웹&앱 응용SW 개발자' 카테고리의 다른 글
| 자바기반의 웹&앱 응용 SW개발자 양성과정 10일차 -20 (0) | 2020.03.10 |
|---|---|
| 자바기반의 웹&앱 응용 SW개발자 양성과정 10일차 -19 (0) | 2020.03.10 |
| 자바기반의 웹&앱 응용 SW개발자 양성과정 8일차 -17 (0) | 2020.03.09 |
| 코로나 휴강 대체할 t형의 복습문제13. (0) | 2020.03.08 |
| 코로나 휴강 대체할 t형의 복습문제12. (0) | 2020.03.08 |
댓글