Java/명품 Java Programming

5장 Open Challenge

M_master 2022. 2. 18. 01:10

이 게임에는 Bear의 Fish 객체가 등장하며, 이들은 10행 20열의 격자판에서 각각 정해진 규칙에 의해 움직인다. Bear는 사용자의 키에 의해 왼쪽(a 키), 아래(s 키), 위(d 키), 오른쪽(f 키)으로 한 칸씩 움직이고, Fish는 다섯 번 중 세 번은 제자리에 있고, 나머지 두 번은 4가지 방향 중 랜덤하게 한 칸씩 움직인다. 게임은 Bear가 Fish를 먹으면(Fish의 위치로 이동) 성공으로 끝난다. 다음은 각 객체의 이동을 정의하는 move()와 각 객체의 모양을 정의하는 getShape()을 추상 메소드로 가진 추상 클래스 GameObject이다. GameObject를 상속받아 Bear과 Fish 클래스를 작성하라. 그리고 전체적인 게임을 진행하는 Game 클래스와 main() 함수를 작성하고 프로그램을 완성하라.

import java.util.Scanner;

abstract class GameObject {
	protected int distance;
	protected int x, y;
	public GameObject(int startX, int startY, int disstance) {
		this.x = startX;
		this.y = startY;
		this.distance = disstance;
	}
	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 {
	Bear(int x, int y, int distance) {
		super(x, y, distance);
	}
	
	public int getX() { return x; }
	public int getY() { return y; }
	
    @Override
	public void move() {
		Scanner sc = new Scanner(System.in);
		
		Game.field[this.x][this.y] = '-';
		
		System.out.print("왼쪽(a), 아래(s), 위(d), 오른쪽(f) >> ");
		String n = sc.next();
		
		switch(n) {
			case "a": this.y = (this.y > 0) ? (this.y - distance) : 0; break;
			case "s": this.x = (this.x < 9) ? (this.x + distance) : 9; break;
			case "d": this.x = (this.x > 0) ? (this.x - distance) : 0; break;
			case "f": this.y = (this.y < 19) ? (this.y + distance) : 19; break;
			default: 
				System.out.println("a, s, d, f 에서 입력해주세요.");
		}
		
		Game.field[this.x][this.y] = this.getShape();
		
		sc.close();
	}
		
	public char getShape() {
		return 'B';
	}
}

class Fish extends GameObject {	
	Fish(int x, int y, int distance) {
		super(x, y, distance);
	}
	
	public int getX() { return x; }
	public int getY() { return y; }
	
    @Override
	public void move() {
		Game.field[this.x][this.y] = '-';
		
		int ran = (int) (Math.random() * 4);
		
		switch(ran) {
			case 0: this.y = (this.y > 0) ? (this.y - distance) : 0; break;
			case 1: this.x = (this.x < 9) ? (this.x + distance) : 9; break;
			case 2: this.x = (this.x > 0) ? (this.x - distance) : 0; break;
			case 3: this.y = (this.y < 19) ? (this.y + distance) : 19; break;
		}
		
		Game.field[this.x][this.y] = this.getShape();
	}
		
	public char getShape() {
		return '@';
	}
}

public class Game {
	Scanner sc = new Scanner(System.in);
	
	Bear bear;
	Fish fish;
	
	public static char[][] field = new char[10][20];
	
	public void gameReset() {
		bear = new Bear(0, 0, 1);
		fish = new Fish(5, 5, 1);
		
		for(int i = 0; i < field.length; i++) {
			for(int j = 0; j < field[i].length; j++) {
				field[i][j] = '-';
			}
		}
		
		field[bear.x][bear.y] = bear.getShape();
		field[fish.x][fish.y] = fish.getShape();
	}
	
	public void fieldShow() {
		for(int i = 0; i < field.length; i++) {
			for(int j = 0; j < field[i].length; j++) {
				System.out.print(field[i][j]);
			}
			System.out.println();
		}
	}
	
	public void run() {		
		System.out.println("** Bear의 Fish 먹기 게임을 시작합니다. **");
		
		gameReset();
		
		int ran;
		while(true) {
			fieldShow();
			
			ran = (int) (Math.random() * 2);
			
			if(ran == 1) fish.move();
			
			bear.move();
			
			if(bear.collide(fish) == true) {
				fieldShow();
				System.out.println("Bear Wins!!");
				break;
			}
		}
		
		sc.close();
	}
	
	public static void main(String[] args) {
		Game game = new Game();
		game.run();
	}

}
728x90