我的纸牌游戏在学校项目中出现多个错误

时间:2019-07-15 19:23:47

标签: java arraylist linked-list

我正在Java 2类中创建战争游戏,但是每次运行代码时,我总是收到链表错误,并且找不到解决方案。

我尝试删除pop()函数并添加removeFirst()方法,但是都返回错误。

package ca.sheridancollege.project;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
/**
 * @author Rich Smith at ZenOfProgramming.com
 */
public class StartTheWar
{
   public static void main (String[] args)
   {
      System.out.println("Let the War begin!!!\n\nWe will now deal the cards. Best of luck to both players\n ");
      List<Card> cardDeck = new ArrayList<>();

      for (int x = 0; x < 4; x++) {          //for loop using the 4 suits
         for (int y = 2; y < 15; y++) {     // for loop for 13 for cards 2-10 and faced cards1
            cardDeck.add(new Card(x, y)); //create new card then use add method to combine to array list
         } //end cards for loop
      }//end suit for

      Collections.shuffle(cardDeck, new Random()); //deck shuffler

      //using our newly learnt knowledge on Linked lists to create 2 - one for each player. player 1/player2
      LinkedList<Card> deck1 = new LinkedList<>();
      LinkedList<Card> deck2 = new LinkedList<>();


      //found interesting Java operation called List which can be broken down into sublists to store information. Just add the total # of cards needed in each sublist
      deck1.addAll(cardDeck.subList(0, 26));              //26 cards for p1       

      deck2.addAll(cardDeck.subList(26, cardDeck.size()));//26 cards for p2

      //Array for player objects to be held
      ArrayList<Player> p3 = new ArrayList<>();
      Player p1 = new Player(deck1);
      Player p2 = new Player(deck2);
      p3.add(p1);
      p3.add(p2);

      War game = new War(p3);
      game.play();

   }
}


/**
 * SYST 17796 Project Winter 2019 Base code.
 * Students can modify and extend to implement their game.
 * Add your name as a modifier and the date!
 */
package ca.sheridancollege.project;
public class Card
{
   private int rank; //initialize the rank (2,3,4...King, Ace)
   private int suit; //initialize the suit (spades, hearts...)

   public Card (int suit, int rank)
   {
      this.rank = rank;
      this.suit = suit;
   }//end construcor
   //getter method

   public int getCard ()
   {
      return rank;
   }//end getCard

   //setter method
   public void setCard (int rank)
   {
      this.rank = rank;
   }//end setCard

   @Override
   public String toString ()
   {
      //combine rank and suit together into a single string(ex: Ace of Diamonds)
      //suing StringBuilder for modifiability later on
      StringBuilder displayCard = new StringBuilder();
      //personal choice to use switch
      switch (rank) {
         //since rank is int type, now match int 11 to String jack...14 to Ace
         case 11:
            displayCard.append("Jack");
            break;
         case 12:
            displayCard.append("Queen");
            break;
         case 13:
            displayCard.append("King");
            break;
         case 14:
            displayCard.append("Ace");
            break;
         default:
            displayCard.append(rank); //number from 2 to 10 does not need to modify
            break;
      }//end rank switch
      displayCard.append(" of "); //setting the format of the output

      switch (suit) {
         case 0:
            displayCard.append("Spades");
            break;
         case 1:
            displayCard.append("Hearts");
            break;
         case 2:
            displayCard.append("Clubs");
            break;
         case 3:
            displayCard.append("Diamonds");
            break;
         default: //anything else, do nothing
            break;
      }//end suit switch
      //return the result of an entire cmombined string
      return displayCard.toString();
   }//end toString
}


package ca.sheridancollege.project;
/**
 * @author Rich Smith at ZenOfProgramming.com
 */
import java.util.ArrayList;
import java.util.List;
public class War extends Game
{
   public War (ArrayList<Player> players)
   {
      super(players);

   }

