Poker.java
package com.wdp;
public class Poker implements Comparable<Poker> {
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((ds == null) ? 0 : ds.hashCode());
result = prime * result + ((hs == null) ? 0 : hs.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 (ds == null) {
if (other.ds != null)
return false;
} else if (!ds.equals(other.ds))
return false;
if (hs == null) {
if (other.hs != null)
return false;
} else if (!hs.equals(other.hs))
return false;
return true;
}
//扑克牌属性
private String hs;
private String ds;
//有参的构造方法
public Poker(String hs,String ds) {
this.hs = hs;
this.ds = ds;
}
//setter、getter
public String getHs() {
return hs;
}
public void setHs(String hs) {
this.hs = hs;
}
public String getDs() {
return ds;
}
public void setDs(String ds) {
this.ds = ds;
}
@Override
public int compareTo(Poker o) {
// TODO Auto-generated method stub
if(this.getValueOfDs()==o.getValueOfDs()){
return this.getValueOfHs()-o.getValueOfHs();
}else {
return this.getValueOfDs()-o.getValueOfDs();
}
}
public int getValueOfHs() {
String[] hs = {"黑桃","红桃","梅花","方片"};
for(int i=0;i<hs.length;i++) {
if(hs[i].equals(this.hs));
return i;
}
return -1;
}
public int getValueOfDs() {
String[] ds = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
for(int i=0;i<ds.length;i++) {
if(ds[i].equals(this.ds));
return i;
}
return -1;
}
}
PlayGame.java
package com.wdp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class PlayGame {
String[] hs = {"黑桃","红桃","梅花","方片"};
String[] ds = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
List<Poker> pokerList = new ArrayList<Poker>();
Map<Integer,Player> playerMap = new HashMap<Integer, Player>();
List<Poker> pokerOnHande1 = new ArrayList<Poker>();
List<Poker> pokerOnHande2 = new ArrayList<Poker>();
int[] ID = new int[10] ;
/**
* 创建扑克牌、打乱扑克牌
*/
public void CreatePoker(){
for(int i=0;i<hs.length;i++) {
for(int j=0;j<ds.length;j++) {
Poker poker = new Poker(hs[i],ds[j]);
pokerList.add(poker);
}
}
System.out.println("---------创建扑克牌完成----------");
System.out.println("已创建扑克牌如下:");
for (Poker p : pokerList) {
System.out.println(p.getHs()+p.getDs());
}
Collections.shuffle(pokerList);
System.out.println("---------扑克牌已打乱-------------");
// for (Poker p : pokerList) {
// System.out.println(p.getHs()+p.getDs());
// }
}
/**
* 创建玩家
*/
public void CreatePlayer() {
Scanner sc = new Scanner(System.in);
System.out.println("----------创建玩家--------------");
for(int i = 0 ; i<2;i++) {
System.out.println("请创建第"+(i+1)+"位玩家");
System.out.println("请输入玩家ID:");
int id = sc.nextInt();
System.out.println("请输入玩家姓名:");
String name = sc.next();
Player player = new Player(id,name);
playerMap.put(id, player);
}
System.out.println("创建玩家成功!");
System.out.println("---欢迎玩家:"+playerMap.get(1).getName());
System.out.println("---欢迎玩家:"+playerMap.get(2).getName());
}
/**
* 发牌
*/
public void PutPoker() {
for(int i = 0;i<4;i++) {
if(i%2==0) {
pokerOnHande1.add(pokerList.get(i));
System.out.println("玩家:"+playerMap.get(1).getName()+"拿牌。");
}else{
pokerOnHande2.add(pokerList.get(i));
System.out.println("玩家:"+playerMap.get(2).getName()+"拿牌。");
}
}
playerMap.get(1).setPokerOnHande(pokerOnHande1);
playerMap.get(2).setPokerOnHande(pokerOnHande2);
System.out.println("-------发牌结束--------");
for (Poker pk2 : playerMap.get(1).getPokerOnHande()) {
System.out.println("玩家:"+playerMap.get(1).getName()+"手中的牌是:"+
pk2.getHs()+pk2.getDs());
}
for (Poker pk2 : playerMap.get(2).getPokerOnHande()) {
System.out.println("玩家:"+playerMap.get(2).getName()+"手中的牌是:"+
pk2.getHs()+pk2.getDs());
}
Collections.sort(playerMap.get(1).getPokerOnHande());
Collections.sort(playerMap.get(2).getPokerOnHande());
System.out.println("-------------排序后--------------");
for (Poker pk2 : playerMap.get(1).getPokerOnHande()) {
System.out.println("玩家:"+playerMap.get(1).getName()+"手中的牌是:"+
pk2.getHs()+pk2.getDs());
}
for (Poker pk2 : playerMap.get(2).getPokerOnHande()) {
System.out.println("玩家:"+playerMap.get(2).getName()+"手中的牌是:"+
pk2.getHs()+pk2.getDs());
}
}
/**
* 比较
*/
public void pokerComparable(){
System.out.println("-------------开始游戏----------------");
Collections.sort(pokerOnHande1);
System.out.println("玩家:"+playerMap.get(1).getName()+"的最大手牌是:"+
pokerOnHande1.get(1).getHs()+pokerOnHande1.get(1).getDs());
Collections.sort(pokerOnHande2);
System.out.println("玩家:"+playerMap.get(2).getName()+"的最大手牌是:"+
pokerOnHande2.get(1).getHs()+pokerOnHande2.get(1).getDs());
}
public static void main(String[] args) {
PlayGame pg = new PlayGame();
pg.CreatePoker();
pg.CreatePlayer();
pg.PutPoker();
// pg.pokerComparable();
}
}
1 回答
树那边
TA贡献6条经验 获得超0个赞
playerMap.get(
1
)、
playerMap.get(
2
)这个有问题,应该是0和1,也就是:
playerMap.get(
0
),
playerMap.get(
1
)
添加回答
举报
0/150
提交
取消