⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 playrun.java

📁 一个古老的游戏
💻 JAVA
字号:
import javax.microedition.lcdui.*;
import java.util.*;
public class PlayRun extends Form implements CommandListener {
	Random ran;
	String final_resultText;
	String playerChoice;
	String XishiChoice;
	String sentence;
	String playerName = PlayerName.playerName;
	StringItem siPlayer;
	StringItem siXishi;
	StringItem siCurrentResult;
	StringItem siFinalResult;
    ChoiceGroup cg;
    String[] sFSC;
	Command cmdOK;
	Command cmdBACK;
	int x = 0; // 胜
	int y = 0; // 平
	int z = 0; // 负
    int index;
	public PlayRun() {
		super("请选择并确认后出招...");
		cmdOK = new Command("确定", Command.OK, 1);
		cmdBACK = new Command("返回", Command.BACK, 1);
		cg = new ChoiceGroup(null, Choice.EXCLUSIVE);
		cg.append("剪刀", null);
		cg.append("石头", null);
		cg.append("布", null);
		siPlayer = new StringItem(playerName + ":", null);
		siXishi = new StringItem("西施" + ":", null);
		siCurrentResult = new StringItem("本局胜负:", null);
		siFinalResult = new StringItem("游戏结果:", null);
		append(cg);
		append(siPlayer);
		append(siXishi);
		append(siCurrentResult);
		append(siFinalResult);
		addCommand(cmdOK);
		addCommand(cmdBACK);
		setCommandListener(this);
	}
	public void commandAction(Command c, Displayable d) {
        index = cg.getSelectedIndex();
		if(c == cmdBACK){
			MainMidlet.display.setCurrent(new MenuList());
		}
		if(c == cmdOK){  // 确定时显示本局结果
			siPlayer.setText(cg.getString(index));
			siXishi.setText(run(index));
			siCurrentResult.setText(logic(index,1));
			siFinalResult.setText(logic(index,2));
		}
	}
	// 得到一定概率的随机数
	public int rn() {
		ran = new Random();
		int[] n = new int[10];
		for (int x = 0, i = 0; i < 10; i++) {
			if (ran.nextInt(10) < 1) { // 绝胜
				x = 0;
			} else if (ran.nextInt(10) < 2) { // 绝平
				x = 1;
			} else if (ran.nextInt(10) < 5) { // 伪随机
				x = 2;
			} else { // 绝负
				x = 3;
			}
			n[i] = x;
		}
		return n[ran.nextInt(10)];
	}
	// 设备端应招:一定概率的随机反馈
	public String run(int index) {
		String[] sFSC = { "剪刀", "石头", "布" };
		int lable = rn();
		switch (lable) {
		case 0: // 绝胜
			if (index == 0) {
				XishiChoice = "石头";
			}
			if (index == 1) {
				XishiChoice = "布";
			}
			if (index == 2) {
				XishiChoice = "剪刀";
			}
			break;
		case 1: // 绝平
			XishiChoice = cg.getString(index);
			break;
		case 2: // 伪随机
			XishiChoice = sFSC[rn()];
			break;
		case 3: // 绝负
			if (index == 0) {
				XishiChoice = "布";
			}
			if (index == 1) {
				XishiChoice = "剪刀";
			}
			if (index == 2) {
				XishiChoice = "石头";
			}
			break;
		}
		return XishiChoice;
	}

	// 逻辑比较并反馈
	public String logic(int index, int n) {
		String outputString = "";
		playerChoice = cg.getString(index);
		
		if (XishiChoice.equals(playerChoice)) {
			sentence = "平";
			y++;
		}
		if (XishiChoice.equals("剪刀") && playerChoice.equals("石头")) {
			sentence = "胜";
			x++;
		}
		if (XishiChoice.equals("剪刀") && playerChoice.equals("布")) {
			sentence = "负";
			z++;
		}
		if (XishiChoice.equals("石头") && playerChoice.equals("剪刀")) {
			sentence = "负";
			z++;
		}
		if (XishiChoice.equals("石头") && playerChoice.equals("布")) {
			sentence = "胜";
			x++;
		}
		if (XishiChoice.equals("布") && playerChoice.equals("石头")) {
			sentence = "负";
			z++;
		}
		if (XishiChoice.equals("布") && playerChoice.equals("剪刀")) {
			sentence = "胜";
			x++;
		}
		final_resultText = "胜" + x/2 + "局,平" + y/2 + "局,负" + z/2 + "局。";
		if (n == 1){
			outputString = sentence;
		}
		if (n == 2){
			outputString = final_resultText;
		}
		return outputString;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -