手记

Java第三季简易扑克游戏

package MyPokerGame;

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

public class Player {
    private int ID;
    private String name;
    List<Poker> ownPoker ;

    public Player(){
        ownPoker = new ArrayList<Poker>();
    }

    public void init(){
        Scanner input = new Scanner(System.in);
        System.out.println("请输入选手ID(必须为整数):");
        int initID = input.nextInt();
        System.out.println("请输入选手姓名:");
        String initName = input.next();
        this.ID = initID;
        this.name = initName;
        System.out.println("---------");
    }

    public int getID() {
        return ID;
    }

    public void setID(int iD) {
        ID = iD;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
package MyPokerGame;public class Poker {
private String color;
private int number;

public Poker(String color,int number){
    this.color = color;`输入代码`
    this.number = number;
}

public String getColor() {
    return color;
}

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

public int getNumber() {
    return number;
}

public void setNumber(int number) {
    this.number = number;
}

public String PokerName(){
    switch(this.number){
    case 11:
        return color + "J";
    case 12:
        return color + "Q";
    case 13:
        return color + "K";
    case 14:
        return color + "A";
    default:
        return color + number;
    }
}

public int colorInt(){
    String[] colors = new String[]{"黑桃", "红桃", "梅花", "方片"};
    for(int i=0;i<4;i++){
        if(this.getColor().equals(colors[i])){
            return i;
        }
    }
    return -1;      
}

public Poker comparaTo(Poker poker){
    if(this.number>poker.number){
        return this;
    }else if(this.number<poker.number){
        return poker;
    }else{
        if(this.colorInt()>poker.colorInt()){
            return this;
        }else{
            return poker;
        }
    }
}

}

package MyPokerGame;

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

public class StartGame {
    public static void main(String[] args){
        List<Poker> pokers = new ArrayList<Poker>();
        String[] colors = new String[]{"黑桃", "红桃", "梅花", "方片"};
        for(int i=2;i<=14;i++){
            for(String color:colors){
                pokers.add(new Poker(color,i));
            }
        }
        System.out.println("游戏开始");
        Collections.shuffle(pokers);
        for(Poker poker:pokers){
            System.out.print(poker.PokerName());
        }
        System.out.println("-------------");

        System.out.println("创建第一位玩家");
        Player play1 = new Player();
        play1.init();
        System.out.println("创建第二位玩家");
        Player play2 = new Player();
        play2.init();
        System.out.println("--------创建完毕---------");

        play1.ownPoker.add(pokers.get(0));
        play2.ownPoker.add(pokers.get(1));
        play1.ownPoker.add(pokers.get(2));
        play2.ownPoker.add(pokers.get(3));

        System.out.println("发牌结束,开始比点数");

        Poker play1Max = play1.ownPoker.get(0).comparaTo(play1.ownPoker.get(1));
        Poker play2Max = play2.ownPoker.get(0).comparaTo(play2.ownPoker.get(1));

        if(play1Max.comparaTo(play2Max)==play1Max){
            System.out.println("玩家:"+play1.getName()+"获胜");
        }else{
            System.out.println("玩家:"+play2.getName()+"获胜");
        }
        System.out.println(play1.getName()+"的牌是"+play1.ownPoker.get(0).getColor()+play1.ownPoker.get(0).getNumber()+"  "+play1.ownPoker.get(1).getColor()+play1.ownPoker.get(1).getNumber());
        System.out.println(play2.getName()+"的牌是"+play2.ownPoker.get(0).getColor()+play2.ownPoker.get(0).getNumber()+"  "+play2.ownPoker.get(1).getColor()+play2.ownPoker.get(1).getNumber());
    }

}
14人推荐
随时随地看视频
慕课网APP

热门评论

这个能收藏吗?…啊啊啊啊

颜色比较是不是错了,黑桃为0,红桃为1,梅花为2,方块为3,所以,比较花色应该为this.colorInt()<poker.colorInt()
,才有下面的结果吧???

查看全部评论