티스토리 뷰

1. 할머니-엄마-짱구 상속 관계 퀴즈

 

문제점 : 오버로딩된 짱구의 핸드폰뺏기 메소드에서 mom.핸드폰압수bag의 값을 넣어서 setPhone() 메소드를 찍었는데도 왜 this.getPhone()값이 계속 null일까....? 분명 main에서 엄마클래스의 핸드폰뺏기 메소드를 호출한 후(짱구의 핸드폰을 엄마의 핸드폰압수백에 넣었음) 짱구의 핸드폰뺏기 메소드를 실행했는데도 말이다... 그걸 아직 해결 못했다.

 

 

2. 배틀 코드 리팩토링 - 모듈화 부분

기존의 문제가 있는 배틀 코드를 고쳐보기로 한다.

package player;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

import view.Mywin;

public class Player implements Attackable {
    public int hp;
    public int power;
    public int speed;
    public int protection;
    public int dodge;
    public int accuracy;
    public int dice;
    public int stress=0;
    public String name;
    public int stressCount=0;
    
    public Player() {}
    
    public Player(String name, int hp, int power, int speed, int protection, int dodge, int accuracy) {

    	this.name = name;
    	this.hp = hp;
    	this.power = power;
    	this.speed = speed;
    	this.protection = protection;
    	this.dodge = dodge;
    	this.accuracy =accuracy;
    	
    	//이거 이미지파일이름+메소드 다 삭제해도 될 듯.. 내 코드 개 비효율적으로 짜놔서 필요업음

    	}

    public void attack(Player target){
    	if ((int) (Math.random() * 1000) % 100 <(this.accuracy-target.dodge)) {
    		Mywin.ta.append("공격이 성공했습니다!\n");
    		System.out.println("공격이 성공했습니다!");
    		int r=(int) (Math.random() * 1000) % 6;
    		if(r==0) {
    			target.hp-=(this.power-target.protection);
    			System.out.printf("%s의 공격이 %s에게 %d 만큼의 데미지를 입혔습니다!",this.name,target.name,this.power-target.protection);
    			System.out.println();
    			String str= this.name +"의 공격이" + target.name+ "에게"+(this.power-target.protection)+"만큼의 데미지를 입혔습니다!\n";
    			Mywin.ta.append(str);
    		}
    		else if(r==1) {
    			target.hp-=(this.power-target.protection+1);
    			System.out.printf("%s의 공격이 %s에게 %d 만큼의 데미지를 입혔습니다!",this.name,target.name,this.power-target.protection+1);
    			System.out.println();
    			String str = this.name +"의 공격이" + target.name+ "에게"+(this.power-target.protection+1)+"만큼의 데미지를 입혔습니다!\n";
    			Mywin.ta.append(str);
    		}
    		else if(r==2) {
    			target.hp-=(this.power-target.protection+2);
    			System.out.printf("%s의 공격이 %s에게 %d 만큼의 데미지를 입혔습니다!",this.name,target.name,this.power-target.protection+2);
    			System.out.println();
    			String str = this.name +"의 공격이" + target.name+ "에게"+(this.power-target.protection+2)+"만큼의 데미지를 입혔습니다!\n";
    			Mywin.ta.append(str);
    		}
    		else if(r==3) {
    			target.hp-=(this.power-target.protection+3);
    			System.out.printf("%s의 공격이 %s에게 %d 만큼의 데미지를 입혔습니다!",this.name,target.name,this.power-target.protection+3);
    			System.out.println();
    			String str = this.name +"의 공격이" + target.name+ "에게"+(this.power-target.protection+3)+"만큼의 데미지를 입혔습니다!\n";
    			Mywin.ta.append(str);
    		}
    		else if(r==4) {
    			target.hp-=(this.power-target.protection+4);
    			System.out.printf("%s의 공격이 %s에게 %d 만큼의 데미지를 입혔습니다!",this.name,target.name,this.power-target.protection+4);
    			System.out.println();
    			String str = this.name +"의 공격이" + target.name+ "에게"+(this.power-target.protection+4)+"만큼의 데미지를 입혔습니다!\n";
    			Mywin.ta.append(str);
    		}
    		else if(r==5) {
    			target.hp-=(this.power-target.protection+5);
    			System.out.printf("%s의 공격이 %s에게 %d 만큼의 데미지를 입혔습니다!",this.name,target.name,this.power-target.protection+5);
    			System.out.println();
    			String str = this.name +"의 공격이" + target.name+ "에게"+(this.power-target.protection+5)+"만큼의 데미지를 입혔습니다!\n";
    			Mywin.ta.append(str);
    		}
    		if(target instanceof Heroes) {
	    		r=(int) (Math.random() * 1000) % 11;
	    		target.stress+=r;
	    		System.out.printf("공격으로 인해 %s의 스트레스가 %d만큼 증가합니다!",target.name,r);
	    		System.out.println();
	    		String str = "공격으로 인해"+target.name+ "의 스트레스가"+ r+"만큼 증가합니다!\n";
    			Mywin.ta.append(str);
    		}
    	}
    	else {
    		System.out.println("공격이 빗나갔습니다!");
    		String str = "공격이 빗나갔습니다!\n";
			Mywin.ta.append(str);
    	}
    }
    
