说在前头:为什么说是简易实现呢,其实所有的功能都实现了,包括用户输入id的整形及重复判断,
简易的地方是在牌的大小判断上,因为只有一副牌,所以每张牌其实都是不重复的,可以在new card的时候给上一个自增的id,根据id来判断每张牌的大小就好啦~!
有四个类:
Card:牌类
Player:玩家类
PlayCard:主类
CardComparator:牌类的临时比较规则(根据id)
注:在牌类中定义了默认比较规则compareTo,也是根据id排序
废话不多说,看代码吧
package poker;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/*
* 扑克牌类
* 属性:牌id,花色,点数
* 方法:getId,getColor,getPoint,创建牌堆,看牌,洗牌
*
* */
public class Card implements Comparable<Card>{
private String sCardColor;
private String sCardPoint;
private int iCardId;
// 静态变量num 给id赋值
private static int num = 1;
//牌堆
private java.util.List<Card> cardList = new ArrayList<Card>();
// 构造器
public Card() {
}
public Card(String color, String point) {
this.sCardColor = color;
this.sCardPoint = point;
this.iCardId = num;
num++;
}
//获取id
public int getId(){
return iCardId;
}
// 获取花色
public String getColor() {
return sCardColor;
}
// 获取点数
public String getPoint(){
return sCardPoint;
}
//获取牌堆
public List<Card> getCard(){
return cardList;
}
//创建牌堆
public java.util.List<Card> createCard(){
System.out.println("------------------初始化牌堆------------------");
String[] colors = {"方块","梅花","红心","黑桃"};
String[] points = {"2","3","4","5","6","7","8","9","10","J","Q","k","A",};
for (String point : points) {
for (String color : colors) {
Card card = new Card(color,point);
cardList.add(card);
}
}
return cardList;
}
//看牌
public void watchCard(List<Card> cardList){
int i = 0;
for (Card card : cardList) {
System.out.printf("%8s","["+card.getId()+",:"+card.getColor()+","+card.getPoint()+"]");
if ((i+1)%8==0) {
System.out.println();
}
i++;
}
}
//洗牌
public java.util.List<Card> washCard(List<Card> cardList){
Collections.shuffle(cardList);
return cardList;
}
//牌的默认比较规则:牌的id
//包装类Integer才可比较
@Override
public int compareTo(Card o) {
// TODO Auto-generated method stub
return ((Integer)this.iCardId).compareTo(((Integer)o.iCardId));
}
}
package poker;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/*
* 选手类
* 属性:选手id,选手姓名,选手手牌
* 方法:获取id,获取姓名,获取手牌,获取选手集合
* */
public class Player {
private int playerId;
private String playerName;
private List<Card> handCard;
//构造器
public Player(){}
public Player(int id,String name){
this.playerId = id;
this.playerName = name;
this.handCard = new ArrayList<Card>();
}
//获取id
public int getPlayerId(){
return playerId;
}
//获取姓名
public String getPlayerName(){
return playerName;
}
//获取手牌
public List<Card> getHandCard(){
return handCard;
}
//查看手牌
public void watchHandCard(){
for (Card card : handCard) {
System.out.println("["+card.getId()+","+card.getColor()+","+card.getPoint()+"]");
}
}
}
package poker;
import java.io.Console;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.concurrent.SynchronousQueue;
public class PlayGame {
//玩家ID列表
private List<Integer> playerIdList=new ArrayList<Integer>();
public static void main(String[] args) {
// TODO Auto-generated method stub
//1.创建两位玩家
PlayGame playGame = new PlayGame();
Player player1 = playGame.createPlayer();
Player player2 = playGame.createPlayer();
Card card = new Card();
//2.创建牌堆
List<Card> cardList = card.createCard();
//看牌
card.watchCard(cardList);
//3.洗牌
System.out.println();
System.out.println("------------------洗牌------------------");
card.washCard(cardList);
card.watchCard(cardList);
System.out.println();
System.out.println("------------------洗牌完毕------------------");
System.out.println("------------------选手抽牌------------------");
//4.玩家1、2 抽取手牌
for (int i = 0; i < 4; i++) {
//要抽取的手牌
Card handCard = cardList.get(i);
//玩家2抽取偶数倍的牌 如:牌2、牌4
if ((i+1)%2==0) {
player2.getHandCard().add(handCard);
}else{
//玩家2抽取奇数倍的牌 如:牌1、牌3
player1.getHandCard().add(handCard);
}
}
//5.查看手牌
System.out.println("------------------玩家1手牌------------------");
//调用临时比较规则CardComparator,会根据牌的id进行排序
Collections.sort(player1.getHandCard(),new CardComparator());
player1.watchHandCard();
System.out.println("------------------玩家2手牌------------------");
Collections.sort(player2.getHandCard(),new CardComparator());
player2.watchHandCard();
System.out.println("------------------抽牌完毕------------------");
//6.两位玩家选出各自最大手牌,比较大小
//因为已经排序好了,所以最大的牌都是get(1)位置的
Card maxCard1 = player1.getHandCard().get(1);//玩家1最大手牌
System.out.println("玩家1的最大手牌为:["+maxCard1.getId()+","+maxCard1.getColor()+","+maxCard1.getPoint()+"]");
Card maxCard2 = player2.getHandCard().get(1);//玩家2最大手牌
System.out.println("玩家2的最大手牌为:["+maxCard2.getId()+","+maxCard2.getColor()+","+maxCard2.getPoint()+"]");
//比较
if (maxCard1.getId()>maxCard2.getId()) {
System.out.println("玩家"+player1.getPlayerName()+"获胜!");
}else{
System.out.println("玩家"+player2.getPlayerName()+"获胜!");
}
}
public Player createPlayer(){
/*1.初始化id,将初始化id放在while循环外面是因为while循环里只负责id的判断
2.但是实例化player对象时,同时需要id和name,而name是在while循环外的,所以int也要在循环体外,方便访问
*/
int id = 0;
while(true){
Scanner console = new Scanner(System.in);
try {
System.out.println("请输入用户id,必须为整数");
id = (int)console.nextInt();
//先判断id是否已经存在,如果存在,继续循环让用户重新输入id
if (playerIdList.contains(id)) {
System.out.println("用户名重复!");
continue;
}else{
//若不存在,将id加入playerIdList,并跳出循环
playerIdList.add(id);
break;
}
} catch (Exception e) {
// TODO: handle exception
System.out.println("用户id必须为整数!");
continue;
}
}
Scanner console = new Scanner(System.in);
System.out.println("请输入用户名");
String name = console.next();
System.out.println("创建成功!ID:"+id+",姓名:"+name);
Player player = new Player(id,name);
//返回创建的对象
return player;
}
}
package poker;
import java.util.Comparator;
public class CardComparator implements Comparator<Card>{
@Override
public int compare(Card o1, Card o2) {
// TODO Auto-generated method stub
return ((Integer)o1.getId()).compareTo(((Integer)o2.getId()));
}
}