java--demo之猜拳游戏

时间:2023-12-15 17:03:50

版本1:人机大战  基础随机出    用户键盘录入

package com.hainiu.demo;

import java.util.Scanner;

/*
* 人机大战石头剪刀布
*/
public class Cycles { public static void main(String[] args) {
while(true){
System.out.println("-----欢迎来到游戏界面----");
System.out.println("1:剪刀、2:石头、3:布");
Scanner sc = new Scanner(System.in);
int person = sc.nextInt();
int computer = (int)(Math.random()*(3)+1);
String per="用户";
String com="电脑";
//用户
switch(person){
case 1:
per="剪刀";
break;
case 2:
per="石头";
break;
case 3:
per="布";
break;
//电脑
}
switch(computer){
case 1:
com="剪刀";
break;
case 2:
com="石头";
break;
case 3:
com="布";
break;
//判断
}
if(person==1&&computer==2||person==2&&computer==3||person==3&&computer==1){
System.out.println("你出的是"+per+"电脑出的是"+com);
System.out.println("你输啦");
}else if(person==2&&computer==1||person==3&&computer==2||person==1&&computer==3){
System.out.println("你出的是"+per+"电脑出的是"+com);
System.out.println("你赢了");
}else if(person==computer){
System.out.println("你出的是"+per+"电脑出的是"+com);
System.out.println("平局");
}else{
System.out.println("您的输入有误");
break;
}
}
}
}

运行结果:

-----欢迎来到游戏界面----
1:剪刀、2:石头、3:布
2
你出的是: 石头   电脑出的是: 剪刀
你赢了
-----欢迎来到游戏界面----
1:剪刀、2:石头、3:布

版本2:

猜拳游戏说明:

⦁    任务
⦁    完成人机猜拳互动游戏的开发
⦁    主要功能
⦁    选取对战角色
⦁    猜拳
⦁    记录分数
⦁    需求说明
⦁    分析业务
⦁    抽象出类、类的特征和行为
⦁    实现思路:
⦁    分析业务,抽象出类、类的特征和行为

package com.hainiu.demo;

import java.util.Scanner;

class User{
Scanner sc = new Scanner(System.in);
private String name;
private int integral;
private String punch;
public User() {
// TODO Auto-generated constructor stub
}
public User(String name, int integral, String punch) {
super();
this.name = name;
this.integral = integral;
this.punch = punch;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIntegral() {
return integral;
}
public void setIntegral(int integral) {
this.integral = integral;
}
public String getPunch() {
return punch;
}
public void setPunch(String punch) {
this.punch = punch;
}
//用户输入名称
public void inputName(){
System.out.println("请输入用户录入名");
name = sc.next();
}
//猜拳方法
public void UserGuess(){ int n= sc.nextInt();
System.out.println("1、剪刀,2、石头、3、布");
if(n>0&&n<=3){
switch (n) {
case 1:
this.punch="剪刀";
break;
case 2:
this.punch="石头";
break;
case 3:
this.punch="布";
break;
}
}else{
System.out.println("输入有误");
} }
}
class Computer{
Scanner sc = new Scanner(System.in);
private String name;
private int integral;
private String punch;
public Computer() { }
public Computer(Scanner sc, String name, int integral, String punch) {
this.sc = sc;
this.name = name;
this.integral = integral;
this.punch = punch;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIntegral() {
return integral;
}
public void setIntegral(int integral) {
this.integral = integral;
}
public String getPunch() {
return punch;
}
public void setPunch(String punch) {
this.punch = punch;
}
//选择用户
public void computerName(){
System.out.println("请选择您的对手:");
System.out.println("1、扫地僧,2、逍遥子、3、独孤求败");
int n = sc.nextInt();
switch (n) {
case 1:
this.name="扫地僧";
break;
case 2:
this.name="逍遥子";
break;
case 3:
this.name="独孤求败";
break;
}
}
//猜拳方法
public void comuterGuess(){
//System.out.println("对手出的是:");
int t = (int)(Math.random()*(3)+1);
switch (t) {
case 1:
this.punch="剪刀";
break;
case 2:
this.punch="石头";
break;
case 3:
this.punch="布";
break;
}
}
}
class TestGame{
User u = new User();
Computer c =new Computer();
//欢迎进入游戏菜单
public void start(){
//初始化对象
System.out.println("欢迎进入游戏菜单");
System.out.println("游戏开始");
//选择您的对手
c.computerName();
u.inputName();
System.out.println(u.getName()+"vs"+c.getName());
}
//判断出拳的结果
public void game(){
u.UserGuess();;
c.comuterGuess();
System.out.println("你出的是"+u.getPunch());
System.out.println(c.getName()+"出的是"+c.getPunch());
//具体判断输赢的 剪刀 石头 布
if(u.getPunch().equals("剪刀") && c.getPunch().equals("石头")||u.getPunch().equals("石头") && c.getPunch().equals("布") || u.getPunch().equals("布") && c.getPunch().equals("剪刀")){
c.setIntegral(c.getIntegral()+1);
System.out.println("你输了");
}else if(u.getPunch().equals("石头")&&c.getPunch().equals("剪刀")||u.getPunch().equals("布")&&c.getPunch().equals("石头")||u.getPunch().equals("剪刀")&&c.getPunch().equals("布")){
u.setIntegral(u.getIntegral()+1);
System.out.println("你赢啦");
}else {
System.out.println("平局");
}
} public void last(){
System.out.println("最后结果是:");
System.out.println(u.getName()+"赢了"+u.getIntegral()+"局");
System.out.println(c.getName()+"赢了"+c.getIntegral()+"局");
if(u.getIntegral()>c.getIntegral()){
System.out.println("算总分"+u.getName()+"或的最后的胜利");
}else if(u.getIntegral()<c.getIntegral()){
System.out.println("算总分"+c.getName()+"或的最后的胜利");
}else if(u.getIntegral()==c.getIntegral()){
System.out.println("最后平局");
}else {
System.out.println("输入有误,这不是系统的问题");
}
}
}
public class Cycles2 { public static void main(String[] args) {
// 测试
Scanner sc = new Scanner(System.in);
System.out.println("---------游戏开始----------\n");
TestGame tg = new TestGame();
tg.start();
System.out.println("输入y继续,其他人任意键结束");
String panduan = sc.next();
//循环game方法
while(panduan.equals("y")){
System.out.println("******************");
System.out.println("1、剪刀,2、石头、3、布");
tg.game();
System.out.println("y:继续-任意:结束");
panduan = sc.next();
}
System.out.println("游戏结束");
tg.last();
} }