简单的猜拳游戏 却有不简单的算法
2016-08-16 00:11
225 查看
import java.util.Scanner; class Zhangsan { void showTitle() { System.out.println(" **********************"); System.out.println(" *********猜拳,开始******"); System.out.println(" **********************"); } void ShowRule() { System.out.println("比拳规则:1.剪刀 2.石头 3.布"); } void showPlayerMachineInfor(String playerName, String machineName) { System.out.println(playerName+"vs"+machineName+"对战"); } String askIfBegin(){ System.out.println("要开始吗?(y/n)"); Scanner input = new Scanner(System.in); String a = input.next(); while((a.compareTo("y")!=0) && (a.compareTo("n")!=0)) { System.out.print("重新输入:"); a = input.next(); } return a; } void showOnceResult(int playerFist, int machineFist, int[] playerWins, int[] machineWins) { if ((playerFist == 0 && machineFist == 1) || (playerFist == 1 && machineFist == 2) || (playerFist == 2 && machineFist == 0)){ System.out.println("^-^ 你输了 真瓜怂"); machineWins[0] += 1; } else if (playerFist == machineFist) { System.out.println("有缘 平手 再来"); } else { System.out.println("(^^) oh 你赢了 "); playerWins[0]+= 1; } } void showLastResult(String playerName, String machineName, int count,int playerWins, int machineWins) { System.out.println(playerName+" VS "+machineName); System.out.println("对战次数:"+count+"\n"); System.out.println("姓名\t得分"); System.out.println(playerName+"\t"+playerWins); System.out.println(machineName+"\t"+machineWins); if (playerWins >= machineWins) System.out.println("你赢了"); else System.out.println("呵呵,笨笨,下次加油啊!"); } } class Player { String playerName; String machineName; int fist; int[] wins = {0}; void selectMachine() { String [] machineNameArr = new String[]{"刘备","孙权","曹操"}; System.out.print("请选择对方角色(1.刘备 2.孙权 3.曹操)"); Scanner input = new Scanner(System.in); int MachineNum = input.nextInt(); machineName = machineNameArr[MachineNum-1]; } void inputName() { Scanner input = new Scanner(System.in); System.out.print("请输入你的姓名:"); playerName = input.next(); } void playerPlay() { String [] FistArr = new String[]{"剪刀","石头","布"}; System.out.print("请出拳:1.剪刀 2.石头 3.布 (输入相应数字):"); Scanner input = new Scanner(System.in); fist = input.nextInt()-1; System.out.println("你出拳:"+FistArr[fist]); } } class Machine { String name; int fist; int[] wins = {0}; void machinePlay() { fist = (int)Math.random()*3; String [] arr = new String[]{"剪刀","石头","布"}; System.out.println(arr[fist]); } } public class zgw { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Zhangsan zhangsan = new Zhangsan(); Player player = new Player(); Machine machine = new Machine(); int count = 0; zhangsan.showTitle(); zhangsan.ShowRule(); player.selectMachine(); player.inputName(); zhangsan.showPlayerMachineInfor(player.playerName, player.machineName); while (zhangsan.askIfBegin().equals("y")) { player.playerPlay(); machine.machinePlay(); zhangsan.showOnceResult(player.fist, machine.fist, player.wins, machine.wins); count++; // zhangsan.showLastResult(player.playerName, player.machineName, count, player.wins, machine.wins); } zhangsan.showLastResult(player.playerName, player.machineName, count, player.wins[0], machine.wins[0]); } }
相关文章推荐
- Android Native 绘图方法
- C#中struct和class的区别详解
- VBS ArrayList Class vbs中的数组类
- 大家看了就明白了css样式中类class与标识id选择符的区别小结
- 深入了解PHP类Class的概念
- jquery 表单验证之通过 class验证表单不为空
- 详解JavaScript中void语句的使用
- setAttribute 与 class冲突解决
- JavaScript通过HTML的class来获取HTML元素的方法总结
- JavaScript中的类(Class)详细介绍
- 原生js实现class的添加和删除简单代码
- javascript面向对象包装类Class封装类库剖析
- jQuery针对input的class属性写了多个值情况下的选择方法
- 详解js中class的多种函数封装方法
- jQuery使用hide方法隐藏指定元素class样式用法实例
- jQuery给多个不同元素添加class样式的方法
- jQuery点击改变class并toggle及toggleClass()方法定义用法
- jquery采用oop模式class类的使用示例
- jquery点击改变class并toggle的实现代码
- 解决C++ 无法从void 转换为LRESULT的方法详解