/*
花了2个小时时间写的代码,有什么错误,欢迎指出
**/
//创建卡牌类
public class Card implements Comparable<Card>{
public int id;//大小属性
public int huasesort; //花色属性的隐藏大小属性
public String huase;//花色属性
public String name;//点数属性
//无参构造器
public Card(){}
//有参构造器
public Card(int id,String huase,String name){
this.id=id;
this.huase=huase;
this.name=name;
//创建实例并给它传入花色时,分配整数型的大小顺序
switch(huase){
case "方块":huasesort=1;break;
case "梅花":huasesort=2;break;
case "红心":huasesort=3;break;
case "黑桃":huasesort=4;break;
}
}
@Override
public int compareTo(Card o) {
// TODO Auto-generated method stub
Integer int1=new Integer(this.id);
Integer int2=new Integer(o.id);
return int1.compareTo(int2);
}
}
//创建玩家类
import java.util.ArrayList;
import java.util.List;
public class Player {
public int id; //玩家ID
public String name; //玩家姓名
public List<Card> shoupai=new ArrayList(); //玩家手牌
public Player(int id,String name){
this.id=id;
this.name=name;
}
}
//游戏主程序类
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class PlayGame {
List<Card> puke1st;//一副扑克牌
Player player1;//玩家1
Player player2;//玩家2
Scanner console=new Scanner(System.in);
//创建一副扑克牌
public void creatPuke(){
//创建方块牌
puke1st=new ArrayList();
for(int i=1;i<=13;i++){
if(i>=2&&i<=10){
puke1st.add(new Card(i,"方块",i+""));
}else if(i==1){puke1st.add(new Card(i,"方块","A"));}
else if(i==11)
{puke1st.add(new Card(i,"方块","J"));}
else if(i==12){puke1st.add(new Card(i,"方块","Q"));}
else if(i==13){puke1st.add(new Card(i,"方块","k"));}
}
//创建梅花牌
for(int i=1;i<=13;i++){
if(i>=2&&i<=10){
puke1st.add(new Card(i,"梅花",i+""));
}else if(i==1){puke1st.add(new Card(i,"梅花","A"));}
else if(i==11)
{puke1st.add(new Card(i,"梅花","J"));}
else if(i==12){puke1st.add(new Card(i,"梅花","Q"));}
else if(i==13){puke1st.add(new Card(i,"梅花","k"));}
}
//创建红心牌
for(int i=1;i<=13;i++){
if(i>=2&&i<=10){
puke1st.add(new Card(i,"红心",i+""));
}else if(i==1){puke1st.add(new Card(i,"红心","A"));}
else if(i==11)
{puke1st.add(new Card(i,"红心","J"));}
else if(i==12){puke1st.add(new Card(i,"红心","Q"));}
else if(i==13){puke1st.add(new Card(i,"红心","k"));}
}
//创建黑桃牌
for(int i=1;i<=13;i++){
if(i>=2&&i<=10){
puke1st.add(new Card(i,"黑桃",i+""));
}else if(i==1){puke1st.add(new Card(i,"黑桃","A"));}
else if(i==11)
{puke1st.add(new Card(i,"黑桃","J"));}
else if(i==12){puke1st.add(new Card(i,"黑桃","Q"));}
else if(i==13){puke1st.add(new Card(i,"黑桃","k"));}
}
System.out.println("已创建好一副牌");
}
//对牌进行遍历打印输出
public void print(){
for(Card c:puke1st){
System.out.print(c.huase+c.name+" ");
}
System.out.println();
}
//洗牌
public void xipai(){
Collections.shuffle(puke1st);
System.out.println("洗牌已完成!");
}
//创建2个玩家对象
public void creat2Players(){
System.out.println("请输入第一个玩家ID:");
int id1=console.nextInt();
System.out.println("请输入第一个玩家姓名:");
String name1=console.next();
player1=new Player(id1,name1);
System.out.println("玩家1已创建成功");
System.out.println("请输入第二个玩家ID:");
int id2=console.nextInt();
System.out.println("请输入第二个玩家姓名:");
String name2=console.next();
player2=new Player(id2,name2);
System.out.println("玩家2已创建成功");
}
//分别抽2张牌给玩家
public void choupai(){
System.out.println("---开始抽牌---");
this.player1.shoupai.add(puke1st.get(0));
this.player2.shoupai.add(puke1st.get(1));
this.player1.shoupai.add(puke1st.get(2));
this.player2.shoupai.add(puke1st.get(3));
System.out.println("---抽牌结束---");
}
//判定输赢
public void winOrLose(){
//给玩家的手牌进行排序
Collections.sort(player1.shoupai);
Collections.sort(player2.shoupai);
//抽出各玩家最大的牌
Card player1card=player1.shoupai.get(1);
Card player2card=player2.shoupai.get(1);
if(player1card.id>player2card.id)
{System.out.println("玩家"+player1.name+"获胜!");}
else if(player1card.id<player2card.id){
System.out.println("玩家"+player2.name+"获胜!");
}else {
//如果数值相等,通过花色判定输赢
if(player1card.huasesort>player2card.huasesort){
System.out.println("玩家"+player1.name+"获胜!");
}else if(player1card.huasesort<player2card.huasesort){
System.out.println("玩家"+player2.name+"获胜!");
}else
System.out.println("平局!!!");
}
//对各玩家的牌进行遍历输出
System.out.println(player1.name+"的手牌如下:");
for(Card c:player1.shoupai){
System.out.println(c.huase+" "+c.name);
}
System.out.println(player2.name+"的手牌如下:");
for(Card c:player2.shoupai){
System.out.println(c.huase+" "+c.name);
}
}
public static void main(String[] args) {
PlayGame play=new PlayGame();
play.creatPuke();
play.print();
play.xipai();
play.creat2Players();
play.choupai();
play.winOrLose();
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章