继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

简易扑克牌游戏的简易实现

qq_赌书泼茶_03914343
关注TA
已关注
手记 2
粉丝 0
获赞 10

今天学完了Java入门第三季的最后一课,完成了课后作业,实现了简易扑克游戏。游戏很简单,代码实现也不难,但做了这个很能理解Java面向对象的作用。 就这个游戏而言,我主要是分为三个class:Poker,Player,Game。

  • 扑克牌类,包含元素是扑克牌的点数和花色,并且定义扑克牌之间比较大小的规则。
  • 玩家类复杂点,除了玩家的ID和名字,还有所拥有的手牌,在后面发现游戏用到的最大手牌,也设在玩家类里,这样能更好处理对应关系。
  • 主程序main在Game里面,把游戏的整个过程实现出来,分步用成员函数实现:新建整副扑克牌,产生游戏玩家,洗牌,发牌,开始游戏。
程序运行如下图:

图片描述

剩下的代码来展示,注释比较少,但输出的提示应该够看了。

游戏主程序
package com.imooc.homework;

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

/**
 * Created by LinHongwei on 2017/9/14.
 */
public class Game {
    private final String[] cardPointArr={"A","K","Q","J","10","9","8","7","6","5","4","3","2"};
    private final String[] cardTypeArr={"黑桃","红桃","梅花","方块"};
    private List<Poker> allPoker;
    private List<Player> players;

    public void createPokerCards(){
        allPoker = new ArrayList<Poker>();
        System.out.println("~~~~~~~~~创建并展示所有扑克牌~~~~~~~~~~");
        for(int i=0;i<cardPointArr.length;i++){
            for(int j=0;j<cardTypeArr.length;j++){
                Poker p =new Poker(cardPointArr[i],cardTypeArr[j]);
                allPoker.add(p);
                System.out.print(p.getInfo()+"  ");
            }
            System.out.println();
        }
    }
    public void createPlayers(){
        Scanner console= new Scanner(System.in);
        String id,name;
        System.out.println("~~~~~~~~~创建游戏玩家~~~~~~~~~~");
        players= new ArrayList<Player>();
        System.out.println("请输入第一个玩家的ID:");
        id=console.next();
        System.out.println("请输入第一个玩家的姓名");
        name=console.next();
        Player player1=new Player(id,name);
        players.add(player1);
        System.out.println("~~~~~欢迎玩家:"+player1.getName());

        System.out.println("请输入第二个玩家的ID:");
        id=console.next();
        System.out.println("请输入第二个玩家的姓名");
        name=console.next();
        Player player2=new Player(id,name);
        players.add(player2);
        System.out.println("~~~~~欢迎玩家:"+player2.getName());
    }

    public void shuffleCard() {
        System.out.println("——————————开始洗牌——————————");
        Collections.shuffle(allPoker);
        System.out.println("——————————洗牌结束——————————");
    }

    public void dealCard(){
        System.out.println("——————————开始发牌——————————");
        for(int i=0;i<2;i++){
            for(Player p:players){
                Poker poker = allPoker.get(0);
                p.getHasCards().add(poker);
                System.out.println("玩家"+p.getId()+":"+p.getName()+"拿牌");
                allPoker.remove(0);
            }
        }
        System.out.println("——————————发牌结束——————————");
    }

    public void playGame(){
        System.out.println("——————————开始游戏——————————");
        Player[] p =new Player[2];
        List<Poker>maxCards=new ArrayList<Poker>();
        for(int i=0;i<2;i++) {
            p[i]=players.get(i);
            Poker it = p[i].getMaxCard();
            maxCards.add(it);
            System.out.println("玩家:" + p[i].getName() + "的最大手牌为:" + it.getInfo());
        }
        Collections.sort(maxCards);
        Poker max = maxCards.get(1);
        if(max.equals(p[0].getMaxCard())){
            System.out.println("**********玩家"+p[0].getId()+":"+p[0].getName()+"获胜!");
        }
        else{
            System.out.println("**********玩家"+p[1].getId()+":"+p[1].getName()+"获胜!");
        }
        System.out.println("**********玩家各自的手牌为:");
        System.out.println(p[0].getName()+":["+p[0].getHasCards().get(0).getInfo()+","+p[0].getHasCards().get(1).getInfo()+"]");
        System.out.println(p[1].getName()+":["+p[1].getHasCards().get(0).getInfo()+","+p[1].getHasCards().get(1).getInfo()+"]");
    }

    public static void main(String[] args){
        Game game = new Game();
        game.createPokerCards();
        game.createPlayers();
        game.shuffleCard();
        game.dealCard();

        game.playGame();
    }
}
扑克牌
package com.imooc.homework;

import java.util.*;

/**
 * Created by LinHongwei on 2017/9/14.
 */
public class Poker implements Comparable<Poker>{
    private String type;
    private String Num;
    public Poker(String a,String b){
        Num=a;
        type=b;
    }
    public Poker(){

    }

    public String getInfo(){
        return Num+"-"+type;
    }

    @Override
    public int compareTo(Poker o) {
        final String[] cardPointArr={"A","K","Q","J","10","9","8","7","6","5","4","3","2"};
        final String[] cardTypeArr={"黑桃","红桃","梅花","方块"};
        List<String> cardPointList= Arrays.asList(cardPointArr);
        List<String> cardTypeList= Arrays.asList(cardTypeArr);
        int a,b;
        a=cardPointList.indexOf(this.Num);
        b=cardPointList.indexOf(o.Num);
        if(a<b) return 1;
        else if(a>b) return -1;
        else{
            a=cardTypeList.indexOf(this.type);
            b=cardTypeList.indexOf(o.type);
            if(a<b) return 1;
            else if(a>b) return -1;
            else return 0;
        }
    }

    public static void main(String[] args){
        Poker A=new Poker("A","黑桃");
        Poker B=new Poker("4","黑桃");
        Poker C=new Poker("A","方块");
        List<Poker> list= new ArrayList<Poker>();
        list.add(A);
        list.add(B);
        list.add(C);
        Collections.sort(list);
        for(int i=0;i<3;i++){
            System.out.println(list.get(i).getInfo());
        }
    }
}
游戏玩家
package com.imooc.homework;

import javax.sql.PooledConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by LinHongwei on 2017/9/14.
 */
public class Player {
    private String id;
    private String name;
    private List<Poker> hasCards;
    private Poker maxCard;

    public Player(String id, String name) {
        this.id = id;
        this.name = name;
        hasCards = new ArrayList<Poker>();
        maxCard = new Poker();
    }

    public List<Poker> getHasCards() {
        return hasCards;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public Poker getMaxCard() {
        Collections.sort(this.hasCards);
        this.maxCard =this.hasCards.get(1);
        return maxCard;
    }

}
打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP