分了5个类,Poke储存所有扑克牌,Player存储玩家,Game写明方法,Test测试类,Poker存储分到手牌的玩家信息
package 扑克牌游戏;
public class Poke {
private String color;
private Integer colornumber;
private String value;
private Integer valuenumber;
public Poke(String color,Integer colornumber,String value,Integer valuenumber) {
this.color=color;
this.colornumber=colornumber;
this.value=value;
this.valuenumber=valuenumber;
}
public String getColor() {
return color;
}
public int getColornumber() {
return colornumber;
}
public String getValue() {
return value;
}
public int getValuenumber() {
return valuenumber;
}
public String toString() {
return color+value;
}
}
package 扑克牌游戏;
import java.util.ArrayList;
import java.util.List;
public class Player {
private int id;
private String name;
public List<Poker>pokerr=new ArrayList<Poker>();//玩家拿的牌和权值
public Player(int id,String name) {
this.id=id;
this.name=name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}
package 扑克牌游戏;
public class Poker implements Comparable<Poker>{
private String name;
private String color;
private Integer colornumber;
private String value;
private Integer valuenumber;
public Poker(String name,String color,int colornumber,String value,int valuenumber) {
this.name=name;
this.color=color;
this.colornumber=colornumber;
this.value=value;
this.valuenumber=valuenumber;
}
public String getName() {
return name;
}
public String getColor() {
return color;
}
public String getValue() {
return value;
}
public int getColornumber() {
return colornumber;
}
public int getValuenumber() {
return valuenumber;
}
public String toString() {
return color+value;
}
public int compareTo(Poker o) {
// TODO Auto-generated method stub
if(this.valuenumber!=o.valuenumber)
return o.valuenumber.compareTo(this.valuenumber);
else
return o.colornumber.compareTo(this.colornumber);
}
}
package 扑克牌游戏;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Game {
/*
* 创建玩家和扑克牌的List
*/
private List<Poke>pokeList=new ArrayList<Poke>();//花色牌值,权值
private List<Player>playerList=new ArrayList<Player>();//玩家
private List<Poke>poker=new ArrayList<Poke>();//发的四张牌
/*
*创建扑克牌
*/
public void createPoke() {
System.out.println("------------创建扑克牌.....-----------");
String[] color= {"黑桃","红桃","梅花","方块" };
String[] value= {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
for(int i=0;i<4;i++) {
for(int j=0;j<13;j++) {
pokeList.add(new Poke(color[i],i,value[j],j));
}
}
}
/*
*创建成功,输出扑克牌
*/
public void show() {
System.out.println("---------------扑克牌创建成功!!!-----------");
for(Poke string:pokeList) {
System.out.print(string.getColor()+string.getValue());
}
}
/*
* 开始洗牌,洗牌结束
*/
public void Pokeshuffle() {
System.out.println("---------------开始洗牌-------------");
Collections.shuffle(pokeList);
System.out.println("-----------洗牌结束--------------");
}
/*
* 创建玩家
*/
public void createPlayer() {
System.out.println("----------------创建玩家------------------");
int id;
String name;
Scanner scan=new Scanner(System.in);
System.out.println("有几位玩家参加?");
int num=scan.nextInt();
for(int i=0;i<num;i++) {
System.out.println("请输入第"+(i+1)+"位玩家的ID和姓名");
while(true) {
try {
System.out.println("输入ID");
id=scan.nextInt();
if(playerList.contains(id)) {
System.out.println("存在此ID,重新输入");
continue;
}
break;
}catch(Exception e) {
System.out.println("输入错误,请重新输入ID");
}
}
while(true) {
System.out.println("输入姓名");
name=scan.next();
if(playerList.contains(name)) {
System.out.println("存在此姓名,请重新输入");
continue;
}
break;
}
playerList.add(new Player(id,name));
}
scan.close();
for(Player pl:playerList) {
System.out.println("--------欢迎玩家"+pl.getName());
}
}
/*
* 发牌,拿牌
*/
public void getPoke() {
System.out.println("----------开始发牌--------");
for(int i=0,j=0;i<2;i++) {
for(Player pl:playerList) {
System.out.println("------玩家:"+pl.getName()+"---拿牌");
poker.add(pokeList.get(j));
j++;
}
}
// for(Poke pk:poker) {
// System.out.println(pk.getColor()+pk.getColornumber()+pk.getValue()+pk.getValuenumber());
// }
// System.out.println("----------------发牌结束--------------");
}
/*
* 存入玩家各自手牌
*/
public void Gamestart() {
for(int i=0,j=0;i<2;i++) {
for(Player pl:playerList) {
pl.pokerr.add(new Poker(pl.getName(),poker.get(j).getColor() , poker.get(j).getColornumber(), poker.get(j).getValue(), poker.get(j).getValuenumber()));
j++;
}
}
}
/*
* 显示玩家的手牌
*
*/
public void showpoke() {
for(Player pl:playerList) {
System.out.println(pl.getName()+":["+pl.pokerr.toString());
}
}
/*
*,开始比较手牌,提示获胜
*/
public void win() {
System.out.println("---------开始游戏---------");
List<Poker>big=new ArrayList<Poker>();
for(Player pp:playerList) {
Collections.sort(pp.pokerr);
System.out.println("玩家"+pp.getName()+"最大的手牌为"+pp.pokerr.get(0).toString());
big.add(pp.pokerr.get(0));
}
Collections.sort(big);
Poker bigPoke=big.get(0);
for(Player pl:playerList) {
if(pl.pokerr.contains(bigPoke))
System.out.println("----------玩家:"+pl.getName()+"获胜--------------");
}
System.out.println("玩家各自的手牌为");
showpoke();
}
}
package 扑克牌游戏;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Game ga=new Game();
ga.createPoke();
ga.show();
ga.Pokeshuffle();
ga.createPlayer();
ga.getPoke();
ga.Gamestart();
ga.win();
}
}