I am writing a program to validate the following scenarios:
Scenario 1:
I am using the Random class from java.util. The random class will generate 10 letters from a-z and within 10 letter, minimum 2 letters must be a vowels.
Scenario 2:
When the player 1 and player 2 form a word from A-Z, he will score some points. There will be a score for each letter. I have already assigned the values for A-Z. At the end of the game, the system should display a scores for player 1 and player 2. How do i do it?
Please help. I will post my code here.
Thanks a lot.
===========================================
import java.util.Random;
import java.util.Scanner;
public class FindYourWords {
public static void main(String[] args) {
Random rand = new Random();
Scanner userInput = new Scanner(System.in);
//==================Player object===============================================
Player playerOne = new Player();
playerOne.wordScore = 0;
playerOne.choice = "blah";
playerOne.turn = true;
Player playerTwo = new Player();
playerTwo.wordScore = 0;
playerTwo.choice = "开发者_运维知识库blah";
playerTwo.turn = false;
//================== Alphabet ==================================================
String[] newChars = { "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"
}; //values of the 26 alphabets to be used
int [] letterScore = {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}; // to assign score to the player1 and player 2
String[] vowel = { "a", "e", "i", "o", "u" }; // values for vowels
int vow=0;
System.out.println("FINDYOURWORDS\n");
int[] arrayRandom = new int[10]; //int array for word limiter
String[] randomLetter = new String[10]; //storing the letters in newChars into this array
//===============================================================================
boolean cont = true;
while (cont) {
if (playerOne.turn) {
System.out.print("Letters of Player 1: ");
}
else if (!playerOne.turn) {
System.out.print("Letters of Player 2: ");
}
for (int i = 0; i < arrayRandom.length; i++) { //running through the array limiter
int r = rand.nextInt(newChars.length); //assigning random nums to the array of letters
randomLetter[i] = newChars[r];
System.out.print(randomLetter[i]+ " ");
}
//input section for player
System.out.println("");
System.out.println("Enter your word (or '@' to pass or '!' to quit): ");
if (playerOne.turn) {
playerOne.choice = userInput.next();
System.out.println(playerOne.turn);
playerOne.turn = false;
}
else if (!playerOne.turn){
playerTwo.choice = userInput.next();
System.out.println(playerOne.turn);
playerOne.turn = true;
}
//System.out.println(choice);
String[] wordList = FileUtil.readDictFromFile("words.txt"); //Still dunno what this is for
if (playerOne.choice.equals("@")) {
playerOne.turn = false;
}
else if (playerTwo.choice.equals("@")) {
playerOne.turn = true;
}
else if (playerOne.choice.equals("!")) {
cont = false;
}
for (int i = 0; i < wordList.length; i++) {
//System.out.println(wordList[i]);
if (playerOne.choice.equalsIgnoreCase(wordList[i]) || playerTwo.choice.equalsIgnoreCase(wordList[i])){
}
}
}
}}
For Scenario 1, IMO the simplest solution would be to generate the 10 chars, count the number of vowels, and if it is less than 2, regenerate the whole set. This would make the result look more "random", in the sense that the vowels can be anywhere within the sequence, and there can be more than 2 of them.
Btw here
String[] wordList = FileUtil.readDictFromFile("words.txt"); //Still dunno what this is for
the word list is used to check that the word entered by the actual player is a valid word (i.e. it exists in a dictionary).
Once the word is validated, just iterate through its characters, find their index within the alphabet, then sum up their values. For this, you would be better off using a char[]
instead of String[]
.
Update: code example for generating and validating the characters:
final char[] alphabet = new char[] { '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' };
final Set<Character> vowels = new HashSet<Character>(Arrays.asList('a', 'e', 'i', 'o', 'u'));
char[] randomLetters = new char[10];
int vowelCount;
do {
for (int i = 0; i < randomLetters.length; i++) {
int r = rand.nextInt(alphabet.length);
randomLetters[i] = alphabet[r];
}
vowelCount = 0;
for (char actualChar: randomLetters) {
if (vowels.contains(actualChar))
vowelCount++;
}
} while (vowelCount < 2);
for (char actualChar: randomLetters) {
System.out.print(actualChar + " ");
}
精彩评论