2018年6月12日 星期二

[JAVA] 1A2B

※ 此程式碼並非優化版本,僅供個人學習用途,若有任何問題請告知。



import java.util.Scanner;
public class oneAtwoB {
public static int[] random(int num_count) { //隨機產生數組
int[] r = new int[num_count];
for(int i = 0; i < num_count; ++i) {
r[i] = (int)((int)(Math.random() * 1000 + 0) / 100);
for(int j = 0; j < i; ++j) {
while(r[i] == r[j]) {
r[i] = (int)((int)(Math.random() * 1000 + 0) / 100);
}
for(int k = 0; k < j; ++k) {
while(r[i] == r[k]) {
r[i] = (int)((int)(Math.random() * 1000 + 0) / 100);
}
}
}
}
return r;
}
public static void printarr(int[] arr) { //印出陣列
for(int r : arr) {System.out.print(r+" ");}
}
public static void rule() { //印出遊戲規則
for(int i = 0; i < 38; ++i) {System.out.print("#");}
System.out.print("\n# \t\t\t\t\t\t #\n# 玩法說明:\t\t\t\t\t #");
System.out.print("\n#   猜數字,程式隨機生成一組數字,玩家猜出正確答案即可進入下一級別。 #\n# \t\t\t\t\t\t #");
System.out.print("\n# 重新開始:\t\t\t\t\t #");
System.out.print("\n#   輸入888即可重新開始。\t\t\t\t #\n# \t\t\t\t\t\t #");
System.out.print("\n# 結束遊戲:\t\t\t\t\t #");
System.out.print("\n#   輸入999即可結束程式。\t\t\t\t #\n# \t\t\t\t\t\t #\n");
for(int i = 0; i < 38; ++i) {System.out.print("#");}
}
public static void state(int level, int num_count, int[] arr) { //印出狀態
System.out.println("\n\n\t\t★ 當前 Level "+level+" ★\n");
System.out.print("自動生成數組中...\t\n數組生成完畢 ");
printarr(arr); //印出目前生成數組 可自行取消顯示
}
public static int[] checkinput(int level, int num_count, int[] arr, int[] ans) {
Scanner scn = new Scanner(System.in);
System.out.print("\n請輸入"+num_count+"個數字:");
for(int j = 0; j < num_count; ++j) {
int temp = scn.nextInt();
switch(temp) {
case 888:
ans[j] = 888;
return ans;
case 999:
ans[j] = 999;
return ans;
default: //檢查輸入數值是否為個位數
while((temp / 1) > 9 || (temp / 1) < 0) {
System.out.println("請重新輸入 只能輸入個位數");
System.out.print("\n請輸入"+num_count+"個數字:");
temp = scn.nextInt();
}
ans[j] = temp;
}
}
System.out.print("你的答案為 ");
printarr(ans);
return ans;
}
public static boolean checkans(int[] arr, int[] ans, int num_count) { //判斷AB數
//ans玩家答案->i arr程式出題->j
int A = 0, B = 0;
for(int i = 0; i < num_count; ++i) {
for(int j = 0; j < num_count; ++j) {
if(ans[i] == arr[j] && i == j) {++A;}
else if(ans[i] == arr[j]) {++B;}
}
}
System.out.println("\n目前提示:"+A+"A "+B+"B");
if(A == num_count) {return true;}
return false;
}
public static void main(String[] args) {
int end = 0, restart = 0;
int[] arr, ans;
rule();
System.out.println("\n\n\t\t* GAME START *\n");
while(end != 999 || end == 888) {
int level = 1, num_count = 4;
for(int i = level; i <= 10; ++i) { //共10級別
if(i >= 6) {num_count = 5;}
arr = new int[num_count];
arr = random(num_count);
ans = new int[num_count];
state(i, num_count, arr);
int[] game = checkinput(i, num_count, arr, ans);
if(game[0] == 888) {
end = 888;
break;
}
else if(game[0] == 999) {
end = 999;
break;
}
else {
while(checkans(arr, ans, num_count) != true) {
checkinput(i, num_count, arr, ans);
}
}
if(i >= 10) {
System.out.println("\n\n\t\t▲ 恭喜你,遊戲已結束! ▲");
end = 999;
}
}
}
System.out.println("\n\n\t\t* GAME END *\n");
}
}
view raw oneAtwoB.java hosted with ❤ by GitHub
結果如下:
######################################
#                                    #
# 玩法說明:                                #
#   猜數字,程式隨機生成一組數字,玩家猜出正確答案即可進入下一級別。 #
#                                       #
# 重新開始:                                #
#   輸入888即可重新開始。                        #
#                                       #
# 結束遊戲:                                #
#   輸入999即可結束程式。                        #
#                                       #
######################################

  * GAME START *



  ★ 當前  Level 1 ★

自動生成數組中... 
數組生成完畢 8 4 0 6 
請輸入4個數字:8 4 0 6
你的答案為 8 4 0 6 
目前提示:4A 0B


  ★ 當前  Level 2 ★

自動生成數組中... 
數組生成完畢 7 8 1 5 
請輸入4個數字:7 8 1 5
你的答案為 7 8 1 5 
目前提示:4A 0B


  ★ 當前  Level 3 ★

自動生成數組中... 
數組生成完畢 9 4 6 2 
請輸入4個數字:9 4 6 2
你的答案為 9 4 6 2 
目前提示:4A 0B


  ★ 當前  Level 4 ★

自動生成數組中... 
數組生成完畢 0 7 9 3 
請輸入4個數字:0 7 9 3
你的答案為 0 7 9 3 
目前提示:4A 0B


  ★ 當前  Level 5 ★

自動生成數組中... 
數組生成完畢 9 5 2 3 
請輸入4個數字:9 5 2 3
你的答案為 9 5 2 3 
目前提示:4A 0B


  ★ 當前  Level 6 ★

自動生成數組中... 
數組生成完畢 4 0 2 1 3 
請輸入5個數字:4 0 2 1 3
你的答案為 4 0 2 1 3 
目前提示:5A 0B


  ★ 當前  Level 7 ★

自動生成數組中... 
數組生成完畢 6 8 1 5 3 
請輸入5個數字:6 8 1 5 3
你的答案為 6 8 1 5 3 
目前提示:5A 0B


  ★ 當前  Level 8 ★

自動生成數組中... 
數組生成完畢 0 8 9 4 7 
請輸入5個數字:0 8 9 4 7
你的答案為 0 8 9 4 7 
目前提示:5A 0B


  ★ 當前  Level 9 ★

自動生成數組中... 
數組生成完畢 6 4 7 8 1 
請輸入5個數字:6 4 9 7 8 1
你的答案為 6 4 9 7 8 
目前提示:2A 2B

請輸入5個數字:6 4 7 8 1 
你的答案為 6 4 7 8 1 
目前提示:5A 0B


  ★ 當前  Level 10 ★

自動生成數組中... 
數組生成完畢 4 3 2 1 7 
請輸入5個數字:4 3 2 1 7
你的答案為 4 3 2 1 7 
目前提示:5A 0B


  ▲ 恭喜你,遊戲已結束! ▲


  * GAME END *


沒有留言:

張貼留言