java简单的人机猜拳小游戏

来源:互联网 发布:化学物质数据库 编辑:程序博客网 时间:2024/05/18 07:36
今天翻以前的代码,偶然看见了自己刚学java的时候写的一个简单的猜拳小游戏,非常基础,不过挺有意思的,也包括了积分制,贴到这里让大家看看
package game;public class Computer {static int i;public static int shownum() {i = (int) (Math.random() * 3);return i;}public static String showFist() {String a[] = { "剪刀", "石头", "布" };return a[i];}public static String choos(int a) {String b = null;switch (a) {case 1:b = "刘备";break;case 2:b = "孙权";break;case 3:b = "曹操";break;}return b;}// public static void main(String[] args) {// System.out.println(Computer.guess());// }}
package game;/* * 我们自己定义类的时候,要考虑的问题 *    1: 要职责明确  一个类,就干自己专长的事情 *    2: IPO *     *//* *  customer 类 是做什么用的,处理游戏 */public class Customer {static String name = "玩家";static int score;public static String getFirst(int c) {String d = null;switch (c) {case 1:d = "剪刀";break;case 2:d = "石头";break;case 3:d = "布";break;}return d;}}

package game;import java.util.Scanner;public class Game {public static void starGame() {System.out.println("---------欢迎进入游戏世界---------");System.out.println("\t***************");System.out.println("\t****猜拳开始****");System.out.println("\t***************");System.out.println("请选择你想和谁对战(1:刘备2:孙权3:曹操)");int computerjifen=0;int gamerjifen=0;int equ=0;int count=0;Scanner sd = new Scanner(System.in);int a = sd.nextInt();Computer.choos(a);System.out.print("要开始吗y/n:");String b=sd.next();do{Computer.shownum();int x=Computer.shownum()+1;Computer.showFist();if(b.equals("y")){//boolean f;System.out.print("\n请出拳:1.剪刀2.石头3.布(请输入相应数字)");int num = sd.nextInt();System.out.println("你出拳:"+Customer.getFirst(num));System.out.println(Computer.choos(a)+"出拳:"+Computer.showFist());if(x==num){System.out.println("此局:平局,嘿嘿,你等着瞧吧!");equ++;}else if(x==1&&num==2){System.out.println("结果,你赢了");gamerjifen++;}else if(x==1&&num==3){System.out.println("结果,你输了");computerjifen++;}else if(x==2&&num==1){System.out.println("结果,你输了");computerjifen++;}else if(x==2&&num==3){System.out.println("结果,你赢了");gamerjifen++;}else if(x==3&&num==1){System.out.println("结果,你赢了");gamerjifen++;}else if(x==3&&num==2){System.out.println("结果,你输了");computerjifen++;}count++;System.out.println("-------------------");System.out.println("\n是否开始下一轮y/n");String r=sd.next();//if(r.equals("y")){//f=true;//}if(r.equals("n")){if(computerjifen>gamerjifen){System.out.println("-------------------");System.out.println(Computer.choos(a)+"vs玩家");System.out.println(Computer.choos(a)+"的积分为"+computerjifen+",玩家的积分为"+gamerjifen);System.out.println("对战次数"+count+",出的一样的情况有"+equ+"次");System.out.println("结果,你输了");break;}else if(computerjifen<gamerjifen){System.out.println("-------------------");System.out.println(Computer.choos(a)+"vs玩家");System.out.println(Computer.choos(a)+"的积分为"+computerjifen+",玩家的积分为"+gamerjifen);System.out.println("对战次数"+count+",出的一样的情况有"+equ+"次");System.out.println("结果,你赢了");break;}else{System.out.println("-------------------");System.out.println(Computer.choos(a)+"vs玩家");System.out.println(Computer.choos(a)+"的积分为"+computerjifen+",玩家的积分为"+gamerjifen);System.out.println("对战次数"+count+",出的一样的情况有"+equ+"次");System.out.println("结果,平局");break;}}}else if(b.equals("n")){if(computerjifen>gamerjifen){System.out.println("-------------------");System.out.println(Computer.choos(a)+"vs玩家");System.out.println(Computer.choos(a)+"的积分为"+computerjifen+",玩家的积分为"+gamerjifen);System.out.println("对战次数"+count+",出的一样的情况有"+equ+"次");System.out.println("结果,你输了");break;}else if(computerjifen<gamerjifen){System.out.println("-------------------");System.out.println(Computer.choos(a)+"vs玩家");System.out.println(Computer.choos(a)+"的积分为"+computerjifen+",玩家的积分为"+gamerjifen);System.out.println("对战次数"+count+",出的一样的情况有"+equ+"次");System.out.println("结果,你赢了");break;}else{System.out.println("-------------------");System.out.println(Computer.choos(a)+"vs玩家");System.out.println(Computer.choos(a)+"的积分为"+computerjifen+",玩家的积分为"+gamerjifen);System.out.println("对战次数"+count+",出的一样的情况有"+equ+"次");System.out.println("结果,平局");break;}}else{System.out.println("请重新开始!!!!");break;}}while(true);sd.close();}}

package game;//import java.util.Scanner;public class GameTest {public static void main(String[] args) {Game.starGame();}}



1 0
原创粉丝点击