mojo's Blog
명품 JAVA Programming 제 5장 Open Challenge 본문
Bear의 Fish 먹기 게임 만들기 구현
import java.util.*;
abstract class GameObject{
protected int distance, 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;
}
public abstract void move();
public abstract char getShape();
}
class Bear extends GameObject{
public Bear(int startX, int startY, int distance) {
super(startX, startY, distance);
// TODO Auto-generated constructor stub
}
@Override
public void move() {
// TODO Auto-generated method stub
this.distance++;
}
@Override
public char getShape() {
// TODO Auto-generated method stub
return 'B';
}
}
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
this.distance++;
if(distance % 5 >= 3) {
Random r = new Random();
int dx[]= {1,-1,0,0};
int dy[]= {0,0,1,-1};
while(true) {
int i = r.nextInt(4);
int nextX=getX()+dx[i];
int nextY=getY()+dy[i];
if(nextX>=0 && nextX<10 && nextY>=0 && nextY<20) {
this.x=nextX;
this.y=nextY;
break;
}
}
}
}
@Override
public char getShape() {
// TODO Auto-generated method stub
return '@';
}
}
class Game{
Scanner scanner=new Scanner(System.in);
GameObject b,f;
int dx[]= {0,1,-1,0};
int dy[]= {-1,0,0,1};
Game(){
b=new Bear(0,0,0);
f=new Fish(5,10,0);
game();
}
public void game() {
while(true) {
showTable();
move();
if(f.collide(b)) {
System.out.println("Bear Wins!!");
break;
}
}
}
public void showTable() {
for(int i=0;i<10;i++) {
for(int j=0;j<20;j++) {
if(b.getX()==i && b.getY()==j) {
System.out.print(b.getShape());
}
else if(f.getX()==i && f.getY()==j) {
System.out.print(f.getShape());
}
else {
System.out.print('-');
}
}
System.out.println();
}
}
public void move() {
System.out.print("왼쪽(a), 아래(s), 위(d), 오른쪽(f) >> ");
String s=scanner.next();
int x=b.getX(), y=b.getY(), nextX=-1, nextY=-1;
if(s.charAt(0)=='a') {
nextX=x+dx[0];
nextY=y+dy[0];
}
else if(s.charAt(0)=='s') {
nextX=x+dx[1];
nextY=y+dy[1];
}
else if(s.charAt(0)=='d') {
nextX=x+dx[2];
nextY=y+dy[2];
}
else if(s.charAt(0)=='f') {
nextX=x+dx[3];
nextY=y+dy[3];
}
if(nextX>=0 && nextX<10 && nextY>=0 && nextY<20) {
b.move();
b.x=nextX; b.y=nextY;
System.out.println("Yes~!");
}
f.move();
}
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner=new Scanner(System.in);
System.out.println("** Bear의 Fish 먹기 게임을 시작합니다. **");
Game g=new Game();
scanner.close();
}
}
'Java' 카테고리의 다른 글
문자열을 기본 타입으로, 기본 타입 값을 문자열로 변환 (0) | 2021.07.20 |
---|---|
명품 JAVA Programming 제 5장 실습문제 12번 (0) | 2021.07.19 |
클래스, 멤버 접근 지정 및 static과 non-static, final 메소드 (0) | 2021.07.17 |
this reference (0) | 2021.07.17 |
문자열의 n 번째 문자 가져오기 (0) | 2021.07.17 |
Comments