/*********************************************************** * Typing Tutor-For the Visually Impaired * Comp145 Project * Profesor/Coach: Profesor Kye Hedlund * Client: Gary Bishop * Description: A game intended to teach blind indivduals * to type. The game will drop a letter or word * depending on the level selected. To get a point * added to your score you must hit the correct keys * on the keyboard. There will be a set of beeps to * let you know that you are running out of time. * You will hear "Correct" if the proper key(s) are * hit, or try again if they were not. When you * hit the stop key it will tell you your score, and * if you have a new high score. * Created by: Quentin Dubois, Jack Enloe, Ryan Hillman, * Mirza Nagji, Tony Penta, Ian Quattlebaum, and * Jessica Whitley * Authorship: Most code was written individually, and the * authors of the sections are indicated. As a team * on two different occasions we had meetings to debug * and brainstorm. The first meeting consisted of * Ian Quattlebaum, Jessica Whitley, and Tony Penta * working with the code. The second meeting was * held with Quentin Dubois, Ryan Hillman, Mirza * Nagji, Tony Penta, Ian Quattlebaum, Jack Enloe, * ane Jessica Whitley. * Date: April 25, 2003 **********************************************************/ using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using SpeechLib; using System.IO; using Microsoft.DirectX; using Microsoft.DirectX.DirectSound; using Buffer = Microsoft.DirectX.DirectSound.SecondaryBuffer; namespace TypingTutor { public class Game : System.Windows.Forms.Form { //private variables private const float TextSpeedX = 10; //textSpeed variable for x-axis private float TextSpeedY = 1; //textSpeed variable for y-axis private SpVoice voice; //SpVoice object private SpeechVoiceSpeakFlags spFlags; //SpeechVoiceSpeakFlags object private float textX, textY; private bool addX, addY; private String text=""; //used to hold the word or letter that will be dropped private bool game= false; //variable used to determine whether the game is active //arrays to hold the letters or words from the //input files that will be dropped depending //on the level private String [] level7; private String [] level1; private String [] level2; private String [] level3; private String [] level4; private String [] level5; private String [] level6; //variable to hold the randomly selected element of the array private int countLine=0; private String ex=""; //variable to hold what the user has typed private int score=0; //variable to hold the score private int line=3; //variable to hold max number of lines in the highscore file private int a=0; private int max=0; //variable to hold the max score private SecondaryBuffer ApplicationBuffer = null; private Device ApplicationDevice = null; private string PathSoundFile = string.Empty; /*************************************************** * declaring the components that are part of the GUI * *************************************************/ //comboBox to pick the voice private System.Windows.Forms.ComboBox voiceCombo; //start button private System.Windows.Forms.Button speakButton; //panel to hold all the components private System.Windows.Forms.Panel panel1; //checkbox 1 for a level private System.Windows.Forms.CheckBox checkBox1; //label for a component private System.Windows.Forms.Label label2; //textbox to see what has been typed private System.Windows.Forms.TextBox textBox1; //timer to know when the user is running out of time //to type the letter or character private System.Windows.Forms.Timer timer1; //stop button private System.Windows.Forms.Button stop; //checkboxes for the rest of the levels private System.Windows.Forms.CheckBox checkBox2; private System.Windows.Forms.CheckBox checkBox3; private System.Windows.Forms.CheckBox checkBox4; private System.Windows.Forms.CheckBox checkBox5; private System.Windows.Forms.CheckBox checkBox6; //labels for the checkboxes private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label4; private System.Windows.Forms.Label label5; private System.Windows.Forms.Label label6; private System.Windows.Forms.Label label7; private System.Windows.Forms.CheckBox checkBox7; private System.Windows.Forms.Label label8; //instruction button private System.Windows.Forms.Button button1; //label for comboBox private System.Windows.Forms.Label SelectVoice; //highscores button private System.Windows.Forms.Button button2; private System.Windows.Forms.TextBox scoreBox; private System.Windows.Forms.Label scoreLabel; private System.ComponentModel.IContainer components; /********************************************************************************************* * Method: Game() * Description: Main Function that is called to load the game. It loads and stores all * files that are used in the program. The method also gets the speech objects ready * for the rest of the program, and it calls a method to bring up all components on * the GUI * Author: Quentin Dubois and Jessica Whitley ********************************************************************************************/ public Game() { //call to the method that initializing the GUI InitializeComponent(); /****************************************************************************************** * Create a FileStream for all seven files corresponding to the levels at the beginning * of the program this allows the user the option to switch to a different level at any point * Each file contains on the first line a number indicating how many lines the file contains * proceded with the letters or words for that level * Author: Jessica Whitley *******************************************************************************************/ FileStream file7 = new FileStream("medium.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite); FileStream file1 = new FileStream("level1.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite); FileStream file2 = new FileStream("level2.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite); FileStream file3 = new FileStream("level3.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite); FileStream file4 = new FileStream("level4.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite); FileStream file5 = new FileStream("level5.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite); FileStream file6 = new FileStream("level6.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite); //Create Stream Readers for all seven files //Author: Jessica Whitley StreamReader sr = new StreamReader(file7); StreamReader sr1=new StreamReader(file1); StreamReader sr2=new StreamReader(file2); StreamReader sr3=new StreamReader(file3); StreamReader sr4=new StreamReader(file4); StreamReader sr5=new StreamReader(file5); StreamReader sr6=new StreamReader(file6); //Create the FileStream and the StreamReader for the highscores file //Author: Jessica Whitley FileStream highscore = new FileStream("highscores.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite); StreamReader streamR2 = new StreamReader(highscore); //Read through the highscores file with a loop String value2=""; for(int i=0; i=max) max=a; } } //Close the FileStream and the StreamReader for the highscores file streamR2.Close(); highscore.Close(); //Written by: Jessica Whitley //read in the number of lines for the file int line7=Convert.ToInt16(sr.ReadLine()); //instantiate the array to that number level7=new String[line7]; //create a for loop to loop through the number //of lines in the file for( int i=0; i180 && textY<270) { //play the noise //author: Quentin Dubois if(null != ApplicationBuffer) ApplicationBuffer.Play(0, (BufferPlayFlags.Default)); } } else { /*********************************** * Check the checkboxes to know which * array to update the new word or * letter to be dropped * Author: Jessica Whitley **********************************/ if(checkBox1.Checked==true) { countLine=randomNumber(level1.Length); text=level1[countLine]; countLine=randomNumber(level1.Length); } if(checkBox7.Checked==true) { countLine=randomNumber(level7.Length); text=level7[countLine]; countLine=randomNumber(level7.Length); } if(checkBox3.Checked==true) { countLine=randomNumber(level3.Length); text=level3[countLine]; countLine=randomNumber(level3.Length); } if(checkBox4.Checked==true) { countLine=randomNumber(level2.Length); text=level2[countLine]; countLine=randomNumber(level2.Length); } if(checkBox6.Checked==true) { countLine=randomNumber(level6.Length); text=level6[countLine]; countLine=randomNumber(level6.Length); } if(checkBox2.Checked==true) { countLine=randomNumber(level5.Length); text=level5[countLine]; countLine=randomNumber(level5.Length); } if(checkBox5.Checked==true) { countLine=randomNumber(level4.Length); text=level4[countLine]; countLine=randomNumber(level4.Length); } //updates variables and says the word to be typed textY = 1; addY = true; voice.Speak(text, spFlags); } //author: Quentin Dubois //checks whehter to keep the string moving down the screen or start over if (e.Graphics.MeasureString(textY.ToString(), this.Font, panel1.Width / 2).Height + textY > panel1.Height) { addY = false; } else if (textY < 0) { addY = true; } } //draws the start message up if the game is not active //author: Quentin Dubois else { e.Graphics.DrawString("Press Start to begin", panel1.Font, Brushes.Blue, new RectangleF(new PointF(0, 10), new SizeF(panel1.Width , panel1.Height))); } } #endregion #region Form event /********************************************************************************************* * Method: speechBox_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e) * Description: The method is called when a key has been pressed on the keyboard * Returns: nothing it is void * Author: Ryan Hillman, Quentin Dubois, and Jessica Whitley ********************************************************************************************/ private void speechBox_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e) { //Author: Quentin Dubois //checks to see if shift has been pressed if(e.Shift) { //keeps up with what the user types //and displays it in the textBox ex=ex + (e.KeyCode.ToString()); textBox1.Text=ex; } //otherwise the letter typed is lowercase else { //keeps up with what the user types //and displays it in the textBox ex=ex+(e.KeyCode.ToString().ToLower()); textBox1.Text=ex; } //checks to see if the user entered in the //correct word or letter if (ex.Equals(text)) { //author: Ryan Hillman //plays a correct noise for the user this.LoadSoundFile("correct.wav"); if(null != ApplicationBuffer) ApplicationBuffer.Play(0, (BufferPlayFlags.Default)); this.LoadSoundFile("s1.wav"); //increases the score if they are right score++; this.scoreBox.Text = score.ToString(); //author: Jessica Whitley //check all of the checkboxes to see //which one has been selected to chose //an element of that checkboxes array if(checkBox1.Checked==true) { countLine=randomNumber(level1.Length-1); text=level1[countLine]; countLine=randomNumber(level1.Length-1); } if(checkBox7.Checked==true) { countLine=randomNumber(level7.Length); text=level7[countLine]; countLine=randomNumber(level7.Length); } if(checkBox3.Checked==true) { countLine=randomNumber(level3.Length); text=level3[countLine]; countLine=randomNumber(level3.Length); } if(checkBox4.Checked==true) { countLine=randomNumber(level2.Length); text=level2[countLine]; countLine=randomNumber(level2.Length); } if(checkBox6.Checked==true) { countLine=randomNumber(level6.Length); text=level6[countLine]; countLine=randomNumber(level6.Length); } if(checkBox2.Checked==true) { countLine=randomNumber(level5.Length); text=level5[countLine]; countLine=randomNumber(level5.Length); } if(checkBox5.Checked==true) { countLine=randomNumber(level4.Length); text=level4[countLine]; countLine=randomNumber(level4.Length); } //the next word is said to the user if(text==null) text = "d"; //Next three lines to pause here for 1-2 seconds to wait for correct.wav to play voice.Speak(" ",spFlags); voice.Speak(" ",spFlags); voice.Speak(" ",spFlags); voice.Speak(text, spFlags); textY =1; ex=""; } //if the letters the user has typed in do not match the //beginning of the word then a try again message is given //author: Ryan Hillman if(text.StartsWith(ex)==false) { //set the variable to keep up with what the user typed back //to empty ex=""; //play the message this.LoadSoundFile("tryagain.wav"); if(null != ApplicationBuffer) ApplicationBuffer.Play(0, (BufferPlayFlags.Default)); this.LoadSoundFile("s1.wav"); } } /********************************************************************************************* * Method: speakButton_Click(object sender, System.EventArgs e) * Description: This method is used to manage the event that happens when the start button * is clicked. * Returns: nothing it is void * Author: Jessica Whitley ********************************************************************************************/ private void speakButton_Click(object sender, System.EventArgs e) { //Stop playing sounds if(null != ApplicationBuffer) ApplicationBuffer.Stop(); voice = null; //gets the voice ready to speak this.LoadSoundFile("s1.wav"); voice = new SpVoiceClass(); voice.Voice = voice.GetVoices(null, null).Item(voiceCombo.SelectedIndex); voice.Rate = 1; //Checks the checkboxes to determine which array //to get the word or letter from to be dropped //for the user if(checkBox1.Checked==true) { countLine=randomNumber(level1.Length); text=level1[countLine]; countLine=randomNumber(level1.Length); } if(checkBox2.Checked==true) { countLine=randomNumber(level5.Length); text=level5[countLine]; countLine=randomNumber(level5.Length); } if(checkBox7.Checked==true) { countLine=randomNumber(level7.Length); text=level7[countLine]; countLine=randomNumber(level7.Length); } if(checkBox3.Checked==true) { countLine=randomNumber(level3.Length); text=level3[countLine]; countLine=randomNumber(level3.Length); } if(checkBox4.Checked==true) { countLine=randomNumber(level2.Length); text=level2[countLine]; countLine=randomNumber(level2.Length); } if(checkBox6.Checked==true) { countLine=randomNumber(level6.Length); text=level6[countLine]; countLine=randomNumber(level6.Length); } if(checkBox2.Checked==true) { countLine=randomNumber(level5.Length); text=level5[countLine]; countLine=randomNumber(level5.Length); } if(checkBox5.Checked==true) { countLine=randomNumber(level4.Length); text=level4[countLine]; countLine=randomNumber(level4.Length); } //speak the chooosen word and //set the variables accordingly to //start the game voice.Speak(text, spFlags); game=true; score=0; this.scoreBox.Text = "0"; ex=""; } /********************************************************************************************* * Method: stop_Click(object sender, System.EventArgs e) * Description: A method that manages the event that happens when the stop button * is clicked * Returns: nothing it is void * Author: Jessica Whitley ********************************************************************************************/ private void stop_Click(object sender, System.EventArgs e) { //Stop playing sounds if(null != ApplicationBuffer) ApplicationBuffer.Stop(); if (game)// cannot stop if the game did not stop { //sets the game to inactive game= false; //checks to see which checkbox is checked //to use the appropriate array to load //a new value into the word that will be //dropped for the user when the game //resumes if(checkBox1.Checked==true) { countLine=randomNumber(level1.Length); text=level1[countLine]; countLine=randomNumber(level1.Length); } if(checkBox7.Checked==true) { countLine=randomNumber(level7.Length); text=level7[countLine]; countLine=randomNumber(level7.Length); } if(checkBox3.Checked==true) { countLine=randomNumber(level3.Length); text=level3[countLine]; countLine=randomNumber(level3.Length); } if(checkBox4.Checked==true) { countLine=randomNumber(level2.Length); text=level2[countLine]; countLine=randomNumber(level2.Length); } if(checkBox6.Checked==true) { countLine=randomNumber(level6.Length); text=level6[countLine]; countLine=randomNumber(level6.Length); } if(checkBox2.Checked==true) { countLine=randomNumber(level5.Length); text=level5[countLine]; countLine=randomNumber(level5.Length); } if(checkBox5.Checked==true) { countLine=randomNumber(level4.Length); text=level4[countLine]; countLine=randomNumber(level4.Length); } textY=1; ex=""; //speak the score voice = new SpVoiceClass(); voice.Voice = voice.GetVoices(null, null).Item(voiceCombo.SelectedIndex); voice.Rate = 1; voice.Speak("Your score is"+ score.ToString(), spFlags); //Create the FileStream and StreamWriter for highscores FileStream highscore = new FileStream("highscores.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite); StreamWriter streamW = new StreamWriter(highscore); //Compare the score with the with the max if it is greater //replace the max with the new score and set the highscore //to the newly attained score if(score>max) { max=score; streamW.WriteLine(score.ToString()); voice.Speak("you have a new high score", spFlags); } //Close the StreamWriter and the FileStream streamW.Close(); highscore.Close(); } } /********************************************************************************************* * Method: Form1_Load(object sender, System.EventArgs e) * Description: Loads the form * Returns: nothing it is void * Author: Quentin Dubois ********************************************************************************************/ private void Form1_Load(object sender, System.EventArgs e) { ApplicationDevice = new Device(); ApplicationDevice.SetCooperativeLevel(this, CooperativeLevel.Priority); } /********************************************************************************************* * Method: randomNumber(int v) * Description: Returns a random number between zero and v * Returns: an integer * Author: Jessica Whitley ********************************************************************************************/ private int randomNumber(int v) { //create a random number generator Random r = new Random(); //generate a value between 0 and v //and return the value int i=Convert.ToInt16(r.NextDouble()*(v-1)); return i; } /********************************************************************************************* * Method: timer1_Tick_1(object sender, System.EventArgs e) * Description: handles the event for when the timer goes off * Returns: nothing it is void * Author: Quentin Dubois ********************************************************************************************/ private void timer1_Tick_1(object sender, System.EventArgs e) { this.panel1.Invalidate(); } #endregion /********************************************************************************************* * Method: randomChar() * Description: returns a random character on the keyboard * Returns: a String * Author: Quentin Dubois ********************************************************************************************/ #region private methods private String randomChar() { //create a random number generator Random r = new Random(); double d = r.NextDouble()*122; while(true) { //give the KeyCode bounds for the characters that //should be returned if ((d >= 65 && d<133) || (d >= 97 && d<123)) break; d = r.NextDouble()*122; } //put the character in proper form //and return it int i = Convert.ToInt16(d); char c = Convert.ToChar(i); return c.ToString(); } /********************************************************************************************* * Method: LoadSoundFile(string name) * Description: The method loads a sound file watching for exception. * Returns: a boolean value-true if the sound file was loaded and false * if it was not * Author:Quentin Dubois ********************************************************************************************/ private bool LoadSoundFile(string name) { //try to load the file try { ApplicationBuffer = new SecondaryBuffer(name, ApplicationDevice); } catch(SoundException) { //return false if unsuccessful return false; } //return true if successful return true; } #endregion /********************************************************************************************* * Method: checkBox1_CheckedChanged(object sender, System.EventArgs e) * Description: This method handles the event when the level for the checkbox is clicked. * Returns: nothing it is void * Author: Jessica WHitley ********************************************************************************************/ private void checkBox1_CheckedChanged(object sender, System.EventArgs e) { //makes sure that if checkbox 1 is clicked no //other checkboxes are clicked if(checkBox1.Checked==true) { checkBox2.Checked=false; checkBox3.Checked=false; checkBox4.Checked=false; checkBox5.Checked=false; checkBox6.Checked=false; checkBox7.Checked=false; } } /********************************************************************************************* * Method: checkBox4_CheckedChanged(object sender, System.EventArgs e) * Description: This method handles the event when the level for checkbox 4 is clicked. * Returns:nothing it is void * Author: Jessica Whitley ********************************************************************************************/ private void checkBox4_CheckedChanged(object sender, System.EventArgs e) { //makes sure that if checkbox 4 is clicked no //other checkboxes are clicked if(checkBox4.Checked==true) { checkBox2.Checked=false; checkBox3.Checked=false; checkBox1.Checked=false; checkBox5.Checked=false; checkBox6.Checked=false; checkBox7.Checked=false; } } /********************************************************************************************* * Method: checkBox3_CheckedChanged(object sender, System.EventArgs e) * Description: This method handles the event when the level for checkbox 3 is clicked * Returns: nothing it is void * Author: Jessica Whitley ********************************************************************************************/ private void checkBox3_CheckedChanged(object sender, System.EventArgs e) { //makes sure that if checkbox 3 is clicked no //other checkboxes are clicked if(checkBox3.Checked==true) { checkBox2.Checked=false; checkBox4.Checked=false; checkBox1.Checked=false; checkBox5.Checked=false; checkBox6.Checked=false; checkBox7.Checked=false; } } /********************************************************************************************* * Method: checkBox5_CheckedChanged(oject sender, System.EventArgs e) * Description: This method handles the event when the level for checkbox 5 is clicked. * Returns: nothing it is void * Author: Jessica Whitley ********************************************************************************************/ private void checkBox5_CheckedChanged(object sender, System.EventArgs e) { //makes sure that if checkbox 5 is clicked no //other checkboxes are clicked if(checkBox5.Checked==true) { checkBox2.Checked=false; checkBox4.Checked=false; checkBox1.Checked=false; checkBox3.Checked=false; checkBox6.Checked=false; checkBox7.Checked=false; } } /********************************************************************************************* * Method: checkBox2_CheckedChanged(object sender, System.EventArgs e) * Description: This method handles the event when the level for checkbox 2 is clicked. * Returns: nothing it is void * Author: Jessica Whitley ********************************************************************************************/ private void checkBox2_CheckedChanged(object sender, System.EventArgs e) { //makes sure that if checkbox 2 is clicked no //other checkboxes are clicked if(checkBox2.Checked==true) { checkBox3.Checked=false; checkBox4.Checked=false; checkBox1.Checked=false; checkBox5.Checked=false; checkBox6.Checked=false; checkBox7.Checked=false; } } /********************************************************************************************* * Method: checkBox6_CheckedChanged(object sender, System.EventArgs e) * Description: This method handles the event when the level for checkbox 6 is clicked * Returns: nothing it is void * Author: Jessica Whitley ********************************************************************************************/ private void checkBox6_CheckedChanged(object sender, System.EventArgs e) { //makes sure that if checkbox 6 is clicked no //other checkboxes are clicked if(checkBox6.Checked==true) { checkBox2.Checked=false; checkBox4.Checked=false; checkBox1.Checked=false; checkBox5.Checked=false; checkBox3.Checked=false; checkBox7.Checked=false; } } /********************************************************************************************* * Method: checkBox7_CheckedChanged(object sender, System.EventArgs e) * Description: This method handles the event when the level for checkbox 7 is clicked. * Returns: nothing it is void * Author: Jessica Whitley ********************************************************************************************/ private void checkBox7_CheckedChanged(object sender, System.EventArgs e) { //makes sure that if checkbox 7 is clicked no //other levels are clicked if(checkBox7.Checked==true) { checkBox2.Checked=false; checkBox4.Checked=false; checkBox3.Checked=false; checkBox5.Checked=false; checkBox6.Checked=false; checkBox1.Checked=false; } } /********************************************************************************************* * Method: button1_Click(object sender, System.EventArgs e) * Description: This method handles the event when the instruction button is pressed. It * reads the instructions from a wav file * Returns: nothing it is void * Author: Ryan Hillman ********************************************************************************************/ private void button1_Click(object sender, System.EventArgs e) { if (!game)// cannot ask for the instruction during the Game { //loads the instruction wav file this.LoadSoundFile("instructions.wav"); //play it if(null != ApplicationBuffer) { ApplicationBuffer.Play(0, (BufferPlayFlags.Default)); } } } /********************************************************************************************* * Method: button2_Click(Object sender, System.EventArgs e) * Description: This method handles the event when the highscores button is pressed. It * announces the highest score. * Returns: nothing it is void * Author: Jessica Whitley ********************************************************************************************/ private void button2_Click(object sender, System.EventArgs e) { if (!game)// cannot ask for the hight score during the Game { //Stop playing sounds if(null != ApplicationBuffer) ApplicationBuffer.Stop(); //opens the FileStream and Stream Reader for the highscores file FileStream highscore = new FileStream("highscores.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite); StreamReader streamR1 = new StreamReader(highscore); //gets the voice components ready for use voice = new SpVoiceClass(); voice.Voice = voice.GetVoices(null, null).Item(voiceCombo.SelectedIndex); voice.Rate = 1; //go through file and extract the highest score int line=3; int newline=0; String value2=""; for(int i=0; i