慕慕309619
2015-01-13 14:17
package com.puke; 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 = pflower+pnum; } /** * 获取扑克点数 * @return * 由于根据默认字符串的比较规则 10是比2 . 3 . 4 ....9 要小的 * 字符串的默认排序规则是 先排数字第一位 如果相等比较第二位 .. 然后在大写字母 小写字母 依次类推 * 这里的 10 J Q K A 要返回一个数字类型的字符串 用来给扑克排序 */ public String getPokerNum(){ if (this.num == "10") { this.num = "91"; }else if (this.num == "J") { this.num = "92"; }else if (this.num == "Q") { this.num = "93"; }else if (this.num == "K") { this.num = "94"; }else if (this.num == "A") { this.num = "1"; } return this.num; } /** * 获取扑克的花 * @return */ public String getPokerFlower(){ return this.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; } } ================================================================== package com.puke; import java.util.Comparator; public class PokerComparator implements Comparator<Poker> { /** * 比较规则类 */ @Override //比较优先级 先比较点数 点数相同再比较花色 public int compare(Poker arg0, Poker arg1) { String num0 = ((Poker) arg0).getPokerNum(); String num1 = ((Poker) arg1).getPokerNum(); int ret = 0; if((ret = num0.compareTo(num1)) == 0){ String flw0 = ((Poker) arg0).getPokerFlower(); String flw1 = ((Poker) arg1).getPokerFlower(); ret = flw0.compareTo(flw1); } return ret; } } =============================================================== package com.puke; import java.util.ArrayList; import java.util.List; /** * 玩家类 * @author momo1a@qq.com * * */ public class Player { private int ID; //玩家ID private String name; //玩家名字 private List<Poker> pokerCard; //玩家手牌集合 /** * 无参构造 */ public Player() { } /** * 初始化玩家 * @param ID 玩家ID * @param name 玩家用户名 * 初始化手牌类 */ public Player(int ID,String name) { this.pokerCard = new ArrayList<Poker>(); this.ID = ID; this.name = name; } /** * 获取用户ID * @return 用户ID */ public int getPlayerID(){ return this.ID; } /** * 设置用户ID * @param ID */ public void setPlayerID(int ID) { this.ID = ID; } /** * 获取用户名 * @return 用户名 */ public String getPlayerName(){ return this.name; } /** * 设置用户名 * @param name */ public void setPlayerName(String name) { this.name = name; } /** * 设置玩家手牌 * @param poker * */ public void setPokerCard(Poker poker){ this.pokerCard.add(poker); } /** * 获取玩家手牌 * @return 玩家手牌集合 */ public List<Poker> getPokerCard(){ return this.pokerCard; } @Override public String toString() { return name; } } =============================================================== package com.puke; 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 numbers[]={"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; private String flowers[]={"方块","梅花","红桃","黑桃"}; private List<Poker> pokerList; //一副扑克的集合 private List<Player> players; //玩家集合 private List<Poker> maxPokerList; //出牌的集合 (也就是各自拿来比较那张大牌的集合) /** * 无参构造方法 初始化一副扑克 */ public PlayGame(){ players = new ArrayList<Player>(); pokerList = new ArrayList<Poker>(); maxPokerList = new ArrayList<Poker>(); System.out.println("-----------创建扑克牌----------"); for (int i = 0; i < flowers.length; i++) { for (int j = 0; j < numbers.length; j++) { Poker poker = new Poker(numbers[j], flowers[i]); this.pokerList.add(poker); //创建一张扑克就添加到扑克的集合 } } System.out.println("-----------扑克牌创建成功----------"); System.out.println("为:"+this.pokerList); } /** * 洗牌功能 */ public void washPoker() { System.out.println("---------开始洗牌------------"); for (int i = 0; i < pokerList.size(); i++) { Random random = new Random(); int j = random.nextInt(pokerList.size()); Poker temp = pokerList.get(i); //用一个变量保存 i位置的一张扑克 pokerList.set(i, pokerList.get(j)); //用j位置(随机长生)的扑克替换i位置的扑克 pokerList.set(j, temp); //j位置的扑克就替换成原来i位置的扑克 } System.out.println("---------洗牌结束!-----------"); } /** * 创建玩家 并把玩家添加到玩家集合 * 输入玩家的id必须为数字类型 * */ public void createPlayer(){ Scanner input = new Scanner(System.in); this.washPoker(); //洗牌 System.out.println("请输入两位玩家的ID和用户名"); for (int i = 0; i < 2; i++) { System.out.println("输入第"+(i+1)+"位玩家ID:"); int ID = 0; try { ID = input.nextInt(); System.out.println("输入第"+(i+1)+"位玩家用户名:"); } catch (Exception e) { System.out.println("请输入整数类型ID!!!"); this.createPlayer(); } String name = input.next(); players.add(new Player(ID, name)); //添加用户到用户集合中 } //输出欢迎字样 for (int i = 0; i < players.size(); i++) { System.out.println("----欢迎玩家:"+players.get(i)); } } /** * 发牌功能 * 从第一张开始发牌 轮流拿牌 各自发两张牌 */ public void perPokerCard(){ System.out.println("-----开始发牌------"); for (int i = 0, n=0; i < 2; i++) { for (int j = 0; j < players.size(); j++,n++) { this.players.get(j).setPokerCard(this.pokerList.get(n)); System.out.println("----玩家:"+players.get(j)+"-拿牌"); } } System.out.println("------发牌结束!!!------"); } /** * 开始玩游戏 比大小 * 比手牌大小的规则来自PokerComparator类 * 先比玩家的点数 A<2<3<4<5<6<7<8<9<10<J<Q<K * 如果点数相同 在比较花色 方块<梅花<红桃<黑桃 */ 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())); maxPokerList.add(Collections.max(this.players.get(i).getPokerCard(),new PokerComparator())); //添加各自的大牌到一个List } Poker winPoker = Collections.max(maxPokerList, new PokerComparator()); //比较大牌的List集合 决出 最大的一张扑克 //判断最大那张扑克属于谁的 谁就是胜利者 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)+":"+players.get(i).getPokerCard()); } } } ================================================================== package com.puke; public class ShowPlay { /** * main()方法 * @param args */ public static void main(String[] args) { PlayGame showpoker = new PlayGame(); //初始化游戏 新建52张扑克 showpoker.createPlayer(); //新建玩家 showpoker.perPokerCard(); //发牌 showpoker.playGame(); //玩游戏 比大小 } }
// 创建一副牌
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)
求解救啊
之前寒假在家写的,代码比较啰嗦,求批评指正
package pokergame;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.Random;
import java.util.Scanner;
//使用int来表示扑克,点数为2-14,花色为100-400,如104即为方片4
class Poker{
private int color;
private int num;
private int poker;
private String colorString;
private String numString;
//设置扑克的花色和点数
public Poker(int color,int num) {
this.color = color;
this.num = num;
this.poker = color*100+num;
}
public Poker(){
}
//将扑克的花色与点数提取出来
public Poker(int pokerint){
this.num = pokerint%100;
this.color = pokerint/100;
}
public int getPoker() {
return poker;
}
//将int表示的扑克转换为字符串
public String toStringPoker(int pokerint){
num = pokerint%100;
color = pokerint/100;
if (num == 11) {
this.numString = "J";
} else if (num == 12) {
this.numString = "Q";
}else if (num == 13) {
this.numString = "K";
}else if (num == 14) {
this.numString = "A";
}else {
this.numString = num+"";
}
switch (color) {
case 1:
this.colorString = "方片";
break;
case 2:
this.colorString = "梅花";
break;
case 3:
this.colorString = "红桃";
break;
case 4:
this.colorString = "黑桃";
break;
}
return (colorString+numString);
}
}
//玩家类,用来表示玩家
class Player{
private int ID;
private String name;
//玩家的手牌集合,用ArrayList来表示
ArrayList<Integer> poker = new ArrayList<>();
Poker pk = new Poker();
//用来存储玩家最大的手牌
private int maxPoker;
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<Integer> getPoker() {
return poker;
}
public void setPoker(ArrayList<Integer> poker) {
this.poker = poker;
}
//将玩家的手牌转换为字符串并输出,index代表玩家的第几张手牌
public String getPokerString(int index){
return pk.toStringPoker(poker.get(index));
}
public int getMaxPoker() {
return maxPoker;
}
public void setMaxPoker(int maxPoker) {
this.maxPoker = maxPoker;
}
}
public class PlayGame {
ArrayList<Integer> poker;
//用来存放洗牌后的扑克
int poker1[];
//创建两个玩家的对象
Player player1;
Player player2;
public PlayGame(){
this.poker = new ArrayList<>();
this.poker1 = new int[52];
this.player1 = new Player();
this.player2 = new Player();
}
//创建一副扑克,将其放在ArrayList集合中
public void makePoker(){
for (int i = 1; i <= 4; i++) {
for (int j = 2; j <= 14; j++) {
poker.add(new Poker(i,j).getPoker());
}
}
}
//将创建好的扑克输出到屏幕上
public void getPoker(){
Poker pk = new Poker();
System.out.print("为:[");
for (Iterator<Integer> iterator = poker.iterator(); iterator.hasNext();) {
Integer integer = (Integer) iterator.next();
System.out.print(pk.toStringPoker(integer)+" ");
}
System.out.println("]");
}
//用来将排序后的ArrayList的扑克打乱顺序并放在poker1数组中
public void shuffle(){
//将ArrayList的扑克先全部复制到poker1数组中
for (int i = 0; i < poker1.length; i++) {
poker1[i] = poker.get(i);
}
//打乱poker1中扑克
Random rd = new Random();
for (int i = 0; i < 52; i++) {
int rdnum = rd.nextInt(52);
poker1[i] = poker1[i] + poker1[rdnum];
poker1[rdnum] = poker1[i] - poker1[rdnum];
poker1[i] = poker1[i] - poker1[rdnum];
}
}
//用来创建玩家的方法
public void makePlayer(Player player){
int ID;
System.out.print("输入ID:");
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
while (true) {
try {
if (scanner.hasNext()) {
ID = scanner.nextInt();
player.setID(ID);
}
break;
} catch (InputMismatchException e) {
scanner.next();
System.out.println("ID应该为整数");
}
}
System.out.print("输入姓名:");
if (scanner.hasNext()) {
player.setName(scanner.next());
}
}
//找出一个玩家手中最大的牌
public String comparPoker(Player player){
if (player.poker.get(0)%100 > player.poker.get(1)%100) {
player.setMaxPoker(player.poker.get(0));
return player.pk.toStringPoker(player.poker.get(0));
}else if (player.poker.get(0)%100 < player.poker.get(1)%100) {
player.setMaxPoker(player.poker.get(1));
return player.pk.toStringPoker(player.poker.get(1));
}else if (player.poker.get(0)/100 > player.poker.get(1)/100) {
player.setMaxPoker(player.poker.get(0));
return player.pk.toStringPoker(player.poker.get(0));
}else if (player.poker.get(0)/100 < player.poker.get(1)/100) {
player.setMaxPoker(player.poker.get(1));
return player.pk.toStringPoker(player.poker.get(1));
}else {
player.setMaxPoker(player.poker.get(0));
return player.pk.toStringPoker(player.poker.get(0));
}
}
//将两个玩家手中最大的牌做比较,找出其中的赢家
public String comparPoker(Player player1,Player player2){
if (player1.getMaxPoker()%100 > player2.getMaxPoker()%100) {
return player1.getName();
}else if (player1.getMaxPoker()%100 < player2.getMaxPoker()%100) {
return player2.getName();
}else if (player1.getMaxPoker()/100 > player2.getMaxPoker()/100) {
return player1.getName();
}else if (player1.getMaxPoker()/100 < player2.getMaxPoker()/100) {
return player2.getName();
}else {
return "平手";
}
}
public static void main(String[] args) {
PlayGame pg = new PlayGame();
System.out.println("----------创建一副扑克牌----------");
pg.makePoker();
System.out.println("----------扑克牌创建成功---------");
pg.getPoker();
System.out.println("--------开始洗牌---------");
pg.shuffle();
System.out.println("--------洗牌结束---------");
System.out.println("--------创建玩家---------");
System.out.println("请输入第一位玩家的ID和姓名:");
pg.makePlayer(pg.player1);
System.out.println("请输入第二位玩家的ID和姓名:");
pg.makePlayer(pg.player2);
System.out.println("\n欢迎玩家"+pg.player1.getName());
System.out.println("欢迎玩家"+pg.player2.getName());
System.out.println("\n--------开始发牌---------");
pg.player1.poker.add(pg.poker1[0]);
System.out.println("玩家"+pg.player1.getName()+"拿牌");
pg.player2.poker.add(pg.poker1[1]);
System.out.println("玩家"+pg.player2.getName()+"拿牌");
pg.player1.poker.add(pg.poker1[2]);
System.out.println("玩家"+pg.player1.getName()+"拿牌");
pg.player2.poker.add(pg.poker1[3]);
System.out.println("玩家"+pg.player2.getName()+"拿牌");
System.out.println("--------发牌结束----------");
System.out.println("--------开始游戏----------");
System.out.println("玩家"+pg.player1.getName()+"的最大的手牌为"+pg.comparPoker(pg.player1));
System.out.println("玩家"+pg.player2.getName()+"的最大的手牌为"+pg.comparPoker(pg.player2));
String resultString = pg.comparPoker(pg.player1, pg.player2);
if (resultString != "平手") {
System.out.println("----------玩家"+resultString+"获胜----------");
}else {
System.out.println("------------平手-------------");
}
System.out.println("玩家各自的手牌为:");
System.out.println(pg.player1.getName()+"["+pg.player1.pk.toStringPoker(pg.player1.poker.get(0))+","+pg.player1.pk.toStringPoker(pg.player1.poker.get(1))+"]");
System.out.println(pg.player2.getName()+"["+pg.player2.pk.toStringPoker(pg.player2.poker.get(0))+","+pg.player2.pk.toStringPoker(pg.player2.poker.get(1))+"]");
}
}
我觉得不用String,用int来存储扑克牌比较好处理
Java入门第三季
409792 学习 · 4340 问题
相似问题