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

java的Collection工具类编写简单的扑克牌比大小

BLONG
关注TA
已关注
手记 3
粉丝 3
获赞 5
package com.example.poker;

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

public class PockerTest {

    public String[] colorType = { "梅花", "草花", "方块", "红桃" };
    public String[] number = { "A", "2", "3", "4", "5", "6", "7", "8", "9",
            "J", "Q", "K" };
    public List<Pocker> pockers;
    public List<String> pockerName;
    public Scanner console;

    public List<String> pocke1;
    public List<String> pocke2;

    public Player player1;
    public Player player2;
    public List<Pocker> player1Pockers;
    public List<Pocker> player2Pockers;

    public PockerTest() {
        pockers = new ArrayList<Pocker>();
        player1Pockers = new ArrayList<Pocker>();
        player2Pockers = new ArrayList<Pocker>();
        pockerName = new ArrayList<String>();
        console = new Scanner(System.in);

        pocke1 = new ArrayList<String>();
        pocke2 = new ArrayList<String>();

        player1 = new Player();
        player2 = new Player();

    }

    public void createPlayer() {

    }

    public void createPocker() {
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 12; j++) {
                // pockerName.add(colorType[i] + "" + number[j]);
                Pocker pocker = new Pocker(colorType[i], number[j]);
                pockers.add(pocker);
            }
        }
//       Collections.shuffle(pockers);

    }

    public void deal() {

        Collections.shuffle(pockers);
        System.out.println("请输入Player1的ID:");
        String playerID1 = console.next();
        System.out.println("玩家" + playerID1 + "登入成功");

        System.out.println("请输入Player2的ID:");
        String playerID2 = console.next();
        System.out.println("玩家" + playerID2 + "登入成功");
        System.out.println("-------------游戏开始-----------------");
        System.out.println("-------------开始发牌-----------------");
        for (int i = 0; i < 4; i++) {
            if (i == 0) {
                System.out.println("玩家" + playerID1 + "拿牌");
                Pocker pocker = pockers.get(i);
                player1Pockers.add(pocker);
            } else if (i == 1) {
                System.out.println("玩家" + playerID2 + "拿牌");
                Pocker pocker = pockers.get(i);
                player2Pockers.add(pocker);
            } else if (i == 2) {
                System.out.println("玩家" + playerID1 + "拿牌");
                Pocker pocker = pockers.get(i);
                player1Pockers.add(pocker);
            } else if (i == 3) {
                System.out.println("玩家" + playerID2 + "拿牌");
                Pocker pocker = pockers.get(i);
                player2Pockers.add(pocker);
            }
            System.out.println("---------------发牌结束---------");

        }
        player1 = new Player(playerID1, player1Pockers);
        player2 = new Player(playerID2, player2Pockers);

        Pocker pocker1 = getTheLargestPoker(player1);
        Pocker pocker2 = getTheLargestPoker(player2);
        System.out.println(player1.id+"最大的牌为:"+pocker1.colorType+pocker1.number);
        System.out.println(player2.id+"最大的牌为:"+pocker2.colorType+pocker2.number);
        if(compare(pocker1,pocker2)){
            System.out.println("获胜者:"+player1.id);
        }else{
            System.out.println("获胜者:"+player2.id);
        }

        System.out.println("玩家" + playerID1 + "牌为:"
                + player1.pocker.get(0).colorType + ""
                + player1.pocker.get(0).number + ","
                + player1.pocker.get(1).colorType + ""
                + player1.pocker.get(1).number);
        System.out.println("玩家" + playerID2 + "牌为:"
                + player2.pocker.get(0).colorType + ""
                + player2.pocker.get(0).number + ","
                + player2.pocker.get(1).colorType + ""
                + player2.pocker.get(1).number);
    }

    public void createPockerName() {
        System.out.println("----------------创建好牌------------");

        for (Pocker pocker : pockers) {
//           pockerName.add(pocker.colorType + pocker.number);
            System.out.print(pocker.colorType + pocker.number+",");
        }
        System.out.println("");
    }

    public Boolean compare(Pocker o1, Pocker o2) {
        // TODO Auto-generated method stub
        String num = "345678910JQKA2";
        String suit = "方片梅花红桃黑桃";
        int numo1 = num.indexOf(o1.number);
        int numo2 = num.indexOf(o2.number);
        int colo1 = suit.indexOf(o1.colorType);
        int colo2 = suit.indexOf(o2.colorType);
        if (numo1 > numo2) {
            return true;
        } else if (numo1 < numo2) {
            return false;
        } else {
            if (colo1 > colo2)
                return true;
            else
                return false;

        }

    }

    public Pocker getTheLargestPoker(Player one) {
        Pocker p1 = one.pocker.get(0);
        Pocker p2 = one.pocker.get(1);
        if (compare(p1, p2))
            return p1;
        else
            return p2;
    }

    public static void main(String args[]) {
        PockerTest pt = new PockerTest();
        pt.createPocker();
        pt.createPockerName();
        pt.deal();
    }
}
打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP