用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

java赌博程序

2012-10-03 作者: 小蜜锋举报

[java]代码库

import java.util.Scanner;
 
/**
 * java赌博程序
 *
 */
public class CardGame {
    static int[] twoHandsCards = new int[6];
    static int userAMoney = 0;
    static int userBMoney = 0;
 
    public static void main(String[] args) {
 
        System.out.println("欢迎光临【挨踢赌场】 ***马无夜草不肥,人无横财不富!*** ");
 
        Scanner s = new Scanner(System.in);
        System.out.println("请输入总赌本:");
        userAMoney = s.nextInt();
        userBMoney = userAMoney;
        int currentMoney = 0;
        while (userAMoney > 0 && userBMoney > 0) {
            System.out.println("请输入本局赌注:");
            currentMoney = s.nextInt();
            if (currentMoney <= 0 || currentMoney > userAMoney
                    || currentMoney > userBMoney) {
                System.out.println("电脑或你的赌注不足,请重新输入!");
                continue;
            }
            dispatchCards();
 
            printCards(twoHandsCards);
 
            if (compare() > 0) {
                userAMoney += currentMoney;
                userBMoney -= currentMoney;
                System.out.println("玩家获胜!");
            } else if (compare() < 0) {
                userAMoney -= currentMoney;
                userBMoney += currentMoney;
                System.out.println("电脑获胜!");
            } else {
                System.out.println("平局!");
            }
 
            System.out.println("玩家赌本:" + userAMoney + ";电脑赌本:" + userBMoney);
        }
 
        if (userAMoney <= 0) {
            System.out.println("你输光老本了,请下次再来!");
        } else {
            System.out.println("电脑已经输光老本了,你行行好吧!");
        }
 
    }
 
    /**
     * 分牌
     */
    public static void dispatchCards() {
        int i = 0;
        while (i < twoHandsCards.length) {
            int h = (int) (Math.random() * 4) + 1;
            int d = (int) (Math.random() * 13) + 2;
            int j = 0;
            for (; j < i; j++) {
                if (twoHandsCards[j] == h * 100 + d) {
                    break;
                }
            }
            if (j < i) {
                continue;
            } else {
                twoHandsCards[i++] = h * 100 + d;
            }
        }
    }
 
    /**
     * 打印双方的牌
     *
     * @param oneHandCards
     */
    public static void printCards(int[] oneHandCards) {
        for (int i = 0; i < oneHandCards.length; i++) {
            if (i == 0) {
                System.out.print("你的牌:");
            }
 
            if (i == 3) {
                System.out.print("\t电脑的牌:");
            }
 
            switch (oneHandCards[i] / 100) {
            case 1:
                System.out.print("黑");
                break;
            case 2:
                System.out.print("红");
                break;
            case 3:
                System.out.print("梅");
                break;
            case 4:
                System.out.print("方");
                break;
            }
            if (oneHandCards[i] % 100 <= 10) {
                System.out.print(oneHandCards[i] % 100);
            } else {
                switch (oneHandCards[i] % 100) {
                case 11:
                    System.out.print("J");
                    break;
                case 12:
                    System.out.print("Q");
                    break;
                case 13:
                    System.out.print("K");
                    break;
                case 14:
                    System.out.print("A");
                    break;
                }
            }
 
            System.out.print("  ");
 
        }
        System.out.println();
    }
 
    public static int compare() {
        int[] aCards = new int[3];
        int[] bCards = new int[3];
        for (int i = 0; i < 3; i++) {
            aCards[i] = twoHandsCards[i];
        }
        for (int i = 3; i < 6; i++) {
            bCards[i - 3] = twoHandsCards[i];
        }
        return fromCardsToNumber(aCards) - fromCardsToNumber(bCards);
    }
 
    public static int fromCardsToNumber(int[] oneHandCards) {
        for (int i = 0; i < oneHandCards.length - 1; i++) {
            for (int j = i + 1; j < oneHandCards.length; j++) {
                if (oneHandCards[i] % 100 < oneHandCards[j] % 100) {
                    int temp = oneHandCards[i];
                    oneHandCards[i] = oneHandCards[j];
                    oneHandCards[j] = temp;
                }
            }
        }
        int num = 0;
 
        if (oneHandCards[0] % 100 == oneHandCards[1] % 100
                && oneHandCards[1] % 100 == oneHandCards[2] % 100) {
            num = 6 * 1000000 + oneHandCards[0] % 100 * 10000 + oneHandCards[1]
                    % 100 * 100 + oneHandCards[2] % 100;
        } else if (oneHandCards[0] / 100 == oneHandCards[1] / 100
                && oneHandCards[1] / 100 == oneHandCards[2] / 100
                && oneHandCards[0] % 100 == oneHandCards[1] % 100 + 1
                && oneHandCards[1] % 100 == oneHandCards[2] % 100 + 1) {
            num = 5 * 1000000 + oneHandCards[0] % 100 * 10000 + oneHandCards[1]
                    % 100 * 100 + oneHandCards[2] % 100;
        } else if (oneHandCards[0] / 100 == oneHandCards[1] / 100
                && oneHandCards[1] / 100 == oneHandCards[2] / 100) {
            num = 4 * 1000000 + oneHandCards[0] % 100 * 10000 + oneHandCards[1]
                    % 100 * 100 + oneHandCards[2] % 100;
        } else if (oneHandCards[0] % 100 == oneHandCards[1] % 100 + 1
                && oneHandCards[1] % 100 == oneHandCards[2] % 100 + 1) {
            num = 3 * 1000000 + oneHandCards[0] % 100 * 10000 + oneHandCards[1]
                    % 100 * 100 + oneHandCards[2] % 100;
        } else if (oneHandCards[0] % 100 == oneHandCards[1] % 100) {
            num = 2 * 1000000 + oneHandCards[0] % 100 * 10000 + oneHandCards[1]
                    % 100 * 100 + oneHandCards[2] % 100;
        } else if (oneHandCards[1] % 100 == oneHandCards[2] % 100) {
            num = 2 * 1000000 + oneHandCards[1] % 100 * 10000 + oneHandCards[2]
                    % 100 * 100 + oneHandCards[0] % 100;
        } else {
            num = 1 * 1000000 + oneHandCards[0] % 100 * 10000 + oneHandCards[1]
                    % 100 * 100 + oneHandCards[2] % 100;
        }
 
        return num;
 
    }
 
}

[代码运行效果截图]


java赌博程序


网友评论    (发表评论)

共2 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...