package pokerGame;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class GameDemo {
//创建人物和扑克牌
List<Poker> pokers;
List<Player> players;
Player player1;
Player player2;
Poker player1Max;
Poker getPlayer1Min;
Poker player2Max;
Poker getPlayer2Min;
//创建扑克牌资源
String[] PokerType = {"红桃","黑桃","方片","梅花"};
String[] PokerNum = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
public GameDemo(){
this.pokers = new ArrayList<Poker>();
this.players = new ArrayList<Player>();
}
public void createPokers(){
System.out.println("------------创建扑克牌......-----------");
System.out.println("------------扑克牌创建成功--------------");
for(int i = 0;i < PokerNum.length;i++){
for(int j = 0;j<PokerType.length;j++)
{
pokers.add(new Poker(PokerNum[i],i*13+j,PokerType[j]));
}
}
StringBuffer allPokers = new StringBuffer();
for(Poker poker:pokers){
allPokers.append(poker.getType()+poker.getNum()+",");
}
System.out.println("52张如下牌:");
System.out.println(allPokers);
}
public void washPokers(){
System.out.println("-------------开始洗牌---------------");
Collections.shuffle(pokers);
System.out.println("-------------洗牌完毕---------------");
}
public void createPlayers(){
System.out.println("-------------创建玩家----------------");
Scanner console = new Scanner(System.in);
System.out.println("请输入第1位玩家的ID和姓名");
System.out.println("请输入ID:");
String ID = console.next();
System.out.println("请输入姓名:");
String name = console.next();
player1 = new Player(ID,name);
players.add(player1);
System.out.println("请输入第2位玩家的ID和姓名");
System.out.println("请输入ID:");
String ID2 = console.next();
System.out.println("请输入姓名:");
String name2 = console.next();
player2 = new Player(ID2,name2);
players.add(player2);
System.out.println("欢迎玩家"+player1.getName());
System.out.println("欢迎玩家"+player2.getName());
}
public void distributePokers(){
System.out.println("------------开始发牌-------------");
System.out.println("----玩家:"+player1.getName()+"拿牌--------");
player1.pokerList.add(pokers.get(0));
System.out.println("----玩家:"+player2.getName()+"拿牌--------");
player2.pokerList.add(pokers.get(1));
System.out.println("----玩家:"+player1.getName()+"拿牌--------");
player1.pokerList.add(pokers.get(2));
System.out.println("----玩家:"+player2.getName()+"拿牌--------");
player2.pokerList.add(pokers.get(3));
System.out.println("------------发牌结束-------------");
}
public void play(){
System.out.println("------------开始游戏-------------");
Collections.sort(player1.pokerList);
Collections.sort(player2.pokerList);
player1Max = player1.pokerList.get(1);
player2Max = player2.pokerList.get(1);
System.out.println("玩家:"+player1.getName()+"最大的手牌是"+player1Max.getType()+player1Max.getNum());
System.out.println("玩家:"+player2.getName()+"最大的手牌是"+player2Max.getType()+player2Max.getNum());
String winner = null;
if(player1Max.getRank()>player2Max.getRank()){
winner = player1.getName();
}
else if(player1Max.getRank()<player2Max.getRank()) {
winner = player2.getName();
}
else {
System.out.println("平局!");
}
System.out.println("------------恭喜玩家:"+winner+"获胜!-------------");
System.out.println("玩家各自的手牌:");
player1.printPlayersPokers();
player2.printPlayersPokers();
}
public static void main(String[] args){
GameDemo gameDemo = new GameDemo();
gameDemo.createPokers();
gameDemo.washPokers();
gameDemo.createPlayers();
gameDemo.distributePokers();
gameDemo.play();
}
}
package pokerGame;
import java.util.ArrayList;
import java.util.List;
/**
* 玩家类
*/
public class Player {
private String Id;
private String name;
List<Poker> pokerList;//用于存放玩家手牌
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;
}
public Player(String Id, String name){
this.Id = Id;
this.name = name;
pokerList = new ArrayList<Poker>();
}
public void printPlayersPokers(){
System.out.print(name+":");
for(Poker poker:pokerList){
System.out.print(poker.getType()+poker.getNum()+"\n" );
}
}
}
package pokerGame;
/**
* 扑克牌类
*/
public class Poker implements Comparable<Poker> {
private String num;
private int rank;//rank越大等级越高
private String type;
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public String getNum(){
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getType(){
return type;
}
public void setType(String type) {
this.type = type;
}
public Poker(){
}
public Poker(String num,int rank,String type){
this.num = num;
this.rank = rank;
this.type = type;
}
@Override
public int compareTo(Poker poker){
return this.rank-poker.rank;
}
}