English 中文(简体)
Java 中的基于文本的卡游戏, 无法添加到矩阵列表吗?
原标题:Text based card game in Java, can t add to arraylists?

好吧,所以我用 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();
      }
   }
}
问题回答

它之所以没有在 b>d 情况下添加牌, 原因是 < enger @ em> 您不告诉它 添加到此情况下的数组列表中。 您有两个“ 大小对齐” 命令, 还有一个“ 取消” 命令, 但是没有“ 添加” 命令 。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签