项目-人机猜拳

Computer

package com.company;

import java.util.Random;
import java.util.Scanner;

public class Computer {
Scanner input;
private String name;
private int count;
private String chuquan;

public Computer() {
    this.input = new Scanner(System.in);
}

public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}

public int getCount() {
    return this.count;
}

public void setCount(int count) {
    this.count = count;
}

public String getChuquan() {
    return this.chuquan;
}

public void setChuquan(String chuquan) {
    this.chuquan = chuquan;
}

public void giveName() {
    System.out.print("请选择对方角色(1.刘备  2.孙权  3.曹操)");
    int name = 0;
    boolean panduan = true;

    do {
        try {
            do {
                name = this.input.nextInt();
                if (name > 0 && name < 4) {
                    panduan = false;
                } else {
                    System.err.println("输入错误,请输入1-3之间的整数:");
                }
            } while(panduan);
        } catch (Exception var4) {
            System.err.println("输入错误,请输入1-3之间的整数:");
            this.input.next();
        }
    } while(panduan);

    if (name == 1) {
        this.name = "刘备";
    } else if (name == 2) {
        this.name = "孙权";
    } else {
        this.name = "曹操";
    }

}

public void guess() {
    Random random1 = new Random();
    int jishu = random1.nextInt(3);
    if (jishu == 1) {
        this.chuquan = "剪刀";
    } else if (jishu == 2) {
        this.chuquan = "石头";
    } else {
        this.chuquan = "布";
    }

}
}
Game

public Game() {
}

public void start() {
    this.c.giveName();
    this.p.giveName();
    PrintStream var10000 = System.out;
    String var10001 = this.p.getName();
    var10000.println(var10001 + " VS " + this.c.getName());
}

public void game() {
    this.p.guess();
    this.c.guess();
    System.out.println("你出拳:" + this.p.getChuquan());
    PrintStream var10000 = System.out;
    String var10001 = this.c.getName();
    var10000.println(var10001 + "出拳:" + this.c.getChuquan());
    if (this.p.getChuquan().equals("剪刀") && this.c.getChuquan().equals("布") || this.p.getChuquan().equals("石头") && this.c.getChuquan().equals("剪刀") || this.p.getChuquan().equals("布") && this.c.getChuquan().equals("石头")) {
        this.p.setCount(this.p.getCount() + 1);
        System.out.println("哇,你赢了,好厉害!");
    } else if (this.c.getChuquan().equals("剪刀") && this.p.getChuquan().equals("布") || this.c.getChuquan().equals("石头") && this.p.getChuquan().equals("剪刀") || this.c.getChuquan().equals("布") && this.p.getChuquan().equals("石头")) {
        this.c.setCount(this.c.getCount() + 1);
        System.out.println("你输了,真笨");
    } else if (this.c.getChuquan().equals(this.p.getChuquan())) {
        System.out.println("平局!");
    }

}

public void versult() {
    PrintStream var10000 = System.out;
    String var10001 = this.p.getName();
    var10000.println(var10001 + " VS " + this.c.getName());
    var10000 = System.out;
    var10001 = this.p.getName();
    var10000.println(var10001 + "赢了" + this.p.getCount() + "局");
    System.out.println("电脑赢了" + this.c.getCount() + "局");
    if (this.p.getCount() > this.c.getCount()) {
        System.out.println("最终对战结果:" + this.p.getName() + "赢了");
    } else if (this.p.getCount() < this.c.getCount()) {
        System.out.println("最终对战结果:电脑赢了");
    } else if (this.p.getCount() == this.c.getCount()) {
        System.out.println("最终对战结果:平局");
    }

}

}
Player

package com.company;

import java.util.Scanner;

public class Player {
Scanner input;
private String name;
private int count;
private String chuquan;

public Player() {
    this.input = new Scanner(System.in);
}

public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}

public int getCount() {
    return this.count;
}

public void setCount(int count) {
    this.count = count;
}

public String getChuquan() {
    return this.chuquan;
}

public void setChuquan(String chuquan) {
    this.chuquan = chuquan;
}

public void giveName() {
    System.out.println("请输入你的名字:");
    this.name = this.input.next();
}

public void guess() {
    System.out.print("请出拳:\t1.剪刀\t2.石头\t3.布(输入相应数字):");
    int jishu = 0;
    boolean panduan = true;

    do {
        try {
            do {
                jishu = this.input.nextInt();
                if (jishu > 0 && jishu < 4) {
                    panduan = false;
                } else {
                    System.err.println("输入错误,请输入1-3之间的整数:");
                }
            } while(panduan);
        } catch (Exception var4) {
            System.err.println("输入错误,请输入1-3之间的整数:");
            this.input.next();
        }
    } while(panduan);

    if (jishu == 1) {
        this.setChuquan("剪刀");
    } else if (jishu == 2) {
        this.setChuquan("石头");
    } else if (jishu == 3) {
        this.setChuquan("布");
    }

}

}
Test

ackage com.company;

import java.util.Scanner;

public class Test {
public Test() {
}

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("********************************");
    System.out.println("************猜拳, 开始************");
    System.out.println("********************************");
    System.out.println("出拳规则:\t1.剪刀\t2.石头\t3.布");
    Game g = new Game();
    g.start();
    System.out.println();
    System.out.println("要开始吗?(输入y继续,输入其他结束)");

    for(String panduan = input.next(); panduan.equals("y"); panduan = input.next()) {
        System.out.println("********************************");
        g.game();
        System.out.println("是否开始下一局(输入y继续,输入其他结束) : ");
    }

    System.out.println("你输了游戏");
    g.versult();
}

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 今天感恩节哎,感谢一直在我身边的亲朋好友。感恩相遇!感恩不离不弃。 中午开了第一次的党会,身份的转变要...
    迷月闪星情阅读 10,610评论 0 11
  • 彩排完,天已黑
    刘凯书法阅读 4,275评论 1 3
  • 表情是什么,我认为表情就是表现出来的情绪。表情可以传达很多信息。高兴了当然就笑了,难过就哭了。两者是相互影响密不可...
    Persistenc_6aea阅读 126,028评论 2 7