1.人机猜拳

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1.人机猜拳相关的知识,希望对你有一定的参考价值。

package zuoYe;

import java.util.Scanner;

public class 人机猜拳 {
    boolean can = true;
    Scanner input = new Scanner(System.in);
    String name1;
    int i = 0;
    int j=0;
    int o=0;
    String name;
    public void show() {
        System.out.println("**********欢迎进入游戏世界**********");
        System.out.println("******************");
        System.out.println("**猜拳,开始**");
        System.out.println("******************");
    }

    public void showFist() {
        do {
            System.out.println("出拳规则: 1.剪刀. 2.石头 3.布");
            System.out.println("请选择对方角色(1.刘备 2.孙权 3.曹操)");
            int juese = input.nextInt();
            if (juese == 1) {
                // System.out.println("你选择的对战对手是:" + "刘备");
                name1 = "刘备";
            } else if (juese == 2) {
                // System.out.println("你选择的对手是:孙权");
                name1 = "孙权";
            } else if (juese == 3) {
                // System.out.println("你选择的对手是:曹操");
                name1 = "曹操";
            } else {
                System.out.println("你输入有误,请从新输入");
                can = true;
            }
            System.out.println("请输入你的姓名:");
             name = input.next();
            System.out.println(name + "VS" + name1);
            can = false;
        } while (can);
    }

    public void showTwo() {
        System.out.println("要开始么?  (y/n)");
        char xuanze = input.next().charAt(0);
        if (xuanze == ‘n‘) {
            showFist();
        } else if (xuanze == ‘y‘) {
            o++;
            // while (xuanze == ‘y‘) {
            System.out.println("请出拳: 1.石头 2.剪刀 3.布(输入相应数字)");
            int chu = input.nextInt();
            switch (chu) {
            case 1:
                System.out.println("你出拳:石头");
                break;
            case 2:
                System.out.println("你出拳:剪刀");
                break;
            case 3:
                System.out.println("你出拳:布");
                break;
            }
            int a = (int) (Math.random() * 3);
            if (a == 0) {
                a = 3;
            }
            switch (a) {
            case 1:
                System.out.println(name1 + "出拳:石头");
                break;
            case 2:
                System.out.println(name1 + "出拳:剪刀");
                break;
            case 3:
                System.out.println(name1 + "出拳:布");
                break;
            }
            if (chu == a) {
                System.out.println("结果:平局");
            } else if ((chu == 1 && a == 2) || (chu == 2 && a == 3)
                    || (chu == 3 && a == 1)) {
                System.out.println("结果: 恭喜你,赢了! ");
                i++;
            } else if ((chu == 1 && a == 3) || (chu == 2 && a == 1) || chu == 3
                    && a == 2) {
                System.out.println("结果:  真衰,输了!");
                j++;
            }
        }
    }

    public void showThree() {
        System.out.println("是否继续下一轮  (y/n)");
        char ze = input.next().charAt(0);
        if (ze == ‘n‘) {
            showFive();
        }
        while (ze == ‘y‘) {
            o++;
            System.out.println("请出拳: 1.石头 2.剪刀 3.布(输入相应数字)");
            int quan = input.nextInt();
            switch (quan) {
            case 1:
                System.out.println("你出拳:石头");
                break;
            case 2:
                System.out.println("你出拳:剪刀");
                break;
            case 3:
                System.out.println("你出拳:布");
                break;
            }
            int b = (int) (Math.random() * 3);
            if (b == 0) {
                b = 3;
            }
            switch (b) {
            case 1:
                System.out.println(name1 + "出拳:石头");
                break;
            case 2:
                System.out.println(name1 + "出拳:剪刀");
                break;
            case 3:
                System.out.println(name1 + "出拳:布");
                break;
            }
            if (quan == b) {
                System.out.println("结果:平局");
            } else if ((quan == 1 && b == 2) || (quan == 2 && b == 3)
                    || (quan == 3 && b == 1)) {
                System.out.println("结果: 恭喜你,赢了! ");
                i++;
            } else if ((quan == 1 && b == 3) || (quan == 2 && b == 1)
                    || quan == 3 && b == 2) {
                System.out.println("结果:  真衰,输了!");
                j++;
            }
            break;
        }

    }

    public void showFour() {
        System.out.println("是否继续下一轮  (y/n)");
        char ji = input.next().charAt(0);
        if (ji == ‘n‘) {
            showFive();
        }
        while (ji == ‘y‘) {
            o++;
            System.out.println("请出拳: 1.石头 2.剪刀 3.布(输入相应数字)");
            int cei = input.nextInt();
            switch (cei) {
            case 1:
                System.out.println("你出拳:石头");
                break;
            case 2:
                System.out.println("你出拳:剪刀");
                break;
            case 3:
                System.out.println("你出拳:布");
                break;
            }
            int c = (int) (Math.random() * 3);
            if (c == 0) {
                c = 3;
            }
            switch (c) {
            case 1:
                System.out.println(name1 + "出拳:石头");
                break;
            case 2:
                System.out.println(name1 + "出拳:剪刀");
                break;
            case 3:
                System.out.println(name1 + "出拳:布");
                break;
            }
            if (cei == c) {
                System.out.println("结果:平局");
            } else if ((cei == 1 && c == 2) || (cei == 2 && c == 3)
                    || (cei == 3 && c == 1)) {
                System.out.println("结果: 恭喜你,赢了! ");
                i++;
            } else if ((cei == 1 && c == 3) || (cei == 2 && c == 1)
                    || cei == 3 && c == 2) {
                System.out.println("结果:  真衰,输了!");
                j++;
            }
            System.out.println("是否继续下一轮  (y/n)");
            char xu = input.next().charAt(0);
            ji=xu;
        }
    }
    public void showFive(){
        System.out.println(name + "VS" + name1);
        System.out.println("对战次数:"+o);
        System.out.println("姓名\t得分");
        System.out.println(name+"\t"+i);
        System.out.println(name1+"\t"+j);
        System.out.println("结果:恭喜恭喜");
        
    }
}

测试类:

package zuoYe;

public class test {
    public static void main(String[] args) {
        人机猜拳 b=new 人机猜拳();
        b.show();
        b.showFist();
        b.showTwo();
        b.showThree();
        b.showFour();
        b.showFive();
    }

}

 

以上是关于1.人机猜拳的主要内容,如果未能解决你的问题,请参考以下文章

1.人机猜拳

人机对战(猜拳)

shell案例分享之猜拳人机大战!

JavaScript实现人机猜拳

人机猜拳.

人机猜拳