/*** 计算机玩家类** */import java.util.Scanner;public class ComputerPlayer {//属性String name;int score;//方法public int showFist(){int fistNo = (int)(Math.random() * 3 + 1);String fistType = "";switch (fistNo){case 1:fistType = "剪刀";break;case 2:fistType = "石头";break;case 3:fistType = "布";break;}System.out.println(name + "出拳:" + fistType);return fistNo;}
}
public class TestComputerPlayer {public static void main(String[] args) {ComputerPlayer computerplay = new ComputerPlayer();computerplay.name ="曹操";System.out.println(computerplay.showFist());}
}
/*** 玩家类* */import java.util.Scanner;public class PersonPlayer {String name;int score;public int showFist() {Scanner input = new Scanner(System.in);System.out.print("请出拳:1.剪刀 2.石头 3.布(输入相应的数字):");int fistNo = input.nextInt();String fistType = "";switch (fistNo){case 1:fistType = "剪刀";break;case 2:fistType = "石头";break;case 3:fistType = "布";break;}System.out.println("你出拳:" + fistType);return fistNo;}
}
public class TestPersonPlayer {public static void main(String[] args) {PersonPlayer personplayer = new PersonPlayer();System.out.println(personplayer.showFist());}
}
import java.util.Scanner;public class Game {int times;   //记录游戏的次数ComputerPlayer computerPlayer;   //nullPersonPlayer personPlayer;    //nullpublic void init() {System.out.println("出拳规则:1.剪刀  2.石头  3.布");System.out.print("选择对方角色(1:刘备 2:孙权 3:曹操):");Scanner input = new Scanner(System.in);int role = input.nextInt();computerPlayer = new ComputerPlayer();switch (role) {case 1:computerPlayer.name = "刘备";break;case 2:computerPlayer.name = ("孙权");break;case 3:computerPlayer.name = ("曹操");break;default:break;}System.out.print("请输入你的名字:");personPlayer = new PersonPlayer();personPlayer.name = input.next();System.out.println(personPlayer.name + "  VS  " + computerPlayer.name + " 对战");System.out.println();}public void star() {Scanner input = new Scanner(System.in);System.out.print("要开始吗(y/n):");String isBegin = input.next();if (isBegin.equalsIgnoreCase("y")) {int personFistNo = 0;int computerFistNo = 0;String isNext = "";do {times++;personFistNo = personPlayer.showFist();computerFistNo = computerPlayer.showFist();calcOnceResult(personFistNo, computerFistNo);System.out.print("是否进入下一轮(y/n)");isNext = input.next();} while (isNext.equalsIgnoreCase("y"));}print();}public void calcOnceResult(int personFistNo, int computerFistNo) {if (personFistNo == computerFistNo) {//各加1分personPlayer.score++;computerPlayer.score++;System.out.println("握手言和");} else {//人胜if ((personFistNo == 1 && computerFistNo == 3) || (personFistNo == 2 && computerFistNo == 1)|| (personFistNo == 3 && computerFistNo == 2)) {personPlayer.score = personPlayer.score + 2;System.out.println("人定胜天");} else {computerPlayer.score += 2;  //computerPlayer.score = computerPlayer.score + 2System.out.println("计算机胜利");}}}public void print() {System.out.println("------------------------------------------");System.out.println(computerPlayer.name + "  VS  " + personPlayer.name);System.out.println("对战次数:" + times);System.out.println("姓名\t\t得分");System.out.println(personPlayer.name + "\t\t" + personPlayer.score);System.out.println(computerPlayer.name + "\t\t" + computerPlayer.score);calcTotalResult();System.out.println("------------------------------------------");}public void calcTotalResult() {if (personPlayer.score > computerPlayer.score) {System.out.println("最终结果:人胜");} else if (personPlayer.score == computerPlayer.score) {System.out.println("最终结果:平局");} else {System.out.println("最终结果:人机胜");}}
}
public class TestGame {public static void main(String[] args) {Game game = new Game();game.init();game.star();}
}

最终组合成完全体

public class GameApp {public static void main(String[] args) {//游戏界面System.out.println("----------------------欢迎进入游戏世界----------------------");System.out.println("                  ************************                  ");System.out.println("                  *******猜拳 ,开始******                  ");System.out.println("                  ************************                  ");//组装游戏部分Game game = new Game();//初始化game.init();//开始游戏game.star();//输出游戏统计结果game.print();//输出游戏最终结果game.calcTotalResult();}
}

【面向对象】猜拳游戏相关推荐

  1. python使用面向对象思想开发一个人机对战的猜拳游戏(石头剪刀布)

    使用面向对象思想开发一个人机对战的猜拳游戏(石头剪刀布) import random class Role: def init(self, name=None, score=0): self.name ...

