手记

简易洗牌发牌的游戏支持可拓展功能

package playcards;

public class PlayCards {

public static void main(String[] args) {
// TODO Auto-generated method stub
MakeCards mc=new MakeCards();
mc.Cards();
mc.ShuffleCards();
mc.CreatPlayers();
mc.SetCardandConstrastCard();
}

}

创建牌类,特别将牌色进行了定义颜色与ID,ID是为了后面的比大小用的;
牌的大小也做了拆分,因为有J,Q,K的存在使得牌的大小不适合比较
定义牌的大小属性方便做比较

package playcards;
/*

  • 1.创建牌类,颜色类,牌的大小类
  • 2.分别做属性的定义
    */
    public class Card implements Comparable<Card>{
    public String color;
    public String cardNumName;
    public int colorid;
    public int cardNumId;
    public Card(String color,String carNumName){
    this.color=color;
    this.cardNumName=carNumName;
    }
    public Card(){}

    @Override
    public int compareTo(Card o) {
    // TODO Auto-generated method stub
    int result=0;
    if(this.cardNumId>o.cardNumId){
    result=1;
    }
    else if(this.cardNumId==o.cardNumId)
    {
    if(this.colorid>o.colorid){
    result=1;
    }
    else if(this.colorid==o.colorid){
    result=0;
    }
    else if(this.colorid<o.colorid){
    result=-1;
    }
    }
    else if(this.cardNumId<o.cardNumId){
    result=-1;
    }
    return result;
    }
    }
    class Color {
    public String color;
    public int colorid;
    public Color(int colorid,String color){
    this.colorid=colorid;
    this.color=color;
    }
    }
    class CardNum{
    public String cardNumName;
    public int cardNumId;
    public CardNum(int cardNumId,String cardNumName){
    this.cardNumId=cardNumId;
    this.cardNumName=cardNumName;
    }
    public CardNum(){}
    }

创建玩家类,将玩家类也加入到list中,统一一个方法做比较

package playcards;

import java.util.ArrayList;
import java.util.List;
/*

  • 创建了玩家类,并对玩家类的compareTo方法进行了重写,因最终要比较的是玩家手中持有的手牌
    /
    public class Player implements Comparable<Player>{
    public int id;
    public String name;
    public List<Card> card;
    public Player(int id,String name){
    this.id=id;
    this.name=name;
    this.card=new ArrayList<Card>();
    }
    public Player(){}
    @Override
    public int compareTo(Player o) {
    // TODO Auto-generated method stub
    /
    • 重写compareto方法,本次游戏只对花色与大小进行比大小排序,
      而且取的都是不重复的手牌,只需要进行最大数比大小就可以
      */
      int result=0;
      // int size=this.card.size();
      // for(int i=size-1;i>=0;i--){
      if(this.card.get(this.card.size()-1).compareTo
      (o.card.get(o.card.size()-1))>0)
      {
      result=1;
      }
      // }
      return result;
      }

}

主方法代码处:
1、创建扑克牌
2、洗牌
3、发牌
4、比牌

