手记

Java第三季-7.1-简易扑克牌游戏作业

游戏规则:
/* 一、创建一副扑克牌

  • 四种花色:黑桃、红桃、梅花、方片
  • 十三种点数:2-10,J,Q,K,A,不考虑大小王
  • 二、创建两名玩家
  • 玩家至少有ID、姓名、手牌等属性,手牌为扑克牌的集合
  • 三、洗牌
  • 将之前创建的“一副扑克牌”打乱顺序
  • 四、发牌
  • 将洗牌之后的扑克牌集合,从第一张开始,
  • 发给两名玩家,按照一人一张方式,每人发两张
  • 五、游戏
  • 比较两名玩家手中的扑克牌,规则为:
  • 去两个人各自手中点数最大的牌进行比较,点数大的赢;
  • 若两人各自的点数最大的牌相等,则再按花色比较。
  • */
    代码主要有三个类,玩家类Player,扑克类Poker,游戏流程类ListPoker
    玩家类Player:
import java.util.ArrayList;
import java.util.List;
public class Player {
    public String id;
    public String name;
    public List<Poker> shoupai;
    public Player(String id,String name) {
        this.id=id;
        this.name=name;
        this.shoupai=new ArrayList<Poker>();
    }
}

扑克类Poker:

public class Poker implements Comparable<Poker>{
 public String flower;
 public String num;
 public Poker(String fl,String  num) {
    this.flower=fl;
    this.num=num;
}
 public Poker(){

 }
@Override
//四种花色:黑桃、红桃、梅花、方片
public int compareTo(Poker o) {
    Integer a=0,b=0;
    if(this.flower==o.flower){
        return this.num.compareTo(o.num);
    }
    else{
        switch(this.flower)
        {
        case "黑桃": a=4;
        break;
        case "红桃": a=3;
        break;
        case "梅花": a=2;
        break;
        case "方片": a=3;
        break;
        default: a=0;
        break;
        }
        switch(o.flower)
        {
        case "黑桃": b=4;
        break;
        case "红桃": b=3;
        break;
        case "梅花": b=2;
        break;
        case "方片": b=3;
        break;
        default: b=0;
        break;
        }
        return a.compareTo(b);              
    }
}
}

游戏流程类ListPoker:

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

public class ListPoker {
    public List<Poker> PairPorker;
    private Scanner console;
    public List<Player> Playerlist;
    public Player p1;
    public Player p2;
    public ListPoker() {
        this.PairPorker=new ArrayList<Poker>();
        this.Playerlist=new ArrayList<Player>();
    }
    //创建扑克牌
    public void MakePoker(){
        System.out.println("-------------------创建扑克牌-------------------------");
        for(int i=2;i<11;i++){

            PairPorker.add(new Poker("黑桃",i+""));
            PairPorker.add(new Poker("红桃",i+""));
            PairPorker.add(new Poker("梅花",i+""));
            PairPorker.add(new Poker("方片",i+""));
        }
        PairPorker.add(new Poker("黑桃","J"));
        PairPorker.add(new Poker("红桃","J"));
        PairPorker.add(new Poker("梅花","J"));
        PairPorker.add(new Poker("方片","J"));
        PairPorker.add(new Poker("黑桃","Q"));
        PairPorker.add(new Poker("红桃","Q"));
        PairPorker.add(new Poker("梅花","Q"));
        PairPorker.add(new Poker("方片","Q"));
        PairPorker.add(new Poker("黑桃","K"));
        PairPorker.add(new Poker("红桃","K"));
        PairPorker.add(new Poker("梅花","K"));
        PairPorker.add(new Poker("方片","K"));
        System.out.println("------------------扑克牌创建成功-----------------------");
        System.out.print("为:[");
        for (Poker pk: PairPorker) {
            System.out.print(pk.flower+pk.num+" ");
        }
        System.out.print("]\n");
    }

    //三、洗牌 将之前创建的“一副扑克牌”打乱顺序
    public void Shuffle(){
        List<Poker> pk=new  ArrayList<Poker>();
        Poker ck=new Poker();
        Random random=new Random();
        Integer length=PairPorker.size();
        System.out.println("--------------------开始洗牌-------------------------");
        for(int i=0;i<length;i++)
        {
            do{
            ck=PairPorker.get(random.nextInt(length));
            }while(pk.contains(ck));
            pk.add(ck);
        }
        PairPorker=pk;
        System.out.println("--------------------洗牌结束-------------------------");
    }

    //判断输入id是否合理,处理异常
    private int scanf() throws Exception{
        try {
            int inner=console.nextInt();
            return inner;
        } catch (Exception e) {
            console=new Scanner(System.in);
            throw new Exception("id输入不合理,请重新输入!");
        }   
    }
    public void Makeplayer(){
        System.out.println("----------------------创建玩家-------------------------");      
            for (int i = 1; i <3; i++) {

                System.out.println("请输入第"+i+"位玩家的ID和姓名:");
                System.out.println("输入ID:");            
                console=new Scanner(System.in);
            int ID=0;
            try {
                ID=scanf();
            } catch (Exception e) {
                System.out.println(e.getMessage());
                i--;
                continue;
            }
            System.out.println("输入姓名:");
            String name1;       
                 name1=console.next();
            Playerlist.add(new Player(ID+"", name1));   
            }
            p1=Playerlist.get(0);
            p2=Playerlist.get(1);
            console.close();
    }
    public void Fapai(){
        System.out.println("-----------------开始发牌----------------------");
        for(int i=0;i<2;i++)
        {
            Playerlist.get(0).shoupai.add(PairPorker.get(2*i));
            System.out.println("----玩家:"+p1.name+"-拿牌");
            Playerlist.get(1).shoupai.add(PairPorker.get(2*i+1));
            System.out.println("----玩家:"+p2.name+"-拿牌");
        }
        System.out.println("-----------------发牌结束----------------------");
    }
    public void PlayStart(){
        System.out.println("-----------------开始游戏----------------------");
        Collections.sort(p1.shoupai);
        Collections.sort(p2.shoupai);
        Poker pkk1=p1.shoupai.get(1);
        Poker pkk2=p2.shoupai.get(1);
        System.out.println("玩家:"+p1.name+"最大的手牌为:"+pkk1.flower+pkk1.num);
        System.out.println("玩家:"+p2.name+"最大的手牌为:"+pkk2.flower+pkk1.num);
        if(pkk1.compareTo(pkk2)>0){
            System.out.println("-----------玩家:"+p1.name+"获胜--------------");
        }
        else{
            System.out.println("-----------玩家:"+p2.name+"获胜--------------");
        }
        System.out.println("玩家各自的手牌为:");
        System.out.print(p1.name+":[");
        for (Poker pk1: p1.shoupai) {
            System.out.print(pk1.flower+pk1.num+" ");
        }
        System.out.print("]");
        System.out.print("\n"+p2.name+":[");
        for (Poker pk1: p2.shoupai) {
            System.out.print(pk1.flower+pk1.num+" ");
        }
        System.out.print("]\n");
    }   
    public static void main(String[] args) {
        ListPoker lp=new ListPoker();
        lp.MakePoker();
        lp.Shuffle();
        lp.Makeplayer();
        lp.Fapai();
        lp.PlayStart();
    }
}
0人推荐
随时随地看视频
慕课网APP