Dason
2016-02-20 00:11
// 创建一副牌
public class Poker {
private String num;//点数
private String flower;//花色
private String poker;//一张牌
public Poker(String pnum,String pflower){
this.num=pnum;
this.flower=pflower;
this.poker=num+flower;
}
// 用字符的比较规则,10比2,3,4...要小
public String getnum(){
if(num=="10"){
num="91";
}
else if(num=="J"){
num="92";
}
else if(num=="Q"){
num="93";
}
else if(num=="K"){
num="94";
}
else if(num=="A"){
num="1";
}
return this.num;
}
public String getFlower() {
return flower;
}
@Override
public String toString() {
return poker;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((flower == null) ? 0 : flower.hashCode());
result = prime * result + ((num == null) ? 0 : num.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Poker))
return false;
Poker other = (Poker) obj;
if (flower == null) {
if (other.flower != null)
return false;
} else if (!flower.equals(other.flower))
return false;
if (num == null) {
if (other.num != null)
return false;
} else if (!num.equals(other.num))
return false;
return true;
}
}
import java.util.ArrayList;
import java.util.List;
public class Player{
private int id;
private String name;
private List<Poker>pokercard;
// 无参构造
public Player(){
}
// 初始化玩家,手牌
public Player(int id,String name){
this.id = id;
this.name = name;
this.pokercard = new ArrayList<Poker>();
}
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;
}
public List<Poker> getPokercard() {
return this.pokercard;
}
public void setPokercard(Poker poker) {
this.pokercard.add(poker);
}
@Override
public String toString() {
return "Player [name=" + name + "]";
}
}
import java.util.Comparator;
public class PokerComparator implements Comparator<Poker> {
public int compare(Poker o1, Poker o2) {
String num1=(String)o1.getnum();
String num2=(String)o2.getnum();
int c=0;
if((c=num1.compareTo(num2))==0){
String flower1=(String )o1.getFlower();
String flower2=(String )o2.getFlower();
c=flower1.compareTo(flower2);
}
return c;
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class PlayGame {
private String[] flower = { "方块", "梅花", "红心", "黑桃" };
private String[] num = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "K" };
// 牌集合
private List<Poker> pokers;
// 玩家集合
private List<Player> players;
// 大牌集合
private List<Poker> maxcards;
private Scanner scanner;
// 初始化牌、
public PlayGame() {
pokers = new ArrayList<Poker>();
players = new ArrayList<Player>();
maxcards = new ArrayList<Poker>();
System.out.println("创建扑克牌");
for (int i = 0; i < flower.length; i++) {
for (int j = 0; j < num.length; j++) {
Poker poker = new Poker(flower[i], num[j]);
this.pokers.add(poker);
}
}
System.out.println("创建成功" + pokers);
}
// 初始化玩家
public void CreatePlayers() {
scanner = new Scanner(System.in);
this.WashCard();
for (int i = 0; i < 2; i++) {
System.out.println("请输入第" + (i + 1) + "位玩家ID");
int id = 0;
try {
id = scanner.nextInt();
System.out.println("请输入第" + (i + 1) + "位玩家姓名");
} catch (Exception o) {
System.out.println("请输入整数型ID");
this.CreatePlayers();
}
String name = scanner.next();
players.add(new Player(id, name));
}
for (int i = 0; i < players.size(); i++) {
System.out.println("----欢迎玩家:"+players.get(i));
}
}// 洗牌
public void WashCard() {
System.out.println("开始洗牌");
for (int i = 0; i < pokers.size(); i++) {
Random random = new Random();
int j = random.nextInt(pokers.size());
Poker temp = pokers.get(i);
pokers.set(i, pokers.get(j));
pokers.set(j, temp);
}
System.out.println("洗牌结束");
}
// 拿牌
public void TakeCard() {
for (int i = 0, n = 0; i < 2; i++) {
// 每人依次拿牌
for (int j = 0; j < players.size(); j++, n++) {
this.players.get(j).setPokercard(this.pokers.get(n));
System.out.println("玩家" + players.get(j) + "拿牌");
}
}
System.out.println("结束发牌");
}
// 比较大小
public void playGame(){
System.out.println("开始游戏");
for (int i = 0; i < players.size(); i++) {
System.out.println("玩家:"+players.get(i)+"最大的手牌为:"+ Collections.max(this.players.get(i).getPokercard(),new PokerComparator()));
maxcards.add(Collections.max(this.players.get(i).getPokercard(),new PokerComparator())); //添加各自的大牌到一个List
}
}//比较大牌的List集合 决出 最大的一张扑克
public void playGame2(){
// 判断大牌集合中最大牌
Poker winPoker=Collections.max(maxcards, new PokerComparator());
for(int i=0;i<players.size();i++){
if(players.get(i).getPokercard().contains(winPoker)){
System.out.println("胜出的玩家是:"+players.get(i));
}
}
System.out.println("玩家各自的手牌是");
for(int i=0;i<players.size();i++){
System.out.println(players.get(i).getPokercard());
}
}
}
public class GaneTest {
public static void main(String[] args) {
PlayGame pg=new PlayGame();
pg.CreatePlayers();
pg.playGame();
pg.playGame2();
}
}
------------------------------------------------------------------------------------------------------------
创建扑克牌
创建成功[方块A, 方块2, 方块3, 方块4, 方块5, 方块6, 方块7, 方块8, 方块9, 方块10, 方块J, 方块Q, 方块K, 梅花A, 梅花2, 梅花3, 梅花4, 梅花5, 梅花6, 梅花7, 梅花8, 梅花9, 梅花10, 梅花J, 梅花Q, 梅花K, 红心A, 红心2, 红心3, 红心4, 红心5, 红心6, 红心7, 红心8, 红心9, 红心10, 红心J, 红心Q, 红心K, 黑桃A, 黑桃2, 黑桃3, 黑桃4, 黑桃5, 黑桃6, 黑桃7, 黑桃8, 黑桃9, 黑桃10, 黑桃J, 黑桃Q, 黑桃K]
开始洗牌
洗牌结束
请输入第1位玩家ID
1
请输入第1位玩家姓名
s
请输入第2位玩家ID
2
请输入第2位玩家姓名
f
----欢迎玩家:Player [name=s]
----欢迎玩家:Player [name=f]
开始游戏
Exception in thread "main" java.util.NoSuchElementException
at java.util.ArrayList$Itr.next(Unknown Source)
at java.util.Collections.max(Unknown Source)
at PlayGame.playGame(PlayGame.java:88)//上面黑体部分
at GaneTest.main(GaneTest.java:8)
-----------------------------------------------------------
求解救啊
takecard方法都没有调用,怎么可能不报错
Java入门第三季
409792 学习 · 4340 问题
相似问题