剪刀石头布

package com.renjicaiquan.entity;

public class Computer {
    private String name;
    private int score;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
    public Computer(String name, int score) {
        super();
        this.name = name;
        this.score = score;
    }
    public Computer() {
        super();
    }
    @Override
    public String toString() {
        return "Computer [name=" + name + ", score=" + score + "]";
    }
    public int doGame(){
        int x=(int) (Math.random()*3+1);
        switch(x){
        case 1:
            System.out.println(this.getName()+"出拳:剪刀");
            break;
        case 2:
            System.out.println(this.getName()+"出拳:石头");
            break;
        case 3:
            System.out.println(this.getName()+"出拳:布");
            break;
        }
        return x;
    }

}

package com.renjicaiquan.entity;

import java.util.Scanner;

public class Game {
    private User user=new User();
    private Computer computer=new Computer();
    private int count;
    static Scanner input=new Scanner(System.in);
    public void welcome(){
        System.out.println("-----------------------欢迎进入游戏------------------------");
        System.out.println("\t\t***********************");
        System.out.println("\t\t**  猜拳,开始  **");
        System.out.println("\t\t***********************");
    }
    public void start(){
        welcome();
        System.out.println("\n出拳规则:1、剪刀 2、石头 3、布");
        System.out.print("请选择对方角色(1:刘备 2:孙权 3:曹操):");
        int check_1=getIntOneToThree();
        switch(check_1){
        case 1:
            computer.setName("刘备");
            break;
        case 2:
            computer.setName("孙权");
            break;
        case 3:
            computer.setName("曹操");
            break;
        }
        System.out.print("请输入你的名字:");
        user.setName(input.next());
        System.out.println(user.getName()+"\tVS\t"+computer.getName()+"\n");

        System.out.println("要开始吗(y/n)?");
        String answer=getYorN();
        if("n".equals(answer)){
            System.out.println("谢谢使用!");
            System.exit(0);
        }
        String isAgain="";
        do{
            int a=user.doGame();
            int b=computer.doGame();
            compareUserAndComputer(a, b);
            System.out.print("\n是否继续开始下一轮(y/n):");
            isAgain=getYorN();
        }while(isAgain.equals("y"));
        showResult();
    }
    public static int getIntOneToThree(){
        while(!input.hasNext("[1,2,3]{1}")){
            input=new Scanner(System.in);
            System.out.println("输入有误,请重新输入!");
        }
        return input.nextInt();
    }
    public void setUser(User user) {
        this.user = user;
    }
    public User getUser() {
        return user;
    }
    public void setComputer(Computer computer) {
        this.computer = computer;
    }
    public Computer getComputer() {
        return computer;
    }
    public void compareUserAndComputer(int a,int b){
        //a表示人出的拳 b表示电脑出的拳
        int x=a-b;
        this.setCount(this.getCount()+1);
        switch(x){
        case 1:
        case -2:
            user.setScore(user.getScore()+1);
            System.out.println("结果:恭喜,你赢了!");
            break;
        case -1:
        case 2:
            computer.setScore(computer.getScore()+1);
            System.out.println("结果:^_^,你输了,真笨!");
            break;
        default:
            System.out.println("结果:平局!");
        }
    }
    public void setCount(int count) {
        this.count = count;
    }
    public int getCount() {
        return count;
    }
    public void showResult(){
        System.out.println("--------------------------------");
        System.out.println(user.getName()+"\tVS\t"+computer.getName());
        System.out.println("对战次数:"+this.getCount());
        System.out.println("\n姓名\t得分");
        System.out.println(user.getName()+"\t"+user.getScore());
        System.out.println(computer.getName()+"\t"+computer.getScore()+"\n");
        if(user.getScore()>computer.getScore()){
            System.out.println(computer.getName()+":这不可能!");
        }else if(user.getScore()==computer.getScore()){
            System.out.println(computer.getName()+":再战300回合!");
        }else{
            System.out.println(computer.getName()+":我"+computer.getName()+"是无敌的,哈哈哈...");
        }
        System.out.println("--------------------------------");

    }
    public static String getYorN(){
        while(!input.hasNext("[y,n]{1}")){
            input=new Scanner(System.in);
            System.out.println("输入有误,请重新输入!");
        }
        return input.next();
    }

}

package com.renjicaiquan.entity;

public class User {
    private String name;
    private int score;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
    public User(String name, int score) {
        super();
        this.name = name;
        this.score = score;
    }
    public User() {
        super();
    }
    @Override
    public String toString() {
        return "User [name=" + name + ", score=" + score + "]";
    }

    public int doGame(){
        System.out.println("你出拳:");
        int x=Game.getIntOneToThree();
        switch(x){
        case 1:
            System.out.println("你出拳:剪刀");
            break;
        case 2:
            System.out.println("你出拳:石头");
            break;
        case 3:
            System.out.println("你出拳:布");
            break;
        }
        return x;
    }

}

package com.renjicaiquan.test;

import com.renjicaiquan.entity.Game;

public class TestGame {
    public static void main(String[] args){
        Game g=new Game();
        g.start();
    }
}

时间: 2024-07-30 13:33:36

剪刀石头布的相关文章

1. while循环(当循环) 2. do{}while()循环 3. switch cose(多选一) 例子:当选循环下求百鸡百钱 用 switch cose人机剪刀石头布

1. while循环: 当选循环下求百鸡百钱:如下: 代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><me

HDU4115.Eliminate the Conflict(剪刀石头布)——2-sat可行性判断

