好吧,所以我正在用java进行纸牌游戏战争,出于某种原因b>d
和b<d
它没有添加和删除数组中的元素。我知道每张牌都有52张牌,我会解决的。
另外,在b == d时,有什么提示更简单的方法吗? (卡之间的关系) 在此先感谢,这一直困扰着我
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class war {
public static void printYourCard(ArrayList a, int b) {
if (a.get(b).equals(11))
System.out.println("Jack");
else if (a.get(b).equals(12))
System.out.println("Queen");
else if (a.get(b).equals(13))
System.out.println("King");
else if (a.get(b).equals(14))
System.out.println("Ace");
else
System.out.println(a.get(b));
}
public static void printOppCard(ArrayList a, int b) {
if (a.get(b).equals(11))
System.out.println("Jack");
else if (a.get(b).equals(12))
System.out.println("Queen");
else if (a.get(b).equals(13))
System.out.println("King");
else if (a.get(b).equals(14))
System.out.println("Ace");
else
System.out.println(a.get(b));
}
public static void main(String args[]) {
Scanner cards = null;
try {
// Create a scanner to read the file, file name is parameter
cards = new Scanner(new File("C:\\Users\\Phil\\Desktop\\cards.txt"));
} catch (FileNotFoundException e) {
System.out.println("File not found!");
// Stop program if no file found
System.exit(0);
}
Scanner cards2 = null;
try {
// Create a scanner to read the file, file name is parameter
cards = new Scanner(new File("C:\\Users\\Phil\\Desktop\\cards2.txt"));
} catch (FileNotFoundException e) {
System.out.println("File not found!");
// Stop program if no file found
System.exit(0);
}
ArrayList oppDeck = new ArrayList();
ArrayList yourDeck = new ArrayList();
int x = 0;
while (cards.hasNext()) {
/*
* oppDeck[x] = cards.nextInt(); yourDeck[x] = oppDeck[x]; x++;
*/
oppDeck.add(cards.nextInt());
yourDeck = oppDeck;
}
/*
* for(int y = 0; y<=51; y++) { System.out.print(yourDeck.get(y)); }
*/
Random randomGenerator = new Random();
Random randomGenerator2 = new Random();
int randOpp = 0;
int randYour = 0;
Scanner askQ = new Scanner(System.in);
String answer = "";
while (oppDeck.size() != 0 || yourDeck.size() != 0) {
System.out.print("Press 'd' to draw a card: ");
answer = askQ.nextLine();
randYour = randomGenerator.nextInt(yourDeck.size());
System.out.print("You played the card: ");
printYourCard(yourDeck, randYour);
randOpp = randomGenerator.nextInt(oppDeck.size());
System.out.print("Your opponent played the card: ");
printOppCard(oppDeck, randOpp);
System.out.println("");
Integer a = new Integer(yourDeck.get(randYour).toString());
int b = a.intValue();
Integer c = new Integer(oppDeck.get(randOpp).toString());
int d = c.intValue();
if (b > d) {
oppDeck.remove(oppDeck.get(randOpp));
oppDeck.trimToSize();
yourDeck.trimToSize();
} else if (b < d) {
oppDeck.add(yourDeck.get(randYour));
yourDeck.remove(yourDeck.get(randYour));
yourDeck.trimToSize();
} else if (b == d) {
ArrayList keep = new ArrayList();
keep.add(yourDeck.get(randYour));
yourDeck.remove(yourDeck.get(randYour));
keep.add(oppDeck.get(randOpp));
oppDeck.remove(oppDeck.get(randOpp));
// next drawn important card Your
Random nextCard = new Random();
int nCard = nextCard.nextInt(yourDeck.size());
Integer e = new Integer(yourDeck.get(nCard).toString());
int f = e.intValue();
yourDeck.remove(yourDeck.get(nCard));
// draw 3 and remove those
Random warYou1 = new Random();
int warY1 = warYou1.nextInt(yourDeck.size());
keep.add(yourDeck.get(warY1));
yourDeck.remove(yourDeck.get(warY1));
Random warYou2 = new Random();
int warY2 = warYou1.nextInt(yourDeck.size());
keep.add(yourDeck.get(warY2));
yourDeck.remove(yourDeck.get(warY2));
Random warYou3 = new Random();
int warY3 = warYou1.nextInt(yourDeck.size());
keep.add(yourDeck.get(warY3));
yourDeck.remove(yourDeck.get(warY3));
// next drawn important card Opp
Random oppNextCard = new Random();
int oCard = oppNextCard.nextInt(oppDeck.size());
Integer g = new Integer(oppDeck.get(oCard).toString());
int h = g.intValue();
oppDeck.remove(oppDeck.get(oCard));
// draw 3 and remove those
Random warOpp1 = new Random();
int warO1 = warYou1.nextInt(yourDeck.size());
keep.add(oppDeck.get(warO1));
oppDeck.remove(oppDeck.get(warO1));
Random warOpp2 = new Random();
int warO2 = warYou1.nextInt(yourDeck.size());
keep.add(oppDeck.get(warO2));
oppDeck.remove(oppDeck.get(warO2));
Random warOpp3 = new Random();
int warO3 = warYou1.nextInt(yourDeck.size());
keep.add(oppDeck.get(warO3));
oppDeck.remove(oppDeck.get(warO3));
if (f > h) {
for (int i = 0; i < keep.size(); i++) {
yourDeck.add(keep.get(i));
}
} else if (f < h) {
for (int i = 0; i < keep.size(); i++) {
oppDeck.add(keep.get(i));
}
}
}// exit tie breaker
System.out.println("--Your cards left: " + yourDeck.size());
System.out.println("--Your opponents cards left: " + oppDeck.size());
System.out.println();
}
}
}
答案 0 :(得分:0)
在b>d
案例中没有添加卡片的原因是 你没有告诉 在这种情况下添加到数组列表。你有两个“修剪大小”命令和一个“删除”命令,但没有“添加”命令。