package com.poker;
public class Poker implements Comparable<Poker> {
private String type;
private String num;
public Poker(String type ,String num){
this.setType(type);
this.setNum(num);
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((num == null) ? 0 : num.hashCode());
result = prime * result + ((type == null) ? 0 : type.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Poker other = (Poker) obj;
if (num == null) {
if (other.num != null)
return false;
} else if (!num.equals(other.num))
return false;
if (type == null) {
if (other.type != null)
return false;
} else if (!type.equals(other.type))
return false;
return true;
}
@Override
public int compareTo(Poker o) {
String pokernum="2,3,4,5,6,7,8,9,10,J,Q,K,A";
String pokertype="方块,梅花,红桃,黑桃";
int result;
result=pokernum.indexOf(this.getNum())-pokernum.indexOf(o.getNum());
if(result==0){
result=pokertype.indexOf(this.getType())-pokertype.indexOf(o.getType());
}
return result;
}
}
package com.poker;
public class Player {
private String ID;
private String name;
public Poker handpoker;
public Player(String ID,String name){
this.ID=ID;
this.name=name;
}
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.poker;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class Gameplay {
List<Poker>pokerlist=new ArrayList<Poker>();
List<Player>playerlist=new ArrayList<Player>();
List<Poker>player1handpoker=new ArrayList<Poker>();
List<Poker>player2handpoker=new ArrayList<Poker>();
String[]type={"方块","梅花","红桃","黑桃"};
String[]point={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
/**
* 首先创建的是一副扑克牌
*/
public void creatPoker(){
System.out.println("开始创建扑克牌");
for(int i=0;i<type.length;i++){
for(int j=0;j<point.length;j++){
pokerlist.add(new Poker(type[i],point[j]));
}
}
System.out.println("扑克牌创建成功");
}
/**
* 检验扑克牌是否完成
*/
public void testCreatPoker(){
for(Poker pk:pokerlist){
System.out.println("有如下牌:"+pk.getType()+pk.getNum()+" ");}
}
/**
* 然后是创建2个玩家
*/
public void creatPlayer(){
for(int n=0;n<2;n++){
System.out.println("请输入玩家ID");
String playerID=new Scanner(System.in).next();
System.out.println("请输入玩家姓名");
String playerName=new Scanner(System.in).next();
playerlist.add(new Player(playerID,playerName));
}
System.out.println("玩家添加成功!"+"现有玩家是:");
for(Player player:playerlist){
System.out.println(player.getID()+":"+player.getName());
}
}
/**
* 接下来的步骤是洗牌
*/
public void shufflepoker(){
System.out.println("开始洗牌咯~~"+" .....");
Collections.shuffle(pokerlist);
System.out.println("洗牌完成!");
}
/**
* 下面是发牌步骤
*/
public void deliverPoker(){
System.out.println("开始发牌!");
for(int i=0;i<4;i++){
Poker newpoker=pokerlist.get(i);
if(i%2!=0){
player1handpoker.add(newpoker);
System.out.println("玩家1抓了一张牌");
}else
{
player2handpoker.add(newpoker);
System.out.println("玩家2抓了一张牌");
}
}
for(Poker Poker1:player1handpoker){
System.out.println(playerlist.get(0).getName()+"手里的牌是"
+Poker1.getType()+Poker1.getNum());
}
for(Poker Poker2:player2handpoker){
System.out.println(playerlist.get(1).getName()+"手里的牌是"
+Poker2.getType()+Poker2.getNum());
}
}
/**
* 最后的环节是比较大小
*/
public void compare(){
Collections.sort(player1handpoker);
Poker pk1Max=player1handpoker.get(player1handpoker.size()-1);
System.out.println("玩家1手里最大的牌是"+pk1Max.getType()+pk1Max.getNum());
Collections.sort(player2handpoker);
Poker pk2Max=player2handpoker.get(player2handpoker.size()-1);
System.out.println("玩家2手里最大的牌是"+pk2Max.getType()+pk2Max.getNum());
List<Poker>maxpkList=new ArrayList<Poker>();
maxpkList.add(pk1Max);
maxpkList.add(pk2Max);
Collections.sort(maxpkList);
Poker Maxpk=maxpkList.get(maxpkList.size()-1);
if(player1handpoker.contains(Maxpk)){
System.out.println("玩家"+playerlist.get(0).getName()+"获胜!");
}else{
System.out.println("玩家"+playerlist.get(1).getName()+"获胜!");
}
}
/**
*
* @param args
*/
public static void main(String [] args){
Gameplay t1=new Gameplay();
t1.creatPlayer();
t1.creatPoker();
t1.testCreatPoker();
t1.shufflepoker();
t1.deliverPoker();
t1.compare();
}
}