//扑克牌类
public class Card implements Comparable<Card> {
private String name;//牌名称
private int value;//点数
private int keyvalue;//花色值,黑桃,红桃,梅花,方块分别对应4,3,2,1
public Card(String name, int value, int keyvalue) {
super();
this.name = name;
this.value = value;
this.keyvalue = keyvalue;
}
public String getName() {
return name;
}
public int getValue() {
return value;
}
public int getKeyvalue() {
return keyvalue;
}
@Override
public int compareTo(Card o) {
if (this.value!=o.value) {return this.value-o.value;}
else {return this.keyvalue-o.keyvalue;}
}
}
//玩家类
import java.util.ArrayList;
import java.util.List;
public class Player {
private int id;//玩家id
private String name;//玩家姓名
private List<Card> mycards;//玩家手中的牌
public Player(int id, String name) {
super();
this.id = id;
this.name = name;
this.mycards = new ArrayList<Card>();
}
public int getId() {
return id;
}
public void setMycards(List<Card> mycards) {
this.mycards = mycards;
}
public String getName() {
return name;
}
public List<Card> getMycards() {
return mycards;
}
}
//主要游戏逻辑
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class CardGame {
public List<Card> cards;//扑克牌
public List<Integer> cardsindex;//牌的顺序
public Player player1;//玩家1
public Player player2;//玩家2
public void playGame() {
System.out.println("--------开始游戏-------------");
List<Card> card1=player1.getMycards();
List<Card> card2=player2.getMycards();
Collections.sort(card1);
Collections.sort(card2);
Card max1=card1.get(1);
Card max2=card2.get(1);
System.out.println("玩家:"+player1.getName()+"最大手牌为:"+max1.getName());
System.out.println("玩家:"+player2.getName()+"最大手牌为:"+max2.getName());
if (max1.compareTo(max2)>0){
System.out.printf("---------玩家:%s获胜!----------\n",player1.getName());
}else{
System.out.printf("---------玩家:%s获胜!----------\n",player2.getName());
}
System.out.println("玩家各自的手牌为:");
System.out.printf("%s:[%s,%s]\n",player1.getName(),card1.get(0).getName()
,card1.get(1).getName());
System.out.printf("%s:[%s,%s]\n",player2.getName(),card2.get(0).getName()
,card2.get(1).getName());
}
public void dealcards() {
System.out.println("---欢迎玩家:"+player1.getName());
System.out.println("---欢迎玩家:"+player2.getName());
System.out.println("--------开始发牌-------------");
System.out.println("---玩家"+player1.getName()+"拿牌");
System.out.println("---玩家"+player2.getName()+"拿牌");
System.out.println("---玩家"+player1.getName()+"拿牌");
System.out.println("---玩家"+player2.getName()+"拿牌");
List<Card> tempycards=new ArrayList<Card>();
tempycards.add(cards.get(cardsindex.get(0)));
tempycards.add(cards.get(cardsindex.get(2)));
player1.setMycards(tempycards);
List<Card> tempycards2=new ArrayList<Card>();
tempycards2.add(cards.get(cardsindex.get(1)));
tempycards2.add(cards.get(cardsindex.get(3)));
player2.setMycards(tempycards2);
System.out.println("--------发牌结束-------------");
}
public Player creatplayer(int i) {
System.out.println("请输入第"+i+"位玩家的id和姓名:");
int id;String name;
while (true){
try{
Scanner input=new Scanner(System.in);
System.out.println("输入id:");
id=input.nextInt();
System.out.println("输入姓名:");
name=input.next();
break;
}
catch(Exception e){
System.out.println("输入整数类型的ID!");
}
}
return new Player(id,name);
}
public void washcards() {
System.out.println("--------开始洗牌-------------");
cardsindex=new ArrayList<Integer>();
for (int i=0;i<52;i++){
cardsindex.add(i);
}
Collections.shuffle(cardsindex);
System.out.println("--------洗牌结束-------------");
}
public void creatcardslist() {
System.out.println("-------创建扑克牌-----------");
cards=new ArrayList<Card>();
String[] color=new String[]{"黑桃","红桃","梅花","方块"};
String[] valueNumber=new String[]{"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
for (int i=0;i<color.length;i++){
for (int j=0;j<valueNumber.length;j++){
cards.add(new Card(color[i]+valueNumber[j],j,i));
}
}
System.out.println("--------创建成功------------");
for (Card c:cards){System.out.print(c.getName()+" ");}
System.out.println();
}
}
//程序入口类
public class StartGame {
public static void main(String[] args) {
// TODO Auto-generated method stub
CardGame cardgame=new CardGame();
cardgame.creatcardslist();//创建扑克牌
cardgame.washcards();//洗牌
System.out.println("--------创建玩家-------------");
cardgame.player1=cardgame.creatplayer(1);//创建玩家1
cardgame.player2=cardgame.creatplayer(2);//创建玩家2
cardgame.dealcards();//开始发牌
cardgame.playGame();//开始游戏对战
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章