Guess button is not working when i try to guess one of the words i entered into array

neymark

Member
Joined
Nov 9, 2016
Messages
8
Programming Experience
3-5
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WordHandlerAssignment
{//start of program
    public partial class frmWordHandler : Form
    {
        static string word;
        static string[] wordBank = new string[5];
        static int i = 0;
       static int tries=3;
        static string randomWord = " ";
        public frmWordHandler()
        {
            InitializeComponent();
         
        }
        private void btnBackToMain_Click(object sender, EventArgs e)
        {
            {
                frmSplashScreen mainForm = new frmSplashScreen();
                mainForm.Show();
                this.Hide();
            }
        }
        private void frmWordHandler_Load(object sender, EventArgs e)
        {
            
           
        }
        private void btnStoreWord_Click(object sender, EventArgs e)
        {
           
           word = txtStoreWords.Text.ToUpper();
            //lengthCheck(word);
            //vowelStart(word);
            //recur(word);
            if ((lengthCheck(word) == true) && (recur(word) == true) && (vowelStart(word) == true))
            {
                wordBank[i] = txtStoreWords.Text;
                lstStoreWords.Items.Add(word);
                i++;
               // MessageBox.Show("Word Accepted!", "Well Done!");
            }
            if (lstStoreWords.Items.Count == 5)
            {
                
                MessageBox.Show("Press start along bottom to start!", "Well Done!");
                btnStart.Visible = true;
            }
        }
        private bool recur(string word)
        { //start of recur mthod
            int i = 0;
            int charCount = 0;
            for (i = 0; i < word.Length; i++)
            {
                for (int j = 1; j < word.Length; j++)
                {
                    if (j != i && word[i] == word[j])
                    {
                        charCount++;
                    }
                }
            }
            if (charCount == 0)
            {
                MessageBox.Show("Word must have reoccurring letter!");
                txtStoreWords.ResetText();
                txtStoreWords.Focus();
                return false;
            }

            else
            {
                //MessageBox.Show("Word Accepted, has reoccurence");
                txtStoreWords.ResetText();
                txtStoreWords.Focus();
                return true;
            }
        }//end of recur method
        private bool vowelStart(string word)
        {
            char vowel = word[0];

            switch (vowel)
            { //start of switch
                case 'A':
                case 'E':
                case 'I':
                case 'O':
                case 'U':
                    return true;
                    break;
                default:
                    MessageBox.Show("The word must start with vowel!");
                    return false;
                    break;

            }//end of switch

        }
        private bool lengthCheck(string word)
        {
            if (word.Length > 4 && word.Length < 9)
            {
                return true;
            }
            else
            {
                MessageBox.Show("The word must be between 4 and 9 characters long!");
                txtStoreWords.ResetText();
                return false;
            }
        }
        private void btnInstructions_Click(object sender, EventArgs e)
        {
            frmInstructions mainForm = new frmInstructions();
            mainForm.Show();
            this.Hide();
        }
        private void btnStart_Click(object sender, EventArgs e)
        {
            randomNumber();
            btnGuess.Visible = true;
            txtGuess.Visible = true;//setting buttons to become visible to start guessing
            btnStoreWord.Visible = false;
            txtStoreWords.Visible = false;
            lblGuessBox.Visible = true;
            lblGuesses.Visible = true;
            lstStoreWords.Visible = false;
        }
      
        private void btnGuess_Click(object sender, EventArgs e)
        {
            string guess = txtGuess.Text.ToString();

            if (wordGuess(guess) == true)
            {
                MessageBox.Show("Well done!");
                DialogResult answer;
                answer = MessageBox.Show("Play again?");

                if (answer == DialogResult.No)
                {
                    Close();
                    Environment.Exit(0);
                }
                else if (answer == DialogResult.Yes)
                {
                    tries.Equals(3);
                    randomNumber();
                }
                else if(wordGuess(guess) == false)
                {
                    MessageBox.Show("Your ****!");
                    //tries = tries - 1;
                }
            }
             }
        private bool wordGuess(string word)
        {
            if (!word.Equals(randomWord))
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        private void randomNumber()
        {
            Random rand = new Random();
          string randomWord = wordBank[(rand.Next(0,(wordBank.Length - 1)))];
     
        }
        private void lblGuessBox_Click(object sender, EventArgs e)
        {
        }
        private void label1_Click(object sender, EventArgs e)
        {
        }
    } 
            }//end of program
 
There's way too much code there. If you can't narrow the issue down more than that then I don't think you've really thought about it hard enough. Please post only the relevant code and also don't post just code. It should be accompanied by a FULL and CLEAR explanation of the problem. Just dumping all your code on us with no explanation is rather lazy. We're happy to help but we expect you to do what you can to help us do so.
 
There's way too much code there. If you can't narrow the issue down more than that then I don't think you've really thought about it hard enough. Please post only the relevant code and also don't post just code. It should be accompanied by a FULL and CLEAR explanation of the problem. Just dumping all your code on us with no explanation is rather lazy. We're happy to help but we expect you to do what you can to help us do so.
Okay, sorry cheers. My problem is that when my methods to validate the word are true, I want to add the words I enter into an array called workBank. When I have5 words stored in the array, I then use a random generator to pick one of these words that I have to guess. It allows me to do everything but when I try guessing , the random word is just "" when debugging it.
 
Okay, sorry cheers. My problem is that when my methods to validate the word are true, I want to add the words I enter into an array called workBank. When I have5 words stored in the array, I then use a random generator to pick one of these words that I have to guess. It allows me to do everything but when I try guessing , the random word is just "" when debugging it.

That's a better description. Now post just the code that is relevant to the issue, so we don't have go looking through all of it.
 
I got it working. I am trying to create a clue button that will for example output "_ _ _ _ _ " for the random word apple as it is five characters. Everytime I do, it keeps outputting just 1 underscore in the lable.

for (int i = 0; i < randomWord.Length; i++)

{

randomWord
= "_" + "";




}


lblClue
.Text = randomWord;
 
I can't read your code but if you want a string containing underscores that is the same length as another string then do this:
var outStr = new string('_', inStr.Length);
 
I can't read your code but if you want a string containing underscores that is the same length as another string then do this:
var outStr = new string('_', inStr.Length);

do I need to do it in a for loop?
 
do I need to do it in a for loop?

Read what I said. That code will give you a string containing underscores that is the same length as another string. That's what I said because that's what it does. If you needed a loop to get that string containing underscores from another string then the code I provided would contain a loop. Of course, there's always the option of actually trying the code to see what it does for yourself.
 
i am using this code but it is only giving me one underscore?

Read what I said. That code will give you a string containing underscores that is the same length as another string. That's what I said because that's what it does. If you needed a loop to get that string containing underscores from another string then the code I provided would contain a loop. Of course, there's always the option of actually trying the code to see what it does for yourself.
C#:
char[] a = new char[randomWord.Length];
            for (int i = 0; i < a.Length; i++)
            {
                a[i] = '_';
            }
            // Tell the user the number of letters through the dashes
            for (int i = 0; i < a.Length; i++)
            {
                lblClue.Text = (a[i] + "  ");
            }
 
Posting code without any question or explanation is of no value. You're not doing as I said so you've either chosen to ignore my advice or chosen not to explain what issue you're having using it. I can't help any further unless you provide some useful information. Hmmm... are you saying, without actually saying, that you want spaces between the underscores?
 
C#:
char[] a = new char[randomWord.Length];
            for (int i = 0; i < a.Length; i++)
            {
                a[i] = '_';
            }
            // Tell the user the number of letters through the dashes
            for (int i = 0; i < a.Length; i++)
            {
                lblClue.Text = (a[i] + "  ");
            }

I thought i posted the question, i have this code in a clue button , so when i press it , it will give the user a clue to the length of the random word. So if it was apple it would be _ _ _ _ _ . At the minute it is just posting _ (one underscore)
 
I thought i posted the question, i have this code in a clue button , so when i press it , it will give the user a clue to the length of the random word. So if it was apple it would be _ _ _ _ _ . At the minute it is just posting _ (one underscore)

I told you how to create a string containing the same number of underscores as there are characters in another string. You've chosen to ignore that advice. If that advice achieves what you want then make use of it rather than using code that doesn't work. If that advice doesn't achieve what you want then perhaps it would be a good idea to CLEARLY explain EXACTLY what it is that you do want. I'm tiring of having to draw each scrap of information out of you. Provide a FULL and CLEAR explanation of EXACTLY what you want or I'm afraid that I'll give up.
 

Latest posts

Back
Top Bottom