问答详情
源自:7-1 简易扑克牌游戏

求大神帮我看看错在哪,为什么会越界呢?

错误信息:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13

at com.thomas.GameStart.initialize(GameStart.java:19)

at com.thomas.GameStart.main(GameStart.java:141)


源代码

package com.thomas;


import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import java.util.Scanner;


public class GameStart {

String[] point=new String[]{"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

String[] color=new String[]{"方片","梅花","红桃","黑桃"};

Scanner input=new Scanner(System.in);

List<Poker> pokerList=new ArrayList<Poker>();

List<Player> playerList=new ArrayList<Player>();

public void initialize(){

for(int i=0;i<4;i++){

for(int j=0;i<13;j++){

pokerList.add(new Poker(color[i],point[j]));

}

}

System.out.println("-------------------创建扑克牌-----------------------");

int i=0;

for (Poker poker : pokerList) {

System.out.print(poker.color+poker.points+" ");

i++;

if(i%13==0){

System.out.println();

}

}

}

public void shuffle(){

System.out.println("-------------------开始洗牌-----------------------");

Collections.shuffle(pokerList);//打乱顺序

System.out.println("-------------------洗牌完成-----------------------");

}

public void playSet(){

System.out.println("-------------------创建玩家-----------------------");

for(int i=1;i<=2;i++){

System.out.println("请输入玩家"+i+"的id:");

int id=0;

try{

id=scanf();

}catch(Exception e){

System.out.println(e.getMessage());

i--;

continue;

}

System.out.println("请输入玩家"+i+"的姓名:");

String name=input.next();

playerList.add(new Player(id,name));

}

input.close();//Scanner对象开启了之后要记得关闭

System.out.println("-------------------Game Start!-----------------------\n红方玩家+\t蓝方玩家");

for (Player player : playerList) {

System.out.println(player.name+"\t");

}

}

public int scanf()throws Exception{

try{

int in=input.nextInt();

return in;

}catch(Exception e){

throw new Exception("输入异常,请输入整数类型的ID!"); 

}

}

public void deal(){

System.out.println("-------------------开始发牌-----------------------");

System.out.println(playerList.get(0).name+"拿牌");

playerList.get(0).handCards.add(pokerList.get(0));

System.out.println(playerList.get(1).name+"拿牌");

playerList.get(0).handCards.add(pokerList.get(1));

System.out.println(playerList.get(0).name+"拿牌");

playerList.get(0).handCards.add(pokerList.get(2));

System.out.println(playerList.get(1).name+"拿牌");

playerList.get(0).handCards.add(pokerList.get(3));

System.out.println("-------------------发牌结束-----------------------");

}

public void sort(){

Collections.sort(playerList.get(0).handCards, new CompareToPoker());

Collections.sort(playerList.get(1).handCards, new CompareToPoker());

System.out.println(playerList.get(0).name+"最大手牌:"+playerList.get(0).handCards.get(0).color+playerList.get(0).handCards.get(0).points);

System.out.println(playerList.get(0).name+"最大手牌:"+playerList.get(1).handCards.get(0).color+playerList.get(1).handCards.get(0).points);

}

public void compareToPoint(){

System.out.println("-------------------获胜方-----------------------");

List<Poker> maxPoker=new ArrayList<Poker>();

List<Poker> minPoker=new ArrayList<Poker>();

maxPoker.add(playerList.get(0).handCards.get(0));

maxPoker.add(playerList.get(1).handCards.get(0));

minPoker.add(playerList.get(0).handCards.get(1));

minPoker.add(playerList.get(1).handCards.get(1));

Collections.sort(maxPoker, new CompareToPoker());

Collections.sort(minPoker,new CompareToPoker());

if(maxPoker.get(0).points.equals(maxPoker.get(1).points)){

if(minPoker.get(0).points.equals(minPoker.get(1).points)){

if(maxPoker.get(0).equals(playerList.get(0).handCards.get(0))){ 

System.out.println("红方获胜\tWinner:"+playerList.get(0).name); 

}else{ 

System.out.println("蓝方获胜\tWinner:"+playerList.get(1).name); 

}else{ 

if(minPoker.get(0).equals(playerList.get(0).handCards.get(1))){

System.out.println("红方获胜\tWinner:"+playerList.get(0).name);

}else{

System.out.println("蓝方获胜\tWinner:"+playerList.get(1).name);

}

}else if(maxPoker.get(0).equals(playerList.get(0).handCards.get(0))){

System.out.println("红方获胜\tWinner:"+playerList.get(0).name);

}else{ 

System.out.println("蓝方获胜\tWinner:"+playerList.get(1).name);

}

}

public void print(){ 

System.out.println("----------玩家各自手牌-------------"); 

for(Player player : playerList){ 

System.out.print(player.name+":"); 

for(Poker poker : player.handCards){ 

System.out.print("["+poker.color+poker.points+"]"); 

System.out.println(""); 

}

}


public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println("----------欢迎进入纸牌大战-------------");

GameStart gs=new GameStart();

gs.initialize();

gs.shuffle();

gs.playSet();

gs.deal();

gs.sort();

gs.compareToPoint();

gs.print();

System.out.println("----------游戏结束-------------");

}


}


提问者:thomasstar 2017-02-11 22:12

个回答

  • ziom
    2017-02-12 08:46:49
    已采纳

    initialize()的第二重循环写错了

    public void initialize(){		
        for(int i=0;i<4;i++){
            // for(int j=0;i<13;j++){        
            for(int j=0;j<13;j++){
                pokerList.add(new Poker(color[i],point[j]));
            }
        }
        ...
    }

    求采纳!


  • thomasstar
    2017-02-18 20:35:06

    对对对,一快就敲错了,谢谢