0

i have been trying to a cards game with java but some how it keeps falling. this game an odd number of players and each of them has a queue of cards the first player that is card queue is empty wins. every time i compile it keep giving me java.lang.NullPointerException error. thank you for helping.

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;
}

}
kipatbarzel1
  • 35
  • 1
  • 4

1 Answers1

0

If you check out the Queue API, you will find that generally no queue will allow insertion of a null element.

Mapsy
  • 4,192
  • 1
  • 37
  • 43