C#: Hello, I am having trouble turning code from a console application to a wind
ID: 3718477 • Letter: C
Question
C#: Hello, I am having trouble turning code from a console application to a windows form application and then hooking it up to a form. So I am making a word scramble game and have coded the scrambler using arrays that are storing the words and am now confused on how to get it to work. In one class I want to store the words and then when a user hits a radio button (labeled easy, medium, or hard) then that corresponding level will start to output one word of the array at a time to the form. Once the user gets the word correct it moves to another word until there are no more words on that level or the user selects another radio button.
Here is what my form looks like right now:
Here is my code so far:
class Program
{
//Main method
static void Main(string[] args)
{
// String arrays to store original words and scrambled words
// String for easy words
string[] easy = { "cat", "dog", "boy", "ice", "mad", "nut", "old", "ram", "cup",
"car", "kid", "toy", "bin", "bit", "sun", "one", "two", "six", "ten", "act",
"ace", "arc", "box", "bun", "bid", "bee", "run", "see", "sea", "lip" };
string[] easyScramble = new string[easy.Length];
// Object creation
var c = new Converter();
// Call converter class method to scramble each word in the array
// and store into another array
for (int i = 0; i < easy.Length; i++)
{
easyScramble[i] = c.ScrambleWord(easy[i]);
}
// Display original array
Console.WriteLine("Easy Original array:");
Console.Write("[");
for (int i = 0; i < easy.Length; i++)
{
Console.Write(easy[i] + ", ");
}
Console.Write("] ");
// Display scrambled array
Console.WriteLine("Easy Scrampled word array:");
Console.Write("[");
for (int i = 0; i < easyScramble.Length; i++)
{
Console.Write(easyScramble[i] + ", ");
}
Console.Write("] ");
// String for the medium words
string[] medium = { "nose", "easy", "girl", "grew", "nice", "tree", "then",
"than", "this", "that", "bird", "grow", "game", "hard", "sell", "fall",
"more", "gift", "jump", "snow", "rain", "fish", "able", "also", "acid",
"area", "math", "four", "five", "nine" };
string[] mediumScramble = new string[medium.Length];
// Object creation
var a = new Converter();
// Call converter class method to scramble each word in the array
// and store into another array
for (int i = 0; i < medium.Length; i++)
{
mediumScramble[i] = c.ScrambleWord(medium[i]);
}
// Display original array
Console.WriteLine(" Medium Original array:");
Console.Write("[");
for (int i = 0; i < medium.Length; i++)
{
Console.Write(medium[i] + ", ");
}
Console.Write("] ");
// Display scambled array
Console.WriteLine("Medium Scrampled word array:");
Console.Write("[");
for (int i = 0; i < mediumScramble.Length; i++)
{
Console.Write(mediumScramble[i] + ", ");
}
Console.Write("] ");
// String for hard words
string[] hard = { "three", "spell", "grave", "stone", "truck", "apple",
"craft", "horse", "start", "train", "paint", "agree",
"again", "alert", "block", "cover", "daily", "cream", "delay", "earth",
"faith", "lunch", "magic", "rapid", "sleep", "title", "upper", "worse",
"woman", "virus", "water"};
string[] hardScramble = new string[hard.Length];
// Object creation
var b = new Converter();
// Call converter class method to scramble each word in the array
// and store into another array
for (int i = 0; i < hard.Length; i++)
{
hardScramble[i] = c.ScrambleWord(hard[i]);
}
// Display original array
Console.WriteLine("Hard Original array:");
Console.Write("[");
for (int i = 0; i < hard.Length; i++)
{
Console.Write(hard[i] + ", ");
}
Console.Write("] ");
// Display scambled array
Console.WriteLine("Hard Scrampled word array:");
Console.Write("[");
for (int i = 0; i < hardScramble.Length; i++)
{
Console.Write(hardScramble[i] + ", ");
}
Console.Write("] ");
Console.ReadKey();
}
}
and my main class:
class Converter
{
//Convertor function
public string ScrambleWord(string word)
{
//Array to take each charcters in the string to scramble
char[] scramble = new char[word.Length];
//Randomly select the character
Random rand = new Random(10000);
//Variable to get the scramble array index
int index = 0;
//Loop to check until entire word
while (word.Length > 0)
{ // Get a random number between 0 and the length of the word.
int next = rand.Next(0, word.Length - 1); // Take the character from the random position and add to our char array.
scramble[index] = word[next]; // Remove the character from the word.
//Substring method in string to generate next word
word = word.Substring(0, next) + word.Substring(next + 1);
++index;
}
//Return the scrambled word
return new String(scramble);
}
}
Thank you for any help you can provide.
Scrambler - O X Difficulty • Easy O Medium O Hard Submit ExitExplanation / Answer
I was a little confused with the requirements.. so i have implemented it in two ways...
1. On button click the textbox will display the next scrambled word according to the difficulty selected
2. i have used threads to display the scrambled words one after the another with 1sec interval.
Names used- rbEasy,rbMed,rbHard for radio buttons, btnSubmit for the button, textbox1 for the textbox
Change the namespace accordingly or use ScrambleFrm oly
Method 1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ScrambleFrm
{
public partial class Form1 : Form
{
// String arrays to store original words and scrambled words
// String for easy words
static string[] easy = { "cat", "dog", "boy", "ice", "mad", "nut", "old", "ram", "cup",
"car", "kid", "toy", "bin", "bit", "sun", "one", "two", "six", "ten", "act",
"ace", "arc", "box", "bun", "bid", "bee", "run", "see", "sea", "lip" };
string[] easyScramble = new string[easy.Length];
// String for the medium words
static string[] medium = { "nose", "easy", "girl", "grew", "nice", "tree", "then",
"than", "this", "that", "bird", "grow", "game", "hard", "sell", "fall",
"more", "gift", "jump", "snow", "rain", "fish", "able", "also", "acid",
"area", "math", "four", "five", "nine" };
string[] mediumScramble = new string[medium.Length];
// String for hard words
static string[] hard = { "three", "spell", "grave", "stone", "truck", "apple",
"craft", "horse", "start", "train", "paint", "agree",
"again", "alert", "block", "cover", "daily", "cream", "delay", "earth",
"faith", "lunch", "magic", "rapid", "sleep", "title", "upper", "worse",
"woman", "virus", "water"};
string[] hardScramble = new string[hard.Length];
Thread easyThrd, medThrd, hardThrd;
int curINdex = 0;
public Form1()
{
InitializeComponent();
// Object creation
var c = new Converter();
// Call converter class method to scramble each word in the array
// and store into another array
for (int i = 0; i < easy.Length; i++)
{
easyScramble[i] = c.ScrambleWord(easy[i]);
}
// Display original array
Console.WriteLine("Easy Original array:");
Console.Write("[");
for (int i = 0; i < easy.Length; i++)
{
Console.Write(easy[i] + ", ");
}
Console.Write("] ");
// Display scrambled array
Console.WriteLine("Easy Scrampled word array:");
Console.Write("[");
for (int i = 0; i < easyScramble.Length; i++)
{
Console.Write(easyScramble[i] + ", ");
}
Console.Write("] ");
// Object creation
var a = new Converter();
// Call converter class method to scramble each word in the array
// and store into another array
for (int i = 0; i < medium.Length; i++)
{
mediumScramble[i] = c.ScrambleWord(medium[i]);
}
// Display original array
Console.WriteLine(" Medium Original array:");
Console.Write("[");
for (int i = 0; i < medium.Length; i++)
{
Console.Write(medium[i] + ", ");
}
Console.Write("] ");
// Display scambled array
Console.WriteLine("Medium Scrampled word array:");
Console.Write("[");
for (int i = 0; i < mediumScramble.Length; i++)
{
Console.Write(mediumScramble[i] + ", ");
}
Console.Write("] ");
// Object creation
var b = new Converter();
// Call converter class method to scramble each word in the array
// and store into another array
for (int i = 0; i < hard.Length; i++)
{
hardScramble[i] = c.ScrambleWord(hard[i]);
}
// Display original array
Console.WriteLine("Hard Original array:");
Console.Write("[");
for (int i = 0; i < hard.Length; i++)
{
Console.Write(hard[i] + ", ");
}
Console.Write("] ");
// Display scambled array
Console.WriteLine("Hard Scrampled word array:");
Console.Write("[");
for (int i = 0; i < hardScramble.Length; i++)
{
Console.Write(hardScramble[i] + ", ");
}
Console.Write("] ");
//Console.ReadKey();
}
private void btnSubmit_Click(object sender, EventArgs e)
{
// on button click get the next word
if (rbEasy.Checked==true )
{
if (curINdex<easyScramble.Count())
{
textBox1.Text = easyScramble[curINdex];
curINdex++;
}
else
{
MessageBox.Show("End of words");
}
}
else if (rbMed.Checked==true )
{
if (curINdex < mediumScramble .Count())
{
textBox1.Text = mediumScramble[curINdex];
curINdex++;
}
else
{
MessageBox.Show("End of words");
}
}
else if (rbHard.Checked==true )
{
if (curINdex < hardScramble .Count())
{
textBox1.Text = hardScramble[curINdex];
curINdex++;
}
else
{
MessageBox.Show("End of words");
}
}
}
private void rbEasy_CheckedChanged(object sender, EventArgs e)
{
curINdex = 0;
}
private void rbMed_CheckedChanged(object sender, EventArgs e)
{
curINdex = 0;
}
private void rbHard_CheckedChanged(object sender, EventArgs e)
{
curINdex = 0;
}
}
}
MEthod 2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ScrambleFrm
{
public partial class Form1 : Form
{
// String arrays to store original words and scrambled words
// String for easy words
static string[] easy = { "cat", "dog", "boy", "ice", "mad", "nut", "old", "ram", "cup",
"car", "kid", "toy", "bin", "bit", "sun", "one", "two", "six", "ten", "act",
"ace", "arc", "box", "bun", "bid", "bee", "run", "see", "sea", "lip" };
string[] easyScramble = new string[easy.Length];
// String for the medium words
static string[] medium = { "nose", "easy", "girl", "grew", "nice", "tree", "then",
"than", "this", "that", "bird", "grow", "game", "hard", "sell", "fall",
"more", "gift", "jump", "snow", "rain", "fish", "able", "also", "acid",
"area", "math", "four", "five", "nine" };
string[] mediumScramble = new string[medium.Length];
// String for hard words
static string[] hard = { "three", "spell", "grave", "stone", "truck", "apple",
"craft", "horse", "start", "train", "paint", "agree",
"again", "alert", "block", "cover", "daily", "cream", "delay", "earth",
"faith", "lunch", "magic", "rapid", "sleep", "title", "upper", "worse",
"woman", "virus", "water"};
string[] hardScramble = new string[hard.Length];
Thread easyThrd, medThrd, hardThrd;
public Form1()
{
InitializeComponent();
easyThrd = new Thread(EasyScrFunc);
medThrd = new Thread(MedScrFunc);
hardThrd = new Thread(HardScrFunc);
// Object creation
var c = new Converter();
// Call converter class method to scramble each word in the array
// and store into another array
for (int i = 0; i < easy.Length; i++)
{
easyScramble[i] = c.ScrambleWord(easy[i]);
}
// Display original array
Console.WriteLine("Easy Original array:");
Console.Write("[");
for (int i = 0; i < easy.Length; i++)
{
Console.Write(easy[i] + ", ");
}
Console.Write("] ");
// Display scrambled array
Console.WriteLine("Easy Scrampled word array:");
Console.Write("[");
for (int i = 0; i < easyScramble.Length; i++)
{
Console.Write(easyScramble[i] + ", ");
}
Console.Write("] ");
// Object creation
var a = new Converter();
// Call converter class method to scramble each word in the array
// and store into another array
for (int i = 0; i < medium.Length; i++)
{
mediumScramble[i] = c.ScrambleWord(medium[i]);
}
// Display original array
Console.WriteLine(" Medium Original array:");
Console.Write("[");
for (int i = 0; i < medium.Length; i++)
{
Console.Write(medium[i] + ", ");
}
Console.Write("] ");
// Display scambled array
Console.WriteLine("Medium Scrampled word array:");
Console.Write("[");
for (int i = 0; i < mediumScramble.Length; i++)
{
Console.Write(mediumScramble[i] + ", ");
}
Console.Write("] ");
// Object creation
var b = new Converter();
// Call converter class method to scramble each word in the array
// and store into another array
for (int i = 0; i < hard.Length; i++)
{
hardScramble[i] = c.ScrambleWord(hard[i]);
}
// Display original array
Console.WriteLine("Hard Original array:");
Console.Write("[");
for (int i = 0; i < hard.Length; i++)
{
Console.Write(hard[i] + ", ");
}
Console.Write("] ");
// Display scambled array
Console.WriteLine("Hard Scrampled word array:");
Console.Write("[");
for (int i = 0; i < hardScramble.Length; i++)
{
Console.Write(hardScramble[i] + ", ");
}
Console.Write("] ");
//Console.ReadKey();
}
private void btnSubmit_Click(object sender, EventArgs e)
{
}
private void rbEasy_CheckedChanged(object sender, EventArgs e)
{
StartScramble();
}
private void StartScramble()
{
if (easyThrd.IsAlive==true )
{
easyThrd.Suspend();
}
if (medThrd.IsAlive==true )
{
medThrd.Suspend();
}
if (hardThrd.IsAlive==true )
{
hardThrd.Suspend();
}
if (rbEasy.Checked==true)
{
easyThrd = new Thread(EasyScrFunc);
easyThrd.Start();
}
else if (rbMed.Checked==true )
{
medThrd = new Thread(MedScrFunc);
medThrd .Start();
}
else if (rbHard.Checked == true)
{
hardThrd = new Thread(HardScrFunc);
hardThrd.Start();
}
}
public void TextBoxOp(string value)
{
if (InvokeRequired)
{
this.Invoke(new Action<string>(TextBoxOp), new object[] { value });
return;
}
textBox1.Text = value;
}
void EasyScrFunc()
{
for (int i = 0; i < easyScramble.Count(); i++)
{
TextBoxOp(easyScramble[i]);
Thread.Sleep(1000);
}
}
void MedScrFunc()
{
for (int i = 0; i < mediumScramble.Count(); i++)
{
TextBoxOp(mediumScramble [i]);
Thread.Sleep(1000);
}
}
void HardScrFunc()
{
for (int i = 0; i < hardScramble.Count(); i++)
{
TextBoxOp(hardScramble [i]);
Thread.Sleep(1000);
}
}
private void rbMed_CheckedChanged(object sender, EventArgs e)
{
StartScramble();
}
private void rbHard_CheckedChanged(object sender, EventArgs e)
{
StartScramble();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.