http://acm.hdu.edu.cn/showproblem.php?pid=4115 题目描述: Bob,和Alice两人进行n轮的剪刀石头布,已知Bob n轮的出的情况,对于Alice给出m组的限制,只要每轮Alice没输,n轮之后就算Alice赢.判断她是否能赢 分析: 对于第i轮Bob出的情况,则alice有两种选择(相当于第i个变量有两种状态),然后我们只要处理矛盾边即可 //62MS 2156K 3312 B #include<cstring> #include<cst

【BZOJ 2597】 [Wc2007]剪刀石头布

2597: [Wc2007]剪刀石头布 Time Limit: 20 Sec  Memory Limit: 128 MBSec  Special Judge Submit: 492  Solved: 227 [Submit][Status] Description 在一些一对一游戏的比赛(如下棋.乒乓球和羽毛球的单打)中,我们经常会遇到A胜过B,B胜过C而C又胜过A的有趣情况,不妨形象的称之为剪刀石头布情况.有的时候,无聊的人们会津津乐道于统计有多少这样的剪刀石头布情况发生,即有多少对无序三元组

[Wc2007]剪刀石头布

[Wc2007]剪刀石头布 http://www.lydsy.com/JudgeOnline/problem.php?id=2597 Time Limit: 20 Sec  Memory Limit: 128 MBSec  Special Judge Description 在一些一对一游戏的比赛(如下棋.乒乓球和羽毛球的单打)中,我们经常会遇到A胜过B,B胜过C而C又胜过A的有趣情况,不妨形象的称之为剪刀石头布情况.有的时候,无聊的人们会津津乐道于统计有多少这样的剪刀石头布情况发生,即有多少对

李洪强漫谈iOS开发[C语言-039]-剪刀石头布

 李洪强漫谈iOS开发[C语言-039]-剪刀石头布

从键盘接收字符类型和字符串类型的数据(剪刀石头布)

在程序里定义一个剪刀石头布的规则,然后定义两个字符型变量,接着从键盘上接收两个数据分别赋给这两个变量,再按照已经设定好了的规则来 比较,最后输出结果. .        这个需要首先导入Scanner类:import java.util.Scanner;,定义一个Scanner对象,Scanner p = new Scanner(System.in);        然后定义一个字符串变量,先从键盘上接收这个字符串的内容,String p1 = p.next();         //大神说,要

BZOJ2597 [Wc2007]剪刀石头布(最小费用最大流)

题目大概是说n个人两两进行比赛,问如何安排几场比赛的输赢使得A胜B,B胜C,C胜A这种剪刀石头布的三元组最多. 这题好神. 首先,三元组总共有$C_n^3$个 然后考虑最小化不满足剪刀石头布条件的三元组个数: 对于三个人构不成剪刀石头布现象,当且仅当,其中一个人赢了其他两个人 而由于这是完全图,如果一个人赢了$x_i$场那么包含这个人不满足剪刀石头布现象的三元组就有$C_{x_i}^2$个 所以目的就是最小化$\sum C_{x_i}^2$,即$\sum x_i^2-C_n^2$,其中$C_n^

Java设计模式之从[剪刀石头布AI策略]分析策略(Strategy)模式

策略模式是一个很easy的模式. 它定义一系列的算法,把它们一个个封装起来,而且使它们能够相互替换. 考虑到我在做一个剪刀石头布的游戏,能够和计算机对战. 计算机的难度等级分为2个等级:普通难度和无法战胜难度. 普通难度是指电脑会随机出石头.剪刀.布.而无法战胜难度是指电脑会"作弊".电脑会事先知道玩家出的是什么手势. 假设玩家出的是剪刀.那么电脑会出石头,玩家永远的无法取胜. 那么,这两个难度分别代表两种算法,为了使得它们可以被游戏的主类装载,它们都应该继承于同一个接口或类.并暴露出

HTML5游戏开发,剪刀石头布小游戏案例

剪刀石头布,非常可爱的小游戏,相信大家都非常的怀念这款小游戏,小时候也玩过很多次,陪伴着我的童年的成长,现在是不是还会玩一下,剪刀石头布游戏的规则我们都知道是:剪刀剪布,石头砸剪刀,布包石头.跟朋友.同学.兄弟姐妹有意见分歧?通过“剪刀石头布游戏”来一局吧,谁赢了听谁的.躲猫猫的时候,通过“剪刀石头布游戏”来一局吧,谁输了谁找.洗衣服.做饭.扫地等等什么的,通过“剪刀石头布游戏”来一局吧,谁输了谁做.这是我的处女座游戏,学校的时候跟着培训老师一步一步写出来的,今天在这里将这款游戏分享给伙伴们,可

使用JavaScript实现剪刀石头布的小游戏(由浅到深)

使用JavaScript实现剪刀石头布的小游戏 简单的解析: 剪刀石头布的原理类似于一个数组中数字大小的比较,当然我们也可以将其分别使用对应的数字来代表剪刀石头布,在这里我们将 0 - 剪刀, 1 – 石头 , 2 – 布 我们要得到自己胜利的方式有一下几种可能 ① 我们胜利 ② 和电脑平局 ③ 电脑胜利 思路一: 将剪刀石头布分别使用数字代替,将数字作为实参,传入一个进行比较的方法,由于数字的不同,也使得出现的组合就不同.(这里我能使用随机数来让电脑随机生成0,1,2三个数的任意一个) 假设我