본문 바로가기

명품자바

[명품자바] 오픈 챌린지 5

import java.util.Scanner;

public 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 {
	Scanner scan = new Scanner(System.in);
	
	public Bear(int startX, int  startY, int distance) {
		super(startX, startY, distance);
	}
	
	protected void move() {
		String input = scan.nextLine();
		switch(input)
		{
		case "a":
			if (this.x == 0)
				break;
			this.x--;
			break;
		case "f":
			if (this.x == 19)
				break;
			this.x++;
			break;
		case "s":
			if (this.y == 9)
				break;
			this.y++;
			break;
		case "d":
			if (this.y == 0)
				break;
			this.y--;
			break;
		default:
			break;
		}
	}
	
	protected char getShape() { return 'B'; }
}

class Fish extends GameObject {	
	public Fish(int startX, int  startY, int distance) {
		super(startX, startY, distance);
	}
	
	protected void move() {
		int direction = (int)(Math.random()*3);
		switch(direction)
		{
		case 0:
			if (this.x == 0)
				break;
			this.x--;
			break;
		case 1:
			if (this.x == 19)
				break;
			this.x++;
			break;
		case 2:
			if (this.y == 9)
				break;
			this.y++;
			break;
		case 3:
			if (this.y == 0)
				break;
			this.y--;
			break;
		default:
			break;
		}
	}
	
	protected char getShape() { return '@'; }
}


class Game {
	char map[][] = new char[10][20];
	
 	public void draw() {
 		for (int i = 0; i < 10; i++) {
 			for (int j = 0; j < 20; j++)
 				map[i][j] = '-';
 		}
 	}
	
 	public void  printMap() {
 		for (int i = 0; i < 10; i++) {
 			for (int j = 0; j < 20; j++)
 				System.out.print(map[i][j]);
 			System.out.println("");
 		}
 	}
 	
	public void main() {
		Bear b = new Bear(0,0,0);
		Fish f = new Fish(5, 5, 0);
		
		System.out.println("** Bear의 Fish 먹기 게임을 시작합니다. **");
		
		int turn = 0;	// 게임이 진행된 턴
		int move = 0;	// 물고기가 움직인 횟수
		while(true) 
		{
			draw();
			map[b.getY()][b.getX()] = b.getShape();
			map[f.getY()][f.getX()] = f.getShape();
			printMap();
			
			System.out.print("왼쪽(a), 아래(s), 위(d), 오른쪽(f) >>");
			b.move();
			
			if (b.collide(f))
				break;
			
			
			// 물고기 이동
			if (turn%5 == 0 && turn != 0)	// 다섯판이 지날 때마다 물고기의 이동회수 초기화 한다
				move = 0;
			else if ((turn%5 == 4 && move == 1) || (turn%5 == 3 && move == 0))	//  물고기가 무조건 5판중 2번은 이동하게 한다. 
			{
				move++;
				f.move();
				move++;
			}
			else if (move < 2)	// 물고기가 2번이상 안 움직였으면 랜덤한 확률로 움직인다.
			{
				int r = (int)(Math.random()*2);
				
				if (r == 1)
				{
					f.move();
					move++;
				}
			}
			turn++;
		}
		b.scan.close();
		
		draw();
		map[b.getY()][b.getX()] = b.getShape();
		printMap();
		System.out.println("Bear Wins!");
	}
}

 

 

'명품자바' 카테고리의 다른 글

[명품자바] 오픈 챌린지 6  (0) 2024.06.10
[명품자바] 오픈 챌린지 4  (0) 2024.06.10
[명품자바] 오픈 챌린지 3  (0) 2024.06.10
[명품자바] 오픈 챌린지 10  (0) 2024.06.09
[명품자바] 오픈 챌린지 9  (0) 2024.06.09