您的位置:首页 > 其它

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

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

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  class void scanner