java 関数を複数呼び出ししてしまう
関数が複数呼び出されてしまう
<問題と、したいこと>
別の関数の戻り値を利用して、その値を別の関数に利用しようとした
その結果,mainでコメントアウトした処理をすると同じ処理を何度もしてしまう
あと、levelUp関数が機能してくれない。
該当するソースコード
class character{
String name;
int maxHitPoint;
int hitPoint;
int atkPoint;
int speed;
character(String name, int maxHitPoint, int hitPoint, int atkPoint, int speed){
this.name = name;
this.maxHitPoint = maxHitPoint;
this.hitPoint = hitPoint;
this.atkPoint = atkPoint;
this.speed = speed;
}
}
class player extends character{
int level;
int p_exPoint;
player(String name, int level, int maxHitPoint, int hitPoint, int atkPoint, int speed, int p_exPoint){
super(name, maxHitPoint, hitPoint, atkPoint, speed);
this.level = level;
this.p_exPoint = p_exPoint;
}
int adventure(player[] player, monster[] monster) {
int random_adventure = (int)(Math.random()*100) + 1;
if(1 <= random_adventure && random_adventure <= 10) {
/*何もなかった*/
System.out.println("今日は何も見つからなかった。");
player[0].hitPoint = player[0].hitPoint + (player[0].maxHitPoint*10)/100;
return player[0].hitPoint;
}
else if(11 <= random_adventure && random_adventure <= 100) {
/*モンスターに出くわす*/
int monsterCount = 0;
for(int i = 0; i < monster.length; i++) {
if(monster[i].conditionsPoint <= player[0].level) {
//当てはまったモンスターの番号を格納する配列作る
monsterCount++;
}
}
int random_monster = (int)(Math.random()* monsterCount);
System.out.println(monster[random_monster].name + "に出くわした!");
return random_monster;
}
return 0;
}
int fight(player[] player, monster[] monster) {
Integer random_monster = adventure(player, monster);
int battlestart = 0;
int MyTurn = 0;
int EnemyTurn = 0;
if(battlestart == 0) {
if(player[0].speed >= monster[random_monster].speed) {
MyTurn = 1 ;
EnemyTurn = 0;
}
else {
MyTurn = 0 ;
EnemyTurn = 1;
}
battlestart = 1;
}
for(int i = 1; i <= 15; i++) {
if(MyTurn == 1) {
/////////////////////自分のターンが先行////////////////////////////
//////////////////////戦闘エフェクト//////////////////////////////
System.out.println(player[0].name + "の攻撃");
//////////////////////ダメージ計算////////////////////////////////
monster[random_monster].hitPoint = monster[random_monster].hitPoint - player[0].atkPoint;
System.out.println(monster[random_monster].name + "に" + player[0].atkPoint + "のダメージ");
if(monster[random_monster].hitPoint <= 0) {
System.out.println(monster[random_monster].name + "を倒した!");
int exPointBox = monster[random_monster].m_exPoint;
return exPointBox;
}
System.out.println(monster[random_monster].name + "の攻撃");
player[0].hitPoint = player[0].hitPoint - monster[random_monster].atkPoint;
System.out.println(player[0].name + "は" + monster[random_monster].atkPoint + "のダメージ");
if(player[0].hitPoint <= 0) {
System.out.println(player[0].name + "は倒れてしまった!");
return player[0].hitPoint;
}
}
if(EnemyTurn == 1) {
/////////////////////相手ターンが先行////////////////////////////
//////////////////////戦闘エフェクト//////////////////////////////
System.out.println(monster[random_monster].name + "の攻撃");
//////////////////////ダメージ計算////////////////////////////////
player[0].hitPoint = player[0].hitPoint - monster[random_monster].atkPoint;
System.out.println(player[0].name + "は" + monster[random_monster].atkPoint + "のダメージ");
if(player[0].hitPoint <= 0) {
System.out.println(player[0].name + "は倒れてしまった!");
return player[0].hitPoint;
}
System.out.println(player[0].name + "の攻撃");
monster[random_monster].hitPoint = monster[random_monster].hitPoint - player[0].atkPoint;
System.out.println(monster[random_monster].name + "に" + player[0].atkPoint + "のダメージ");
if(monster[random_monster].hitPoint <= 0) {
System.out.println(monster[random_monster].name + "を倒した!");
int exPointBox = monster[random_monster].m_exPoint;
return exPointBox;
}
}
}
return 0;
}
int levelUp(player[] player, monster[] monster) {
/*Integer across = fight(monster, player);*/
int exPointBox= fight(player,monster);
double levelUpPoint = 1;
for(int i = 1; i <= player[0].level; i++) {
levelUpPoint = levelUpPoint * 1.5;
}
player[0].p_exPoint = (player[0].p_exPoint + exPointBox);
int count = 0;
while(player[0].p_exPoint < levelUpPoint) {
player[0].p_exPoint = (int)(player[0].p_exPoint - levelUpPoint);
player[0].level = player[0].level + 1;
count++;
}
System.out.println("レベルが" + count + "上がった");
return player[0].level;
}
}
class monster extends character{
int conditionsPoint;
int m_exPoint;
monster(String name, int conditionsPoint, int maxHitPoint, int hitPoint, int atkPoint, int speed, int m_exPoint){
super(name, maxHitPoint, hitPoint, atkPoint, speed);
this.conditionsPoint = conditionsPoint;
this.m_exPoint = m_exPoint;
}
}
class TextQuest_ver1 {
public static void main(String[] args) {
player[] player = new player[2];{
player[0] = new player("冒険者",1,5,5,2,3,0);
player[1] = new player("相棒",1,4,4,3,4,0);
}
monster[] monster = new monster[21];{
monster[0] = new monster("オオカミ",1,2,2,3,5,5);
monster[1] = new monster("スライム",1,3,3,1,3,5);
monster[2] = new monster("グール",3,5,5,3,3,10);
monster[3] = new monster("ゴブリン",5,5,5,5,4,15);
monster[4] = new monster("コボルト",5,4,4,5,5,15);
monster[5] = new monster("クマ",10,10,10,8,7,20);
monster[6] = new monster("スケルトン",10,10,10,5,4,20);
monster[7] = new monster("オーク",15,15,15,10,6,25);
monster[8] = new monster("ゴーレム",15,40,40,10,5,25);
monster[9] = new monster("ミミック",15,20,20,15,10,25);
monster[10] = new monster("ドレイク",20,30,30,15,8,30);
monster[11] = new monster("トロール",20,40,40,15,5,30);
monster[12] = new monster("オーガ",25,50,50,20,8,40);
monster[13] = new monster("ドラゴン",25,40,40,20,10,40);
monster[14] = new monster("勇者",25,60,60,22,9,40);
monster[15] = new monster("グリフォン",26,55,55,23,12,50);
monster[16] = new monster("ケルベロス",26,60,60,25,10,50);
monster[17] = new monster("リッチ",27,70,70,25,8,60);
monster[18] = new monster("デュラハン",28,80,80,28,15,70);
monster[19] = new monster("雪の女王",28,65,65,30,12,70);
monster[20] = new monster("魔王",30,100,100,35,100,80);
}
/*int adv = player[0].adventure(player, monster);
if(adv == player[0].hitPoint) {
System.out.println("明日は何をする?");
}
else {
int result = player[0].fight(player, monster);
if(result == player[0].hitPoint) {
System.out.println("Gameover");
}
else {
player[0].levelUp(player, monster);
System.out.println("明日はなにする?");
}
}*/
player[0].levelUp(player, monster);
}
}