为了账号安全,请及时绑定邮箱和手机立即绑定

java入门最后一章 简易扑克牌比较大小

标签:
Java

package com.practice.zhipai;

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

public class PracticeZhiPai {
public List<String> list;
public String tempStr;
public List<User> listUser;
public List<String> user1List;//用户1的手牌
public List<String> user2List;//用户2的手牌
public void createList(){
list = new ArrayList<String>();

    for (int i = 0; i < 4; i++) {
        if (i==0) {
            tempStr="黑桃";
        }
        else if (i==1) {
            tempStr="红桃";
        }
        else if (i==2) {
            tempStr="梅花";
        }
        else{
            tempStr="方片";
        }
        for (int j = 1; j <= 13; j++) {
            if (j==1) {
                list.add(tempStr+"A");
            }
            else if(j==11){
                list.add(tempStr+"J");
            }
            else if(j==12){
                list.add(tempStr+"Q");
            }
            else if(j==13){
                list.add(tempStr+"K");
            }
            else{
                list.add(tempStr+j);
            }
        }
    }

    for (String string : list) {
        System.out.print(string+",");
    }
    System.out.println("-----------扑克牌创建成功--------------");
}
public void xiPai(){
    System.out.println("-----------开始洗牌--------------");
    Collections.shuffle(list);
    System.out.println("-----------洗牌结束--------------");
}
public void createUser(){
    listUser =new ArrayList<User>();
    System.out.println("-----------创建玩家--------------");
    Scanner scan = new Scanner(System.in);
    for (int i = 0; i < 2; i++) {
        System.out.println("-----------请输入第"+(i+1)+"位玩家的id--------------");
        String id= scan.next();
        System.out.println("-----------请输入第"+(i+1)+"位玩家的姓名--------------");
        String name = scan.next();
        User user = new User(id, name);
        listUser.add(user);
    }

    for (User user : listUser) {
        System.out.println("欢迎玩家"+user.getName());
    }

}
public void faPai(){
    System.out.println("-----------开始发牌--------------");
    user1List = new ArrayList<String>();
    user2List = new ArrayList<String>();

    user1List.add(list.get(0));
    user2List.add(list.get(1));
    user1List.add(list.get(2));
    user2List.add(list.get(3));
    System.out.println("-----------发牌结束--------------");
}
public void comparePai(){
    String user1Max;
    String user2Max;
    PracticeZhiPai pz = new PracticeZhiPai();
    //比较user1手牌哪个大
    //result=0 相等  result=1 a > b  result=-1 a < b
    int result = pz.compareA(user1List.get(0), user1List.get(1));
    if (result == 0) {
        user1Max = user1List.get(0);
        System.out.println("User1两张牌一样大");
    }
    else if (result == -1) {
        user1Max = user1List.get(1);
        System.out.println("User1大的牌是"+user1List.get(1));
    }
    else {
        user1Max = user1List.get(0);
        System.out.println("User1大的牌是"+user1List.get(0));
    }

    //比较user2手牌哪个大
    //result=0 相等  result=1 a > b  result=-1 a < b
    int result1 = pz.compareA(user2List.get(0), user2List.get(1));
    if (result1 == 0) {
        user2Max = user2List.get(0);
        System.out.println("User2两张牌一样大");
    }
    else if (result1 == -1) {
        user2Max = user2List.get(1);
        System.out.println("User2大的牌是"+user2List.get(1));
    }
    else {
        user2Max = user2List.get(0);
        System.out.println("User2大的牌是"+user2List.get(0));
    }

    //比较两个人最大的手牌
    int finalResult = pz.compareA(user1Max, user2Max);
    if (finalResult == 0) {
        System.out.println("平局");
    }
    else if (finalResult == -1) {
        System.out.println("User2获胜");
    }
    else {
        System.out.println("User1获胜");
    }

    //打印每个用户的手牌
    for (int i = 0; i < 2; i++) {
        System.out.println("User1的手牌是:"+user1List.get(i));
    }

    //打印每个用户的手牌
    for (int i = 0; i < 2; i++) {
        System.out.println("User2的手牌是:"+user2List.get(i));
    }
}
public int compareA(String a,String b){
    int u1;
    if (a.substring(2).equals("J")) {
        u1=11;
    }
    else if (a.substring(2).equals("Q")){
        u1 = 12;
    }
    else if (a.substring(2).equals("K")){
        u1=13;
    }
    else if (a.substring(2).equals("A")){
        u1=1;
    }
    else{
        u1=Integer.parseInt(a.substring(2));
    }

    int u2;
    if (b.substring(2).equals("J")) {
        u2=11;
    }
    else if (b.substring(2).equals("Q")){
        u2 = 12;
    }
    else if (b.substring(2).equals("K")){
        u2=13;
    }
    else if (b.substring(2).equals("A")){
        u2=1;
    }
    else{
        u2=Integer.parseInt(b.substring(2));
    }

    //return 1表示a大于b   return -1表示a小于b return 0表示相等
    if (u1 > u2) {
        return 1;
    }
    else if (u1 < u2) {
        return -1;
    }
    else{
        //取是什么花色的

        if (a.substring(0,2).equals("黑桃")) {
            //数字一样 花色也一样 那就是一样大
            if (b.substring(0,2).equals("黑桃")) {
                return 0;
            }
            else if (b.substring(0,2).equals("红桃")) {
                return 1;
            }
            else if (b.substring(0,2).equals("梅花")) {
                return 1;
            }
            else if (b.substring(0,2).equals("方片")) {
                return 1;
            }
        }
        else if (a.substring(0,2).equals("红桃")) {
            if (b.substring(0,2).equals("黑桃")) {
                return -1;
            }
            else if (b.substring(0,2).equals("红桃")) {
                return 0;
            }
            else if (b.substring(0,2).equals("梅花")) {
                return 1;
            }
            else if (b.substring(0,2).equals("方片")) {
                return 1;
            }
        }
        else if (a.substring(0,2).equals("梅花")) {
            if (b.substring(0,2).equals("黑桃")) {
                return -1;
            }
            else if (b.substring(0,2).equals("红桃")) {
                return -1;
            }
            else if (b.substring(0,2).equals("梅花")) {
                return 0;
            }
            else if (b.substring(0,2).equals("方片")) {
                return 1;
            }
        }
        //(a.substring(0,2).equals("方片")) 
        else{
            if (b.substring(0,2).equals("黑桃")) {
                return -1;
            }
            else if (b.substring(0,2).equals("红桃")) {
                return -1;
            }
            else if (b.substring(0,2).equals("梅花")) {
                return -1;
            }
            else if (b.substring(0,2).equals("方片")) {
                return 0;
            }
        }
    }
    //不能走到这里
    return -2;
}
public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println("-----------创建扑克牌--------------");
    PracticeZhiPai pp = new PracticeZhiPai();
    pp.createList();

    pp.xiPai();

    pp.createUser();

    pp.faPai();

    pp.comparePai();
}

}

class User.java
package com.practice.zhipai;

public class User {
public User(String id,String name) {
// TODO Auto-generated constructor stub
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String id;
private String name;
}

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消