1. 程式人生 > >小專案2——猜拳遊戲

小專案2——猜拳遊戲

石頭剪刀布遊戲人類玩家:

/**
 * 石頭剪刀布遊戲人類玩家
 * @author 棣哩哩
 * @date 2018年11月13日 下午7:10:10
 * @remarks TODO
 */
public class HumanPlayer {
	/** 靜態常量:出拳的資訊*/
	public static final int MessageTypeFist = 1;
	/** 靜態常量:贏得比賽時的資訊*/
	public static final int MessageTypeWin = 2;
	/** 靜態常量:輸掉比賽時的資訊*/
	public static final int MessageTypeLose = 3;
	
	//屬性:名稱、分數、所出的拳、(角色對應的音效)、角色的個性化臺詞
	private String name;
	private int score;
	private int fist;
	
	/**角色在出拳的時候,所說的個性化臺詞*/
	private String[] fistWords = {
		"動感光波",
		"大象大象你的鼻子為什麼這麼長",
		"必殺技:露PP",
		"大姐姐你喜歡吃青椒麼",
		"歐巴桑,吃我一拳"
	};
	
	private String[] winWords = {
		"excellent",
		"nice",
		"hello",
		"hi",
		"You had me at hello"
	};
	
	private String[] loseWords = {
			"sad",
			"worse",
			"bad",
			"bye",
			"piss off"
		};
	//方法:設定和得到名稱、分數、(播放音效)、設定個性化臺詞、出拳

	public String getName() {
		return name;
	}

	/** 
	 * 根據傳入的訊息型別,隨機列印對應型別的訊息
	 * @param msgType 對應HumanPlayer類中定義的靜態常量
	 */
	public void sendMessage(int msgType){
		//生成一個0-4之間的隨機數字
		int index = ((int)(Math.random() * 1000)) % 5;
		String message = null;  //要列印的訊息
		switch (msgType) {
		case MessageTypeFist:
			message = fistWords[index];
			break;
		case MessageTypeWin:
			message = winWords[index];
			break;
		case MessageTypeLose:
			message = loseWords[index];
			break;
		}
		System.out.println(message);
	}
	public void setName(String name) {
		this.name = name;
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	public int getFist() {
		return fist;
	}

	public void setFist(int fist) {
		this.fist = fist;
	}

}

石頭剪刀布遊戲電腦玩家:

/**
 * 石頭剪刀布遊戲電腦玩家
 * @author 棣哩哩
 * @date 2018年11月13日 下午7:10:10
 * @remarks TODO
 */
public class ComPlayer {
	/** 靜態常量:出拳的資訊*/
	public static final int MessageTypeFist = 1;
	/** 靜態常量:贏得比賽時的資訊*/
	public static final int MessageTypeWin = 2;
	/** 靜態常量:輸掉比賽時的資訊*/
	public static final int MessageTypeLose = 3;
	
	//屬性:名稱、分數、所出的拳、(角色對應的音效)、角色的個性化臺詞
	private String name;
	private int score;
	private int fist;
	
	/**角色在出拳的時候,所說的個性化臺詞*/
	private String[] fistWords = {
		"三三兩兩",
		"你的心事",
		"悠悠晃晃",
		"停在我心上",
		"一艘小船"
	};
	
	private String[] winWords = {
		"bravo",
		"fantastic",
		"amazing",
		"welldone",
		"call me by your name"
	};
	
	private String[] loseWords = {
			"pity",
			"grieve",
			"console",
			"see you",
			"next"
		};
	//方法:設定和得到名稱、分數、(播放音效)、設定個性化臺詞、出拳

	public String getName() {
		return name;
	}

	/** 
	 * 根據傳入的訊息型別,隨機列印對應型別的訊息
	 * @param msgType 對應HumanPlayer類中定義的靜態常量
	 */
	public void sendMessage(int msgType){
		//生成一個0-4之間的隨機數字
		int index = ((int)(Math.random() * 1000)) % 5;
		String message = null;  //要列印的訊息
		switch (msgType) {
		case MessageTypeFist:
			message = fistWords[index];
			break;
		case MessageTypeWin:
			message = winWords[index];
			break;
		case MessageTypeLose:
			message = loseWords[index];
			break;
		}
		System.out.println(message);
	}
	public void setName(String name) {
		this.name = name;
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	public int getFist() {
		//電腦的出拳實際是生成1-3之間的隨機數字時
		fist = (int)(Math.random() * 30000) % 3 + 1;
		return fist;
	}

//	public void setFist(int fist) {
//		this.fist = fist;
//	}

}

遊戲房間

import java.util.Scanner;

/**
 * 遊戲房間
 * @author 棣哩哩
 * @date 2018年11月13日 下午8:11:48
 * @remarks TODO
 */
public class GameRoom {
	//屬性:記分牌、道具、參與遊戲的玩家、房間號、房間名稱、房間密碼
	//private String weapon;
	private HumanPlayer player1 = null;
	private ComPlayer player2 = null;
	/** 房間ID*/
	private int roomID;
	/** 房間名稱*/
	private String roomName;
	/** 房間密碼*/
	private String roomPass;
	
