프로그래밍 언어 복습/JAVA
#28 JAVA 낚시게임 클래스화 하기
사재원 교수
2022. 5. 21. 17:30
package hihihi;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
//낚시배열 만들기 + 확인 이미지 만들기
class Making{
Random r = new Random();
int[][] array = new int[5][5]; //어항 만들기
String[][] images = new String[5][5]; //확인 이미지 만들기
public void Maingarray() {
for(int i=0;i<3;i++) {
int x = r.nextInt(5);
int y = r.nextInt(5);
if(array[x][y] == 0) {
array[x][y] = 1;
}else {
i--;
}
}
for(int i=0;i<images.length;i++) {
for(int j=0;j<images[i].length;j++) {
images[i][j] = "O";
}
}
}
}
//캐스팅
class Casting{
Scanner sc = new Scanner(System.in);
public int[] cas() {
int[] jupo = new int[2];
while(true) {
System.out.println("캐스팅을 하겠습니다.");
System.out.print("x 좌표 입력 : ");
jupo[0] = sc.nextInt();
System.out.print("y 좌표 입력 : ");
jupo[1] = sc.nextInt();
if((jupo[0]>4 || jupo[0]<0) || (jupo[1] >4 || jupo[1] <0)){
System.out.println("없는 좌표입니다.");
continue;
}else {
break;
}
}
return jupo;
}
}
//이동할 방향 선택
class Where{
Scanner sc = new Scanner(System.in);
public int num() {
System.out.println("이동할 방향 선택 위(1) 아래(2) 오른쪽(3) 왼쪽(4)");
int num = sc.nextInt();
while(true) {
if(num>4 || num <0) {
System.out.println("없는 방향입니다.");
}else {
break;
}
}
return num;
}
}
//물고기 마리수 세기
class How{
int count = 0;
Making m = new Making();
Casting c = new Casting();
Where w = new Where();
public void Start() {
m.Maingarray(); //어항 생성 , 이미지 생성
int[] jupo = c.cas();
int x = jupo[0];
int y = jupo[1];
if(m.array[x][y]==1) {
System.out.println("물고기를 잡았습니다 ! ");
m.array[x][y] = 0; //물고기를 잡았으므로 0으로 초기화
count ++; //잡은 마리수 +1
}else {
System.out.println("해당 좌표에는 물고기가 없습니다.");
}
while(true) {
m.images[x][y] = "X"; //현재 좌표위치에 X표시
for(int i=0;i<5;i++) {
for(int j=0;j<5;j++) {
System.out.print(m.images[i][j]+"\t");
}
System.out.println();
}//좌표 위치 보여주기
m.images[x][y] = "O"; //보여주고 난후 좌표 초기화
int where = w.num();
if(where==1) {//위
if(x-1 < 0) {
System.out.println("이동할수없습니다.");
}else {
x = x - 1;
}
}else if(where ==2) {//아래
if(x+1 > 4) {
System.out.println("이동할수없습니다.");
}else {
x = x + 1;
}
}else if(where ==3) {//오른쪽
if(y+1 > 4) {
System.out.println("이동할수없습니다.");
}else {
y = y + 1;
}
}else if(where ==4) {//왼쪽
if(y-1 < 0) {
System.out.println("이동할수없습니다.");
}else {
y = y - 1;
}
}
if(m.array[x][y] == 1) {
System.out.println("물고기를 잡았습니다 !");
m.array[x][y] = 0;
count ++;
}
if(count == 3) {
System.out.println("물고리를 모두 잡았습니다.");
break;
}
}
}
}
public class gta {
public static void main(String[] args) {
How h = new How();
h.Start();
}
}
감사합니다.