下面是电脑程序将会展示出这个程序时怎么运行的,请仔细的观看: FindYourWords Letters of player 1 : a c f h i n p r s w Enter your word (or ‘@’ to pass or ‘!’ to quit) : saw Total score for word : 6 Total score for game : 6
Letters of player 2 : b c e g n n o t x z Enter your word (or ‘@’ to pass or ‘!’ to quit): bench Error : A valid word is formed but one or more letter(s) used are not yours Enter your word (or ‘@’ to pass or ‘!’ to quit): benc Error : An invalid word is formed Enter your word (or ‘@’ to pass or ‘!’ to quit): bet Total score for word : 5 Total score for game : 5
Letters of player 1 : c e f g h i n p r t Enter your word (or ‘@’ to pass or ‘!’ to quit): @ Total score for word : 0 Total score for game : 6
Letters of player 2 : a c i g n n o v x z Enter your word (or ‘@’ to pass or ‘!’ to quit): gain Total score for word : 5 Total score for game : 10
Letters of player 1 : c e f g h i n p r t Enter your word (or ‘@’ to pass or ‘!’ to quit): !
Total score for player 1: 6 Total score for player 2: 10 Player 2 won!
public static String dict[] = FileUtil.readDictFromFile("words.txt");
public static int totalScoreForPlayer1 = 0; public static int totalScoreForPlayer2 = 0;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in); String str = null; int index = 1; boolean exitFlg = false; do {
char[] arr = getWords(); System.out.print("Letters of player " + index + ": \n"); diplayOfWords(arr);
boolean flag = false;
do { System.out.print("Enter your word (or ‘@’ to pass or ‘!’ to quit) : "); str = scan.next();
if (str.equals("@")) { break; } else if (str.equals("!")) { exitFlg = true; break; }
flag = isWithinWords(arr, str); if (!flag) { System.out.println("Error : A valid word is formed but one or more letter(s) used are not yours"); continue; }
flag = isValid(str);
if (!flag) { System.out.println("Error : An invalid word is formed"); continue; }
if (flag) { System.out.println("Total score for word :" + getScoreForWord(str, index));
if (index == 1) { System.out.println("Total score for game :" + totalScoreForPlayer1); System.out.println(""); } else if (index == 2) { System.out.println("Total score for game :" + totalScoreForPlayer2); System.out.println(""); }
}
} while (!flag && !exitFlg);
if (index == 1) { index++; } else if (index == 2) { index--; }
} while (!exitFlg);
System.out.println("Total score for player 1:" + totalScoreForPlayer1); System.out.println("Total score for player 2:" + totalScoreForPlayer2); if (totalScoreForPlayer1 > totalScoreForPlayer2) { System.out.println("Player 1 won!"); } else if (totalScoreForPlayer1 < totalScoreForPlayer2){ System.out.println("Player 2 won!"); } else { System.out.println("TotalScore is same!"); }
}
public static char[] getWords() { int random = 0; char[] array = new char[10];
for (int i = 0; i < 8; i++) { random = (int) (Math.random() * 26 + 97); char c = (char) random;
array[i] = c; }
array[8] = getYuanyin(); array[9] = getYuanyin();
Arrays.sort(array); return array; }
public static boolean isYuanyin(char c) {
if (c == 97 || c == 101 || c == 105 || c == 111 || c == 117) { return true; }
return false; }
public static char getYuanyin() { int random = (int) (Math.random() * 5); if (random == 0) { return 'a'; } else if (random == 1) { return 'e'; } else if (random == 2) { return 'i'; } else if (random == 3) { return 'o'; } else { return 'u'; } }
public static void diplayOfWords(char[] c) { for (int i = 0; i < c.length; i++) { System.out.print(c[i] + " "); } }
public static boolean isValid(String str) { boolean flag = false;
for (int i = 0; i < dict.length; i++) { if ((str.toUpperCase()).equals(dict[i])) { flag = true; } }
return flag; }
public static boolean isWithinWords(char[] arr, String str) { boolean flag = true;
char[] arrStr = str.toCharArray();
for (int i = 0; i < arrStr.length; i++) { flag = false; for (int j = 0; j < arr.length; j++) { if (arrStr[i] == arr[j]) { flag = true; break; } }
if (flag == false) { break; } }
return flag; }
public static int getScoreForWord(String str, int index) {
int score = 0;
char[] arrC = str.toCharArray(); for (int i = 0; i < arrC.length; i++) { switch (arrC[i]) { case 'a': case 'e': case 'i': case 'o': case 'n': case 'r': case 't': case 'l': case 's': case 'u': score = score + 1; break; case 'd': case 'G': score = score + 2; break; case 'b': case 'c': case 'm': case 'p': score = score + 3; break; case 'f': case 'h': case 'v': case 'w': case 'y': score = score + 4; break; case 'k': score = score + 5; break; case 'j': case 'x': score = score + 8; break; case 'q': case 'z': score = score + 10; break; default: break; } }