/**
* Name: Team Orange <br />
* Course: 4010-361-03 <br />
* Assignment: Project <br />
* Date: 2/19/07 <br />
* <br>
* Class: FillInTheWOrd <br />
* Purpose: This class implements a game where a sntence is missing a word
* The user is then to select the best
* available option.
* <br>
* @author Celena
*
*/
package game;
import java.awt.event.*;
import java.io.IOException;
import java.util.*;
import mainController.ConfigurationSettings;
import dataObject.GameResult;
import dataObject.QuestionList;
import dataObject.Question;
import dataObject.Student;
public class FillInTheWord implements ActionListener{
private ArrayList<Question> sentenceList; // stores all words used in game
private ArrayList<String> wrongWords;// stores all words or sentences user gets wrong
private ArrayList<String> rightWords;//stores all words or sentences user gets right
private String currentS;//stores current word or sentence in use
private int count;//keeps track of which word or sentence the game is up to
private int right;//stores the number of right answers
private Student student1;
private GameResult gameResult;
private String guess1, guess2;//answer choices for GUI
private String viewedWord;
private Random generator;
private FillInTheWordGUI newGame;
private ArrayList<ActionListener> observers;
static final String theGame = "FillInTheWord";
static final String GAME_FINISHED = "GAME_FINISHED";
/**
* Creates an instance of a FillInTheWord game that takes a word list
*
* @param n
* @param g
* @param words
*/
public FillInTheWord( ){
right = 0;
count = 0;
viewedWord = "";
wrongWords = new ArrayList<String>();
rightWords = new ArrayList<String>();
generator = new Random();
newGame = new FillInTheWordGUI();
newGame.addActionListener(this);
observers = new ArrayList<ActionListener>();
}
/**
* Starts the game when using a sentence list
*
*/
public void play(QuestionList words, Student stu){
sentenceList = words.getWordSubset(4, 0);
student1 = stu;
newGame.gameStop();
newGame.setVisible(true);
}
/**
* removes letter or word to create question in game
* @return
*/
public String remove(){
viewedWord = sentenceList.get(count).getSentence().replace( sentenceList.get(count).getWord(),"_______");
return viewedWord;
}
/**
* Checks to if answer given by user is correct
* @param l
* @return
*/
public boolean ansCorrect(String l){
boolean check = false;
if(currentS.equals(l))
check = true;
return check;
}
/**
* displays rules of game
*
*/
public void help(){
System.out.println("To select a letter press the key that represents" +
" on the keyboard");
}
/**
* displays options
*
*/
public void options(){
}
/**
* closes game
*
*/
public void stop() {
newGame.setVisible( false );
}
/**
* generates possible answers
*
*/
public void possibleAnswers(){
guess1 = sentenceList.get(generator.nextInt(sentenceList.size())).getWord();
guess2 = sentenceList.get(generator.nextInt(sentenceList.size())).getWord();
//guess1 = "gay";
//guess2 = "pete";
while(guess1 == guess2 || currentS == guess1 || currentS == guess2){
if(guess1 == guess2){
guess2 = sentenceList.get(generator.nextInt(sentenceList.size())).getWord();
}else if(currentS == guess1){
guess1 = sentenceList.get(generator.nextInt(sentenceList.size())).getWord();
}else if(currentS == guess2){
guess2 = sentenceList.get(generator.nextInt(sentenceList.size())).getWord();
}
}
}
/**
* sets up next question and possible questions
*
*/
public void next(String choice,ActionEvent e){
if(ansCorrect(choice)){
newGame.rightOrWrong(true);
rightWords.add(choice);
right++;
}else{
newGame.rightOrWrong(false);
wrongWords.add(choice);
}
if(count != sentenceList.size()){
currentS = sentenceList.get(count).getWord();
remove();
possibleAnswers();
newGame.setAnswers(guess1, guess2, currentS, viewedWord);
count++;
}else{
gameResult = new GameResult(rightWords.toArray(new String[rightWords.size()]),
wrongWords.toArray(new String[wrongWords.size()]),
theGame);
student1.addGameResult(gameResult);
newGame.endGame();
stop();
ActionEvent ae = new ActionEvent( e.getSource(), e.getID(), ConfigurationSettings.SHOW_MAIN_MENU );
ActionEvent gf = new ActionEvent( e.getSource(), e.getID(), ConfigurationSettings.GAME_FINISHED );
informObservers(ae);
informObservers(gf);
}
}
/**
* handles any responses needed for buttons in GUI
*
*/
public void actionPerformed(ActionEvent e){
if( e.getActionCommand().equals( FillInTheLetterGUI.START_GAME_COMMAND) ) {
newGame.gameStart();
currentS = sentenceList.get(count).getWord();
remove();
possibleAnswers();
newGame.setAnswers(guess1,guess2, currentS, viewedWord);
count++;
}else if( e.getActionCommand().equals( FillInTheLetterGUI.QUIT_GAME_COMMAND) ) {
stop();
ActionEvent ae = new ActionEvent( e.getSource(), e.getID(), ConfigurationSettings.SHOW_MAIN_MENU );
informObservers(ae);
}else if( e.getActionCommand().equals( FillInTheLetterGUI.ANSWER1_QUESTION_COMMAND) ) {
next(newGame.answerChoice1(), e);
}else if( e.getActionCommand().equals( FillInTheLetterGUI.ANSWER2_QUESTION_COMMAND) ) {
next(newGame.answerChoice2(), e);
}else if( e.getActionCommand().equals( FillInTheLetterGUI.ANSWER3_QUESTION_COMMAND) ) {
next(newGame.answerChoice3(),e);
}
}
/**
* adds an ActionListener
*
*/
public void addActionListener(ActionListener al){
observers.add(al);
}
/**
* adds an ActionListener
*
*/
public void removeActionListener(ActionListener al){
observers.remove(al);
}
/**
* Gets the final printout for the game.
*
* @return String of the no of correct guesses, total words, and words to study.
*/
public String printEndStatus(){
String wordsToStudy = "";
for(int x = 0; x < wrongWords.size(); x++){
//if statement is only for printout syntax
if(x == 0){
wordsToStudy = " " + wordsToStudy + wrongWords.get(x);
}
else{
wordsToStudy = wordsToStudy + ", " + wrongWords.get(x);
}
}
return "SESSION COMPLETE!" + "\n" +
"Number of Correct Guesses: " + right+ "\n" +
"Total Number of Words: " + (count - 1) + "\n" + "\n" +
"Study these words: " + wordsToStudy;
}
private void informObservers(ActionEvent e){
for(int i = 0; i < observers.size(); i++){
observers.get(i).actionPerformed(e);
}
}
/**
* Tests FillinBlank class for functionality
* @param args
*/
public static void main( String [] args ){
QuestionList test;
try{
test = QuestionList.getInstance("readtext");
Student tim = new Student();
test.addQuestion(new Question("run", 3, "See spot run"));
test.addQuestion(new Question("Church", 3, "Church is on Sunday"));
test.addQuestion(new Question("hurts", 3, "My back hurts very bad"));
test.addQuestion(new Question("Class", 3, "Class is on Monday through Sunday"));
FillInTheWord game2 = new FillInTheWord();
game2.play(test,tim);
}catch(ClassNotFoundException e){
}catch(IOException e){
}
System.out.println();
System.out.println("GameOver");
}
public void setQuestions(ArrayList Questions) {
// TODO Auto-generated method stub
}
}