package playcards;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class MakeCards {
/
定义了分别对四个类定义了四张表,虽然繁琐但是让思路不复杂。
*/
List<Card> cardList=new ArrayList<Card>();
List<Color> colorList=new ArrayList<Color>();
List<CardNum> cardnumList=new ArrayList<CardNum>();
List<Player> playerList=new ArrayList<Player>();
public MakeCards(){
}
public void Cards(){
Card card=new Card();
//手动创建扑克牌的颜色实例,并且加入到color.List中,方便取值;
System.out.println("--------创建扑克牌--------");
Color color1=new Color(4,"黑桃");
colorList.add(color1);
Color color2=new Color(3, "桃花");
colorList.add(color2);
Color color3=new Color(2, "方片");
Color color4=new Color(1, "红心");
colorList.add(color3);
colorList.add(color4);
CardNum cm=new CardNum();
cm.cardNumName="";
//使用for循环创建牌的大小,然后将color与cardNumName放入cardlist中,把cm放入cardNumList中
for(int k=0;k<colorList.size();k++)
{
for(int i=1;i<14;i++){
if(i<11)
{
cm.cardNumName=i+"";
cm.cardNumId=i;
cardnumList.add(cm);
card=new Card(colorList.get(k).color,cm.cardNumName);
cardList.add(card);
card.cardNumId=i;
card.colorid=colorList.get(k).colorid;
}
else if(i==12){
cm.cardNumName="Q"; //生成Q
cm.cardNumId=i;
cardnumList.add(cm);
card=new Card(colorList.get(k).color,cm.cardNumName);
cardList.add(card);
card.cardNumId=i;
card.colorid=colorList.get(k).colorid;
}
else if(i==11){
cm.cardNumName="J";
cm.cardNumId=i;
cardnumList.add(cm);
card=new Card(colorList.get(k).color,cm.cardNumName);
cardList.add(card);
card.cardNumId=i;
card.colorid=colorList.get(k).colorid;
}else if(i==13)
{
cm.cardNumName="K";
cm.cardNumId=i;
cardnumList.add(cm);
card=new Card(colorList.get(k).color,cm.cardNumName);
cardList.add(card);
card.cardNumId=i;
card.colorid=colorList.get(k).colorid;
}
}
}
System.out.println("--------扑克牌创建成功--------");
for (Card card1 : cardList) {
System.out.print(card1.color+card1.cardNumName+",");
}
System.out.println();
}
public void ShuffleCards(){
//排序并洗牌
// for (Card card1 : cardList) {
// System.out.print(card1.color+card1.cardNumName+",");
// }
// System.out.println();
//先对创建好的牌进行排序
Collections.sort(cardList);
// System.out.println("-------排序后---------");
// for(int i=0;i<cardList.size();i++)
// {
// System.out.print(cardList.get(i).color+cardList.get(i).cardNumName+",");
// }
// System.out.println();
System.out.println("--------开始洗牌-------");
//调用Collections.shuffle方法使牌变得无序
Collections.shuffle(cardList);
// for (Card card : cardList) {
// System.out.print(card.color+card.cardNumName+",");
// }
// System.out.println();
System.out.println("--------洗牌结束--------");
}
public void CreatPlayers(){
//创建玩家
Scanner console=new Scanner(System.in);
Player p=new Player();
// Player p1=new Player(1,"King");
// Player p2=new Player(2,"dusheng");
// playerList.add(p1);
// playerList.add(p2);
for(int i=0;i<2;i++){
System.out.println("请输入第"+(i+1)+"位玩家ID和名字");
int ID=0;
while(true){
try{
System.out.println("请输入ID:");
ID=console.nextInt();
// ID =Integer.parseInt(console.nextLine());
}catch (Exception e) {
System.out.println("请输入整型ID:");
System.out.println("请重新输入ID:");
console.next(); //清空缓存区中的非数字内容
continue;
}
break;
}
System.out.println("请输入名字:");
String name=console.next();
p=new Player(ID,name);
playerList.add(p);
}
for (Player player : playerList) {
System.out.println(player.id+player.name);
}
}
public void SetCardandConstrastCard(){
//取牌并比牌
Player p1 =playerList.get(0);
Player p2 =playerList.get(1);
for(int i=0;i<2;i++){
System.out.println("欢迎玩家:"+playerList.get(i).name);
}
//玩家p1与p2,依次取两张牌;使用循环可进行扩展
//定义取牌次数
int plarercardnum=4;
System.out.println("--------开始发牌-------");
for(int i=0;i<plarercardnum;i=i+2)
{
p1.card.add(cardList.get(i));
System.out.println("玩家"+p1.name+"拿牌");
// System.out.println("玩家"+p1.name+"拿到"+cardList.get(i).color
// +cardList.get(i).cardNumName);
p2.card.add(cardList.get(i+1));
System.out.println("玩家"+p2.name+"拿牌");
// System.out.println("玩家"+p2.name+"拿到"+cardList.get(i+1).color
// +cardList.get(i+1).cardNumName);
}
System.out.println("--------发牌结束--------");
System.out.println("--------开始游戏--------");
//分别对p1.card与p2.card进行排序,最大值就是下标最大的数,只要比较最后一个数就可以比大小了
Collections.sort(p1.card);
Collections.sort(p2.card);
System.out.println("玩家"+p1.name+"最大的手牌是:"+
p1.card.get(p1.card.size()-1).color+p1.card.get(p1.card.size()-1).cardNumName);
System.out.println("玩家"+p2.name+"最大的手牌是:"+
p2.card.get(p2.card.size()-1).color+p2.card.get(p2.card.size()-1).cardNumName);
//进行比牌输出各自手中的牌;
if(p1.compareTo(p2)==1){
System.out.println("--------玩家:"+p1.name+"胜--------");
}else {
System.out.println("--------玩家:"+p2.name+"胜--------");
}
System.out.println("--------游戏结束--------");
//显示各个玩家手中的牌
System.out.println("玩家各自的手牌为:");
System.out.print(p1.name+":");
for (Card card : p1.card) {
System.out.print(card.color+card.cardNumName+",");
}
System.out.println();
System.out.print(p2.name+":");
for (Card card : p2.card) {
System.out.print(card.color+card.cardNumName+",");
}
System.out.println();
}
}

  1. 列表项
2人推荐
随时随地看视频
慕课网APP

热门评论

http://img.mukewang.com/58db2ca100010d9111580558.jpg

图片漏了一张--还有牌太多没有全部显示

查看全部评论