首页 > 编程语言 > 详细

java实现数字炸弹人机对局

时间:2021-04-22 15:42:31      阅读:15      评论:0      收藏:0      [点我收藏+]

(本文是CSDN中的博客,今天搬迁至此)

上一篇实现了简单的单人游戏的数字炸弹,这次我又改进了一下,让玩家和电脑对局。

话不多说,直接上代码:

import java.util.Scanner;

/**
 * @Author:yxq
 * @Date: 2020/7/19 21:02
 * @Tools: IntelliJ IDEA
 **/

/*
  数字炸弹版本2,有电脑参与
 */
public class DigitalBomb2 {
    public static void main(String[] args) throws InterruptedException {
        int bomb = (int) (100 * Math.random());     //定义随机炸弹数
        int front = 0, behind = 100, person_guess = 0, computer_guess = 0;       //定义范围边界
        int flog = 1, guess = 0;       //flog是标识人还是电脑猜,1表示人,0表示电脑

        Scanner input = new Scanner(System.in);


        //没猜对,一直循环
        while (person_guess != bomb && computer_guess != bomb) {
            System.out.println("在" + front + "~" + behind + "之间");

            if (flog == 1) {
                System.out.print("你猜的是:");
                person_guess = input.nextInt();
                System.out.println();
                guess = person_guess;
                flog = 0;
                if (guess > bomb) {
                    behind = guess;
                } else {
                    front = guess;
                }
                Thread.sleep(2000);     //使其等待2秒,否则太快
            } else {
                System.out.print("电脑猜的是:");
                computer_guess = (int) (Math.random() * (behind - front) + front);     //电脑猜的数,使电脑随机生成范围内的整数
                guess = computer_guess;
                System.out.println(computer_guess);
                System.out.println();
                flog = 1;
                if (guess > bomb) {
                    behind = guess;
                } else {
                    front = guess;
                }
                Thread.sleep(2000);     //使其等待2秒,否则太快
            }

        }

        if (person_guess == bomb) {
            System.out.println("\\\\\\!!!!!!!!!!!!!!!//////");
            System.out.println("------!!!!!BOOM!!!!!!------");
            System.out.println("//////!!!!!!!!!!!!!!!\\\\\\");
            System.out.println();
            System.out.println("≥◇≤");
            System.out.println("你完了!!!");
            System.out.println("哦!!!  炸弹数字就是" + bomb);
        }
        if (computer_guess == bomb) {
            System.out.println("\\\\\\!!!!!!!!!!!!!!!//////");
            System.out.println("------!!!!!BOOM!!!!!!------");
            System.out.println("//////!!!!!!!!!!!!!!!\\\\\\");
            System.out.println();
            System.out.println("≥◇≤");
            System.out.println("电脑完了!!!");
            System.out.println("哦!!!  炸弹数字就是" + bomb);
        }
    }
}

电脑猜的数有时可能会超出范围,但我多次实验,没有超出的情况。还有就是会猜得和玩家的一样。

仅是娱乐,若有不足,欢迎指出,洗耳恭听。

java实现数字炸弹人机对局

原文:https://www.cnblogs.com/XQ-Yang/p/14689270.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!