2014-03-13 10 views
0

我覺得這有一個非常明顯的答案,所以請原諒我,如果我是愚蠢的。 我正在爲小學生創建一個簡單的基於文本的遊戲,其中用戶作爲法師扮演着需要通過混合各種拼寫卡(在我的情況下,將他們的值相乘以嘗試並獲得最接近龍的弱點數字)。這個弱點數字每回合都會改變,直到龍的HP值小於或等於0,此時另一隻龍會產卵,玩家也必須避開這一點。整個項目需要五個階段,一個爲玩家,另一個爲卡牌,另一個爲龍,另一個爲遊戲的玩法,第五個爲司機。 我想到的,我的第一件事就是讓在Player.java一個方法是這樣的:在我的遊戲類我想測試兩個整數中哪一個最接近Java中的第三個int

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

隨後if語句玩兒法:

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

我意識到這是可行的,但我想確保我不會錯過明顯而簡單的事情,我可以做到。 Integer類中的某些方法可能是? 請記住,這是該項目的一個非常早期的版本,我希望稍後可以實施Slick2D圖形。 這裏是我的所有代碼迄今: 從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(); 
    } 
} 
} 

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

最後,卡。 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 + "."; 
} 
} 

首先,我遇到的問題是Game類的play()循環,我需要計算玩家所造成的傷害。顯然,(在我用元素邏輯編程之前),我希望這個傷害實際上會造成傷害,而不是以負數結尾,並使龍獲得HP。任何和所有的幫助將不勝感激。謝謝! -Nick

+0

你可以採取馬克斯 - INT1和馬克斯 - INT2和取最接近0將是你最親密的INT – mig

+1

'如果(Math.abs(max-num1) exception1

+0

return Math.abs(max-num1)

回答

0

在java中,當一個方法開始於 「是」,則返回布爾值。但是你的項目可能由你自己維護,它可以是任何你想要的。 你的isClosestToo方法需要2個參數,它們是num和max,都是整數,但是當你調用它時,你只需要一個參數。

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*/ 
    } 

方法名稱本身是混亂的,你可以做公衆詮釋ClosestTo(INT [] NUM,詮釋最大值)採取和數量的列表,並返回最接近的數字或做DiffMax(INT NUM)取在一個數字中,並用最大值減去它,在這種情況下,您的最大值必須可以在方法範圍內訪問。

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 
} 

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

那麼你可以做

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

這是一個選項

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 
} 
相關問題