为了账号安全,请及时绑定邮箱和手机立即绑定

自己摸索的纸牌游戏代码,感觉还有很多知识不懂,任重道远啊!

package com.poker;

/*

 * 定义一个纸牌的实体类

 */

public class Poker implements Comparable<Poker>{

String color ;//定义花色

String card ;//定义牌

Double num ;//定义用来比较的数字

public Poker() {

}

public Poker(String color,String card,double num) {//含参构造器

this.color = color ;

this.card = card ;

this.num = num ;

}

@Override

public String toString() {//重写下,打印输出的时候屏蔽掉数字

return color+card;

}

@Override

public int compareTo(Poker o) {//定义比较规则

// TODO Auto-generated method stub

return this.num.compareTo(o.num);

}

}


package com.poker;


import java.util.ArrayList;

import java.util.List;

/*

 * 定义一个玩家实体类

 */

public class Player {

String name ;

Integer id ;

List<Poker> playingcards;

public Player() {

}

public Player(String name , Integer id ,List<Poker> playingcards) {//含参构造器

playingcards = new ArrayList<Poker>();//终于知道在构造器中初始化对象的用处了,写在上面某些情况下会出现空指针异常

this.name = name ;

this.id = id ;

this.playingcards = playingcards ;

}

@Override

public String toString() {//重写,方便打印

return "昵称:"+name+"  id:"+id;

}

}


package com.poker;


import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collections;

import java.util.InputMismatchException;

import java.util.List;

import java.util.Scanner;


public class Game {

List<Poker> poker = new ArrayList<Poker>();//创建扑克牌集合的对象

Player pr1 ; //玩家1 对象

Player pr2 ; //玩家2 对象

public static void main(String[] args) {

// TODO Auto-generated method stub

Game game = new Game();

game.pokerAdd();

game.playerAdd();

game.licensing();

}

public void pokerAdd() {

//创建扑克牌数组

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

//建立卡牌数组

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

//建立花色数组

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

//建立比较值数组

int [] num = {2,3,4,5,6,7,8,9,10,11,12,13,14};

//建立花色比较值数组

double [] colorNum = {0.4,0.3,0.2,0.1};

for(int i = 0 ; i < card.length ; i++) {

for(int j = 0 ; j< color.length; j++) {

Poker pk = new Poker(color[j],card[i],num[i]+colorNum[j]);

poker.add(pk);

}

}

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

System.out.println("为:"+poker);

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

Collections.shuffle(poker);//把数组元素顺序打乱

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

// System.out.println("为:"+poker);//测试用

}

public void playerAdd() {

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

System.out.println("-------请输入第"+(i+1)+"位玩家的ID和昵称------------");

System.out.println("输入ID");

try {

Scanner sca = new Scanner(System.in);//初始化输入对象,这步必须写在这里,否则会进入死循环

Integer id = sca.nextInt();

System.out.println("请输入昵称");

String name = sca.next();

if(i==0) {

pr1 = new Player(name,id,null);

System.out.println("成功添加玩家"+pr1);

}else if(i==1) {

pr2 = new Player(name,id,null);

System.out.println("成功添加玩家"+pr2);

}

}catch(InputMismatchException e) {

System.out.println("请输入整数类型的ID");

i--;

continue;

}

}

System.out.println("玩家添加完毕,分别为:"+pr1+","+pr2+"  !");

}

public void licensing() {

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

if(i%2==0) {//方便大家看,pr1是玩家1对象,palyingcards 是玩家对象中的ArrayList集合,用来放牌

pr1.playingcards.add(poker.get(i));

System.out.println("玩家"+pr1.name+"拿牌");

}else {

pr2.playingcards.add(poker.get(i));

System.out.println("玩家"+pr2.name+"拿牌");

}

}

// pr1.playingcards.add(poker.get(0));  //测试用

// System.out.println("玩家"+pr1.name+"拿牌");

// poker.remove(0);

// pr2.playingcards.add(poker.get(0));

// System.out.println("玩家"+pr2.name+"拿牌");

// poker.remove(0);

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

System.out.println("--------------游戏开始------------------------------");

Collections.sort(pr1.playingcards);

Collections.sort(pr2.playingcards);

//方便大家看,pr1是玩家1对象,palyingcards 是玩家对象中的ArrayList集合,用来放牌

System.out.println(pr1.name+"最大的手牌为"+pr1.playingcards.get(1).color+

pr1.playingcards.get(1).card);

System.out.println(pr2.name+"最大的手牌为"+pr2.playingcards.get(1).color+

pr2.playingcards.get(1).card);

if(pr1.playingcards.get(1).num>pr2.playingcards.get(1).num){

System.out.println("------------玩家"+pr1.name+"获胜-------------------------");

}else {

System.out.println("------------玩家"+pr2.name+"获胜-------------------------");

}

System.out.println("玩家各自的手牌为:");

System.out.println(pr1.name+":"+pr1.playingcards);

System.out.println(pr2.name+":"+pr2.playingcards);

}


}


正在回答

1 回答

真的感谢注释,创建玩家哪里我就是进入死循环,不知道问题在哪里

0 回复 有任何疑惑可以回复我~
#1

老丶斑鸠 提问者

我也是 死循环了 半天 想起scanner对象没有初始化来着= = 哈哈
2018-08-24 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Java入门第三季
  • 参与学习       409791    人
  • 解答问题       4340    个

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

进入课程

自己摸索的纸牌游戏代码,感觉还有很多知识不懂,任重道远啊!

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信