人机猜拳java代码 如何用java编写一个人机猜拳游戏

用JAVA做一个剪刀,石头,布的人机猜拳游戏 。编写这个小游戏 我们需要几个类
1、第一个 Person 类
import java.util.Scanner;
/**
* @copyright 2018 sugarsLab.com All rights reserved.
* @author jingfei.wu
* @date 2018年11月16日
* @version 1.0
* @ClassName Person
* @description 用户类 用来计算用户输入
*/
public class Person {
public static final Person me = new Person();
private int n = 0;
/**
* @copyright 2018 sugarsLab.com All rights reserved.
* @author jingfei.wu
* @date 2018年11月16日
* @version 1.0
* @description 用户输入的 指令
* @return
*/
public int input() {
System.out.println("请输入:石头,剪刀,布\n输入:@退出 退出系统");
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
String s = scanner.next();
//s 里面存着 用户输入的 指令 切记这里不要使用 s.equals() 而是写 "指令".equals() 这么写 是为了避免空指针
if ("石头".equals(s)) {
n = 1;
} else if ("剪刀".equals(s)) {
n = 2;
} else if ("布".equals(s)) {
n = 3;
} else if ("@退出".equals(s)) {
System.out.print("系统退出了");
System.exit(0);
}
return n;
}
}
2、Computer 类
/**
* @copyright 2018 sugarsLab.com All rights reserved.
* @author jingfei.wu
* @date 2018年11月16日
* @version 1.0
* @ClassName Computer
* @description 游戏中电脑类 用来产生随机数
*/
public class Computer {
public static final Computer me = new Computer();
/**
* @copyright 2018 sugarsLab.com All rights reserved.
* @author jingfei.wu
* @date 2018年11月16日
* @version 1.0
* @description TODO
* @return {int} 返回值为int 类型
*/
public int random() {return (int) (Math.random() * 31);}
}
3、Game类
/**
* @author jingfei.wu
* @date 2018年11月16日
* @version 1.0
* @ClassName Game
* @description 游戏类 用来计算游戏结果
*/
public class Game {
/**
* @author jingfei.wu
* @date 2018年11月16日
* @version 1.0
* @description 返回 人机交互结果
* @param n
*{int} 用户输入 的标识 石头 为 1 剪刀 为 2 布 为 3
* @param m
*{int} 电脑产生的随机数 石头 为 1 剪刀 为 2 布 为 3
*/
public void result(int n, Integer m) {
String res = "";
if (m.intValue() == 1)
res = "石头";
else if (m.intValue() == 2)
res = "剪刀";
else
res = "布";
if (n == m) {
System.out.println("平了 computer出"res);
} else {
if (m == 1) {
if (n == 2)
System.out.println("你输了 computer出 "res);
else if (n == 3)
System.out.println("你赢了 computer出 "res);
} else if (m == 2) {
if (n == 1)
System.out.println("你赢了 computer出 "res);
else if (n == 3)
System.out.println("你输了 computer出 "res);
} else if (m == 3) {
if (n == 1)
System.out.println("你输了 computer出 "res);
else if (n == 2)
System.out.println("你赢了 computer出 "res);
}
}
}
public static void main(String[] args) {
while (true) {
Game gamer = new Game();
gamer.result(Person.me.input(), Computer.me.random());
}
}
}
如下是程序运行截图
猜拳游戏java能输出游戏时间Java实现猜拳游戏的核心在于电脑随机数的生成人机猜拳java代码,Java中的随机数生成方法是人机猜拳java代码:
首先引入包import java.util.*;然后int r=new Random().nextInt(3);(nextInt中的数字三代表随机数生成的个数,从零开始)
所以在猜拳的输入中需要有0、1、2三个数字代替,如果要输入汉字,则用if进行相应判断即可 。
在实现的游戏中实现①猜拳;②记录胜负;③玩家决定游戏局数;④输出获胜、失败及平局;⑤统计总共的胜负结果(根据获胜次数判断)
①猜拳基础功能:该部分代码可以放到一个方法中 , 减少主函数代码量 。
电脑出拳即int r=new Random().nextInt(3);注意:该部分一定要写在for循环内部,否则无法实现每次不同的随机数 。
通过if判断双方出拳是否相等if(a==0r==0)else if(a==0r==1)else if(a==0r==2)即可实现猜拳,if内直接输出相关语句即可
②记录胜负:定义猜拳方法为int,通过返回值记录相关比赛的胜负情况,可以用0--失败;1--获胜;2--平局 进行记录,在主函数中对相应抛出的数字记录即可
if(a==0r==0){
System.out.println("The computer comes out with cloth,it was a draw. ");
return 2;
}
h=comp.compare(a,r);if (h==1)j;
登录后复制
③玩家决定局数: 定义一个数,在循环中不大于该数即可
④输出获胜、失败及平局: j、k即胜利和失败,平局数即n-j-k 。
⑤统计结果,直接用if比较i、j的数字结果即可 。
主函数部分:
package SS2_5;
import java.util.*;
public class Main {
public static void main(String args[]){
Scanner scanner=new Scanner(System.in);
Compare comp=new Compare();
int h=0,j=0,k=0;
System.out.println("rules:0--cloth;1--stone;2--scissors.\nU can choose how many times you want to play:");
int n=scanner.nextInt();
for(int i=1;i=n;i){
System.out.print("It's the " i " round,your turn:");
int a=scanner.nextInt();
int r=new Random().nextInt(3);
switch (a){
case 0:
h=comp.compare(a,r);
break;
case 1:
h=comp.compare(a,r);
break;
case 2:
h=comp.compare(a,r);
break;
default:
System.out.println("Wrong number!");
break;
}
if (h==1)
j;
else if(h==0)
k;
}
System.out.println("The total times you won are " j ",The draw times are " (n-j-k) ".");
if(jk)
System.out.println("You are the final winner");
else if(kj)
System.out.println("The computer is the winner.");
if(j==k)
System.out.println("The final result is draw");
}
}
登录后复制
compare方法部分
package SS2_5;
public class Compare {
public int compare(int a,int r){
int counter=0;
if(a==0r==0){
System.out.println("The computer comes out with cloth,it was a draw. ");
return 2;
}
else if(a==0r==1){
System.out.println("The computer comes out with stone, you won. ");
return 1;
}
else if(a==0r==2){
System.out.println("The computer comes out with scissor,you lost. ");
【人机猜拳java代码 如何用java编写一个人机猜拳游戏】return 0;
}
else if(a==1r==0){
System.out.println("The computer comes out with cloth,you lost. ");
return 0;
}
else if(a==1r==1){
System.out.println("The computer comes out with stone,it was a draw. ");
return 2;
}
else if(a==1r==2){
System.out.println("The computer comes out with scissors,you won. ");
return 1;
}
else if(a==2r==0){
System.out.println("The computer comes out with cloth,you won. ");
return 1;
}
else if(a==2r==1){
System.out.println("The computer comes out with stone,you lost. ");
return 0;
}
else if(a==2r==2){
System.out.println("The computer comes out with scissors,it was a draw. ");
return 2;
}
else
return 0;
}
}
登录后复制
java
704拖拉机
精选推荐
广告
java写简单的猜拳游戏
2下载·0评论
2016年7月27日
用Java编写的猜拳小游戏
2029阅读·0评论·0点赞
2021年3月7日
Java猜拳游戏和Random的应用
21阅读·0评论·0点赞
2022年10月24日
java实现完整版猜拳小游戏
25下载·0评论
2018年11月22日
用python实现功能猜拳
1137阅读·2评论·3点赞
2022年7月14日
java猜拳switch计分制_java----猜拳(10局分胜负)
117阅读·0评论·1点赞
2021年3月15日
二手拖拉机交易市场,你还在高价买吗?
精选推荐
广告
利用Java编写简单的猜拳游戏
911阅读·0评论·1点赞
2022年9月8日
Java简单实现猜拳游戏
1.1W阅读·1评论·2点赞
2022年1月23日
java猜拳游戏代码_Java实现简单猜拳游戏
4496阅读·0评论·0点赞
2021年3月1日
用java来写一个简单的猜拳小游戏
890阅读·1评论·1点赞
2022年7月26日
java实现猜拳游戏
3180阅读·2评论·1点赞
2022年5月4日
JAVA编写猜拳游戏
3037阅读·3评论·3点赞
2021年3月16日
[Java教程]17.实战,趣味猜拳小游戏
8040阅读·2评论·3点赞
2020年6月24日
怎么用java实现人机猜拳?
1959阅读·6评论·9点赞
2021年7月22日
Java Random经典例子【猜拳游戏】
4318阅读·0评论·0点赞
2014年3月22日
java的人机猜拳代码_Java实现人机猜拳游戏
702阅读·0评论·2点赞
2021年3月12日
Java基础练习之猜拳游戏
363阅读·1评论·1点赞
2021年8月19日
用java写猜拳小游戏
1096阅读·0评论·1点赞
2021年9月1日
Java猜拳小游戏
97阅读·0评论·0点赞
2022年8月23日
java猜拳小游戏
500阅读·1评论·0点赞
2022年7月14日
Java使用循环,实现猜拳游戏统计多少局及胜率?为人机猜拳java代码了让游戏有参与感人机猜拳java代码 , 并体现java面对对象人机猜拳java代码的思想,人机猜拳java代码我先创建一个Player选手类,包含选手的名字playerName还有出拳方法guess() 。出拳时采用随机获取0、1和2的方式分别代表石头、剪刀和布,代码如下:
public class Player {
private String playerName;
public Player(String playerName) {
this.playerName = playerName;
}
public String getPlayerName() {
return playerName;
}
//出拳方法 0-石头 1-剪刀 2-布
public int guess() {
//随机获取0、1、2
int num = new Random().nextInt(3);
if (num == 0) {
System.out.print("选手"this.playerName"出的是石头 ");
} else if (num == 1) {
System.out.print("选手"this.playerName"出的是剪刀 ");
} else if (num == 2) {
System.out.print("选手"this.playerName"出的是布 ");
}
return num;
}
}
然后在主类中,首先要输入对局的总数,然后创建两名选手进行pk , 在pk()方法中制定了获胜规则,详见代码注释 。最终统计并利用BigDecimal计算胜率(BigDecimal可以很完美的解决整数除法及其四舍五入保留小数的问题):
public class Main {
public static void main(String[] args) {
System.out.println("请输入本局局数:");
Scanner scanner = new Scanner(System.in);
int sum = scanner.nextInt();
//创建结果数组,resultArray[0]代表p1的获胜局数,resultArray[1]代表p2的获胜局数 , resultArray[2]代表平局局数
int[] resultArray = new int[3];
//创建两名选手
Player p1 = new Player("张三");
Player p2 = new Player("李四");
for (int i = 0; isum; i) {
//根据总局数进行pk
int result = pk(p1, p2);
if (result == 1) {
resultArray[0];
} else if (result == -1) {
resultArray[1];
} else {
resultArray[2];
}
}
System.out.println("");
System.out.println("最终结果统计:");
System.out.println("选手["p1.getPlayerName()"]获胜局数为:"resultArray[0]",胜率为:"
new BigDecimal(resultArray[0]).multiply(new BigDecimal(100).divide(new BigDecimal(sum), 2, BigDecimal.ROUND_HALF_UP))"%");
System.out.println("选手["p2.getPlayerName()"]获胜局数为:"resultArray[1]" , 胜率为:"
new BigDecimal(resultArray[1]).multiply(new BigDecimal(100).divide(new BigDecimal(sum), 2, BigDecimal.ROUND_HALF_UP))"%");
System.out.println("平局局数为:"resultArray[2]",平局率为:"
new BigDecimal(resultArray[2]).multiply(new BigDecimal(100).divide(new BigDecimal(sum), 2, BigDecimal.ROUND_HALF_UP))"%");
}
//0-石头 1-剪刀 2-布
//return 0:平局 1:p1获胜 -1:p2获胜
private static int pk(Player p1, Player p2) {
System.out.println("--------------------");
int a = p1.guess();
int b = p2.guess();
System.out.print("\n对局结果:");
//出拳相同平局
if (a == b) {
System.out.println("平局");
return 0;
}
//p1获胜条件:p1出石头时p2出剪刀 , p1出剪刀时p2出步 , p1出布时p2出石头
else if ((a == 0b == 1) || (a == 1b == 2) || (a == 2b == 0)) {
System.out.println("选手["p1.getPlayerName()"]获胜");
return 1;
}
//p2获胜条件:p1出石头时p2出布 , p1出剪刀时p2出石头,p1出布时p2出剪刀
else if ((a == 0b == 2) || (a == 1b == 0) || (a == 2b == 1)) {
System.out.println("选手["p2.getPlayerName()"]获胜");
return -1;
} else {
//因为规定了随机数产生0、1、2,所以其实不会走到本分支
throw new IllegalArgumentException("本局无效");
}
}
}
对局5局的运行结果:
人机猜拳java代码我这里就只能统计当前游戏的数据了 , 如果你想统计多局游戏总的胜率信息 , 那么需要将每一局的比赛结果写到txt文件里 , 最终根据txt文件内容统计即可 。
java人机猜拳游戏initial方法import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Random; public class FingerGuessing { private String[] op = new String[] { "布", "剪刀", "石头" }; Random r = new Random(); private int wj = 0; private int dn = 0; private int count = 0; private int go() { int k = r.nextInt(3); System.out.println("电脑:"op[k]); return k; } private void compare(int i) { count; System.out.println("玩家:"op[i - 1]); int k = go(); if ( i - 1 == k) { System.out.println("打平"); } else if ( i - 1 - k == 1 || i-1-k == -2) { System.out.println("玩家获胜"); wj; } else { System.out.println("电脑获胜"); dn; } } private void info() { System.out.println("共"count"盘"); System.out.println("玩家获胜"wj"盘"); System.out.println("电脑获胜"dn"盘"); System.out.println("打平"(count-wj-dn)"盘"); } public void start() { String xz = ""; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); do { System.out.println("\n请选择:\n1.布\n2.剪刀\n3.石头\n结束请输入exit"); try { xz = br.readLine(); if (xz.equalsIgnoreCase("exit")) { info(); continue; } if (!xz.equals("1")!xz.equals("2")!xz.equals("3")) { System.out.println("选择错误,请重新选择"); continue; } compare(Integer.parseInt(xz)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } while (!xz.equals("exit")); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new FingerGuessing().start(); } }
求JAVA人机猜拳的代码,类似一下界面 。自己纯手打,老半天才弄出来啊
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Demo2 extends JFrame {
private JLabel lb1, lb2, lb3, lb4; // 提示标签
private JTextField ta1, ta2;// 两个文本框
private JButton b1, b2, b3; // 三个按钮
private JPanel p1, p2; // 两个JPanel面板
public Demo2() {
// 初始化所有组件
lb1 = new JLabel("欢迎使用人机猜拳程序");
lb2 = new JLabel("你出拳:");
lb3 = new JLabel("电脑出拳:");
lb4 = new JLabel("结果");
ta1 = new JTextField();
ta1.setPreferredSize(new Dimension(60, 60)); // 设置大小
ta1.setEditable(false);//设置不可编辑
ta2 = new JTextField();
ta2.setPreferredSize(new Dimension(60, 60));
ta2.setEditable(false);//设置不可编辑
b1 = new JButton("剪刀");
b2 = new JButton("石头");
b3 = new JButton("布");
p1 = new JPanel();
p2 = new JPanel();
// 设置第一个面板内容
Box box = Box.createVerticalBox();
Box box1 = Box.createHorizontalBox();
box1.add(lb2);
box1.add(ta1);
box1.add(lb3);
box1.add(ta2);
box.add(lb1);
box.add(Box.createVerticalStrut(40));
box.add(box1);
box.add(Box.createVerticalStrut(10));
box.add(lb4);
box.add(new JLabel());
p1.add(box);
// 设置第二个面板
p2.setLayout(new GridBagLayout()); // 使用GridBagLayout布局管理器
p2.setPreferredSize(new Dimension(0, 60));
GridBagConstraints g2 = new GridBagConstraints();
g2.fill = GridBagConstraints.BOTH;
g2.weightx = 1.0;
g2.weighty = 1.0;
g2.gridx = 0;
g2.gridy = 0;
p2.add(b1, g2);
g2.gridx = 1;
p2.add(b2, g2);
g2.gridx = 2;
p2.add(b3, g2);
//为3个按钮添加事件
b1.addActionListener(new buttonAction());
b2.addActionListener(new buttonAction());
b3.addActionListener(new buttonAction());
this.getContentPane().add(p1);
this.getContentPane().add(p2, BorderLayout.SOUTH);
this.setTitle("机器人猜拳游戏");
this.setSize(300, 300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//事件类
class buttonAction extends AbstractAction{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1){
ta1.setText("剪刀");
init(ta1.getText());
}else if(e.getSource()==b2){
ta1.setText("石头");
init(ta1.getText());
}else if(e.getSource()==b3){
ta1.setText("布");
init(ta1.getText());
}
}
// 模拟电脑出拳 , 产生三个随机数 。0代表剪刀,1代表石头 , 2代表布
public String getQuan(){
String str="";
int num=new Random().nextInt(3) ;
if(num==0){
str="剪刀";
}else if(num==1){
str="石头";
}else if(num==2){
str="布";
}
return str;
}
// 判断输赢方法
public String isying(String s1,String s2){
String s="";
if(s1.equals(s2)){
s="平局";
}else if(s1.equals("剪刀")s2.equals("布")){
s="你赢";
}else if(s1.equals("石头")s2.equals("剪刀")){
s="你赢";
}else if(s1.equals("布")s2.equals("石头")){
s="你赢";
}else{
s="电脑赢";
}
return s;
}
public void init(String wo){
String sy=""; // 保存输赢结果
String dncq=getQuan(); //电脑出拳
if(wo.equals(dncq)){
sy="平局";
}else if(wo.equals("剪刀")dncq.equals("布")){
sy="你赢";
}else if(wo.equals("石头")dncq.equals("剪刀")){
sy="你赢";
}else if(wo.equals("布")dncq.equals("石头")){
sy="你赢";
}else{
sy="电脑赢";
}
ta2.setText(dncq);// 电脑出拳
lb4.setText("结果:" sy);
}
}
public static void main(String[] args) {
new Demo2();
}
}
求java人机猜拳游戏的程序我之前写了个猜拳游戏的源代码,不过没你想的这么精彩 。你才给5分就给你你自己修改了 , 应该很简单的 。要多给点分我可以帮你修改 。
import java.util.Scanner;
import java.util.Random;
public class caiquan
{
final int jiandao=0;
final int shitou=1;
final int bu=2;
public static void main(String[] args)
{
String yn=;y;;
while (yn.equals(;y;))
{
Scannerscanner = new Scanner(System.in);
System.out.println(;欢迎玩猜拳游戏 。请输入0,1,2:0表示剪刀,1表示石头,2表示布;);
int a = scanner.nextInt();
Random rd = new Random();
int b = rd.nextInt(3);
switch (b)
{
case 0:
{
System.out.println(;系统出的是剪刀;);
switch(a)
{
case 0:System.out.println(;平;);break;
case 1:System.out.println(;赢;);break;
case 2:System.out.println(;输;);break;
}
}
break;
case 1:
{
System.out.println(;系统出的是石头;);
switch(a)
{
case 0:System.out.println(;输;);break;
case 1:System.out.println(;平;);break;
case 2:System.out.println(;赢;);break;
}
}
break;
case 2:
{
System.out.println(;系统出的是布;);
switch(a)
{
case 0:System.out.println(;赢;);break;
case 1:System.out.println(;输;);break;
case 2:System.out.println(;平;);break;
}
}
}
Scanner ynn = new Scanner(System.in);
System.out.println(;是否继续?是请输入y,否则输入n 。;);
yn=ynn.next();
}
}
}
关于人机猜拳java代码和如何用java编写一个人机猜拳游戏的介绍到此就结束了 , 不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读