[명품 Java Programming] 5장 Open Challenge

2022. 1. 7. 10:05Java/명품 Java

** 해당 문제가 수록된 장까지의 교재 내용만을 이용하여 소스코드를 작성하였습니다**

 

소스코드

import java.util.*;

abstract class GameObject{
	protected int distance;
	protected int x, y;
	public GameObject(int startX, int startY, int distance) {
		this.x= startX;
		this.y= startY;
		this.distance = distance;
	}
	public int getX() {return x;}
	public int getY() {return y;}
	public boolean collide(GameObject p) {
		if(this.x ==p.getX()&&this.y ==p.getY())
			return true;
		else
			return false;
	}
	protected abstract void move();
	protected abstract char getShape(); 
}


class Bear extends GameObject{
	
	public Bear(int x, int y, int distance) {
		super(x, y, distance);
	}
	
	public void move() {
		switch(Game.key) {
		case "a":
			if(this.x == 1) {
				this.x = 0;
				System.out.println("왼쪽으로 더 움직일 수 없습니다.");
			}else
				this.x -= this.distance;
			break;
			
		case "s":
			if(this.y == 10) {
				this.y = 10;
				System.out.println("아래로 더 움직일 수 없습니다.");
			}else
				this.y += this.distance;
			break;
			
		case "d":
			if(this.y == 1) {
				this.y = 1;
				System.out.println("위로 더 움직일 수 없습니다.");
			}else
				this.y -= this.distance;
			break;
		
		case "f":
			if(this.x == 10) {
				this.x = 10;
				System.out.println("오른쪽으로 더 움직일 수 없습니다.");
			}else
				this.x += this.distance;
			break;
		}
	}
	public char getShape() {
		return 'B';
	}
	
}
class Fish extends GameObject{
	
	public Fish(int x, int y, int distance) {
		super(x, y, distance);
	}
	
	public void move() {
		int randomKey = (int)(Math.random()*4 + 1);
		
		switch(randomKey) {
		case 1:
			if(this.x == 1) 
				this.x = 0;
			else
				this.x -= this.distance;
			break;
			
		case 2:
			if(this.y == 10) 
				this.y = 10;
			else
				this.y += this.distance;
			break;
			
		case 3:
			if(this.y == 1) 
				this.y = 1;
			else
				this.y -= this.distance;
			break;
		
		case 4:
			if(this.x == 10) 
				this.x = 10;
			else
				this.x += this.distance;
			break;
		}
	}
	public char getShape() {
		return '@';
	}
	
}

public class Game{
	Scanner sc1 = new Scanner(System.in);
	int n,m;
	int index1, index2;
	int count = 0;
	Bear bear;
	Fish fish;
	Character[][] background;
	static String key;
	
	public void Run() {   //Game 진행
		System.out.println("**Bear의 Fish 먹기 게임을 시작합니다.**");
		Set();
		Show();
		
		while(true){             // 무한루프
			System.out.print("왼쪽(a), 아래(s), 위(d), 오른쪽(f) >>");
			key = sc1.next();
			
			if(count == 0) {
				for(int i = 0;i<1;i++){       //fish 움직일 경우 정하기
					index1 = (int)(Math.random()*5);
					index2 = (int)(Math.random()*5);
					if(index1 == index2) 
						i--;
				}
			}
			
			bear.move();               //bear 움직이기
			if(count == index1|| count == index2)  //fish 움직이기
				fish.move();
			
			if(bear.collide(fish)) {   //이긴 경우
				winnerShow();
				break;
			}else if(count == 4){     //아직 잡지 못한 경우
				Show();
				count = 0;
			}else {
				Show();
				count++;
			}
		}
		
	}
	
	public void Set() {   //Game 초기설정
		background = new Character[10][20]; //격자판 생성
		
		for(int i=0; i<background.length; i++) {          //격자판 모양 설정
			for(int j = 0; j<background[0].length; j++) 
				background[i][j] = '-';
		}
		
		bear = new Bear(1,1,1); //Bear 생성
		
		for(int i = 0; i<1; i++){						  //Fish 초기 위치 설정
			n = (int)(Math.random()*10 + 1);
			m = (int)(Math.random()*20 + 1);
			
			if(n ==0 && m ==0)
				i--;
		}
		fish = new Fish(n,m,1);
	}
	
	public void Show() {    // 격자판 보여주기
		for(int i = 0; i<background.length; i++) {
			for(int j = 0; j<background[0].length; j++) {
				if(i == bear.y-1 && j == bear.x-1) 
					System.out.print(bear.getShape());
				else if(i == fish.y-1 && j == fish.x-1) 
					System.out.print(fish.getShape());
				else
					System.out.print(background[i][j]);
			}
			System.out.println();
		}
	}
	
	public void winnerShow() {
		for(int i = 0; i<background.length; i++) {
			for(int j = 0; j<background[0].length; j++) {
				if(i == bear.y-1 && j == bear.x-1) 
					System.out.print(bear.getShape());
				else
					System.out.print(background[i][j]);
			}
			System.out.println();
		}
		System.out.println("Bear Wins!!");
	}
	

    public static void main(String[] args){
    	Game game = new Game();
    	game.Run();
     }
}

 

결과