본문 바로가기
IT/JAVA

JAVA - 실습 - 다마고치 키우기

by 비준 2023. 6. 13.

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;
}
}

}

}

댓글