    public void playerStress(Player pl,JLabel Label,String image1, String image2) {
    	if(pl.stress>=25&&stressCount==0) {
    		System.out.println("영웅이 고통의 굴림에 빠졌습니다...");
    		String str = "영웅이 고통의 굴림에 빠졌습니다...\n";
			Mywin.ta.append(str);
    		int r=(int) (Math.random() * 1000) % 4;
    		
    		
    		if(r==0) {
    			Label.setIcon(new ImageIcon(Mywin.class.getResource(image2)));
    			System.out.println("영웅의 기상에 돌입합니다!");
    			str = "영웅의 기상에 돌입합니다!\n";
    			Mywin.ta.append(str);
    			pl.power+=2;
    			System.out.println("power가 2만큼 증가합니다!");
    			str = "power가 2만큼 증가합니다!\n";
    			Mywin.ta.append(str);
    			pl.stress=0;
    			
    		}
    		else {
    			Label.setIcon(new ImageIcon(Mywin.class.getResource(image1)));
    			stressCount++;
    			System.out.println("영웅이 붕괴합니다..");
    			str = "영웅이 붕괴합니다..\n";
    			Mywin.ta.append(str);
    			pl.dodge-=10;
    			System.out.println("dodge가 10만큼 감소합니다..");
    			str = "dodge가 10만큼 감소합니다..\n";
    			Mywin.ta.append(str);
    		}
    	}
    	if(pl.stress>=50&&stressCount==1) {
    		System.out.println("스트레스가 50이 되어 영웅이 심장마비로 죽습니다.");
    		String str = "스트레스가 50이 되어 영웅이 심장마비로 죽습니다.\n";
			Mywin.ta.append(str);
    		pl.hp=0;
    	}
    }
    
    
}

 

원래 코드는 이러했다.

문제점

1. 콘솔 출력용 SYSO와 Mywin 윈도우빌더 출력용 메시지들이 반복됐다. (그래도 사실 이걸 보고 이렇게 append하는 게 올바른 방법이라는 걸 알았다. 저번에 배틀 과제를 했을 땐 그냥 TextAreaOutputStream을 썼었기 때문이다.)

-> public static void 동시출력(String s) 메소드로 빼보자.

 

2. 랜덤값인 r값에 따라 if-else문으로 분기했는데 이로 인해 재활용성이 떨어지고 코드가 반복된다.

->  r값이 정해지면, 그 r값을 인자로 받아서 반복되는 코드를 수행하는 메소드로 만들어보자.

 

3. target instanceof Heroes 부분 instanceof를 안쓰고 싶다.

 

4. playerStress 메소드 부분의 if-else문을 고치고 싶다. 

 

package player;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

import view.Mywin;

public class Player implements Attackable {
    public int hp;
    public int power;
    public int speed;
    public int protection;
    public int dodge;
    public int accuracy;
    public int dice;
    public int stress=0;
    public String name;
    public int stressCount=0;
    
    public Player() {}
    
    public Player(String name, int hp, int power, int speed, int protection, int dodge, int accuracy) {

    	this.name = name;
    	this.hp = hp;
    	this.power = power;
    	this.speed = speed;
    	this.protection = protection;
    	this.dodge = dodge;
    	this.accuracy =accuracy;
    	
    	//이거 이미지파일이름+메소드 다 삭제해도 될 듯.. 내 코드 개 비효율적으로 짜놔서 필요업음

    	}
    
