質問

I feel like this has an extremely obvious answer, so forgive me if I'm being stupid. I'm creating a simple text-based game for elementary school kids in which the user, playing as a mage, needs to ward off mighty dragons by mixing together various spell cards (in my case, multiplying their values together to try and get the closest to the dragon's weakness number). This weakness number changes every turn, until the dragon hits an HP less than or equal too 0, at which point another dragon will spawn and the player will have to ward that one off too. The entire project takes up five classes, one for the player, another for the cards, another for the dragon, another for the game's play methods, and a fifth, the driver. The first thing that comes to mind for me is to make a method in Player.java that goes something like this:

    public int isClosestToo(int num, int max){
        int counter = 0;
        for(int i = num; i <= max; i++){
            counter++
        }
        return counter;    
    }

followed by an if statement in my game class' play method:

    if(isClosestToo(num1) > isClosestToo(num2){
        /*do something*/
    }

I realize that this would work, but I want to make sure I'm not missing something blatantly obvious and simpler that I could do. Some method in the Integer class maybe? Please keep in mind this is a very early version of the project, and I hope to implement Slick2D graphics later. Here's all of my code thus far: From Game.java:

import java.util.Scanner;

   public class Game {
Scanner scan = new Scanner(System.in);
public Game(){}

public void play(){
    Player p = new Player();
    while(p.getHP() > 0){
        Dragon d = new Dragon();
        System.out.println("You have " + p.getHP() + " HP");
        System.out.println(d);
        System.out.println(p.elementHandToString());
        System.out.println(p.attackHandToString());
        System.out.println("Choose the two cards that multiply to be closest too the weakness of the dragon for the most damage!");
        int element = scan.nextInt();
        int attack = scan.nextInt();
        int damage = d.getWeakness() - (p.getElementCard(element - 1).getNum() * p.getAttackCard(attack - 1).getNum());
        d.setHP(d.getHP() - damage);
        p.afterTurn(element, attack);
        d.newWeakness();
    }
}
}

And Player.java:

import java.util.Random;
import java.util.ArrayList;

public class Player {
Random r = new Random();
int hp;
ArrayList<Card> attackHand;
ArrayList<Card> elementHand;
ArrayList<String> elements;
ArrayList<String> attacks;


public Player(){ 
    this.hp = 100;
    this.genHands();
    attackHand = new ArrayList<Card>();
    elementHand = new ArrayList<Card>();

    //arraylist of types of elements, feel free to add more using the .add method
    elements = new ArrayList<String>();
    elements.add("Fire");
    elements.add("Ice");
    elements.add("Water");
    elements.add("Air");
    elements.add("Rock");
    elements.add("Mana");

    //arraylist of types of attacks, feel free to add more using the .add method
    attacks = new ArrayList<String>();
    attacks.add("Projectile");
    attacks.add("Self");
    attacks.add("Explosion");
    attacks.add("Repeated");
    attacks.add("Debuff");
    attacks.add("Melee");
}

public Player(int hp){ this.hp = hp; }

public int getHP(){ return hp; }

public ArrayList<Card> getAttackHand(){ return attackHand; }

public ArrayList<Card> getElementHand(){ return elementHand; }

public Card getAttackCard(int whichCard){ return attackHand.get(whichCard); }

public Card getElementCard(int whichCard){ return elementHand.get(whichCard); }

public void afterTurn(int element, int attack){ 
    attackHand.add(new Card(genRand(1, 12), attacks.get(genRand(1, 12))));
    elementHand.add(new Card(genRand(1, 12), elements.get(genRand(1,12))));
    attackHand.remove(attack - 1);
    elementHand.remove(element - 1);
}

public String elementHandToString(){ return "Card 1: " + elementHand.get(0) + "/nCard 2: " + elementHand.get(1) + "/nCard 3: " + elementHand.get(2) + "/nCard 4: " + elementHand.get(3); }

public String attackHandToString(){ return "Card 1: " + attackHand.get(0) + "/nCard 2: " + attackHand.get(1) + "/nCard 3: " + attackHand.get(2) + "/nCard 4: " + attackHand.get(3); }

//generates the player's hands
public void genHands(){
    //creates a deck of random elemental cards
    for(int x = 0; x <= 4; x++){
        elementHand.add(new Card(genRand(1, 12), elements.get(genRand(0, elements.size() - 1))));
    }

    //creates a deck of random attack cards
    for(int i = 0; i <= 4; i++){
        attackHand.add(new Card(genRand(1, 12), attacks.get(genRand(0, attacks.size() - 1))));
    }
}

//returns a random integer between min and max
//@param1 minimum number for random to be
//@param2 maximum number for random to be
public int genRand(int min, int max){ return r.nextInt(max) + min; }
}

Dragon.java:

package alchemy;

import java.util.Random;
public class Dragon {
int hp;
int weakness;
Random r = new Random();

public Dragon(){
    hp = genRand(1, 144);
    weakness = genRand(1, hp);
}

public int getHP(){
    return hp;
}

public void setHP(int newHP){
    hp = newHP;
}

public String toString(){
    return "A dragon with " + hp + "HP has appeared!" + "/nIts current weakness is " + weakness + "/nPlay two cards that multiply to a number close to its weakness for damage!";
}

public int getWeakness(){
    return weakness;
}

public void newWeakness(){
    weakness = genRand(1, hp);
}

public int genRand(int min, int max){
    return r.nextInt(max) + min;
}
}

And lastly, Card.java:

import java.util.Random;

public class Card{
int num;
Random r = new Random();
String element;

public Card(){
    num = 0;
    element = "n/a";
}

public Card(int n, String e){
    num = n;
    element = e;
}

public int getNum(){
    return num;
}

public String getElement(){
    return element;
}

public String toString(){
    return "This is a " + element + "card with a value of " + num + ".";
}
}

Primarily, the issue I'm running into is the play() loop of the Game class, where I need to calculate the damage that the player does. Obviously, (before I program in elemental logic), I want that damage to actually do damage instead of ending up as a negative number and making the dragon gain HP. Any and all help would be greatly appreciated. Thank you! -Nick

役に立ちましたか?

解決

In java, when a method starts with "is", it returns boolean. But your project probably maintained by you yourself, it can be whatever you want. Your isClosestToo method, takes in 2 arguments, which is num and max, both integer, but when you call it, you only takes in one argument.

    public int isClosestToo(int num, int max){
        int counter = 0;
        for(int i = num; i <= max; i++){
            counter++
        }
        return counter;    
    }


    if(isClosestToo(num1) > isClosestToo(num2){
        /*do something*/
    }

the method name itself is confusing, you can do public int ClosestTo(int[] num,int max) to take and a list of number and return the closest number or do DiffMax(int num) to take in a number and subtract it with the max, in which this case your max must be accessible in the method scope.

public int ClosestTo(int[] num,int max)
{
    1.declare a variable to store the closest num
    2.for every element in num, if Math.abs(max-num) < closestNum, store num to closestNum
    3.return closestNum
}

or

public int DiffMax(int num)
{
    1. return Math.abs(max-num) < closestNum
}

then you can do

 if(DiffMax(num1) > DiffMax(num2){
        /*do something*/
    }

他のヒント

this is an option

if((max-int1)>(max-int2) && max-int2>=0){
   //do something with int2
}
else if((max-int1)<(max-int2) && max-int1>=0){
   //do something with int1
}
else if(max-int1>=0){
   //do something with int1
}
else if(max-int2>=0){
   //do something with int2
}
else{
   //do something if conditions aren't met
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top