手记

扑克牌游戏两人手牌最大者胜

扑克牌类

package com.yy.poke;

public class pokeCard implements Comparable<pokeCard> {
    private Integer color;
    private Integer num;

    public pokeCard(Integer color, Integer num) {
        this.color = color;
        this.num = num;
    }

    public Integer getColor() {
        return color;
    }

    public void setColor(Integer color) {
        this.color = color;
    }

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }

    @Override
    public int compareTo(pokeCard arg0) {
        // TODO 自动生成的方法存根
        if(getNum()<arg0.getNum()){
            return -1;
        }else if (getNum()>arg0.getNum()) {
            return 1;
        }else{
            if(getColor()<arg0.getColor())return -1;
            if(getColor()>arg0.getColor())return 1;
            if(getColor()==arg0.getColor())return 0;
        }
        return 0;
    }
}

玩家类

package com.yy.poke;

import java.util.ArrayList;
import java.util.List;

public class Player {
    private Integer ID;
    private String name;
    public List<pokeCard> pokeList;
    public Player(Integer ID,String name){  
        this.ID=ID;
        this.name=name;
        pokeList=new ArrayList<pokeCard>();
    }
    public void setPokeList(ArrayList<pokeCard> pokeList){
        this.pokeList=pokeList;
    }
    public Integer getID() {
        return ID;
    }
    public void setID(Integer iD) {
        ID = iD;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

主类

package com.yy.poke;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;

public class Game {
    public Player p1;
    public Player p2;
    List<pokeCard> pokeToSelect;
    Map<Integer, String> colorMap;
    Map<Integer, String> numMap;
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Game g=new Game();
        g.createPlayerAndPokecard();
        g.givePokecard();
        g.sortPokecard();
        g.comparePokecard();
    }
    public void createPlayerAndPokecard(){
        for (int i = 0; i < 2; i++) {
            Integer id = null;
            String name;
            System.out.println("请输入用户ID");
            do{
                Scanner sc1=new Scanner(System.in);
                try {
                    id=sc1.nextInt();
                } catch (InputMismatchException e) {
                    System.out.println("输入不正确,请重新输入整数");
                }
            }while(id == null);
            System.out.println("请输入用户名");
            Scanner sc2=new Scanner(System.in);
            name=sc2.next();
            if(i==0){
                p1=new Player(id, name);
            }else{
                p2=new Player(id, name);
            }
        }
        System.out.println("用户1的id:"+p1.getID()+"用户名为:"+p1.getName());
        System.out.println("用户2的id:"+p2.getID()+"用户名为:"+p2.getName());

        colorMap=new HashMap<Integer, String>();
        colorMap.put(0, "黑桃");
        colorMap.put(1, "红桃");
        colorMap.put(2, "梅花");
        colorMap.put(3, "方块");

        numMap=new HashMap<Integer,String>();
        numMap.put(9, "J");
        numMap.put(10, "Q");
        numMap.put(11, "K");
        numMap.put(12, "A");

        List<pokeCard> originList=new ArrayList<pokeCard>();
        pokeToSelect=new ArrayList<pokeCard>();
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 13; j++) {
                  originList.add(new pokeCard(i, j));
            }
        }
        while(!originList.isEmpty()){
            Random rd=new Random();
            Integer index=rd.nextInt(originList.size());
            pokeToSelect.add(originList.get(index));
            originList.remove((int)index);
        }

    }
    public void givePokecard(){

        p1.pokeList.add(pokeToSelect.get(0));
        p2.pokeList.add(pokeToSelect.get(1));
        p1.pokeList.add(pokeToSelect.get(2));
        p2.pokeList.add(pokeToSelect.get(3));
        System.out.println("玩家"+p1.getName()+"的手牌为:");
        for (pokeCard pokeCard : p1.pokeList) {
            if(pokeCard.getNum()>8){
                System.out.println(colorMap.get(pokeCard.getColor())+
                        numMap.get(pokeCard.getNum()));
            }else{
                System.out.println(colorMap.get(pokeCard.getColor())+(pokeCard.getNum()+2));
            }
        }
        System.out.println("玩家"+p2.getName()+"的手牌为:");
        for (pokeCard pokeCard : p2.pokeList) {
            if(pokeCard.getNum()>8){
                System.out.println(colorMap.get(pokeCard.getColor())+
                        numMap.get(pokeCard.getNum()));
            }else{
                System.out.println(colorMap.get(pokeCard.getColor())+(pokeCard.getNum()+2));
            }
        }
    }
    public void sortPokecard(){
        Collections.sort(p1.pokeList);
        Collections.sort(p2.pokeList);
        if(p1.pokeList.get(1).getNum()>8){
            System.out.println("玩家"+p1.getName()+"最大手牌为"+colorMap.get(p1.pokeList.get(1).getColor())+
                    numMap.get(p1.pokeList.get(1).getNum()));
        }else{
            System.out.println("玩家"+p1.getName()+"最大手牌为"+colorMap.get(p1.pokeList.get(1).getColor())+
                    (p1.pokeList.get(1).getNum()+2));
        }
        if(p2.pokeList.get(1).getNum()>8){
            System.out.println("玩家"+p2.getName()+"最大手牌为"+colorMap.get(p2.pokeList.get(1).getColor())+
                    numMap.get(p2.pokeList.get(1).getNum()));
        }else{
            System.out.println("玩家"+p2.getName()+"最大手牌为"+colorMap.get(p2.pokeList.get(1).getColor())+
                    (p2.pokeList.get(1).getNum()+2));
        }

    }
    public void comparePokecard(){
        if(p1.pokeList.get(1).compareTo(p2.pokeList.get(1))<0){
            System.out.println("玩家"+p2.getName()+"获胜");
        }else{
            System.out.println("玩家"+p1.getName()+"获胜");
        }
    }
}
0人推荐
随时随地看视频
慕课网APP