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) { //이 객체가 객체p와 충돌했으면 true 리턴
if(this.x == p.getX()&&this.y == p.getY()) {
return true;
}
else {
return false;
}
}
public abstract void move();
public abstract char getShape();
}
import java.util.Scanner;
public class Bear extends GameObject {
Scanner sc;
public Bear(int startX, int startY, int distance) {
super(startX, startY, distance);
// TODO Auto-generated constructor stub
sc = new Scanner(System.in);
}
@Override
public void move() {
// TODO Auto-generated method stub
System.out.print("왼쪽(a), 아래(s), 위(d), 오른쪽(f) >> ");
char c = sc.next().charAt(0);
switch(c) {
case 'a':
x--;
if(x<0) x=0;
break;
case 'f':
x++;
if(x>Game.MAX_X) x=Game.MAX_X-1;
break;
case 'd':
y--;
if(y<0) y=0;
break;
case 's':
y++;
if(y>Game.MAX_Y) y=Game.MAX_Y -1;
break;
}
}
@Override
public char getShape() {
// TODO Auto-generated method stub
return 'B';
}
}
public class Fish extends GameObject {
public Fish(int startX, int startY, int distance) {
super(startX, startY, distance);
// TODO Auto-generated constructor stub
}
@Override
public void move() {
// TODO Auto-generated method stub
// 0,1,2,3,4 중에서 0인 경우 + 방향, 1인 경우 - 방향, 나머지 정지
int n = (int)(Math.random()*5);
if(n==0) x+=distance;
else if(n==1) x-=distance;
if(x<0) x=0;
if(x>=Game.MAX_X) x=Game.MAX_X-1;
n=(int)(Math.random()*5);
if(n==0) y+=distance;
else if (n==1) y-=distance;
if(y<0) y=0;
if(y>=Game.MAX_Y) y=Game.MAX_Y-1;
}
@Override
public char getShape() {
// TODO Auto-generated method stub
return '@';
}
}
public class Game {
public static final int MAX_X = 20;
public static final int MAX_Y = 10;
private char map [][] = new char [MAX_Y][MAX_X];
private GameObject [] m = new GameObject[2];
int state; // 0: 게임 중, 1: Monster가 winner, 2:Human이 winner
public Game() {
for(int i=0;i<MAX_Y;i++) {
for(int j=0;j<MAX_X;j++) {
map[i][j] = '-';
}
}
m[0] = new Bear(0,0,1);
m[1] = new Fish(5,5,2);
}
public void run() {
System.out.println("** Bear의 Fish 먹기 게임을 시작합니다. **");
//초기 좌표에 따른 맵 설정
update();
//초기 게임 맵을 보여줌
draw();
while(!doesEnd()) {
clear();
for(int i=0;i<m.length;i++) {
m[i].move();
}
update();
draw();
}
System.out.println("Bear Wins!!");
}
private void update() {
for(int i=m.length-1;i>=0;i--) {
map[m[i].getY()][m[i].getX()] = m[i].getShape();
}
}
private void clear() {
for(int i = 0;i<m.length;i++) {
map[m[i].getY()][m[i].getX()] ='-';
}
}
private void draw() {
System.out.println();
for(int i=0;i<MAX_Y;i++) {
for(int j=0;j<MAX_X;j++) {
System.out.print(map[i][j]);
}
System.out.println();
}
}
private boolean doesEnd() {
if(m[0].collide(m[1])) {// Bear ate Fish
return true;
}
return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Game game = new Game();
game.run();
}
}
결과
** Bear의 Fish 먹기 게임을 시작합니다. **
B-------------------
--------------------
--------------------
--------------------
--------------------
-----@--------------
--------------------
--------------------
--------------------
--------------------
왼쪽(a), 아래(s), 위(d), 오른쪽(f) >> s
--------------------
B-------------------
--------------------
--------------------
--------------------
-----@--------------
--------------------
--------------------
--------------------
--------------------
왼쪽(a), 아래(s), 위(d), 오른쪽(f) >> s
--------------------
--------------------
B-------------------
--------------------
--------------------
-----@--------------
--------------------
--------------------
--------------------
--------------------
왼쪽(a), 아래(s), 위(d), 오른쪽(f) >> s
--------------------
--------------------
--------------------
B-------------------
--------------------
-----@--------------
--------------------
--------------------
--------------------
--------------------
왼쪽(a), 아래(s), 위(d), 오른쪽(f) >> s
--------------------
--------------------
--------------------
--------------------
B-------------------
--------------------
--------------------
-----@--------------
--------------------
--------------------
왼쪽(a), 아래(s), 위(d), 오른쪽(f) >> s
--------------------
--------------------
--------------------
--------------------
--------------------
B-------------------
--------------------
-----@--------------
--------------------
--------------------
왼쪽(a), 아래(s), 위(d), 오른쪽(f) >> s
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
B-------------------
-----@--------------
--------------------
--------------------
왼쪽(a), 아래(s), 위(d), 오른쪽(f) >> s
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
B----@--------------
--------------------
--------------------
왼쪽(a), 아래(s), 위(d), 오른쪽(f) >> f
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
-B-----@------------
--------------------
--------------------
왼쪽(a), 아래(s), 위(d), 오른쪽(f) >> f
--------------------
--------------------
--------------------
--------------------
--------------------
-------@------------
--------------------
--B-----------------
--------------------
--------------------
왼쪽(a), 아래(s), 위(d), 오른쪽(f) >> f
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
---B---@------------
--------------------
--------------------
왼쪽(a), 아래(s), 위(d), 오른쪽(f) >> f
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
----B--@------------
--------------------
--------------------
왼쪽(a), 아래(s), 위(d), 오른쪽(f) >> f
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
-----B--------------
--------------------
--------------------
Bear Wins!!
더보기
오픈 챌린지가 제일 어렵다...
그냥 카피해서 외우고 있는중..
'명품JAVA프로그래밍 > 5장 상속' 카테고리의 다른 글
[명품JAVA프로그래밍] 5장 실습문제 5번 (0) | 2022.01.12 |
---|---|
[명품JAVA프로그래밍] 5장 실습문제 4번 (0) | 2022.01.12 |
[명품JAVA프로그래밍] 5장 실습문제 3번 (0) | 2022.01.12 |
[명품JAVA프로그래밍] 5장 실습문제 2번 (0) | 2022.01.12 |
[명품JAVA프로그래밍] 5장 실습문제 1번 (0) | 2022.01.12 |