I have been working on this memory game for a while now in my spare time and it is working until I have to run the new game method. I think my problem might be that I am declaring the button array within the new game method and that is throwing of my index numbers after the first game. Its kind of a lot of code to go through but if anyone has some spare time id appreciate the help. Also Is there a way to keep the pic on the button in color while it is not enabled?Heres the code...sorry its still a bit sloppy but it compiles.(I did it in VS 2003).
I took out the region of declaration of the objects cause it pretty self explanatory and kind of long...I probably should declare the buttons with the button array but I already had added them to the form. Looking back it would be nice to make it into a variable # of buttons grid in order to make the game harder or easier. I also didn't add any code to add high scores to the high score and user txt files cause i was still testing it and didn't really need it to work.Code:using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; using System.Threading; [STAThread] static void Main() { Application.Run(new frmMain()); } //declare class level variables bool bln_isFirst = true, bln_isMatch = false, bln_isSame = false; //create array to hold random numbers to randomize pics int[] intBoard = new int[43]; //create array to hold random numbers to fill board array int[] intStorage = new int[250]; //create arrays from files to hold high scores. int[] intHighScores = new int[3]; string[] strUser = new string[3]; //create int and file name variables to keep track of buttons, pictures, guesses, and score private int intCount1, intCount2, intCount3, intGuess, intScore; private string strFileName; private string ImgLocation; //declare random number object int intCount4, intCount5; Random generateRandom; private void ClickHandler(object sender, System.EventArgs e) { //button handler...temporarily set button to false visibility and not enabled Button tempButton = (Button)sender; intCount4 = tempButton.TabIndex-42; //store last int Count2 for checking match and button use. intCount3 = intCount2; //set random number for passing to find file name and //add one to the index se we know how many buttons have been used intCount2 = intBoard[intCount4]; ImgLocation = checkNumbers(intCount2); tempButton.Image = Image.FromFile(ImgLocation); tempButton.Enabled = false; buttonEvents(tempButton, intCount2, intCount3, intCount4); for(int i = 0; i < 10; i++) { System.Threading.Thread.Sleep(100); } } private void buttonEvents(Button tempButton, int intCount2, int intCount3, int intCount4) { if(bln_isFirst == true) { bln_isFirst = false; intCount5 = intCount4; } else { bln_isFirst = true; bln_isMatch = checkMatch(intCount2, intCount3); // if not a match refresh buttons if(bln_isMatch == false) { tempButton.Enabled = true; tempButton.Image = null; _buttonArray[intCount5].Enabled = true; _buttonArray[intCount5].Image = null; intGuess -= 1; } else { intScore += 10; } } if(intGuess <= 0) { MessageBox.Show("game over"); for(int r = 0; r < 42; r++) { _buttonArray[r].Enabled = false; } } else { //display the score and guesses left lblGuess.Text = "Trys Left: "+intGuess.ToString(); lblScore.Text = "Score: "+intScore.ToString(); } } private void frmMain_Load(object sender, System.EventArgs e) { newGame(); } private void newGame() { intScore = 0; bln_isFirst = true; bln_isMatch = false; bln_isSame = false; //set high scores arrays from files and display on label try { StreamReader highScoresStreamReader = new StreamReader("C:\\Documents and Settings\\Owner.Gir\\My Documents\\Visual Studio Projects\\Memory\\HighScores.txt"); while(highScoresStreamReader.Peek() !=-1) { for(int a=0; a < 3; a++) intHighScores[a] = int.Parse(highScoresStreamReader.ReadLine()); } highScoresStreamReader.Close(); } catch { MessageBox.Show("File Reading Error1"); } //set user array from files and display on label try { StreamReader userStreamReader = new StreamReader("C:\\Documents and Settings\\Owner.Gir\\My Documents\\Visual Studio Projects\\Memory\\User.txt"); while(userStreamReader.Peek() !=-1) { for(int a=0; a < 3; a++) strUser[a] = userStreamReader.ReadLine(); } userStreamReader.Close(); } catch { MessageBox.Show("File Reading Error1"); } lblHighScore.Text = "HighScores: "+ strUser[0]+"..."+intHighScores[0]; lblHighScore2.Text = strUser[1]+"..."+intHighScores[1]; lblHighScore3.Text = strUser[2]+"..."+intHighScores[2]; //set number of guesses intGuess = 13; //seed the random number function DateTime dtmCurrent = DateTime.Now; generateRandom = new Random(dtmCurrent.Millisecond); _buttonArray = new Button[42] { btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9,btn10, btn11, btn12, btn13, btn14, btn15, btn16, btn17, btn18, btn19, btn20, btn21, btn22, btn23, btn24, btn25, btn26, btn27, btn28, btn29,btn30, btn31, btn32, btn33, btn34, btn35, btn36, btn37, btn38, btn39, btn40, btn41}; for(int i = 0; i < 42; i++) { this._buttonArray[i].Click += new System.EventHandler(this.ClickHandler); _buttonArray[i].Enabled = true; _buttonArray[i].Image = null; } //fill array with random #'s from 1 to 42 int intAdd = 0; for(int r=0; r < 200; r++) { intStorage[r] = generateRandom.Next(1,43); } for(intCount1=0; intCount1 < 250; intCount1++) { bln_isSame = false; if(intAdd < 43) { //check to see if array contains generated # foreach(int intBoard1 in intBoard) { if(intStorage[intCount1] == intBoard1) { bln_isSame = true; } } //fill board array with random non matching #'s if(bln_isSame == false && intStorage[intCount1] != 0) { intBoard[intAdd] = intStorage[intCount1]; intAdd++; } } } } private string checkNumbers(int intCount2) { //get file name for button image if(intCount2 == 1 || intCount2 == 2 || intCount2 == 3 || intCount2 == 4 || intCount2 == 5 || intCount2 == 6) { strFileName = "C:\\Documents and Settings\\Owner.Gir\\My Documents\\Visual Studio Projects\\Memory\\pic1.bmp"; } else if(intCount2 == 7 || intCount2 == 8 || intCount2 == 9 || intCount2 == 10 || intCount2 == 11 || intCount2 == 12) { strFileName = "C:\\Documents and Settings\\Owner.Gir\\My Documents\\Visual Studio Projects\\Memory\\pic2.bmp"; } else if(intCount2 == 13 || intCount2 == 14 || intCount2 == 15 || intCount2 == 16 || intCount2 == 17 || intCount2 == 18) { strFileName = "C:\\Documents and Settings\\Owner.Gir\\My Documents\\Visual Studio Projects\\Memory\\pic3.bmp"; } else if(intCount2 == 19 || intCount2 == 20 || intCount2 == 21 || intCount2 == 22 || intCount2 == 23 || intCount2 == 24) { strFileName = "C:\\Documents and Settings\\Owner.Gir\\My Documents\\Visual Studio Projects\\Memory\\pic4.bmp"; } else if(intCount2 == 25 || intCount2 == 26 || intCount2 == 27 || intCount2 == 28 || intCount2 == 29 || intCount2 == 30) { strFileName = "C:\\Documents and Settings\\Owner.Gir\\My Documents\\Visual Studio Projects\\Memory\\pic5.bmp"; } else if(intCount2 == 31 || intCount2 == 32 || intCount2 == 33 || intCount2 == 34 || intCount2 == 35 || intCount2 == 36) { strFileName = "C:\\Documents and Settings\\Owner.Gir\\My Documents\\Visual Studio Projects\\Memory\\pic6.bmp"; } else if(intCount2 == 37 || intCount2 == 38 || intCount2 == 39 || intCount2 == 40 || intCount2 == 41 || intCount2 == 42 || intCount2 == 43) { strFileName = "C:\\Documents and Settings\\Owner.Gir\\My Documents\\Visual Studio Projects\\Memory\\pic7.bmp"; } return strFileName; } private bool checkMatch(int intCount3, int intCount2) { //check to see if the images match and return true/false value bool blnMatch = false; if(intCount2 >= 1 && intCount2 <= 6) { if(intCount3 >= 1 && intCount3 <= 6) { blnMatch = true; } else { blnMatch = false; } } else if(intCount2 >= 7 && intCount2 <= 12) { if(intCount3 >= 7 && intCount3 <= 12) { blnMatch = true; } else { blnMatch = false; } } else if(intCount2 >= 13 && intCount2 <= 18) { if(intCount3 >= 13 && intCount3 <= 18) { blnMatch = true; } else { blnMatch = false; } } else if(intCount2 >= 19 && intCount2 <= 24) { if(intCount3 >= 19 && intCount3 <= 24) { blnMatch = true; } else { blnMatch = false; } } else if(intCount2 >= 25 && intCount2 <= 30) { if(intCount3 >= 25 && intCount3 <= 30) { blnMatch = true; } else { blnMatch = false; } } else if(intCount2 >= 31 && intCount2 <= 36) { if(intCount3 >= 31 && intCount3 <= 36) { blnMatch = true; } else { blnMatch = false; } } else if(intCount2 >= 37 && intCount2 <= 42) { if(intCount3 >= 37 && intCount3 <= 42) { blnMatch = true; } else { blnMatch = false; } } return blnMatch; } private void mnuNewGame_Click_1(object sender, System.EventArgs e) { newGame(); } private void mnuResetScore_Click(object sender, System.EventArgs e) { //write empty string to the user file StreamWriter NameStreamWriter = new StreamWriter("C:\\Documents and Settings\\Owner.Gir\\My Documents\\Visual Studio Projects\\Memory\\User.txt",false); for(int p=0; p<3; p++) { NameStreamWriter.WriteLine("NAME"); } NameStreamWriter.Close(); //write 0 strings to the highscores file StreamWriter hScoreStreamWriter = new StreamWriter("C:\\Documents and Settings\\Owner.Gir\\My Documents\\Visual Studio Projects\\Memory\\HighScores.txt",false); for(int p=0; p<3; p++) { hScoreStreamWriter.WriteLine("0"); } hScoreStreamWriter.Close(); } private void mnuExit_Click_1(object sender, System.EventArgs e) { //close this application this.Close(); } private void mnuAbout_Click(object sender, System.EventArgs e) { //display 'programmed by' message MessageBox.Show("Programmed by: Galen Goforth"); } } }
Last edited by gaylo565; 05-08-2008 at 12:41 PM.
no error messages...it compiles fine. The problem is when I run the new game method and start the new game the check match method doesn't work right. Instead of taking a guess away for a wrong selection it adds twice the points of a right selection and leaves the pictures visible.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks