定义一个Poker扑克类,属性为color花色和point点数,并且重新定义了compareTo()比较方法
package com.pokergame;
import java.util.Collection;
/**
* 简易扑克游戏
* 扑克类
* @author tengyu
*
*/
public class Poker implements Comparable<Poker>{
private String color;//扑克花色
private String point;//扑克点数
public Poker(){};
public Poker(String color, String point){
this.color = color;
this.point = point;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getPoint() {
return point;
}
public void setPoint(String point) {
this.point = point;
}
@Override
//自定义比较方法
public int compareTo(Poker o) {
// TODO 自动生成的方法存根
public int compareTo(Poker o) {
// TODO 自动生成的方法存根
if(new Game().points.indexOf(this.point) == new Game().points.indexOf(o.point)){
return Integer.valueOf(new Game().colors.indexOf(this.color)).
compareTo(Integer.valueOf(new Game().colors.indexOf(o.color)));
}else{
return Integer.valueOf(new Game().points.indexOf(this.point)).
compareTo(Integer.valueOf(new Game().points.indexOf(o.point)));
}
}
}
定义一个Person玩家类,属性为sequencenumber玩家序号,name玩家姓名和一个handList的手牌集合
package com.pokergame;
import java.util.ArrayList;
import java.util.List;
/**
* 简易扑克游戏
* 玩家类
* @author tengyu
*
*/
public class Person {
private int sequencenumber;//玩家序号
private String name;//玩家姓名
private List<Poker> handList ;
public Person(){};
public Person(int sequencenumber, String name){
this.sequencenumber = sequencenumber;
this.name = name;
this.handList = new ArrayList<Poker>();
}
public int getSequencenumber() {
return sequencenumber;
}
public void setSequencenumber(int sequencenumber) {
this.sequencenumber = sequencenumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Poker> getHandList() {
return handList;
}
public void setHandList(List<Poker> handList) {
this.handList = handList;
}
}
最后一个Game游戏类也是程序入口,属性为pokerList的扑克集合,personList玩家集合,colors花色集合和points的点数集合。有三个方法,分别为addPoker()添加扑克,addPerson()添加玩家和gaming()发牌并且比较点数
package com.pokergame;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
/**
* 简易扑克游戏
* 游戏类
* @author tengyu
*
*/
public class Game {
List<Poker> pokerList = new ArrayList<Poker>();//扑克的集合
List<Person> personList = new ArrayList<Person>();//玩家的集合
List<String> colors = Arrays.asList("", "", "", "");//花色的集合
List<String> points = Arrays.asList("2", "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "k", "A");//点数的集合
//添加扑克
public void addPoker(){
System.out.println("--------------扑克牌创建中..----------------");
for(int i = 0; i < colors.size(); i++){
for(int j = 0; j < points.size(); j++){
pokerList.add(new Poker((String)colors.get(i), (String)points.get(j)));
}
}
System.out.println("--------------扑克牌创建成功~----------------");
for(Poker poker : pokerList){
System.out.print(poker.getColor() + poker.getPoint() + " ");
}
System.out.println("\n-----------------开始洗牌~-----------------");
Collections.shuffle(pokerList);//使用工具类Collections的suffle()方法对pokerList集合进行随机排序
System.out.println("-----------------洗牌结束~-----------------");
}
//添加玩家
public void addPerson(){
int sn = 0;
String name = null;
System.out.println("-----------------添加玩家~-----------------");
while(true){
try{
//添加第一位玩家
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一位玩家的ID和姓名:");
System.out.println("输入ID:");
sn = sc.nextInt();
System.out.println("输入姓名:");
name = sc.next();
personList.add(new Person(sn , name));
//添加第二位玩家
System.out.println("请输入第二位玩家的ID和姓名:");
System.out.println("输入ID:");
sn = sc.nextInt();
System.out.println("输入姓名:");
name = sc.next();
personList.add(new Person(sn , name));
sc.close();
break;
}catch (InputMismatchException e){
System.out.println("序号请输入正整数");
}
}
for (Person person : personList) {
System.out.println("------欢迎玩家:" + person.getName());
}
}
//发牌并且比较点数
public void gaming(){
System.out.println("-----------------开始发牌~-----------------");
for(int i = 0; i < 4; i++){
System.out.println("------玩家:" + personList.get(0).getName() + "--拿牌");
personList.get(0).getHandList().add(pokerList.get(i));
i++;
System.out.println("------玩家:" + personList.get(1).getName() + "--拿牌");
personList.get(1).getHandList().add(pokerList.get(i));
}
Poker max = new Poker();
int winner = 0;
for(int i = 0; i < 2; i++){
Collections.sort(personList.get(i).getHandList());
Collections.reverse(personList.get(i).getHandList());
Poker maxpoker = personList.get(i).getHandList().get(0);
System.out.println("玩家:" + personList.get(i).getName() + "最大手牌为:"
+ maxpoker.getColor() + maxpoker.getPoint());
if(max.compareTo(maxpoker) < 0){
max = maxpoker;
winner = i;
}
}
System.out.println("--------玩家" + personList.get(winner).getName() +
"获胜--------");
System.out.println("玩家各自的手牌为:");
for(int i = 0; i < 2; i++){
System.out.print("玩家:" + personList.get(i).getName() + "手牌为:");
for(int j = 0; j < 2; j++){
System.out.print(personList.get(i).getHandList().get(j).getColor()
+ personList.get(i).getHandList().get(j).getPoint()
+ " " );
}
System.out.println("");
}
}
public static void main(String[] args){
Game game = new Game();
game.addPoker();
game.addPerson();
game.gaming();
}
}
运行结果
--------------扑克牌创建中..----------------
--------------扑克牌创建成功~----------------
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 A
-----------------开始洗牌~-----------------
-----------------洗牌结束~-----------------
-----------------添加玩家~-----------------
请输入第一位玩家的ID和姓名:
输入ID:
1
输入姓名:
Tom
请输入第二位玩家的ID和姓名:
输入ID:
2
输入姓名:
Jack
------欢迎玩家:Tom
------欢迎玩家:Jack
-----------------开始发牌~-----------------
------玩家:Tom--拿牌
------玩家:Jack--拿牌
------玩家:Tom--拿牌
------玩家:Jack--拿牌
玩家:Tom最大手牌为:6
玩家:Jack最大手牌为:J
--------玩家Jack获胜--------
玩家各自的手牌为:
玩家:Tom手牌为:6 4
玩家:Jack手牌为:J J