1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import java.util.Scanner;

public class Main {
	//global variables

	static char[] vowels = {'a', 'e', 'i', 'o', 'u'};
	static char[] letters = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
	static int[] scores = {1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};
	public static void main(String[] args) {
		//initialization
		String[] dictionary = FileUtil.readDictFromFile("words.txt");
		int currentPlayer = 1;
		String userInput = "I'm a dumbest string";
		Scanner myScanner = new Scanner(System.in);
		boolean askAgain = true;
		int[] scoreBoard = new int[3];
		int tempScore = 0;

		System.out.println("++++++++++Find You Words++++++++++");

		while(currentPlayer<=2){
			//Generate random letters consisting at least 2 vowels
			char[] random = generateRandomLetters();
			System.out.print("Letters for player " + currentPlayer + " : ");
			for (int i = 0; i < random.length; i++) {
				System.out.print(random[i] + " ");
			}
			System.out.println("");
			askAgain = true;
			while(askAgain) {
				//Ask the user for a word
				System.out.print("Player "+currentPlayer+" : Enter your word (or @ to pass or ! to quit) : ");
				userInput = myScanner.nextLine();
				if(userInput.equals("@")) {
					System.out.println("Total score for word : 0");
					System.out.println("Total score for game : "+scoreBoard[currentPlayer]);
					System.out.println();
					break;
				} else if(userInput.equals("!")){
					currentPlayer = 0;
					break;

				}
				//temporary character array
				char[] tempChar = new char[userInput.length()];
				tempChar = userInput.toCharArray();
				userInput = userInput.toUpperCase(); //change it into upper case since words.txt contains only upper case
				if(doesContain(random,tempChar)) { //wrong algorithm
					if(isInDictionary(dictionary,userInput)) {
						tempScore = giveScores(tempChar);
						scoreBoard[currentPlayer] += tempScore;
						System.out.println("Total score for word : "+tempScore);
						System.out.println("Total score for game : "+scoreBoard[currentPlayer]);
						System.out.println();
						askAgain = false;
					} else {
						System.out.println("The word is not a valid word.");
					}
				} else {
					System.out.println("A valid word is formed but one or more letter(s) used are not yours.");
				}
			}
			if(currentPlayer == 1) { 
				currentPlayer = 2;
			}else if(currentPlayer == 0) {
				break;
			} else {
				currentPlayer = 1;
			}
		}
		int winner = checkWinner(scoreBoard);
		System.out.println("Total score for player 1 : " + scoreBoard[1]);
		System.out.println("Total score for player 2 : " + scoreBoard[2]);
		
		if(winner == 1) {
			System.out.println("Player 1 won");
		} else if(winner == 2) {
			System.out.println("Player 2 won");
		} else {
			System.out.println("Tie");
		}
		System.exit(0);
	}


	public static boolean isInDictionary(String[] dict, String userInput) {
		for(int i = 0; i<dict.length;i++) {
			if(userInput.equals(dict[i])) {
				return true;
			}
		}
		return false;
	}


	public static boolean doesContain(char[] randomizedArray,char[] userInputArray) {
		int right = 0;
		for(int a = 0; a < randomizedArray.length;a++) {
			for(int b = 0; b<userInputArray.length;b++) {
				if(randomizedArray[a] == userInputArray[b]) {
					right++;
				}
			}
		}
		if(right >= userInputArray.length) {
			return true;
		}
		return false;
	}
	public static char[] generateRandomLetters() {
		char[] randomPool = new char[10];
		int index = 0;
		for (int i = 0; i < randomPool.length; i++) {
			index = (int) Math.round(Math.random() * 21);
			randomPool[i] = letters[index];
		}
		if (!ifTwoVowels(randomPool)) { //check whether it contains at least 2 vowels. If not it regenerate the random number.
			randomPool = generateRandomLetters();
		}
		;
		return randomPool;
	}

	public static boolean ifTwoVowels(char[] toCheck) {
		int found = 0;
		for (int i = 0; i < toCheck.length; i++) {
			for (int j = 0; j < vowels.length; j++) {
				if (toCheck[i] == vowels[j]) {
					found++;
				}
				if (found > 2) {
					return true;
				}
			}
		}
		return false;
	}

	public static int giveScores(char[] userInput) {
		int score = 0;
		for(int i = 0;i<letters.length;i++) {
			for(int j=0;j<userInput.length;j++) {
				if(letters[i] == userInput[j]) {
					score += scores[i];
				}
			}
		}
		return score;
	}

	public static int checkWinner(int[] scoreBoard) {
		int winner = 0;
		int temp1 = scoreBoard[1];
		int temp2 = scoreBoard[2];
		if(temp1 > temp2) {
			winner = 1;
		} else if (temp1 < temp2) {
			winner = 2;
		}
		return winner;
	}
}