实验五_简单游戏编程

本项目由多个小游戏组成,运行程序后出现一个菜单,选择数字1,2,……进入相应的游戏程序。游戏完成后,返回本界面,选择数字0,退出程序。

进入程序后菜单如下:

1、彩票。 参考题3.15, to generate a lottery of a three digit number. The program prompts the user to enter a three-digit number and determines whether the user wins according to the following rules:

(1). If the user input matches the lottery number in the exact order, the award is

$10,000.

(2). If all digits in the user input match all digits in the lottery number, the award is $3,000.

(3). If one digit in the user input matches a digit in the lottery number, the award is

$1,000.

2、剪刀,石头,布。 参考题5.34,

(Game: scissor, rock, paper) Write a program that plays the popular scissor-rockpaper game. (A scissor can cut a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly generates a number 0, 1, or 2 representing scissor, rock, and paper. The program prompts the user to enter a number 0, 1, or 2 and displays a message indicating whether the user or the computer wins, loses, or draws. Here are sample runs:.

Revise the program to let the user conscientiously play until either the user or the computer wins more than two times than its opponent.

3、豆机

The bean machine, also known as a quincunx or the Galton box(高尔顿瓶), is a device for statistics experiments named after English scientist Sir Francis Galton. It consists of an upright board with evenly spaced nails (or pegs) in a triangular form, as shown in Figure below.

Balls are dropped from the opening of the board. Every time a ball hits a nail, it has a 50% chance of falling to the left or to the right. The piles of balls are accumulated in the slots at the bottom of the board.

Write a program that simulates the bean machine. Your program should prompt the user to enter the number of the balls and the number of the slots in the machine. Simulate the falling of each ball by printing its path. For example, the path for the ball in Figure b is LLRRLLR and the path for the ball in Figure c is RLRRLRR. Display the final buildup of the balls in the slots in a histogram. Here is a sample run of the program:

(Hint: Create an array named slots. Each element in slots stores the number of balls in a slot. Each ball falls into a slot via a path. The number of Rs in a path is the position of the slot where the ball falls. For example, for the path LRLRLRR, the ball falls into slots[4], and for the path RRLLLLL, the ball falls into

slots[2].)

4、随机选四张牌,计算之和正好等于24的次数。

 Write a program that picks four cards from a deck of 52 cards and computes their sum. An Ace, King, Queen, and Jack represent 1, 13, 12, and 11, respectively. Your program should display the number of picks that yields the sum of 24.

0.退出

发挥自己的想象力。使游戏更有趣。


import java.util.Scanner;

public class P5 {
    public static void main(String[] args) {

    }

    Scanner sc = new Scanner(System.in);
    int random;
    boolean logical = true;//判断何时终止最外层循环
    boolean logic = true;//判断合适终止内层循环
    int input;//存储从键盘上输入的数
    String str1;
    String str2;

    public void driver() {
        while(logical) {
            System.out.println("1.彩票  2.石头、剪刀、布  3.豆机  4、随机四张牌,计算和等于24的次数  0、退出游戏");
            input = sc.nextInt();
            switch (input) {
                case 0:
                    exit();
                    break;
                case 1:
                    lotteryTicket();
                    break;
                case 2:
                    rock_paper_scissors();
                    break;
                case 3:
                    GaltonKB();
                    break;
                case 4:
                    Count_24_times();
                    break;
                default:
                    System.out.println("请不要乱选,重新选择。");
            }
        }
    }


    public void lotteryTicket(){
        logic = true;
        while(logic) {
            System.out.print("请输入一个三位数字[100,999]:");
            random = Random(100,999);
            int input = sc.nextInt();
            if(input <100||input>999){
                System.out.println("输入错误");
                lotteryTicket();
            }

            if (isS1(random,input)) {
                System.out.println("恭喜您完全命中,获得$10,000的奖金。");
            } else if (isS2(random,input)) {
                System.out.println("恭喜您数字全中,获得$3,000的奖金。");
            } else if (isS3(random,input)) {
                System.out.println("恭喜您猜对一个数字,获得$1,000的奖金。");
            } else {
                System.out.println("很遗憾,没有数字正确。");
            }
            logic = isExit();

        }


    }

    public void rock_paper_scissors(){
        logic = true;
        while(logic) {
            int count1 = 0;//累加器,累计用户赢了几把
            int count2 = 0;//累计计算机赢了几把
            while (true){
                if(count1 == 2){
                    System.out.println("You won!");
                    break;
                }else if(count2 == 2){
                    System.out.println("You lose.");
                    break;
                }else{
                    System.out.print("scissor(0),rock(1),paper(2):");
                    random = Random(0,2);
                    input = sc.nextInt();
                    switch(input){
                        case 0:
                            switch(random){
                                case 0:
                                    System.out.println("The computer is scisser. You are scisser. It is a draw");
                                    break;
                                case 1:
                                    System.out.println("The computer is rock. You are scisser. You lose");
                                    count2++;
                                    break;
                                case 2:
                                    System.out.println("The computer is paper. You are scisser. You won");
                                    count1++;
                                    break;
                            }
                            break;
                        case 1:
                            switch(random){
                                case 0:
                                    System.out.println("The computer is scisser. You are rock. You won");
                                    count1++;
                                    break;
                                case 1:
                                    System.out.println("The computer is rock. You are rock. It is a draw");
                                    break;
                                case 2:
                                    System.out.println("The computer is paper. You are rock. You lose");
                                    count2++;
                                    break;
                            }
                            break;
                        case 2:
                            switch(random){
                                case 0:
                                    System.out.println("The computer is scisser. You are paper. You lose");
                                    count2++;
                                    break;
                                case 1:
                                    System.out.println("The computer is rock. You are paper. You won");
                                    count1++;
                                    break;
                                case 2:
                                    System.out.println("The computer is paper. You are paper. It is a draw");
                                    break;
                            }
                            break;
                        default:
                            System.out.println("请不要瞎搞");
                    }

                }

            }
            logic = isExit();
        }
    }

    public void GaltonKB(){
        logic = true;
        int input1 = 0;//存储球数
        int input2 = 0;//存储槽数
        int right = 0;//累计左边的次数
        int lift = 0;//累计右边的次数
        while(logic) {
            System.out.print("Enter the number of balls to drop:");
            input1 = sc.nextInt();
            if(input1<=0) {
                System.out.println("不要瞎搞");
                continue;
            }
            System.out.println();
            System.out.print("Enter the number of slots in the bean machine:");
            input2 = sc.nextInt();
            if(input2<=1) {
                System.out.println("不要瞎搞");
                continue;
            }
            System.out.println();
            int[] slot1 = new int[input2];
            for(int k = 0;k<input1;k++) {
                right = 0;
                lift = 0;
                for (int i = 0; i < input2 - 1; i++) {
                    random = Random(0, 1);
                    //判断走的路径,并打印
                    if (random == 0) {
                        System.out.print('L');
                        lift++;
                    } else {
                        System.out.print('R');
                        right++;//累计走右边的次数
                    }
                }
                System.out.println();
                slot1[right]++;
            }
            printslot(maximum(slot1),slot1);
            //打印每个槽内的球数

            System.out.println();
            logic = isExit();
        }
    }

    public void Count_24_times(){
        logic = true;
        int decor;//存储花色
        int num;//存储数字
        int sum = 0;//存储四张牌数字之和
        int count = 0;
        while(logic) {
            count = 0;
            System.out.print("请输入计算次数:");
            input = sc.nextInt();
            if(input<=0){
                System.out.println("请不要瞎搞,重新输入");
                continue;
            }
            for(int i = 0;i<input;i++) {
                int[][] desk = new int[4][13];
                sum = 0;
                for (int j = 0; j < 4; j++) {

                    decor = Random(0, 3);
                    num = Random(0, 12);
                    //判断是否取过
                    if (desk[decor][num] == 1) {
                        j--;
                        continue;
                    }
                    desk[decor][num] = 1;//取过的牌记为1
                    sum += num+1;
                }
                if (sum == 24) {
                    count++;
                    System.out.print("您抽到的牌是:");
                    //打印牌种
                    for (int k = 0; k < 4; k++) {
                        for (int l = 0; l < 13; l++) {
                            if (desk[k][l] == 1) {
                                switch (k) {
                                    case 0:
                                        System.out.print("黑桃");
                                        break;
                                    case 1:
                                        System.out.print("红桃");
                                        break;
                                    case 2:
                                        System.out.print("方块");
                                        break;
                                    case 3:
                                        System.out.print("梅花");
                                        break;
                                }
                                switch (l) {
                                    case 0:
                                        System.out.print("Ace" + "   ");
                                        break;
                                    case 1:
                                        System.out.print("2" + "     ");
                                        break;
                                    case 2:
                                        System.out.print("3" + "     ");
                                        break;
                                    case 3:
                                        System.out.print("4" + "     ");
                                        break;
                                    case 4:
                                        System.out.print("5" + "     ");
                                        break;
                                    case 5:
                                        System.out.print("6" + "     ");
                                        break;
                                    case 6:
                                        System.out.print("7" + "     ");
                                        break;
                                    case 7:
                                        System.out.print("8" + "     ");
                                        break;
                                    case 8:
                                        System.out.print("9" + "     ");
                                        break;
                                    case 9:
                                        System.out.print("10" + "    ");
                                        break;
                                    case 10:
                                        System.out.print("Jack" + "  ");
                                        break;
                                    case 11:
                                        System.out.print("Queen" + " ");
                                        break;
                                    case 12:
                                        System.out.print("King" + "  ");
                                        break;
                                }
                            }
                        }
                    }
                    System.out.println();
                }
            }
            System.out.println();
            //打印24的次数
            System.out.println("和正好等于24的次数为:"+count);
            logic = isExit();
        }
    }
    //退出程序
    public void exit(){
        System.out.println("感谢您的游玩!");
        logical = false;
    }
    //取随机数
    public int Random(int n, int m){
        int number = (int)(n+(Math.random()*(m-n+1)));
        return number;
    }
    //选择是否退出
    public boolean isExit(){
        System.out.println("1、继续游玩  0、退出本游戏");
        input = sc.nextInt();
        if(input == 1){
            System.out.println("你选择了继续游戏。");
            return true;
        }else if(input == 0){
            System.out.println("你选择了结束游戏。");
            return false;
        }else{
            System.out.println("请不要瞎搞,重新选择。");
            isExit();
        }

        return false;
    }
    //完全相等的情况
    public boolean isS1(int random,int input){
        if(random == input){
            return true;
        }else{
            return false;
        }
    }
    //数字一样的情况
    public boolean isS2(int random,int input) {
        int[] inputarr = new int[3];
        int[] randomarr = new int[3];
        int temp;
        for (int i = 0; i < 3; i++) {
            inputarr[i] = input % 10;
            input /= 10;
        }
        temp = random;
        for (int j = 0; j < 3; j++) {
            randomarr[j] = temp % 10;
            temp /= 10;
        }
        if (isS2(inputarr,random)) {
            return true;
        }else{
            return false;
        }
    }

    public boolean isS2(int[] inputarr,int random){
        for(int i = 0;i<inputarr.length;i++){
            for(int j = 0;j<inputarr.length;j++){
                for(int k = 0;k<inputarr.length;k++){
                    if((inputarr[i]*100+inputarr[j]*10+inputarr[k]) == random){
                        return true;
                    }
                }
            }
        }
        return false;
    }
    //一个数字一样的情况
    public boolean isS3(int random,int input){
        int num1;//存input上的每个数字
        int num2;//存random上的每个数字
        int temp;//在第二次循环的时候临时存储random
        boolean[][] is = new boolean[3][3] ;
        for(int i = 0;i<3;i++){
            num1 = input%10;
            input = input/10;
            temp = random;
            for(int j = 0;j<3;j++){
                num2 = temp % 10;
                temp = temp/10;
                if(num1 == num2){
                    return true;
                }
            }
        }
        return false;
    }

    //取数组中的最大值
    public static int maximum(int[] a) {
        int max=0;
        for(int i=0;i<a.length;i++){
            if(a[i]>max){
                max=a[i];
            }
        }
        return max;
    }

    //打印出球的落点
    public static void printslot(int max, int[] slot){
        for(int i = max; i > 0; i--) {
            for(int j = 0; j < slot.length; j++) {
                if(slot[j] == i) {
                    System.out.print("0");
                    slot[j]--;
                }
                else
                    System.out.print(" ");
            }
            System.out.print("");
            System.out.println("");
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Swain_Woo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值