慕粉4078529
2016-12-15 20:54
//创建卡牌对象类 public class Card implements Comparable<Card> { private String shape; private String id; @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Card other = (Card) obj; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; return true; } public Card(){ } public Card(String shape, String id) { //super(); this.shape = shape; this.id = id; } public String getShape() { return shape; } public void setShape(String shape) { this.shape = shape; } public String getId() { return id; } public void setId(String id) { this.id = id; } @Override//重写了卡牌中花色与序号的排序方法 public int compareTo(Card o) { // TODO Auto-generated method stub //return o.getId().compareTo(this.getId()); String[] num = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"}; String[] color = {"黑桃","红桃","梅花","方片"}; int n1 = 0,n2=0,o1=0,o2=0; for(int i=0;i<num.length;i++){ if(this.getId().equals(num[i])) n1=i; if(o.getId().equals(num[i])) n2=i;} for(int i=0;i<color.length;i++){ if(this.getShape().equals(color[i])) o1=i; if(o.getShape().equals(color[i])) o2=i;} if(n1>n2){ return 1;} else if(n1<n2){ return -1; }else{ if(o1>o2){ return -1; }else if(o1<o2){ return 1; }else return 0; } } public Card getCards() { // TODO Auto-generated method stub return null; } } import java.util.ArrayList; import java.util.List; //创建玩家类 public class Player { private String name ; private List<Card> cards; private String ID; public void Player(){ } public List<Card> getCards() { return cards; } public Player(String name, String iD) { //super(); this.name = name; this.cards = new ArrayList<Card>(); ID = iD; } public void setCards(List<Card> cards) { this.cards = cards; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getID() { return ID; } public void setID(String iD) { ID = iD; } } import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class PaiTest { public List<Card> cardList; public PaiTest(){ this.cardList = new ArrayList<Card>(); } //创建扑克牌的方法 public void purcard(){ String[] sh= {"方片","黑桃","红桃","梅花"}; Card cars = new Card(); //第一个循环是创建4个花色,第二个循环创建每种花色的大小 for(int i=0;i<4;i++){ cars.setShape(sh[i]); for( int j=2;j<15;j++){ if(j==11){ cars.setId("J"); cardList.add(new Card(sh[i],"J")); System.out.println("添加了卡牌:"+cars.getShape()+cars.getId()); continue; } if(j==12){ cars.setId("Q"); cardList.add(new Card(sh[i],"Q")); System.out.println("添加了卡牌:"+cars.getShape()+cars.getId()); continue; } if(j==13){ cars.setId("K"); cardList.add(new Card(sh[i],"K")); System.out.println("添加了卡牌:"+cars.getShape()+cars.getId()); continue; } if(j==14){ cars.setId("A"); cardList.add(new Card(sh[i],"A")); System.out.println("添加了卡牌:"+cars.getShape()+cars.getId()); continue; } cars.setId(j+""); System.out.println("添加了卡牌:"+cars.getShape()+cars.getId()); cardList.add(new Card(sh[i],j+"")); } } System.out.println("卡牌创建成功--------------------"); for(Card card:cardList) System.out.println("输出卡牌:"+cardList.size()+card.getShape()+" "+card.getId()); } public void fapai(){ Scanner sc = new Scanner(System.in); System.out.println("请输入玩家一的姓名:"); String p1 = sc.next(); Player play1 = new Player(p1, "1"); System.out.println("玩家:"+p1+"成功进入游戏"); System.out.println("请输入玩家二的姓名:"); String p2 = sc.next(); Player play2 = new Player(p2, "2"); System.out.println("玩家:"+p2+"成功进入游戏"); Collections.shuffle(cardList);//打乱集合中的元素 System.out.println(cardList.get(0).getShape()+cardList.get(0).getId());//打乱集合输出的第一个元素 play1.getCards().add(cardList.get(0));//依次给玩家发牌 play2.getCards().add(cardList.get(1)); play1.getCards().add(cardList.get(2)); play2.getCards().add(cardList.get(3)); Collections.sort(play1.getCards());//对玩家1的手牌排序 Collections.sort(play2.getCards());//对玩家2的手牌排序 for(Card card:play1.getCards()) System.out.println("玩家1的卡牌为:"+card.getShape()+card.getId()); System.out.println("最大点数为:"+play1.getCards().get(1).getShape()+play1.getCards().get(1).getId()); for(Card card:play2.getCards()) System.out.println("玩家2的卡牌为:"+card.getShape()+card.getId()); System.out.println("最大点数为:"+play2.getCards().get(1).getShape()+play2.getCards().get(1).getId()); //利用comparable排序 List<Card> newlist = new ArrayList<Card>();//创建新的集合,把玩家1和2的手牌放入 newlist.addAll( play1.getCards()); newlist.addAll( play2.getCards()); System.out.println("-----------------排序前:"); for(Card st:newlist) System.out.println("排序前:"+st.getShape()+st.getId()); //Collections.sort(newlist, new PaiComparator()); Collections.sort(newlist); System.out.println("-----------------排序后:"); for(Card st:newlist) System.out.println(st.getShape()+st.getId()); //这里用equals方法记得要对卡片对象Card重写 if(newlist.get(3).equals(play1.getCards())){ System.out.println(" 玩家: "+play1.getName()+"获胜!!!手牌为:"+newlist.get(3).getShape()+newlist.get(3).getId()); }else System.out.println(" 玩家: "+play2.getName()+"获胜!!!手牌为:"+newlist.get(3).getShape()+newlist.get(3).getId()); } public static void main(String[] args) { PaiTest pt = new PaiTest(); pt.purcard();//创建扑克牌 pt.fapai();//游戏过程 } }
package zc20170130;
/**
* 创建扑克牌类
* 颜色
* 点数
* @author Administrator
*
*/
public class Card {
private String color;
private String number;
public Card(String color, String number) {
this.color = color;
this.number = number;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
package zc20170130;
import java.util.*;
/**
* 玩家类、id,name,手牌
* @author Administrator
*
*/
public class Player {
private int id;
private String name;
private ArrayList<Card> cards = new ArrayList<Card>();
public Player(){
}
public Player(int id, String name, ArrayList<Card> card) {
this.id = id;
this.name = name;
this.cards = card;
}
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 ArrayList<Card> getCard() {
return cards;
}
public void addCard(Card card) {
cards.add(card);
}
}
package zc20170130;
import java.util.*;
public class GameRule {
/**
* 随机生成一副扑克牌 发牌 判断胜负
*/
private static ArrayList<Card> cards = new ArrayList<Card>(); // 一副牌2-10 J Q
public ArrayList<Card> getCards() {
return cards;
}
private void generateOneColor(String color) {// 生成单一色牌
Card c;
for (int i = 1; i <= 10; i++) {
if (i == 1)
c = new Card(color, "J");
else if (i == 2)
c = new Card(color, "Q");
else if (i == 3)
continue;
else
c = new Card(color, "" + i);
cards.add(c);
}
}
private void generateCard() {// 生成所有的牌
generateOneColor("黑桃");
generateOneColor("红桃");
generateOneColor("梅花");
generateOneColor("方片");
cards.add(new Card("大王", "6"));
cards.add(new Card("小王", "3"));
}
public ArrayList<Card> randomCard() {// 随机排序已有的牌
System.out.println("------------------洗牌!------------------");
generateCard();
Card temp, randomP;
for (int i = 0; i < cards.size(); i++) {
int suijishu = (int) (Math.random() * 10);
randomP = cards.get(suijishu);
temp = cards.get(i);
cards.set(i, randomP);
cards.set(suijishu, temp);
}
for (Card c : cards)
if (c.getColor().equals("大王") || c.getColor().equals("小王"))
System.out.print(c.getColor() + " ");
else
System.out.print(c.getColor() + c.getNumber() + " ");
System.out.println();
System.out.println("------------------洗牌完成!------------------");
return cards;
}
// 发牌
public void licensing(Player player) {
Card card = cards.get(0);
cards.remove(0);
// for (Card c : cards)
// System.out.print(c.getColor() + c.getNumber() + " ");
player.addCard(card);
}
/**
* 获得一张牌的number
*
* @param card
* @return
*/
private int getNumber(Card card) {
int x1;
if (card.getNumber().equals("J"))
x1 = 1;
else if (card.getNumber().equals("Q"))
x1 = 2;
else
x1 = Integer.parseInt(card.getNumber());
return x1;
}
/**
* winer
*
* @param player1
* @param player2
* @return
*/
public Player winer(Player player1, Player player2) {
int x1, x2, y1, y2;
x1 = getNumber(player1.getCard().get(0));
x2 = getNumber(player1.getCard().get(1));
y1 = getNumber(player2.getCard().get(0));
y2 = getNumber(player2.getCard().get(1));
if ((x1 + x2) % 10 > (y1 + y2) % 10)
return player1;
else if ((x1 + x2) % 10 < (y1 + y2) % 10)
return player2;
else
return null;
}
/**
* 直到输入整形的id
*/
public void inputSC() {
Scanner input = new Scanner(System.in);
int id = 0;
while (true) {
try {
id = input.nextInt();
} catch (Exception e) {
System.out.println("请输入整数类型的id:");
inputSC();
}
break;
}
}
public Player createPlayer(String playerName) {
Scanner input1 = new Scanner(System.in);
System.out.println("" + playerName + ": id:");
int id = 0;
inputSC();
System.out.println(playerName + ": name:");
String name = input1.nextLine();
Player player = new Player();
player.setId(id);
player.setName(name);
System.out.println("玩家" + player.getName() + "创建成功!");
return player;
}
public void removeCard(Player player){
player.getCard().remove(0);
player.getCard().remove(0);
}
}
package zc20170130;
import java.util.*;
public class GameStart {
public static GameRule gr = new GameRule();
public static void main(String[] args) {
gr.randomCard();
System.out.println("------------------创建2位玩家:------------------");
Player player1 = gr.createPlayer("玩家1");
Player player2 = gr.createPlayer("玩家2");
int i = 1;
while (true) {
System.out.println("------------------第"+i+"轮发牌:------------------");
System.out.println("玩家1获得第1张牌");
gr.licensing(player1);
System.out.println("玩家2获得第1张牌");
gr.licensing(player2);
System.out.println("玩家1获得第2张牌");
gr.licensing(player1);
System.out.println("玩家2获得第2张牌");
gr.licensing(player2);
Player winer = gr.winer(player1, player2);
if (winer != null)
System.out.println("玩家" + winer.getName() + "获胜!!!");
else
System.out.println("平局");
System.out.println(player1.getName() + "手牌为:"
+ player1.getCard().get(0).getColor()
+ player1.getCard().get(0).getNumber() + "+"
+ player1.getCard().get(1).getColor()
+ player1.getCard().get(1).getNumber());
System.out.println(player2.getName() + "手牌为:"
+ player2.getCard().get(0).getColor()
+ player2.getCard().get(0).getNumber() + "+"
+ player2.getCard().get(1).getColor()
+ player2.getCard().get(1).getNumber());
gr.removeCard(player1);
gr.removeCard(player2);
i++;
if(gr.getCards().size()/4<=0)
break;
}
}
}
学习学习
Java入门第三季
409792 学习 · 4340 问题
相似问题