3 Replies - 2338 Views - Last Post: 11 May 2015 - 04:56 PM Rate Topic: -----

#1 dstevens   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 02-February 13

New to c# need Help with next random Word from List

Posted 11 May 2015 - 04:41 PM

My code is a word scramble code and i am trying to get it to go to the next random word in the list, can someone please help. I am not getting any errors it just will not go to the next random word for the game to proceed. code is as follows...........

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;
using System.IO;

namespace NewWordScramble
{
    struct WordScramble
    {
        public string Word;
    }

    public partial class Form1 : Form
    {
        private string strstoreword;
        private string word;
        Random r = new Random();
        
        public Form1()
        {
            InitializeComponent();
            
            timer1.Enabled = false;
         }

        private void ReadFile()
        {
            try
            {
              StreamReader inputFile;
              string wordline;
        
              WordScramble wordlist = new WordScramble();

                char[] delim = { ' ' };

                inputFile = File.OpenText("WordScrambleListtxt.txt");

               while (!inputFile.EndOfStream)
                {
                    wordline = inputFile.ReadLine();

                    string[] tokens = wordline.Split(delim);

                    wordlist.Word = tokens[0];

                    WordTextBox.Text = wordlist.Word;                   
                }
              // inputFile.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        string DisplayWords()
        {
            string word = WordTextBox.Text;
            Random r = new Random();
            String[] myString = File.ReadAllLines("WordScrambleListtxt.txt");

             string scrambled_Word = ScrambleWord(word);
             WordTextBox.Text = scrambled_Word;
             strstoreword = word;
                       
            return myString[r.Next(0, myString.Length - 1)];
          
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            ReadFile();
               DisplayWords();
        }

        public string ScrambleWord(string word)
        {
            char[] chars = new char[word.Length];
            Random rand = new Random(10000);
            int index = 0;
            while (word.Length > 0)
            {
                int next = rand.Next(0, word.Length - 1);
                chars[index] = word[next];
                word = word.Substring(0, next) + word.Substring(next + 1);
                ++index;
            }
                return new String(chars);
        }

        private void CheckWordButton_Click(object sender, EventArgs e)
        {
            string strGuess;
            strGuess = txtGuess.Text.ToString();

            if (strGuess.ToLower() == strstoreword)
            {
                lblResult.Text = "You are CORRECT!";
                timer1.Enabled = true;
            }
            else
            {
                lblResult.Text = "Wrong! Please try again!";
                txtGuess.Clear();
            }

            txtGuess.Focus();
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (lblResult.BackColor == Color.Navy)
            {
                lblResult.BackColor = Color.Red;
            }
            else
            {
                lblResult.BackColor = Color.Navy;
            }
        }

        private void PlayAgainButton_Click(object sender, EventArgs e)
        {
            txtGuess.Clear();
            WordTextBox.Clear();
            ReadFile();
            DisplayWords();
            timer1.Enabled = false;
            lblResult.Text.Remove(0);
            lblResult.Text = "";

            ReadFile();
            DisplayWords();
        }
    }
}   




Is This A Good Question/Topic? 0
  • +

Replies To: New to c# need Help with next random Word from List

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: New to c# need Help with next random Word from List

Posted 11 May 2015 - 04:48 PM

LIne 67 - take it out.
Do not make a new instance of Random() every time you need it. That' won't get random results.
Make 1 instance of Random() for your entire class then keep using it.
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5543
  • View blog
  • Posts: 14,544
  • Joined: 18-April 07

Re: New to c# need Help with next random Word from List

Posted 11 May 2015 - 04:51 PM

You have this loop here..

while (!inputFile.EndOfStream)
{
    wordline = inputFile.ReadLine();
    string[] tokens = wordline.Split(delim);
    wordlist.Word = tokens[0]; //<-- This is always taking just the first word (the 'zero'th element)
    WordTextBox.Text = wordlist.Word;                   
}



Your list above is always taking just the first word of each line. Is this what you are intending? From the way you have this setup I am guessing you want all the words of each line. In which case you will need to loop through "tokens" and add each. But you are also resetting WordTextBox.Text each time. Meaning you will always see just the one word. You can instead use a listbox or some other control that allows display of multiple items.
Was This Post Helpful? 1
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: New to c# need Help with next random Word from List

Posted 11 May 2015 - 04:56 PM

Sounds like basic debugging techniques would do wonders for you.

See FAQ # 2 "How do I debug my program?"

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 6: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated March 2015
Spoiler

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1