/*
* 创建扑克类
*/
public class Card {
private String color;
private String num;
public Card(String color, String num) {
this.color = color;
this.num = num;
}
public Card() {
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
}
/*
* 创建玩家类
*/
public class Player {
private String name;
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
/*
*
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class CardList {
public List<Card> cards;
public List<Player> players;
public CardList() {
this.cards = new ArrayList<Card>();
this.players = new ArrayList<Player>();
}
/*
* 创建玩家p1、p2并加入到 players List中去
*/
public void addPlayer() {
System.out.println("------开始创建玩家-------");
Scanner sc = new Scanner(System.in);
try {
System.out.println("请输入第一位玩家ID:");
int id1 = sc.nextInt();
System.out.println("请输入第一位玩家姓名:");
String name1 = sc.next();
Player p1 = new Player();
p1.setId(id1);
p1.setName(name1);
players.add(p1);
System.out.println("第一位玩家:" + p1.getName() + "创建完成!");
System.out.println("请输入第二位玩家ID:");
int id2 = sc.nextInt();
System.out.println("请输入第二位玩家姓名:");
String name2 = sc.next();
Player p2 = new Player();
p2.setId(id2);
p2.setName(name2);
players.add(p2);
System.out.println("第二位玩家" + p2.getName() + "创建完成!");
} catch (Exception e) {
System.err.println("请输入整数!");
addPlayer();
}
}
/*
* 通过双for循环遍历实现两位玩家依次拿两张牌
*/
public void deal() {
System.out.println("-------开始发牌--------");
for (int j = 0; j < 2; j++) {
for (int i = 0; i < 2; i++) {
Player p = (Player) players.get(i);
System.out.println("玩家:" + p.getName() + "拿牌");
}
}
System.out.println("-------发牌结束--------");
}
/*
* 创建扑克牌的cards List;
*
*/
public void addCard() {
String[] colors = { "黑桃", "红桃", "梅花", "方块" };
String[] nums = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
for (int i = 0; i < colors.length; i++) {
for (int j = 0; j < nums.length; j++) {
cards.add(new Card(colors[i], nums[j]));
}
}
}
/*
* for循环遍历cards获得其中的每一个元素
*/
public void forGet() {
System.out.println("------扑克牌创建成功------");
for (int i = 0; i < cards.size(); i++) {
Card c = (Card) cards.get(i);
System.out.print(c.getColor() + c.getNum() + " ");
}
System.out.println("");
}
/*
* 通过Collections中的shuffle方法打乱cards中的元素顺序实现洗牌
*/
public void shuffle() {
System.out.println("-------开始洗牌--------");
Collections.shuffle(cards);
System.out.println("-------洗牌结束--------");
}
/*
* 首先获取打乱后的牌库中的前四张牌,玩家1获得第一和第三张牌,玩家2获得第二和第四张牌;
* 创建花色数组和牌的点数数组;
* 通过获得玩家手牌中的花色和点数分别在花色数组和点数数组中的索引位置来判断牌与牌之间的大小
*/
public void get() {
Card temp1 = new Card();
Card temp2 = new Card();
Player p1 = (Player) players.get(0);
Player p2 = (Player) players.get(1);
int k1 = 0, k2 = 0, k3 = 0, k4 = 0;
int l1 = 0, l2 = 0, l3 = 0, l4 = 0;
Card card1 = (Card) cards.get(0);
Card card2 = (Card) cards.get(1);
Card card3 = (Card) cards.get(2);
Card card4 = (Card) cards.get(3);
String[] colors = { "黑桃", "红桃", "梅花", "方块" };
String[] nums = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
for (int i1 = 0; i1 < colors.length; i1++) {
if (card1.getColor().equals(colors[i1])) {
k1 = i1;
break;
}
}
for (int j1 = 0; j1 < nums.length; j1++) {
if (card1.getNum().equals(nums[j1])) {
l1 = j1;
break;
}
}
for (int i2 = 0; i2 < colors.length; i2++) {
if (card2.getColor().equals(colors[i2])) {
k2 = i2;
break;
}
}
for (int j2 = 0; j2 < nums.length; j2++) {
if (card2.getNum().equals(nums[j2])) {
l2 = j2;
break;
}
}
for (int i3 = 0; i3 < colors.length; i3++) {
if (card3.getColor().equals(colors[i3])) {
k3 = i3;
break;
}
}
for (int j3 = 0; j3 < nums.length; j3++) {
if (card3.getNum().equals(nums[j3])) {
l3 = j3;
break;
}
}
for (int i4 = 0; i4 < colors.length; i4++) {
if (card4.getColor().equals(colors[i4])) {
k4 = i4;
break;
}
}
for (int j4 = 0; j4 < nums.length; j4++) {
if (card4.getNum().equals(nums[j4])) {
l4 = j4;
break;
}
}
if (k1 == k3) {
if (l1 > l3) {
System.out.println("玩家:" + p1.getName() + "最大手牌为" + card1.getColor() + card1.getNum() );
temp1 = card1;
} else if(l1<l3) {
System.out.println("玩家:" + p1.getName() + "最大手牌为" + card3.getColor() + card3.getNum());
temp1 = card3;
}else if(l1 == l3){
System.out.println("玩家:" + p1.getName() + "最大手牌为" + card1.getColor() + card1.getNum() );
temp1 = card1;
}
} else if (k1 > k3) {
System.out.println("玩家:" + p1.getName() + "最大手牌为" + card3.getColor() + card3.getNum());
temp1 = card3;
} else if(k1 < k3){
System.out.println("玩家:" + p1.getName() + "最大手牌为" + card1.getColor() + card1.getNum());
temp1 = card1;
}
if (k2 == k4) {
if (l2 > l4) {
System.out.println("玩家:" + p2.getName() + "最大手牌为" + card2.getColor() + card2.getNum());
temp2 = card2;
} else if(l2< l4) {
System.out.println("玩家:" + p2.getName() + "最大手牌为" + card4.getColor() + card4.getNum());
temp2 = card4;
}else if(l2 == l4){
System.out.println("玩家:" + p2.getName() + "最大手牌为" + card2.getColor() + card2.getNum());
temp2 = card2;
}
} else if (k2 > k4) {
System.out.println("玩家:" + p2.getName() + "最大手牌为" + card4.getColor() + card4.getNum());
temp2 = card4;
} else if(k2<k4){
System.out.println("玩家:" + p2.getName() + "最大手牌为" + card2.getColor() + card2.getNum());
temp2 = card2;
}
/*
* 花色m 點數n;
*/
int m1 = 0, n1 = 0;
int m2 = 0, n2 = 0;
for (int i1 = 0; i1 < colors.length; i1++) {
if (temp1.getColor().equals(colors[i1])) {
m1 = i1;
break;
}
}
for (int j1 = 0; j1 < nums.length; j1++) {
if (temp1.getNum().equals(nums[j1])) {
n1 = j1;
break;
}
}
for (int i2 = 0; i2 < colors.length; i2++) {
if (temp2.getColor().equals(colors[i2])) {
m2 = i2;
break;
}
}
for (int j2 = 0; j2 < nums.length; j2++) {
if (temp2.getNum().equals(nums[j2])) {
n2 = j2;
break;
}
}
if (m1 == m2) {
if (n1 > n2) {
System.out.println(" ------玩家" + p1.getName() + "获胜!------" );
} else if(n1 < n2) {
System.out.println(" ------玩家" + p2.getName() + "获胜!------" );
} else if(n1 == n2){
System.out.println(" 太巧了两位玩家最大牌一样大!" +n1+" "+n2);
}
} else if (m1 < m2) {
System.out.println("------玩家" + p1.getName() + "获胜!------" );
} else if (m1 > m2) {
System.out.println("------玩家" + p2.getName() + "获胜!------" );
}
System.out.println("两位玩家各自手牌为:");
System.out.println(p1.getName()+":["+card1.getColor()+card1.getNum()+","
+card3.getColor()+card3.getNum()+"]");
System.out.println(p2.getName()+":["+card2.getColor()+card2.getNum()+","
+card4.getColor()+card4.getNum()+"]");
}
}
/*
* 游戏测试类
*/
public class GameTest {
public static void main(String[] args) {
CardList cl = new CardList();
Player p = new Player();
cl.addCard();
cl.forGet();
cl.shuffle();
cl.addPlayer();
cl.deal();
cl.get();
}
}