  2. Python使用面向对象思想开发一个人机对战的猜拳游戏(石头剪刀布)。

    [开发该软件的操作系统]:windows10 [软件开发环境/开发工具]:PyCharm [编程语言]:Python [开发目的]:这是老师布置的作业啦~ 供初学者参考学习 [开发者]:江西农业大学2 ...

  3. Java学习第二十七天<面向对象综合练习><猜拳游戏>

    面向对象综合练习 找最大值 package chapter06.D10综合练习; //找最大值 public class Homework01 {public static void main(Str ...

  4. 【Java语言】项目实践:人机猜拳游戏(源码)(面向对象方法)

    package com.njwbhz.part0217.Mora;import java.util.Scanner;public class PersonPlayer {//属性//人类玩家的名字St ...

  5. Python 学习笔记-第9讲:面向对象练习-猜拳游戏

    任务需求: 猜拳游戏: 人和机器猜拳 -------------------------------人机猜拳--------------------------- 请选择角色:1. 曹操 2. 张飞  ...

  6. (个人解题思路系列)猜拳游戏

    猜拳游戏的分析解题思路 前言 本系列是我自己关于遇到过的编程题目进行的分析思路总结,希望能够帮助到需要帮助的那些需要帮助的编程小白,同时也欢迎各位前辈予以指正和批评指导,谢谢. 题目: 通过控制台方式 ...

  7. python基础练习(猜拳游戏、扎金花游戏、购物小程序)

    猜拳游戏 需求分析: * 使用面向对象和python的基础语法,运用简单的逻辑处理实现猜拳游戏 * 要求游戏可以多次玩耍 * 要求统计分数 * 要求可以选择角色 # 玩家自己的类, class Own ...

  8. c语言猜拳游戏实训报告,Java猜拳小游戏程序设计实验报告.doc

    Java程序设计实验报告 班级: 学号: 姓名: 实验题目:猜拳小游戏 实验要求: 用java编写一个人机对战的猜拳小游戏.人选择性出拳,电脑随机出拳,判断输赢,记录输赢情况.有简单的操作界面. 实验 ...

  9. Python3学习实战——用类实现简单的猜拳游戏

    Python3学习实战--用类实现简单的猜拳游戏 前言 本笔记仅个人认知和见解,水平有限,还请见谅. 如有错误,还请指出,若有想法,欢迎共享! 内容不代表最优解决方案,甚至可能不是很好的方法,仅供参考 ...

  10. Java人机猜拳游戏(命令行版)

    这是本学期的java作业: java编写一个人机对战的猜拳小游戏.人选择性出拳,电脑随机出拳,判断输赢,记录输赢情况.有简单的操作界面. 学了一个学期的Java,感觉收获还挺多的.第一次用Java写小 ...

最新文章

  1. Leetcode 213.大家劫舍II
  2. TCL with SNPS llengthlappendget_cellsget_ports
  3. 各种软路由 - 自制路由器
  4. c++语言标准 pdf,C++14标准.pdf
  5. leetcode 220. Contains Duplicate III | 220. 存在重复元素 III (Treeset解法+分桶解法)
  6. 存钱吃利息存邮政储蓄好还是农业银行好?
  7. Java 9幕后花絮:新功能从何而来?
  8. java 排列3_java中的三大排序算法
  9. 字符串的回文子序列个数_计算给定字符串中回文子序列的数量
  10. python编程英语单词下载2019_图形化编程软件-mPython下载2019正式版 下载_久友软件下载...
  11. SpringBoot配置RestTemplate的代理和超时时间
  12. import package怎么用
  13. java怎么使用floor_Java NavigableSet floor()用法及代码示例
  14. 电梯设计需求调研报告
  15. python在财务中的应用实训报告-数据科学与大数据技术专业实训解决方案
  16. Matlab 绘制 - 点和向量:向量加减的方法和源码
  17. python判断字符串是否包含英文字母
  18. Hive元数据存储和表数据存储
  19. wps 表格 自动生成序号
  20. 在ANSYS WORKBENCH中使用APDL命令的例子

热门文章

  1. 盈利能力提升,荔枝靠“元宇宙”打开音频产业天花板?
  2. php 富文本双引号问题,将双引号变为quot;html转义入库
  3. 【Golang Leetcode】总目录(Day1~100)
  4. 哈佛大学构建动态网站--第七讲ajax
  5. Android面试问题大全
  6. Vue本地应用(通过Vue实现常见网页效果)
  7. 程序员,如何打破职业瓶颈期?
  8. COOIS选择屏幕增强
  9. window下编译32/64位库文件
  10. Android开发基础:利用 Android Studio开发看美女应用(4)