-2

好的,所以我在 java 中进行纸牌游戏大战,出于某种原因b>db<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();
      }
   }
}
4

1 回答 1

0

在这种情况下它不添加卡片的原因b>d您没有告诉它在这种情况下添加到数组列表中。您有两个“调整大小”命令和一个“删除”命令,但没有“添加”命令。

于 2012-05-24T21:13:39.370 回答