这里我写了两个版本,用的是面向对象
1.游戏币版
2.无尽版
程序思路:
游戏币版
无尽版
效果
游戏币版效果
无尽版效果
程序设想
-
先显示欢迎语句
-
录用NPC与用户的昵称
-
让用户选择自己要出的拳
-
显示NPC的拳
-
判断用户和NPC的拳
-
积分计数
-
询问用户是否要退出
-
退出后判断积分,输出对应语句
代码
我这里用的是 面向对象写的
首先 搭建平台
import java.util.Scanner;
public class Game {
//第几局
int tamptime;
//定义游戏币
int coin = 30;
//定义数组来储存拳
String[] quan = {"石头", "剪刀", "布"};
//实例化用户对象
user user = new user();
//实例化NPC对象
Npc Npc = new Npc();
//1.用户对象
user username;
//2.NPC对象
Npc name;
public void showcoin() {
//显示欢迎语句
showplayer();
//初始化数据
initData();
//游戏循环
System.out.println("请问要开始游戏吗?(y/n)");
//扫描仪
Scanner input = new Scanner(System.in);
//定义变量来接受值
String start = input.next();
//游戏循环
while (start.equals("y")) {
System.out.println("用户开始出拳");
//定义变量接收用户输入的值
int userquan = user.showusergetquan();
//转换用户输入的值
String getVel = getValue(userquan);
System.out.println(user.username + "输入的是:" + getVel);
//接收随机数
int getnpcVel = Npc.showgetquan();
//转换随机数
String getNpcVel = getValue(getnpcVel);
System.out.println(Npc.name + "出的是:" + getNpcVel);
//判断; 接收返回值
//实例化裁判
test test = new test();
//返回值 = 类名.方法名(用户quan,Npcquan);
int res = test.CaiPan(userquan, getnpcVel);
if (res == 1) {
//用户加一份
user.scres++;
System.out.println("恭喜您赢了");
} else if (res == -1) {
//npc加一分
Npc.scres++;
System.out.println("对不起您输了");
} else {
System.out.println("平局");
}
//接受判断出来的游戏币数量
coin +=test.CaiPancoin(userquan, getnpcVel);
//局数加一
tamptime++;
System.out.println("第" + tamptime + "局游戏结束");
//显示积分
showshool();
//显示游戏币
System.out.println(coin);
//判断游戏币够不够下一局
if (coin < 10){
System.out.println("游戏币低于10个,不可继续游戏!");
break;
}
//询问用户是否要继续游戏
System.out.print("请问您要继续游玩吗(y/n):");
start = input.next();
}
//输出游戏币的数量
System.out.println("游戏币:"+coin);
//输出结果
showresult();
}
public void showplayergame() {
//显示欢迎语句
showplayer();
//初始化数据
initData();
//游戏循环
System.out.println("请问要开始游戏吗?(y/n)");
//扫描仪
Scanner input = new Scanner(System.in);
//定义变量来接受值
String start = input.next();
//游戏循环
while (start.equals("y")) {
System.out.println("用户开始出拳");
//定义变量接收用户输入的值
int userquan = user.showusergetquan();
//转换用户输入的值
String getVel = getValue(userquan);
System.out.println(user.username + "输入的是:" + getVel);
//接收随机数
int getnpcVel = Npc.showgetquan();
//转换随机数
String getNpcVel = getValue(getnpcVel);
System.out.println(Npc.name + "出的是:" + getNpcVel);
//判断; 接收返回值
//实例化裁判
test test = new test();
//返回值 = 类名.方法名(用户quan,Npcquan);
int res = test.CaiPan(userquan, getnpcVel);
if (res == 1) {
//用户加一份
user.scres++;
System.out.println("恭喜您赢了");
} else if (res == -1) {
//npc加一分
Npc.scres++;
System.out.println("对不起您输了");
} else {
System.out.println("平局");
}
//局数加一
tamptime++;
System.out.println("第" + tamptime + "局游戏结束");
//显示积分
showshool();
//询问用户是否要继续游戏
System.out.print("请问您要继续游玩吗(y/n):");
start = input.next();
}
//输出结果
showresult();
}
public void showresult() {
//判断
if (user.scres > Npc.scres) {
System.out.println(user.username + "别走,在与我大战三百回合!");
} else if (user.scres < Npc.scres) {
System.out.println("哈哈哈,小子你不行,快回家多练几年");
} else if (user.scres == Npc.scres) {
System.out.println("势均力敌,居然是平均," + user.username + "你别走,再和我继续大战三百回合!");
}
}
//显示积分
public void showshool() {
//输出用户积分
System.out.println(user.username + "的积分是:" + user.scres);
//输出NPC积分
System.out.println(Npc.name + "的积分是:" + Npc.scres);
}
//更改数据
public String getValue(int AAquan) {
//把用户数输入的1 2 3 改成 数组下标
int dix = AAquan - 1;
return quan[dix];
}
//录用 玩家 NPC姓名
public void initData() {
//新建扫描仪
Scanner input = new Scanner(System.in);
//请输入用户昵称
System.out.print("请输入您的昵称:");
//录入玩家姓名
user.username = input.next();
// 请输入NPC的姓名
System.out.print("请输入NPC的名字:");
//录入NPC姓名
Npc.name = input.next();
}
//欢迎语句与菜单
public void showplayer() {
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * *");
System.out.println("欢迎来到猜拳小游戏!");
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * *");
}
}
在写用户与NPC的类
用户
import java.util.Scanner;
public class user {
//定义用户姓名
String username;
//定义变量来接受用户胜利的场数
int scres;
public int showusergetquan(){
System.out.println("请选择出拳:1.石头 2.剪刀 3.布");
//扫描仪
Scanner input = new Scanner(System.in);
//接收用户输入
return input.nextInt();
}
}
NPC
import java.util.Random;
public class Npc {
//定义 NPC 姓名
String name;
//定义变量来接受 NPC 胜利的场数
int scres;
//让NPC随机出拳
public int showgetquan(){
//定义随机数对象
Random random = new Random();
//定义一个int 类型的值来接收 数
return random.nextInt(3)+1;
}
}
用户和NPC编写完成后再写裁判的类
裁判
public class test {
//-判断
public int CaiPan(int userquan , int Npcquan) {
//定义一个初始值
int res = 0;
//定义游戏币
int coin = 0;
// 1.石头 2. 剪刀 3. 布
if (userquan == 1 && Npcquan ==2 || userquan == 2 && Npcquan == 3|| userquan == 3 && Npcquan == 1 ){
coin += 8;
//这里不可以为++;
res = 1;
}else if(Npcquan == 1 && userquan ==2 || Npcquan == 2 && userquan == 3|| Npcquan == 3 && userquan == 1 ){
coin -= 10;
res = -1;
}
return res;
}
public int CaiPancoin(int userquan , int Npcquan) {
//定义游戏币
int coin = 0;
// 1.石头 2. 剪刀 3. 布
if (userquan == 1 && Npcquan ==2 || userquan == 2 && Npcquan == 3|| userquan == 3 && Npcquan == 1 ){
coin += 8;
}else if(Npcquan == 1 && userquan ==2 || Npcquan == 2 && userquan == 3|| Npcquan == 3 && userquan == 1 ){
coin -= 10;
}
return coin;
}
}
用户,NPC和平台都写完后再写开始了
直接调用就可以了
开始
import java.util.Scanner;
public class kaishi {
public static void main(String[] args) {
//扫描仪
Scanner input = new Scanner(System.in);
//实例化对象
Game Game = new Game();
System.out.println("请问要进入那个版本:");
System.out.println("\t\t1.游戏币版");
System.out.println("\t\t2.无尽版");
int a = input.nextInt();
switch (a){
case 1:
Game.showcoin();
break;
case 2:
Game.showplayergame();
break;
}
}
}