	public GameRoom(String roomName, String roomPass){
		if(!("棣哩哩的遊戲盒子".equals(roomName) && "123".equals(roomPass))){
			System.out.println("房間密碼錯誤,遊戲強制退出");
			return;
		}
		this.setRoomName(roomName);
		initGame();
		startGame();
		endName();
	}
	
	//方法:開始遊戲、初始化遊戲、顯示選單、選擇人物、判斷輸贏(可以定義在裁判類中)
	public void initGame(){
		//1.例項化遊戲玩家物件
		player1 = new HumanPlayer();
		player2 = new ComPlayer();
		//2.初始化遊戲其他屬性
		for (int i = 0; i < 30; i++) {
			System.out.print("*");
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println("\n遊戲載入成功!");
	}
	
	public void startGame(){
		showMenu();
		choiceRole();
		gaming();
		//列印結果
	}
	
	/** 遊戲大迴圈*/
	private void gaming() {
		while(true){
			//雙方出拳
			System.out.print(player1.getName() + "請出拳:");
			//player1.setFist(new Scanner(System.in).nextInt());
			Scanner input = new Scanner(System.in);//省略判斷
			player1.setFist(input.nextInt());
			int fist1 = player1.getFist();
			player2.getFist();
			int fist2 = player2.getFist();
			//判斷輸贏
			int result = judge(fist1, fist2);
			//如果返回-1,玩家1勝利;返回0,打平;返回1,玩家2勝利
			if(result == -1){
				System.out.println("【吉永老師】:" + player1.getName() + "勝利!");
				player1.sendMessage(HumanPlayer.MessageTypeWin);
				player2.sendMessage(ComPlayer.MessageTypeLose);
			}else if(result == 0){
				System.out.println("【吉永老師】:平局");
				player1.sendMessage(HumanPlayer.MessageTypeFist);
				player2.sendMessage(ComPlayer.MessageTypeFist);
			}else{
				System.out.println("【吉永老師】:" + player2.getName() + "勝利!");
				player2.sendMessage(ComPlayer.MessageTypeWin);
				player1.sendMessage(HumanPlayer.MessageTypeLose);
			}
			System.out.print("是否繼續遊戲?");
			String answer = new Scanner(System.in).next();
			if(!("y".equalsIgnoreCase(answer))){
				break;
			}
		}
	}
	
	public void showMenu(){
		System.out.println("歡迎進入 《" + roomName +  "》 遊戲房間");
		System.out.println("出拳規則:1-剪刀;2-石頭;3-布");
	}
	
	public void choiceRole(){
		System.out.println("對方角色:1-風間;2-妮妮;3-正南;4-阿呆");
		System.out.print("請選擇:");
		int choice = new Scanner(System.in).nextInt();
		String[] nameArray = {"風間", "妮妮", "正南", "阿呆"};
		//省略對使用者輸入的驗證
		player2.setName(nameArray[choice - 1]);
		player1.setName("野原新之助");
		System.out.println(player1.getName() + "vs." + player2.getName());
	}
	
	public int judge(int fist1, int fist2){
		if(fist1 == fist2) return 0;//平局
		if(fist1 == 1 && fist2 == 3 || fist1 == 2 && fist2 == 1 || fist1 == 3 && fist2 == 2){
			//玩家1分數+1,玩家2分數—1
			player1.setScore(player1.getScore() + 1);
			player2.setScore(player2.getScore() - 1);
			return -1;//玩家1勝利
		}
		//玩家2勝利
		player1.setScore(player1.getScore() - 1);
		player2.setScore(player2.getScore() + 1);
		return 1;
	}
	
	public void endName(){
		System.out.println("遊戲結束,歡迎再來");
		System.out.println(player1.getName() + "\t" + player2.getName());
		System.out.println(player1.getScore() + "\t" + player2.getScore());
	}
	
	public int getRoomID() {
		return roomID;
	}
	public void setRoomID(int roomID) {
		this.roomID = roomID;
	}
	public String getRoomName() {
		return roomName;
	}
	public void setRoomName(String roomName) {
		this.roomName = roomName;
	}
	public String getRoomPass() {
		return roomPass;
	}
	public void setRoomPass(String roomPass) {
		this.roomPass = roomPass;
	}	
}

測試主程式碼

public class TestMain {
	public static void main(String[] args) {
//		HumanPlayer player1 = new HumanPlayer();
//		player1.sendMessage(HumanPlayer.MessageTypeWin);
		new GameRoom("棣哩哩的遊戲盒子", "123");
	}

}