一、分析:
1.扑克牌,属性:花色、点数;实现接口Comparable的CompareTo方法,通过点数和花色List的下标来比较大小。
2.扑克牌列表:属性:扑克牌类的List;在构造函数中,使用LinkedList给属性实例化。方法:扑克牌打乱顺序使用Collections的shuffle方法。
3.玩家:属性:ID,姓名,手牌,手牌为扑克牌的集合。
4.扑克牌游戏,按照实际游戏的顺序写出。
二、代码
- 扑克牌
package com.imooc.java3.ch7x;
import java.util.Arrays;
import java.util.LinkedList;
public class Poker implements Comparable<Poker>{
private String kind;
private String point;
public Poker() {
super();
}
public Poker(String kind, String point) {
super();
this.kind = kind;
this.point = point;
}
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public String getPoint() {
return point;
}
public void setPoint(String point) {
this.point = point;
}
@Override
public String toString() {
return kind + point;
}
@Override
public int compareTo(Poker o) {
//扑克牌分为四种花色:黑桃、方块、梅花和红桃。
LinkedList<String> kinds = new LinkedList<String>();
kinds.addAll(Arrays.asList(new String[]{"黑桃","方块","梅花","红桃"}));
//扑克牌中每一组花色有十三张牌。
LinkedList<String> numbers = new LinkedList<String>();
String[] numlist = "2,3,4,5,6,7,8,9,10,J,Q,K,A".split(",");
numbers.addAll(Arrays.asList(numlist));
if(!this.getPoint().equals(o.getPoint())){
return numbers.indexOf(this.getPoint())-numbers.indexOf(o.getPoint());
}else if(!this.getKind().equals(o.getKind())){
return kinds.indexOf(this.getKind())-kinds.indexOf(o.getKind());
}
return 0;
}
}
2.扑克牌列表
package com.imooc.java3.ch7x;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.junit.Test;
public class PokerCards {
private List<Poker> cards;
public PokerCards() {
super();
this.cards = new LinkedList<Poker>();
}
public List<Poker> getCards() {
return cards;
}
@Test
public void initial(){
//扑克牌分为四种花色:黑桃、方块、梅花和红桃。
LinkedList<String> kinds = new LinkedList<String>();
kinds.addAll(Arrays.asList(new String[]{"黑桃","方块","梅花","红桃"}));
//扑克牌中每一组花色有十三张牌。
LinkedList<String> numbers = new LinkedList<String>();
String[] numlist = "2,3,4,5,6,7,8,9,10,J,Q,K,A".split(",");
numbers.addAll(Arrays.asList(numlist));
for(String kind:kinds){
for(String num:numbers){
this.cards.add(new Poker(kind,num));
}
}
}
public void showForeach(){
for(Poker pk:this.cards){
System.out.println(pk);
}
}
/**
* 洗牌
*/
@Test
public void shuffleCards(){
Collections.shuffle(this.cards);
}
}
3.玩家
package com.imooc.java3.ch7x;
import java.util.HashSet;
import java.util.Set;
public class Player {
private String id;
private String name;
private Set<Poker> cardsSet;
public Player() {
super();
this.cardsSet = new HashSet<Poker>();
}
public Player(String id, String name) {
super();
this.id = id;
this.name = name;
this.cardsSet = new HashSet<Poker>();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Poker> getCardsSet() {
return cardsSet;
}
public void setCardsSet(Set<Poker> cardsSet) {
this.cardsSet = cardsSet;
}
public void addCard(Poker poker){
this.cardsSet.add(poker);
}
public void clearCards(){
this.cardsSet.clear();
}
@Override
public String toString() {
return "Player [id=" + id + ", name=" + name + ", cardsSet=" + cardsSet + "]";
}
}
4.扑克牌游戏:
package com.imooc.java3.ch7x;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class PokerGame {
public static void main(String[] args) {
//创建一副扑克牌
PokerCards pc = new PokerCards();
System.out.println("--------创建扑克牌----------------");
pc.initial();
System.out.println("--------扑克牌创建成功!----------------");
System.out.println(pc.getCards());
//洗牌;
System.out.println("--------开始洗牌----------------");
pc.shuffleCards();
System.out.println("--------洗牌结束!----------------");
System.out.println(pc.getCards());
//创建两个玩家;
System.out.println("--------创建玩家----------------");
List<Player> pylist = new ArrayList<Player>();
Scanner sc = new Scanner(System.in);
for(int i =0;i<2;i++){
System.out.println("请输入第"+(i+1)+"位玩家的ID和姓名");
Player py = new Player();
System.out.println("输入ID:");
py.setId(sc.next());
System.out.println("输入姓名:");
py.setName(sc.next());
pylist.add(py);
System.out.println("----欢迎玩家:"+py.getName());
}
sc.close();
int count =0;
while(true){
System.out.println("------------开始发牌!------------");
//发牌:
for(int i =(count*4);i<(count+1)*4;i+=2){
System.out.println("玩家:"+pylist.get(0).getName()+"-拿牌");
pylist.get(0).addCard(pc.getCards().get(i));
System.out.println("玩家:"+pylist.get(1).getName()+"-拿牌");
pylist.get(1).addCard(pc.getCards().get(i+1));
}
System.out.println("------------发牌结束!------------");
System.out.println("------------开始游戏------------");
//给玩家的牌排序
Poker py1Card = Collections.max(pylist.get(0).getCardsSet());
System.out.println("玩家:"+pylist.get(0).getName()+"最大的手牌为:"+py1Card);
Poker py2Card = Collections.max(pylist.get(1).getCardsSet());
System.out.println("玩家:"+pylist.get(1).getName()+"最大的手牌为:"+py2Card);
int res = py1Card.compareTo(py2Card);
if(res<0){
System.out.println("------------玩家:"+pylist.get(1).getName()+"赢!!!");
}else if(res>0){
System.out.println("------------玩家:"+pylist.get(0).getName()+"赢!!!");
}else{
System.out.println("合局!!");
}
//显示玩家的牌;
System.out.println("玩家各自的手牌为:");
System.out.println(pylist.get(0).getName()+":"+pylist.get(0).getCardsSet());
System.out.println(pylist.get(1).getName()+":"+pylist.get(1).getCardsSet());
//清空玩家的数据;
pylist.get(0).clearCards();
pylist.get(1).clearCards();
count++;
if(count>=3||count*4>=52){
break;
}
}
}
}
三、运行结果