人机猜拳

package com.accp;

/**
 * 计算机类
 */
public class Computer {
    private String name; // 计算机姓名
    private int WinTimes; // 计算机赢得次数

    /**
     * 构造器
     *
     * @param name
     *            计算机的姓名
     */
    public Computer(String name) {
        this.name = name;
    }

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

    public int getWinTimes() {
        return this.WinTimes;
    }

    public void setWinTimes(int winTimes) {
        this.WinTimes = winTimes;
    }

    /**
     * 计算机出拳的方法
     */
    public int showFist() {
        return (int) (Math.random() * 3 + 1);
    }
}



package com.accp;

import java.util.Scanner;

/**
 * 玩家类
*/
public class Player {
    private String name; // 玩家姓名
    private int WinTimes; // 玩家赢的次数

    /**
     * 构造器
     *
     * @param name
     *            玩家的姓名
     */
    public Player(String name) {
        this.name = name;
    }

    public int getWinTimes() {
        return this.WinTimes;
    }

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

    public void setWinTimes(int winTimes) {
        this.WinTimes = winTimes;
    }

    /**
     * 人出拳的方法
     */
    public int showFist() {
        int fist;
        Scanner sc = new Scanner(System.in);
        do {
            System.out.println("请出拳:1--剪刀      2--石头     3--布");
            System.out.println("请选择:  ");
            fist = sc.nextInt();
        } while (fist < 1 || fist > 3);
        return fist;
    }
}



package com.accp;

import java.util.Scanner;

/**
 * 游戏类
*/
public class Game {
    private Computer computer;
    private Player player;
    private int counter;

    public Game(Computer computer, Player player) {
        this.computer = computer;
        this.player = player;
    }

    // 显示结果和获胜的次数
    public void showReult() {
        System.out.println("======比赛结果======");
        System.out.println("共比赛了" + counter + "回合");
        System.out.println(player.getName() + "赢了" + player.getWinTimes() + "次");
        System.out.println(computer.getName() + "赢了" + computer.getWinTimes()
                + "次");
        System.out.println("平局 :"
                + (counter - player.getWinTimes() - computer.getWinTimes()));
    }

    /**
     * 是否继续的方法
     */
    public void start() {
        Scanner sc = new Scanner(System.in);
        String s = " ";
        do {
            play();
            System.out.println("\n要继续吗?(输入yes继续)");
            s = sc.nextLine();
            counter++;
        } while (s.substring(0, 1).equalsIgnoreCase("y"));

    }

    /**
     * 判断输赢的方法
     */
    public void play() {
        int cf = computer.showFist();
        int pf = player.showFist();
        String[] hj = { "剪刀", "石头", "布" };
        System.out.println(computer.getName() + "出了" + hj[cf - 1]);
        System.out.println(player.getName() + "出了" + hj[pf - 1]);
        if (cf == pf) {
            System.out.println("平局!!");
        } else if (pf == 1 && cf == 3 || pf == 2 && cf == 1 || pf == 3
                && cf == 2) {

            System.out.println(player.getName() + "赢了!!");
            player.setWinTimes(player.getWinTimes() + 1);
        }

        else {
            System.out.println(computer.getName() + "赢了!!");
            computer.setWinTimes(computer.getWinTimes() + 1);
        }
    }
}


package com.accp;

/**
 * 运行游戏
*/
public class RunGame {
    public static void main(String[] args) {
        Player p = new Player("曹操");
        Computer c = new Computer("刘备");
        Game ch = new Game(c, p);
        ch.start();
        ch.showReult();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值