好吧,所以我用 java 进行牌游戏战争, 出于某种原因, 当 b>d
和 b<d
它不会从数组中添加和删除元素。 我知道每张牌上都有52张牌, 我会解决这个问题 。
Also, any tips on a simpler way to do things when b==d? (tie between cards) Thanks in advance, this has been bugging me
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();
}
}
}