动手实践才是真理,所以想着把自己写的贴上来,这样监督自己动手,同时也希望能跟大家一起探讨探讨。
从下午开始,断断续续写到了晚上。中间因为扑克比较大小不会,在网上进行了搜索,询问了别人,最终是参考了一位慕课网的朋友的程序。
功能主要在三个java中实现。分别为Player类、Card类以及Test类。
其中,Player类主要定义了玩家的属性,包括玩家的手牌,以及对这些属性的操作方法。最后,还定义了一个在两张手牌的情况下找出最大牌的bigestCard方法;
Card类定义了扑克的两个属性,以及初始化方法,toString方法,还有就是两张扑克比较大小的方法。为测试类的实现打好了基础。
测试类中则主要实现了该游戏界面的实现等。
比较大的遗憾是,老师提到的ID输入异常的处理没有实现。也希望以后技能精金后能再来处理一下。
下面贴一下代码。
首先是Player类
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Player {
private int id;
private String name;
private Set<Card> shouPai;
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 Set<Card> getShouPai() {
return shouPai;
}
public void setShouPai(Set<Card> shouPai, Card pk) {
shouPai.add(pk);
// System.out.println();
}
public Player(int iD, String name) {
this.id = iD;
this.name = name;
this.shouPai = new HashSet<Card>();
}
public Card bigestCard() {
Iterator<Card> it = shouPai.iterator();
Card c1 = it.next();
Card c2 = it.next();
int i = c1.compareTo(c2);
if (i > 0) {
System.out.println("玩家:" + name + "最大的手牌为:" + c1);
return c1;
} else {
System.out.println("玩家:" + name + "最大的手牌为:" + c2);
return c2;
}
}
}
下面是Card类
public class Card implements Comparable<Card> {
private String huase;
private String shuzi;
public Card(String huase, String shuzi) {
this.huase = huase;
this.shuzi = shuzi;
}
@Override
public String toString() {
String ss = huase + shuzi;
return ss;
}
@Override
public int compareTo(Card c) {
String[] s1 = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q",
"K", "A" };
String[] s2 = { "方片", "梅花", "黑桃", "红桃" };
/*
* for (String string : s1) { if(this.shuzi.equals(string)); }
*/
int thisNum = 0;
int cNum = 0;
for (int i = 0; i < 13; i++) {
if (this.shuzi.equals(s1[i])) {
thisNum = i;
break;
}
}
for (int i = 0; i < 13; i++) {
if (c.shuzi.equals(s1[i])) {
cNum = i;
break;
}
}
if(thisNum > cNum){
return 1;
}else if(thisNum < cNum){
return -1;
}else{
int thisHuaSe = 0;
int cHuaSe = 0;
for (int i = 0; i < 4; i++) {
if (this.huase.equals(s2[i])) {
thisHuaSe = i;
break;
}
}
for (int i = 0; i < 4; i++) {
if (c.huase.equals(s2[i])) {
cHuaSe = i;
break;
}
}
if(thisHuaSe > cHuaSe){
return 1;
}else if(thisHuaSe < cHuaSe){
return -1;
}else{
return 0;
}
}
}
}
最后是Test类
import java.util.Random;
import java.util.Scanner;
public class Test {
public Card[] creatPuke() {
Card[] cards = new Card[52];
String[] huase = { "方片", "梅花", "红桃", "黑桃" };
String[] shuzi = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J",
"Q", "K", "A" };
for (int i = 0; i < 52; i++) {
cards[i] = new Card(huase[i / 13], shuzi[i % 13]);
}
return cards;
}
public void printPuke(Card[] puke) {
System.out.print("创建的扑克牌为:[");
for (Card card : puke) {
System.out.print(card.toString() + ",");
}
System.out.println("]");
}
public void xiPai(Card[] puke) {
Random rd = new Random();
for (int i = 0; i < 52; i++) {
int newNum = rd.nextInt(52);
Card tmp = puke[newNum];
puke[newNum] = puke[i];
puke[i] = tmp;
}
}
public static void main(String[] args) {
Test t = new Test();
System.out.println("---------------创建扑克牌-------------------------");
Card[] newPuke = t.creatPuke();
System.out.println("---------------创建扑克牌成功!-------------------------");
t.printPuke(newPuke);
System.out.println("---------------开始洗牌-------------------------");
t.xiPai(newPuke);
System.out.println("---------------洗牌结束!-------------------------");
t.printPuke(newPuke);
System.out.println("---------------创建玩家-------------------------");
int[] ID = new int[2];
String[] Name = new String[2];
Player[] pl = new Player[2];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 2; i++) {
System.out.println("请输入第1位玩家的ID和姓名:");
System.out.println("输入ID:");
int id = sc.nextInt();
ID[i] = id;
System.out.println("输入姓名:");
Name[i] = sc.next();
pl[i] = new Player(ID[i], Name[i]);
}
sc.close();
for (Player player : pl) {
System.out.println("---------欢迎玩家:" + player.getName());
}
System.out.println("---------------开始发牌-------------------------");
for (int i = 0; i < 4; i++) {
if (i % 2 == 0) {
pl[0].setShouPai(pl[0].getShouPai(), newPuke[i]);
System.out.println("玩家:" + pl[0].getName() + "-拿到牌");
} else {
pl[1].setShouPai(pl[1].getShouPai(), newPuke[i]);
System.out.println("玩家:" + pl[1].getName() + "-拿到牌");
}
}
System.out.println("---------------发牌结束!-------------------------");
System.out.println("---------------开始游戏-------------------------");
Card[] bigestCard = { null, null };
for (int i = 0; i < 2; i++) {
bigestCard[i] = pl[i].bigestCard();
}
int max = bigestCard[0].compareTo(bigestCard[1]);
if (max > 0) {
System.out.println("---------------玩家:" + pl[0].getName()
+ "获胜!-------------------------");
} else {
System.out.println("---------------玩家:" + pl[1].getName()
+ "获胜!-------------------------");
}
System.out.println("玩家各自的手牌为:");
for (Player player : pl) {
System.out.println(player.getName() + ":" + player.getShouPai());
}
}
}
热门评论
大概看了下你代码中comparable的实现,和排序的实现。
你这个排序的方法,给数组中元素相互调换值也是蛮特别的,哈。另外compareTo()方法实在没信心看下去,但是如果你为Card这个类,实现了compareTo方法,那么可以直接使用sort()方法对玩家的手牌排序,没必要再用迭代器获得值进行比较哦!