    /*
    Player target = new Player();
    int t = (int) (Math.random() * 1000) % 6;
    int damage = this.power - target.protection;
    
    public void changeAttack(int t) {
    	target.hp -= damage;
    	System.out.printf("%s의 공격이 %s에게 %d 만큼의 데미지를 입혔습니다!",this.name,target.name,this.damage);
		System.out.println();
		//String str= this.name +"의 공격이" + target.name+ "에게"+damage+"만큼의 데미지를 입혔습니다!\n";
		Mywin.ta.append(this.line(t));
    }
    
    public String line(int t) {
    	String str = this.name+"의 공격이"+target.name+"에게 "+damage+t+"만큼의 데미지를 입혔습니다!\n";
    	return str;
    }*/
    
    //int random = (int) (Math.random() * 1000);
    //int r=random % 6;
    
    //int acdo = this.accuracy - target.dodge;
    public int 명중확률(Player target) {
    	return this.accuracy - target.dodge;
    }
    
    public void randomAttack(Player target, int r) {
    	//this.r = r;
    	target.hp-=(this.power-target.protection);
    	String msg = 데미지메시지출력(target, r);
    	
    }
    
    public static void 동시출력(String s) {
    	if(Mywin.ta!=null) Mywin.ta.append(s);
    	System.out.print(s);
    }
    
    public String 데미지메시지출력(Player target, int r) {
    	String str = this.name+"의 공격이 "+target.name+"에게 "+(this.power-target.protection+r)+"만큼의 데미지를 입혔습니다!";
    	return str;
    }
    
    public void attack(Player target){
    	int random = (int)(Math.random()*100);
    	if (random <명중확률(target)) {
    		동시출력("공격이 성공했습니다!\n");
    		
    		int r=(int)(Math.random()*6);
    		randomAttack(target, r);
    		
    		if(target instanceof Heroes) {
	    		r=(int) (Math.random() * 1000) % 11;
	    		target.stress+=r;
    			동시출력("공격으로 인해"+target.name+ "의 스트레스가"+ r+"만큼 증가합니다!\n");
    		}
    	}
    	else {
    		동시출력("공격이 빗나갔습니다!\n");
    	}
    }
    
    /*
    public void randomPlayerStress(Player pl, JLabel Label, String image1, String image2) {
    	Label.setIcon(new ImageIcon(Mywin.class.getResource(image2)));
    	System.out.println("영웅의 ㄱ");
    }*/
    
    public void playerStress(Player pl,JLabel Label,String image1, String image2) {
    	if(pl.stress>=25&&stressCount==0) {
    		동시출력("영웅이 고통의 굴림에 빠졌습니다...\n");
    		int r=(int) (Math.random() * 1000) % 4;
    		
    		
    		if(r==0) {
    			Label.setIcon(new ImageIcon(Mywin.class.getResource(image2)));
    			동시출력("영웅의 기상에 돌입합니다!\n");
    			pl.power+=2;
    			동시출력("power가 2만큼 증가합니다!\n");
    			pl.stress=0;
    			
    		}
    		else {
    			Label.setIcon(new ImageIcon(Mywin.class.getResource(image1)));
    			stressCount++;
    			동시출력("영웅이 붕괴합니다..\n");
    			pl.dodge-=10;
    			동시출력("dodge가 10만큼 감소합니다..\n");
    		}
    	}
    	if(pl.stress>=50&&stressCount==1) {
    		동시출력("스트레스가 50이 되어 영웅이 심장마비로 죽습니다.\n");
    		pl.hp=0;
    	}
    }
    
    
}

 

일단 여기까지 했다. 

1. attack(Player target) 메소드에서 random<명중확률(target) 부분으로 인한 if-else 도 고치고 싶은데 저 자체를 불리안 변수로 받아볼까...?

2. playerStress 메소드는 구조가 아예 똑같진 않고, 로직을 아직 내가 제대로 이해 못한 것 같아서 이해한 후, 역시 r값을 인자로 받는 메소드로 만들어볼까 싶다....

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2026/03   »
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
글 보관함