带游戏的java.lang.NullPointerException

时间:2014-12-20 10:07:02

标签: java oop

我一直在尝试用Java进行纸牌游戏,但有些人一直在尝试下降。这个游戏中有奇数个玩家,他们每个人都有一张卡队列,第一个玩家是卡队列是空胜。每次我编译它继续给我java.lang.NullPointerException错误。谢谢你的帮助。

package cardgame;
import unit4.collectionsLib.Queue;//importing the queue

public class Main {

public static boolean checkIsEmpty(Queue<Player> p){
    boolean is=false;
    p.insert(null);
    while(p.head()!=null){
        if(p.head().getC().isEmpty()){
            is=false;
            p.insert(p.remove());
        }
    }
    p.remove();
    return is;
}
public static int whoWin(Queue<Player> p){
 p.insert(null);
 while(p.head()!=null){
     if(p.head().getC().isEmpty()){
         return p.head().getIndex();
     }
 }
 return 0;
}

public static void main(String[] args) {
  Queue<Player> players = new Queue(); //queue of players 
  for(int i=0;i<5;i++){
  players.insert(new Player());
  }

  boolean isOver=false;

  while(!isOver)
      //the game is over when one player finished his cards
  {
      Player p = players.remove();
      if(p.getC().head().greater(players.head().getC().head())==1){
          p.getC().remove();
          players.insert(p);
          players.insert(players.remove());

      }
      else if(p.getC().head().greater(players.head().getC().head())==2){
        players.head().getC().remove();
        players.insert(p);
        players.insert(players.remove());  
      }
      else{
        players.head().getC().remove();
        p.getC().remove();
        players.insert(p);
        players.insert(players.remove()); 
      }
      if(checkIsEmpty(players)){
        isOver=true;
          System.out.println("the winner in this game is player number :"+whoWin(players));
      }
     }
   }

  }    




package cardgame;
import unit4.collectionsLib.Queue;


public class Player {

static int autonum=1;
private Queue<Card> c;
private int index;

public Player()
{
for(int i=0;i<7;i++){
c.insert(new Card());}
this.index=autonum;
autonum++;

}

/**
 * @return the c
 */
public Queue<Card> getC() {
    return c;
}

/**
 * @param c the c to set
 */
public void setC(Queue<Card> c) {
    this.c = c;
}

public int getIndex(){
    return this.index;
}

}




package cardgame;
import java.util.Random;

public class Card {
private int num ;
 private int shape;

public Card()
{
Random r = new Random();
this.num = r.nextInt(9)+1;
this.shape = r.nextInt(4)+1;
}

public int greater(Card c){
 if(c.getNum()<this.num)
     return 1;
 else {
     if(c.getNum()>this.num){
         return 2;

     }
     else{
         if(c.getShape()>c.shape){
             return 2;
         }
         else{
             if(c.getShape()<c.shape){
                 return 1;
             }
             else{
                 return 0;
             }       
         }
        } 
       }
 }

/**
 * @return the num
 */
public int getNum() {
    return num;
}

/**
 * @param num the num to set
 */
public void setNum(int num) {
    this.num = num;
}

/**
 * @return the shape
 */
public int getShape() {
    return shape;
}

/**
 * @param shape the shape to set
 */
public void setShape(int shape) {
    this.shape = shape;
}

}

1 个答案:

答案 0 :(得分:0)

如果您查看Queue API,您会发现通常没有队列允许插入null元素。