java简单的人机猜拳小游戏

今天翻以前的代码,偶然看见了自己刚学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();


	}

}



  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值