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

简单的扑克牌游戏。。

标签:
Java

package head_first;

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

public class Card {
static Scanner scanner;
static ArrayList<String> arrayList;
static String max;

public static void main(String[] args) {
    arrayList = new ArrayList<>();
    String card[] = { "红桃A", "黑桃A", "方块A", "梅花A", "红桃2", "黑桃2", "方块2", "梅花2", "红桃3", "黑桃3", "方块3", "梅花3", "红桃4",
            "黑桃4", "方块4", "梅花4", "红桃5", "黑桃5", "方块5", "梅花5", "红桃6", "黑桃6", "方块6", "梅花6", "红桃7", "黑桃7", "方块7", "梅花7",
            "红桃8", "黑桃8", "方块8", "梅花8", "红桃9", "黑桃9", "方块9", "梅花9", "红桃10", "黑桃10", "方块10", "梅花10", "红桃J", "黑桃J",
            "方块J", "梅花J", "红桃Q", "黑桃Q", "方块Q", "梅花Q", "红桃K", "黑桃K", "方块K", "梅花K", };
    System.out.println("---------扑克牌创建成功---------");
    for (String string : card) {
        System.out.print(string + " ");
    }
    System.out.println();
    System.out.println("---------扑克牌开始洗牌---------");
    String str;
    for (int i = 0; i < card.length; i++) {
        do {
            str = card[(int) (Math.random() * 52)];
        } while (arrayList.contains(str));
        arrayList.add(str);
    }
    for (String string : arrayList) {
        System.out.print(string + " ");
    }
    System.out.println();
    System.out.println("---------扑克牌洗牌结束---------");
    System.out.println("---------创建游戏玩家 ---------");
    Player player1 = new Player();
    Player player2 = new Player();
    input_id(player1);
    input_name(player1);
    input_id(player2);
    input_name(player2);
    System.out.println("----------" + "欢迎玩家:" + player1.getName() + "--------");
    System.out.println("----------" + "欢迎玩家:" + player2.getName() + "--------");
    System.out.println("--------------开始发牌-------------------");
    int n = (int) Math.random();
    switch (n) {
    case 0:
        System.out.println("玩家:" + player1.getName() + "先发牌");
        input_card(player1, 0, 0);
        input_card(player2, 1, 0);
        input_card(player1, 2, 1);
        input_card(player2, 3, 1);
        break;
    default:
        System.out.println("玩家:" + player2.getName() + "先发牌");
        input_card(player2, 0, 0);
        input_card(player1, 1, 0);
        input_card(player2, 2, 1);
        input_card(player1, 3, 1);
        break;
    }
    System.out.println("---------------发牌结束-------------------");
    System.out.println("---------------开始游戏--------------");
    max_card(player1.getPlay_card(0), player1.getPlay_card(1));
    String play1 = max;
    System.out.println("玩家:" + player1.getName() + "最大牌为" + max);
    max_card(player2.getPlay_card(0), player2.getPlay_card(1));
    String play2 = max;
    System.out.println("玩家:" + player2.getName() + "最大牌为" + max);
    max_card(play1, play2);
    if(max.equals("平局"))
        System.out.println("平局");
    else{
    if (max.equals(play1))
        System.out.println("玩家:" + player1.getName() + "获胜");
    else
        System.out.println("玩家:" + player2.getName() + "获胜");
    }
    System.out.println("玩家各自牌为:");
    System.out.println("玩家:" + player1.getName());
    System.out.println(player1.getPlay_card(0) + " " + player1.getPlay_card(1));
    System.out.println("玩家:" + player2.getName());
    System.out.println(player2.getPlay_card(0) + " " + player2.getPlay_card(1));
}

public static void input_id(Player play) {
    scanner = new Scanner(System.in);
    System.out.println("请输入玩家id:");
    try {
        play.setId(scanner.nextInt());
    } catch (Exception e) {
        System.out.println("输入id不合法,请重新输入");
        scanner = new Scanner(System.in);
        play.setId(scanner.nextInt());
    }
}

public static void input_name(Player play) {
    scanner = new Scanner(System.in);
    System.out.println("请输入玩家姓名:");
    play.setName(scanner.next());
}

public static void input_card(Player play, int i, int j) {
    System.out.println("玩家:" + play.getName() + "拿牌");
    play.setPlay_card(arrayList.get(i), j);
}

static int a;
static int b;

public static void max_card(String str, String str1) {
    a = str.charAt(2);
    b = str1.charAt(2);
    if (str.charAt(2) == '2')
        a = 97;
    if (str1.charAt(2) == '2')
        b = 97;
    if (str.charAt(2) == 'A')
        a = 96;
    if (str1.charAt(2) == 'A')
        b = 96;
    if (str.charAt(2) == 'K')
        a = 95;
    if (str1.charAt(2) == 'K')
        b = 95;
    if (str.charAt(2) == 'Q')
        a = 94;
    if (str1.charAt(2) == 'Q')
        b = 94;
    if (str.charAt(2) == 'J')
        a = 93;
    if (str1.charAt(2) == 'J')
        b = 93;
            if(str.charAt(2) == '1')
        a=92;
    if(str1.charAt(2) == '1')
        b=92;
    System.out.println("a=" + a + " " + "b=" + b);
    if (a == b) {
        char c = str.charAt(0);
        char d = str1.charAt(0);
        if (c == d)
            max = "平局";
        if (c == '黑')
            max = str;
        if (d == '黑')
            max = str1;
        if (c == '红')
            max = str;
        if (d == '红')
            max = str1;
        if (c == '梅')
            max = str;
        if (d == '梅')
            max = str1;
    }
    if (a > b) {
        max = str;
    }
    if (a < b) {
        max = str1;
    }
}

}

package head_first;

import java.util.ArrayList;

public class Player {
private int id;
private String name;
private String Play_card[]=new String[2];

public String getPlay_card(int i) {
    return Play_card[i];
}
public void setPlay_card(String play_card,int i) {
    Play_card[i] = play_card;
}
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

}

点击查看更多内容
1人点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消