Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- serializeObject
- trim #공백 제거 #PHP trim()
- DATEDIFF()
- DATE_SUB()
- MySQL 날짜 관련 함수
- 영카트 #테스트메일
- Java 및 이클립스 설치
- 이클립스 설치
- SQL 중복제거
- 배열관련함수
- BufferedReader #BufferedWriter
- JSTL
- 함수정의확인
- PHP달력관련함수
- db접속
- Now()
- 가상화
- DATE_ADD()
- php5.x
- Java환경변수 설정
- CURTIME()
- PDO #DB접속
- DATE_FORMAT()
- explode #선택제거 #PHP explode
- apt #apt 명령어 #apt-get명령어
- HAVING 절
- strip_tags #html태그제거 #PHP strip_tags
- apt-get #apt-get 옵션 #apt-get options
- CURDATE()
- sql용어
Archives
- Today
- Total
M
5장 Open Challenge 본문
이 게임에는 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
'Java > 명품 Java Programming' 카테고리의 다른 글
5장 실습문제 (0) | 2022.02.23 |
---|---|
5장 연습문제 (0) | 2022.02.20 |
4장 실습문제 (0) | 2022.02.07 |
4장 연습문제 (0) | 2022.02.07 |
4장 Open Challenge (0) | 2022.02.06 |