Skip to content

Instantly share code, notes, and snippets.

@alisha
Created February 18, 2013 00:43
Show Gist options
  • Save alisha/4974413 to your computer and use it in GitHub Desktop.
Save alisha/4974413 to your computer and use it in GitHub Desktop.
A simple game where two players try to create words from the phrase "Computer Science".
//By Alisha Ukani
//github.com/alishau
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
public class WordGame {
public static void main(String[] args) {
//Dictionary source: github.com/atebits/Words
Scanner file;
ArrayList<String> dictionary = new ArrayList<String>();
try {
//Read the text file
file = new Scanner(new File("EnglishDictionary.txt"));
while (file.hasNext()) {
String line = file.nextLine();
dictionary.add(line);
}
} catch (Exception e) {
//Handle the exception
e.printStackTrace();
}
//Initialize score counts and ArrayLists
int scoreA = 0;
int scoreB = 0;
ArrayList<String> aWords = new ArrayList<String>();
ArrayList<String> bWords = new ArrayList<String>();
ArrayList<String> commonWords = new ArrayList<String>();
boolean hasTime = true;
//Introduction
System.out.print("This is a game for two players. When it is your turn, you must find as many words as you can from the phrase \"Computer Science\". Enter \"0\" to end your turn. ");
System.out.println("You will receive points for each distinct word with more than two letters, scaled based on how long it is. Good luck!");
//Player 1's turn
Scanner kboard = new Scanner(System.in);
System.out.println("\nPlayer 1, it is your turn.");
String word = kboard.nextLine().toLowerCase();
while (!(word.equals("0"))) {
if (WordGame.isValidWord(word, dictionary)) {
aWords.add(word);
}
word = kboard.nextLine().toLowerCase(); //Ask for new word
}
hasTime = true;
//Player 2's turn
System.out.println("\nPlayer 2, it is your turn.");
word = kboard.nextLine().toLowerCase();
while (!(word.equals("0"))) {
if (WordGame.isValidWord(word, dictionary)) {
bWords.add(word);
}
word = kboard.nextLine().toLowerCase(); //Ask for new word
}
//Remove duplicates
WordGame.removeDuplicates(aWords);
WordGame.removeDuplicates(bWords);
//Create and print array list of joint words
for (int c = 0; c < aWords.size(); c++) {
for (int d = 0; d < bWords.size(); d++) {
if (aWords.get(c).equals(bWords.get(d))) {
commonWords.add(aWords.get(c));
}
}
}
System.out.println("\nNumber of words in common: " + commonWords.size() + "\nWords in common: " + commonWords);
//Calculate scores and print word lists
scoreA = WordGame.calculateScore(aWords, commonWords);
System.out.println("\nPlayer 1's words: " + aWords);
System.out.println("Player 1's score: " + scoreA);
scoreB = WordGame.calculateScore(bWords, commonWords);
System.out.println("\nPlayer 2's words: " + bWords);
System.out.println("Player 2's score: " + scoreB + "\n");
//Print Results
if (scoreA > scoreB) {
System.out.println("Player 1 won! Congratulations!");
} else if (scoreB > scoreA) {
System.out.println("Player 2 won! Congratulations!");
} else {
System.out.println("It's a tie! Both players won!");
}
}
//Removes the duplicates from a player's list of words
public static void removeDuplicates(ArrayList<String> words) {
for (int x = 0; x < words.size() - 1; x++) {
for (int y = x+1; y < words.size(); y++) {
if (words.get(x).equals(words.get(y))) {
words.remove(y);
y--;
}
}
}
}
//Calculates the player's score based on their word list and the words in common with the other player
public static int calculateScore(ArrayList<String> words, ArrayList<String> commonWords) {
int score = 0;
for (int g = 0; g < words.size(); g++) {
score += words.get(g).length();
}
for (int d = 0; d < commonWords.size(); d++) {
score -= commonWords.get(d).length();
}
if (score < 0) {
score = 0;
}
return score;
}
//Tests whether a word is valid
//Returns true if the word can be made from the given phrase, the word has more than two letters, and if it is an English word
public static boolean isValidWord(String word, ArrayList<String> dictionary) {
boolean valid = true;
int cMax = 3; //Number of a certain letter in the given phrase
int oMax = 1;
int mMax = 1;
int pMax = 1;
int uMax = 1;
int tMax = 1;
int eMax = 3;
int rMax = 1;
int sMax = 1;
int iMax = 1;
int nMax = 1;
int cCount = 0; //Will be used to count number of a certain letter in a player's input
int oCount = 0;
int mCount = 0;
int pCount = 0;
int uCount = 0;
int tCount = 0;
int eCount = 0;
int rCount = 0;
int sCount = 0;
int iCount = 0;
int nCount = 0;
for (int g = 0; g < word.length(); g++) { //Count the letters in the given word compared to the given phrase
String currentLetter = word.substring(g, g+1);
if (currentLetter.equals("c")) {
cCount++;
} else if (currentLetter.equals("o")) {
oCount++;
} else if (currentLetter.equals("m")) {
mCount++;
} else if (currentLetter.equals("p")) {
pCount++;
} else if (currentLetter.equals("u")) {
uCount++;
} else if (currentLetter.equals("t")) {
tCount++;
} else if (currentLetter.equals("e")) {
eCount++;
} else if (currentLetter.equals("r")) {
rCount++;
} else if (currentLetter.equals("s")) {
sCount++;
} else if (currentLetter.equals("i")) {
iCount++;
} else if (currentLetter.equals("n")) {
eCount++;
} else {
valid = false;
}
}
//Test for the word requirements
if (!(cCount <= cMax && oCount <= oMax && mCount <= mMax && pCount <= pMax && uCount <= uMax && tCount <= tMax && eCount <= eMax && rCount <= rMax && sCount <= sMax && iCount <= iMax && nCount <= nMax && valid != false && word.length() > 2 && dictionary.contains(word))) {
valid = false;
}
return valid;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment