1. 문제
-> 다마고치를 키웁시다!!
조건
1. 변수
- 이름
- HP
- 밥먹은 횟수
- 턴 수
- 변을 봤는지에 대한 유무
2. 행동
- 먹기 : 1턴증가, HP 2증가
- 자기 : 3초동안 잠듬, HP 2감소, 턴 2 증가
- 대변보기 : 밥 3번 먹으면 실행, 안치울 경우 1턴당 HP 1 감소
- 청소하기 : 대변을 치운다
- 춤추기 : 1턴 증가, HP 1 증가
- 상태보기
2. 정답 코드
package game;
import java.util.Scanner;
class Character {
//캐릭터 이름
String name;
// hp
int hp;
// 밥 먹은 수
int eatCnt;
// 턴
int turn;
// 변
boolean poo; //ture : o, false : x
public Character(String name, int hp, int eatCnt, int turn, boolean poo) {
super();
this.name = name;
this.hp = hp;
this.eatCnt = eatCnt;
this.turn = turn;
this.poo = poo;
}
//먹기 : 1턴 증가, hp 2 증가
void eat() {
this.turn = this.turn + 1;
this.eatCnt = this.eatCnt + 1;
if(this.eatCnt % 3 == 0) { //그냥 this.eatCnt를 0으로 해야될 것 같다....
poop();
}else {
this.hp = this.hp + 2;
}
}
//자기 : 3초동안 잠에 든다 , 턴 2턴 증가
void sleep() {
for(int i = 0; i < 3; i++) {
System.out.println("zzZ...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("잠에서 꺠어났습니다!");
this.turn = this.turn + 2;
this.hp = this.hp - 2;
}
//대변보기 : 밥을 3번 먹으면 실행, 변을 안치우면 1턴당 hp 1감소
void poop() {
this.poo = true;
this.turn = this.turn + 1;
}
//청소하기 : 대변을 치운다 >> poo > ture >> false 변경
void cleanUp() {
this.poo = false;
this.turn = this.turn + 1;
}
//상태보기
void printState() {
System.out.println("다마고치의 이름은 : " + this.name);
System.out.println("hp : " + this.hp);
System.out.println("먹은 횟수 : " + this.eatCnt);
System.out.println("변 : " + this.poo);
System.out.println("턴 수 : " + this.turn);
}
//춤추기 : 1턴 증가, hp1 증가
void dance() {
System.out.println("신이 납니다!!!");
this.turn = this.turn + 1;
this.hp = this.hp + 1;
}
}
public class Creature {
public static void main(String[] args) {
Character point = new Character("cuti",10,0,0,false);
System.out.println("hi");
Scanner sc = new Scanner(System.in);
boolean life = true;
while(life) {
if(point.hp <= 0) {
System.out.println("다마고치가 운명하였습니다ㅜㅜ");
life = false;
break;
}
System.out.println("다마고치 키우기!!!\n"
+ "1. 먹기\n"
+ "2. 자기\n"
+ "3. 청소하기\n"
+ "4. 상태보기\n"
+ "5. 춤추기\n"
+ "6. 종료\n"
+ "선택하세요 : ");
int choice = sc.nextInt();
switch(choice) {
case 1:
System.out.println("먹이를 줍니다.");
if(point.hp != 0) {
if(point.poo == true) {
System.out.println("변을 치우지 않았습니다 hp가 1감소합니다.");
point.hp = point.hp - 1;
point.eat();
}else if(point.poo == false) {
point.eat();
}
}else {
System.out.println("hp가 없습니다");
}
break;
case 2:
System.out.println("잠을 잡니다.");
if(point.hp != 0) {
if(point.poo == true) {
System.out.println("변을 치우지 않았습니다 hp가 1감소합니다.");
point.hp = point.hp - 1;
point.sleep();
}else if(point.poo == false) {
point.sleep();
}
}else {
System.out.println("hp가 없습니다");
}
break;
case 3:
point.cleanUp();
break;
case 4:
point.printState();
break;
case 5:
if(point.hp != 0) {
if(point.poo == true) {
System.out.println("변을 치우지 않았습니다 hp가 1감소합니다.");
point.hp = point.hp - 1;
point.dance();
}else if(point.poo == false) {
point.dance();
}
}else {
System.out.println("hp가 없습니다");
}
break;
case 6:
System.out.println("종료");
life = false;
break;
default:
System.out.println("잘못된 값을 입력하였습니다");
break;
}
}
}
}
'IT > JAVA' 카테고리의 다른 글
JAVA - 실습 - 로또 번호 추출 (0) | 2023.06.14 |
---|---|
JAVA - 실습 - 주사위 게임 (0) | 2023.06.14 |
JAVA - 예제 - 간단한 TV 전원 및 볼륨 조절 프로그램(리모컨) (0) | 2023.06.13 |
JAVA - 예제 - 생성자 (0) | 2023.06.12 |
JAVA - 예제 - this (0) | 2023.06.12 |
댓글