求大牛指点0.0.

来源:7-1 简易扑克牌游戏

moummm

2017-03-05 20:37

package com.game;
import java.util.ArrayList;
import java.util.Set;

import com.javapractice.Book;
    //玩家类  
public class Player {

    private int id;
    private String name;
    
    //手牌
    public ArrayList<Squeezer> hands;
    
    public Player(){
        this.hands =  new ArrayList<Squeezer>();
        }
    
    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.name = name;
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }    
}


package com.game;
//牌类
public class Squeezer {

    private int id;
    private String name;
    
    public Squeezer(int id, String name){
        this.id = id;
        this.name = name;
    }
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}


package com.game;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;

import com.javapractice.Book;

public class BeginPlayingGame {
    public static List<Squeezer> squeezers;
    public List<Squeezer> list;
    static Scanner console = new Scanner(System.in);
    Player player1 = new Player();
    Player player2 = new Player();
    
    public BeginPlayingGame(){
        this.squeezers = new ArrayList<Squeezer>();
    }

    //创建扑克牌
    public void squeezerList(){
        System.out.println("========开始创建扑克牌...========");
        Squeezer[] squeezer = {
        new Squeezer(1,"黑桃2"),new Squeezer(2,"红桃2"),new Squeezer(3,"梅花2"),new Squeezer(4,"方块2"),
        new Squeezer(5,"黑桃3"),new Squeezer(6,"红桃3"),new Squeezer(7,"梅花3"),new Squeezer(8,"方块3"),
        new Squeezer(9,"黑桃4"),new Squeezer(10,"红桃4"),new Squeezer(11,"梅花4"),new Squeezer(12,"方块4"),
        new Squeezer(13,"黑桃5"),new Squeezer(14,"红桃5"),new Squeezer(15,"梅花5"),new Squeezer(16,"方块5"),
        new Squeezer(17,"黑桃6"),new Squeezer(18,"红桃6"),new Squeezer(19,"梅花6"),new Squeezer(20,"方块6"),
        new Squeezer(21,"黑桃7"),new Squeezer(22,"红桃7"),new Squeezer(23,"梅花7"),new Squeezer(24,"方块7"),
        new Squeezer(25,"黑桃8"),new Squeezer(26,"红桃8"),new Squeezer(27,"梅花8"),new Squeezer(28,"方块8"),
        new Squeezer(29,"黑桃9"),new Squeezer(30,"红桃9"),new Squeezer(31,"梅花9"),new Squeezer(32,"方块9"),
        new Squeezer(33,"黑桃10"),new Squeezer(34,"红桃10"),new Squeezer(35,"梅花10"),new Squeezer(36,"方块10"),
        new Squeezer(37,"黑桃J"),new Squeezer(38,"红桃J"),new Squeezer(39,"梅花J"),new Squeezer(40,"方块J"),
        new Squeezer(41,"黑桃Q"),new Squeezer(42,"红桃Q"),new Squeezer(43,"梅花Q"),new Squeezer(44,"方块Q"),
        new Squeezer(45,"黑桃K"),new Squeezer(46,"红桃K"),new Squeezer(47,"梅花K"),new Squeezer(48,"方块K"),
        new Squeezer(49,"黑桃A"),new Squeezer(50,"红桃A"),new Squeezer(51,"梅花A"),new Squeezer(52,"方块A"),
        };
        squeezers.addAll(0,Arrays.asList(squeezer));
        System.out.println("========成功添加扑克牌========");
        System.out.print("为:[");
        for (Squeezer squeezer2 : squeezer) {
            System.out.print(squeezer2.getName()+" ");
        }
//        String s="    Hello World      ".trim();   "Hello World"
        System.out.print("]");
        System.out.println();
    }
    
    public void deal(){
        System.out.println("---------开始发牌--------");
        player1.hands.add(squeezers.get(0));
        player1.hands.add(squeezers.get(1));
        player2.hands.add(squeezers.get(2));
        player2.hands.add(squeezers.get(3));
        System.out.println("玩家"+player1.getName()+"的手牌为:"+squeezers.get(0).getName()+"和:"+squeezers.get(1).getName());
        System.out.println("玩家"+player2.getName()+"的手牌为:"+squeezers.get(2).getName()+"和:"+squeezers.get(3).getName());
    }
    
    public void shuffle(){
        //打乱扑克牌顺序
        Collections.shuffle(squeezers);
    }
    
    //比较大小
    public void fight(){
        int e =0;
        System.out.println("比赛开始。。。");
        int o = compare(player1.hands.get(0),player1.hands.get(1));
        int w = compare(player2.hands.get(0),player2.hands.get(1));
        if(o == 1&&w ==1){
            e = compare(player1.hands.get(0),player2.hands.get(1));
        }else if(o == 1&&w ==-1){
            e = compare(player1.hands.get(0),player2.hands.get(1));
        }else if(o == -1&&w == 1){
            e = compare(player1.hands.get(1),player2.hands.get(0));
        }else if(o == -1&&w ==-1){
            e = compare(player1.hands.get(1),player2.hands.get(1));
        }
        if(e>0)
            System.out.println("玩家"+player1.getName()+"获胜!");
        else
            System.out.println("玩家"+player2.getName()+"获胜!");
    }
    
    //创建玩家
    public void createPlayer(){
        //获取输入是整数的ID
        while(true){
            try{        
        System.out.println("请输入第一名玩家的ID:");
        String player1Id = console.next();
        player1.setId(Integer.parseInt(player1Id));
        break;
        }catch(NumberFormatException e){
            e.printStackTrace();
            System.out.println("请输入整数的ID:");
        }
    }
        System.out.println("请输入第一名玩家的姓名:");
        String player1name = console.next();
        player1.setName(player1name);
        while(true){
            try{        
        System.out.println("请输入第一名玩家的ID:");
        String player2Id = console.next();
        player2.setId(Integer.parseInt(player2Id));
        break;
        }catch(NumberFormatException e){
            e.printStackTrace();
            System.out.println("请输入整数的ID:");
        }
    }
        System.out.println("请输入第二名玩家的姓名:");
        String player2name = console.next();
        player2.setName(player2name);
        System.out.println("========欢迎"+player1.getName()+"和"+player2.getName()+"进入游戏========");
        }
    
    //比较两张牌的大小
    public int compare(Squeezer e1,Squeezer e2){
        int p1=0;
        int p2=0;
        for(int i=0;i<52;i++){
            if(e1.equals(squeezers.get(i)))
             p1 = squeezers.get(i).getId();
            if(e2.equals(squeezers.get(i)))
             p2 = squeezers.get(i+1).getId();
        }
        if(p1>p2)
            return 1;
        else
            return -1;
        }
    
    public static void main(String[] args) {
        BeginPlayingGame bpg =new BeginPlayingGame();
        bpg.createPlayer();
        bpg.squeezerList();
        bpg.shuffle();
        bpg.deal();
        bpg.fight();
        }
}


请问一下我的错误在哪??为什么会跳出空指针错误?

写回答 关注

2回答

  • moummm
    2017-03-06 00:33:28

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
        at java.util.ArrayList.RangeCheck(ArrayList.java:547)
        at java.util.ArrayList.get(ArrayList.java:322)
        at com.game.BeginPlayingGame.fight(BeginPlayingGame.java:72)
        at com.game.BeginPlayingGame.main(BeginPlayingGame.java:144)

  • slowlog
    2017-03-05 21:32:30

    把错误贴出来

Java入门第三季

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

409792 学习 · 4340 问题

查看课程

相似问题