Java 猜字謎遊戲

package fundmental_excise6;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author : jeasion
 * @date 創建時間:2019年4月10日 上午9:35:57
 * @name 猜字母遊戲——實現遊戲等級
 * @comment 爲猜字母遊戲添加遊戲等級。遊戲等級設爲三等:5、7和9,代表所需要猜測的字母個數。 遊戲    開始時,由玩家選擇遊戲等級(5,7,9)。
 *          如果選擇7,則會隨機產生7個字符, 然後玩家輸入一個字符串包含7個字符,看這7個字符和隨機產生的7個字符比較, 看是否正確,並統計分數。
 *          另外,如果輸入其它,重新提示輸入遊戲等級
 * @return * A-Z 65-90 a-z 97-122
 */
public class GuessingWord {
	// 主函數
	public static void main(String[] args) {
		int degree = 5;// 難度
		int[] statistics = new int[2];

		GuessingWord guessingWord = new GuessingWord();

		degree = guessingWord.degree();// 獲取難度
		char[] ch = guessingWord.generateString(degree);// 生成字符串,並將字符串存儲
		System.out.println("字符串:" + Arrays.toString(ch));

		statistics = guessingWord.regx(ch, degree, statistics);
		while (statistics[1] != degree) {
			System.out.println("再猜");
			statistics = guessingWord.regx(ch, degree, statistics);
		}

		guessingWord.score(statistics[0]);

	}

	// 遊戲等級
	public int degree() {
		int degree;
		Scanner scanner = new Scanner(System.in);
		System.out.print("請選擇遊戲等級5 7 9:");

		degree = scanner.nextInt();
		while (degree != 5 && degree != 7 && degree != 9) {
			System.out.println("抱歉,你輸入的遊戲等級有誤,請重新輸入");
			degree = scanner.nextInt();
		}
		return degree;
	}

	// 生成字符
	public char[] generateString(int degree) {
		char[] ch = new char[degree];

		/*
		 * 構建一個從0-25的數組, 然後將其中的值加65賦值給char 
		 * 裏面的數只要被用過就將其賦值爲1000 保證char不會取到重複的值
		 */
		int[] nums = new int[26];
		for (int i = 0; i < nums.length; i++) {
			nums[i] = i;
		}
		for (int i = 0; i < degree; i++) {
			int temp = 0;
			do {
				temp = (int) (Math.random() * 26);
				ch[i] = (char) (65 + temp);
			} while (nums[temp] == 1000);
			nums[temp] = 1000;
		}

		return ch;
	}

	// 字符匹配
	public int[] regx(char ch[], int degree, int[] statistics) {

		Scanner scanner = new Scanner(System.in);
		char[] inputChar = new char[ch.length];
		int pos = 0;
		int num = 0;

		// 獲取用戶輸入的字符串,並將全部字符轉換成大寫字母,方便統計
		System.out.println("請輸入你的字符串");
		String string = scanner.nextLine().toUpperCase();

		// 用戶輸入的字符串長度和難度不同,要求用戶重新輸入
		while (string.length() != degree) {
			System.out.println("字符長度不正確,請重新輸入");
			string = scanner.next().toUpperCase();
		}

		// 將用戶輸入的字符串拆分成字符數組
		for (int i = 0; i < inputChar.length; i++) {
			inputChar[i] = string.charAt(i);
		}
//		System.out.println("你輸入的字符數組:" + Arrays.toString(inputChar));
//		System.out.println("原字符數組:" + Arrays.toString(ch));
		// 進行字符串匹配
		for (int i = 0; i < inputChar.length; i++) {
			for (int j = 0; j < inputChar.length; j++) {
				if (ch[i] == inputChar[j]) {
					num++;
					if (i == j) {
						pos++;
						break;
					}
				}
			}
		}

		statistics[0]++; // 獲取次數
		statistics[1] = num;// 獲取猜對的個數
		System.out.println("你猜對了" + num + "個字符," + "其中" + pos + "個字符位置正確,總次數=" + statistics[0] + "\t");

		return statistics;
	}

	// 分數統計
	public void score(int count) {
		System.out.println("你一共猜了" + count + "次,得分:" + (500 - count * 10));
	}

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章