简单的猜拳游戏 却有不简单的算法

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]);
	}

}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值