   public void play ()
   {

      while (true) {


         //use pop to remove card from top of Linked List- thank god for Rich teaching us all this lol
         //this is where error is
         Card p1Card = this.getPlayers().get(0).getDeck().pop();  //each player place one card face up
         Card p2Card = this.getPlayers().get(1).getDeck().pop();


         //display the face up card
         System.out.println("Player 1 plays: " + p1Card.toString());
         System.out.println("Player 2 plays: " + p2Card.toString());

         //rank comparation between two cards
         //
         if (p1Card.getCard() > p2Card.getCard()) {//if player 1 win 
            this.getPlayers().get(0).getDeck().pop();  //higher rank wins both cards and 
            this.getPlayers().get(0).getDeck().pop();  //places them at the bottom of his deck.
            System.out.println("Player 1 wins the round and Player 2's cards: " + p2Card.toString());
            System.out.println();
         }//end if

         else if (p1Card.getCard() < p2Card.getCard()) {//if player 2 win 
            this.getPlayers().get(1).getDeck().pop();
            this.getPlayers().get(1).getDeck().pop();
            System.out.println("Player 2 wins the round and Player 1's card : " + p1Card.toString());
            System.out.println();
         }//end else if
         // test

         else { //war happens while both players draw the same car
            System.out.println("War!! Each player places 3 cards face down and flips the 4th! Winner take all!\n");


            //creating new temp array for tie
            List<Card> war1 = new ArrayList<>();
            List<Card> war2 = new ArrayList<>();

            //checking do players have enough (4)cards to play war / if not game is over
            for (int x = 0; x < 4; x++) {
               //either one player runs out of card is game over
               if (this.getPlayers().get(0).getDeck().isEmpty() || getPlayers().get(1).getDeck().isEmpty()) {
                  break;
               }//end if

               System.out.println("War card for Player 1 is xx\nWar card for Player 2 is xx\n");

               war1.add(this.getPlayers().get(0).getDeck().pop());  //place additional card for war
               war2.add(this.getPlayers().get(1).getDeck().pop());
            }//end for

            //only compare result when both players have enough cards for war
            if (war1.size() == 4 && war2.size() == 4) {
               //display the war cards from each player
               System.out.println("War card for Player 1 is " + war1.get(3).toString());
               System.out.println("War card for Player 2 is " + war2.get(3).toString());
               System.out.println();

               //if player 1 wins the war round
               if (war1.get(3).getCard() > war2.get(3).getCard()) {
                  this.getPlayers().get(0).getDeck().addAll(war1); //player1 get all 10 cards
                  this.getPlayers().get(0).getDeck().addAll(war2);
                  System.out.println("Player 1 wins the battle! But as they say, winning the battle does not guarantee winning the War!\n");
               }//end if
               //otherwise player 2 wins the war round
               else {
                  getPlayers().get(1).getDeck().addAll(war1); //player2 get all 10 cards
                  getPlayers().get(1).getDeck().addAll(war2);
                  System.out.println("Player 2 wins the battle! But as they say, winning the battle does not guarantee winning the War!\n");
               }//end else                      
            }//end if

         }//end war round else
      }

   }

}


/**
    * SYST 17796 Project Winter 2019 Base code.
    * Students can modify and extend to implement their game.
    * Add your name as a modifier and the date!
    */
   package ca.sheridancollege.project;
   import java.util.LinkedList;

   public class Player
   {
      private LinkedList<Card> deck;

      public Player (LinkedList<Card> deck)
      {
         this.deck = deck;
      }

      public LinkedList<Card> getDeck ()
      {
         return this.deck;
      }
   }

错误:

线程“ main”中的异常java.util.NoSuchElementException

  

在java.util.LinkedList.removeFirst(LinkedList.java:270)
  在java.util.LinkedList.pop(LinkedList.java:801)
  在ca.sheridancollege.project.War.play(War.java:23)
  在ca.sheridancollege.project.StartTheWar.main(StartTheWar.java:43)

0 个答案:

没有答案