java小游戏-人机猜拳

// java人机猜拳小游戏,代码有不足的地方欢迎小伙伴们来指正

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

public class FigureGame {
	public static void main(String[] args) {
		int times = 0;// 对战次数
		int playScore = 0;// 玩家积分
		int otherScore = 0;// 对方(电脑)积分
		String[] figure = { "剪刀", "石头", "布" };
		Random r = new Random();
		Scanner sc = new Scanner(System.in);

		System.out
				.println("---------------------欢迎进入游戏世界---------------------");
		System.out.println();
		System.out.println("\t\t**************************");
		System.out.println("\t\t**\t猜拳,开始\t**");
		System.out.println("\t\t**************************");
		System.out.println();

		System.out.println("出拳规则:1.剪刀 2.石头 3.布");
		System.out.print("请选择对方角色(1:刘备 2:孙权 3:曹操):");
		int num = sc.nextInt();
		String otherName = null;// 对方角色
		switch (num) {// 通过输入的值判断对方的角色
		case 1:
			otherName = "刘备";
			break;
		case 2:
			otherName = "孙权";
			break;
		case 3:
			otherName = "曹操";
			break;
		default:
			System.out.println("输入有误,请重新开始");
		}

		System.out.print("请输入你的姓名:");
		String playName = sc.next();// 玩家角色
		System.out.println(playName + " VS " + otherName + "对战");
		System.out.println();
		System.out.print("要开始吗?(y/n)");
		String choose = sc.next();
		String chooseNext = "n";

		do {
			if ("y".equals(choose)) {// 开始游戏
				System.out.print("\n请出拳:1.剪刀 2.石头 3.布(输入相应数字):");
				int playFigure = sc.nextInt() - 1;
				System.out.println("你出拳:" + figure[playFigure]);
				int otherFigure = r.nextInt(3);// 随机获取机器的出拳,对应数组中的下标:0、1、2
				System.out.println(otherName + "出拳:" + figure[otherFigure]);
				/*
				 * 第一判断:两人选择相同,平局
				 * 第二次判断:
				 * 当玩家playFigure选择剪刀或石头,并且机器的选择在数组中的下标正好大玩
				 * 家一位,则机器赢。另外还有一种情况,玩家选布,则机器选剪刀才能赢
				 * 第三次判断:剩下的情况即为玩家赢
				 */
				if (playFigure == otherFigure) {// 首先判断两人出拳是否一样
					System.out.println("结果:和局,真衰!嘿嘿,等着瞧吧!");
				} else if ((playFigure == otherFigure - 1)
						|| (playFigure == otherFigure + 2)) {
					System.out.println("糟糕,对方赢了!");
					otherScore++;// 赢家积分+1
				} else {
					System.out.println("恭喜,你赢了!");
					playScore++;// 赢家积分+1
				}
				times++;// 对战次数+1

				System.out.print("\n是否开始下一轮(y/n):");
				chooseNext = sc.next();

			} else if ("n".equals(choose)) {// 一开始就没启动游戏,直接结束
				break;
			}
		} while ("y".equals(chooseNext));// 输入y时,继续下一轮

		System.out
				.println("---------------------------------------------------------");
		System.out.println(playName + " VS " + otherName + "对战");
		System.out.println("对战次数:" + times);
		System.out.println();
		System.out.println("姓名\t得分");
		System.out.println(playName + "\t" + playScore);
		System.out.println(otherName + "\t" + otherScore);

		if (playScore > otherScore) {// 玩家赢
			System.out.println("\n结果:恭喜恭喜!");
		} else if (playScore < otherScore) {// 机器赢
			System.out.println("\n结果:再接再厉");
		} else {// 平局
			System.out.println("\n结果:平局");
		}

		sc.close();
	}
}

运行效果:


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值