package com.fudong;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Card {
private int id; //牌号
private String point; //点数
private String variety; //花色
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPoint() {
return point;
}
public void setPoint(String point) {
this.point = point;
}
public String getVariety() {
return variety;
}
public void setVariety(String variety) {
this.variety = variety;
}
public Card() {
super();
}
public Card(int id, String point, String variety) {
super();
this.id = id;
this.point = point;
this.variety = variety;
}
/*
* 初始化扑克牌,返回的原始序列为方块2,方块3....方块A,梅花2,梅花3....黑桃A
*
*/
public static List<Card> initCard() {
List<Card> cList = new ArrayList<Card>();
for (int j = 0; j < 4; j++) {
/*
* 设置花色
*/
String cvariety = null;
if (j==0) {
cvariety = "方块";
}
if (j==1) {
cvariety = "梅花";
}
if (j==2) {
cvariety = "红桃";
}
if (j==3) {
cvariety = "黑桃";
}
for (int i = 0; i < 13; i++) {
/*
* 设置点数,从2,3,,4-----J,Q,K,A
*/
String apoint = String.valueOf(i+2);
if (i== 9) {
apoint = "J";
}
if (i== 10) {
apoint = "Q";
}
if (i== 11) {
apoint = "K";
}
if (i== 12) {
apoint = "A";
}
cList.add(new Card(j+i*4, apoint, cvariety));
/*
* J+i*4表示牌号的记录顺序为方块2<梅花2<红桃2<黑桃2<方块3<梅花3<红桃3<黑桃3...<黑桃A
*/
}
}
return cList;
}
/*
* 洗牌(打乱牌的顺序)
*/
public static void shuffle(List<Card> list) {
Random random =new Random();
for (int i = 0; i < list.size(); i++) {
int ran = random.nextInt(list.size());
Card temp = list.get(i);
list.set(i, list.get(ran));
list.set(ran, temp);
}
}
/*
* 比较手牌规则:
* 先比大小,再比花色
* (在初始化时,所有的牌号记录 都按照2(方块<梅花<红桃<黑桃)<3(方块<梅花<红桃<黑桃)<4<5....<10<J<Q<k顺序排序,
* 所以牌的大小可以通过牌号比较)
* 另外,"A"为点数1)
* 返回1,则card1>card2
* 返回0,则card1==card2
* 返回-1,则card1<card2
*/
public static int comparaCard(Card card1, Card card2){
if (card1.getPoint()!= "A" && card2.getPoint()!="A") {
if (card1.getId()>card2.getId()) {
return 1;
}else if (card1.getId()==card2.getId()) {
return 0;
}else {
return -1;
}
}
//两牌中为点数为"A"的牌数最小,此时"A"代表点1
else if (card1.getPoint()== "A" && card2.getPoint()=="A") {
return 0;
}
else{
return (card1.getPoint()=="A")?-1:1;
}
}
}
package com.fudong;
import java.util.ArrayList;
import java.util.List;
public class Player {
private int id;
private String name;
public List<Card> myCards;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Player(int id, String name) {
super();
this.id = id;
this.name = name;
}
public Player() {
super();
}
/*
* 发牌
*/
public static void deal(List<Card> cList, List<Player> pList) {
//玩家个数
int pnum = pList.size();
//初始化玩家手牌
for (int j = 0; j < pnum; j++) {
pList.get(j).myCards = new ArrayList<>();
}
//玩家得牌
for (int i = 0; cList.get(i)!=null && i< pnum*2; i++) {
pList.get(i%pnum).myCards.add(cList.get(i));
System.out.println(
"--------玩家:"+pList.get(i%pnum).getName()+"-拿牌");
}
}
/*
* 玩家各自的手牌
*/
public static void handCard(List<Player> pList){
System.out.println("玩家各自手牌为:");
for (int i = 0; i < pList.size(); i++) {
//用card记录玩家的手牌
String card = "[";
for (int j = 0; j < pList.get(i).myCards.size(); j++) {
if ( j==pList.get(i).myCards.size()-1) {
card = card+pList.get(i).myCards.get(j).getVariety()
+pList.get(i).myCards.get(j).getPoint();
}else {
card = card+pList.get(i).myCards.get(j).getVariety()
+pList.get(i).myCards.get(j).getPoint()+",";
}
}
card = card + "]";
//打印玩家手牌
System.out.println(pList.get(i).getName()+":"+card);
}
}
/*
* 最大的手牌
*/
public static Card handMax(List<Card> cList){
Card maxcard = cList.get(0);
for (int i = 1; i < cList.size(); i++) {
if (Card.comparaCard(maxcard, cList.get(i))<=1) {
maxcard = cList.get(i);
}
}
return maxcard;
}
}
package com.fudong;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Game {
public static void main(String[] args) {
System.out.println("----------------创建扑克牌-------------");
List<Card> cList = Card.initCard();
System.out.println("----------------扑克牌创建成功-------------");
System.out.print("[ ");
for (Card card : cList) {
if (card.getPoint().equals("A")&&card.getVariety().equals("黑桃")) {
System.out.print(card.getVariety()+card.getPoint());
}
else {
System.out.print(card.getVariety() + card.getPoint() + ",");
}
}
System.out.println("]");
System.out.println("----------------开始洗牌-------------");
Card.shuffle(cList);
// System.out.print("[ ");
// for (Card card : cList) {
// if (card.getPoint().equals("A")&&card.getVariety().equals("黑桃")) {
// System.out.print(card.getVariety()+card.getPoint());
// }
// else {
// System.out.print(card.getVariety() + card.getPoint() + ",");
// }
// }
// System.out.println("]");
System.out.println("----------------洗牌结束-------------");
System.out.println("----------------创建玩家-------------");
List<Player> pList = new ArrayList<>();
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 2; ) {
System.out.println("请输入第"+(i+1)+"位玩家的ID和姓名:");
System.out.println("请输入ID:");
String ID = scanner.nextLine();
if (!ID.matches("^[0-9]+$") ID==null) {
System.out.println("请重新输入整数ID!");
continue;
}
Player player = new Player();
player.setId(Integer.parseInt(ID));
System.out.println("请输入玩家姓名:");
String name = scanner.nextLine();
player.setName(name);
pList.add(player);
i++;
// System.out.println("请输入第2位玩家的ID和姓名:");
}
System.out.println("--------欢迎玩家:"+pList.get(0).getName());
System.out.println("--------欢迎玩家:"+pList.get(1).getName());
System.out.println("-------------------开始发牌---------------");
Player.deal(cList, pList);
System.out.println("-------------------发牌结束---------------");
for (int i = 0; i < pList.size(); i++) {
Card card = Player.handMax(pList.get(i).myCards);
System.out.println("------"+pList.get(i).getName()+"最大的手牌:"
+card.getVariety()+card.getPoint());
}
int record = Card.comparaCard(Player.handMax(pList.get(0).myCards)
, Player.handMax(pList.get(1).myCards));
if (record==1) {
System.out.println("-----------玩家"+pList.get(0).getName()+"获胜");
} else{
System.out.println("-----------玩家"+pList.get(1).getName()+"获胜");
}
Player.handCard(pList);
}
}