I will create a program that translates English words into Pig Latin ... My problem with the code found below, is that the only word in the last index of the array as reported in the results? Does anyone see the error?
Thanks in advance
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnTrans_Click( object sender, EventArgs e )
{
string engWordText = engWord.Text.ToString();
string let1;
string restLet;
int position;
string pigLatin = "";
string vokal = "AEIOUaeiou";
// split the sentence into individual words
//string[] words = engWordText.Split(' ');
string[] transWord = engWordText.Split(' ');
// translate each word into pig latin
foreach (string word in transWord)
{
// check for empty TextBox
try
{
let1 = word.Substring(0, 1);
restLet = word.Substring(1, word.Length - 1);
position = vokal.IndexOf(let1);
if (position == -1)
{
pigLatin = restLet + let1 + "ay";
}
else
{
pigLatin = word + "way";
}
// display the translation
latinInput.Text = pigLatin.ToString();
engWord.Clear();
}
catch (System.ArgumentOutOfRangeException)
{
MessageBox.Show("Du måste skriva in ett engelskt ord", "PigLatin",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
} // end method translateButton_Click
// pressing enter is the same as clicking the Translate Button
private void engWordText_KeyDown( object sender, KeyEventArgs e )
{
// allow user to press enter in TextBox
if ( e.KeyCode == Keys.Enter )
btnTrans_Click( sender, e );
} // end method inputTextBox_KeyDown
} // end class PigLatinForm
You are assigning the value of pigLatin to the text box's Text property at the end of each loop, which means it will only have the last value that was assigned to it. Try this:
List<string> plWords = new List<string>();
// translate each word into pig latin
foreach (string word in transWord)
{
// check for empty TextBox
try
{
let1 = word[0];
restLet = word.Substring(1, word.Length - 1);
if (!vokal.Contains(let1))
{
pigLatin = restLet + let1 + "ay";
}
else
{
pigLatin = word + "way";
}
plWords.Add(pigLatin);
}
catch (System.ArgumentOutOfRangeException)
{
MessageBox.Show("Du måste skriva in ett engelskt ord", "PigLatin",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
engWord.Clear();
latinInput.Text = string.Join(" ", plWords.ToArray());
As a bit of a bonus, here's how you can make this operation quite a bit cleaner using Linq:
private static string MakePigLatin(string word)
{
const string vowels = "AEIOUaeiou";
char let1 = word[0];
string restLet = word.Substring(1, word.Length - 1);
return vowels.Contains(let1) ? word + "way" : restLet + let1 + "ay";
}
private void btnTrans_Click( object sender, EventArgs e )
{
var plWords = engWord.Text
.Split(new[]{' '}, StringSplitOptions.RemoveEmptyEntries)
.Select(MakePigLatin);
latinInput.Text = string.Join(" ", plWords);
engWord.Clear();
}
Related
I was working on this hangman game in c# and so it's not working how I wanted.
The point of the game is next: When you start the game you need to open the .txt file where the words are written. When u load your .txt file with words game should show as many "" as how many letters your randomly chosen word from the .txt file has. When u click on the button with the letter game should check if the chosen word has that letter in it if it has one or more chosen letters, "" from the word should disappear and the chosen letter should show up, else our attempts should decrease for 1 and should show next picture in panel1.
I also want to set up the game so that only Cyrillic from the Serbian language can be used in the .txt file and the game itself.
Now about the problem, everything is working fine except the code where the game checks if a word has a chosen letter. Another problem is space, there should be the possibility of guessing multiple words at once but when I add space between two or more words game use "_" for space as well as for other characters, I need space to not be replaced with "__"
void NapraviLabels()
{
string cre = UzmiNasumicnuRijec();
string recc = cre.Replace(" ", "");
rec = recc.ToLower();
char[] chars = rec.ToCharArray();
int izmedju = 330 / chars.Length - 1;
for (int i = 0; i < chars.Length - 1; i++)
{
foreach(char c in rec)
{
if( c == ' ')
{
labels[i].Text = "";
}
else
{
labels.Add(new Label());
labels[i].Location = new Point((i * izmedju) + 10, 120);
labels[i].Text = "_";
labels[i].Parent = groupBox2;
labels[i].BringToFront();
labels[i].CreateControl();
}
}
}
}
string UzmiNasumicnuRijec()
{
string listaRijeci = File.ReadAllText(#filePath);
string[] rijeci = listaRijeci.Split('\n');
Random r = new Random();
return rijeci[r.Next(0, rijeci.Length -1)];
}
Button curButton = null;
Label curLabel = null;
private void slovo_Click(object sender, EventArgs e)
{
Button clickedbtn = (Button)sender;
Label lb = GetLBFromButton(clickedbtn);
if (curButton == null)
{
// first button clicked
curButton = clickedbtn;
curLabel = lb;
clickedbtn.Visible = false;
curLabel.Visible = true;
char slovo = curLabel.Text.ToLower().ToCharArray()[0];
if (rec.Contains(slovo))
{
char[] slova = rec.ToCharArray();
for (int i = 0; i < slova.Length; i++)
{
if (slova[i] == slovo)
labels[i].Text = slovo.ToString();
}
foreach (Label l in labels)
if (l.Text == "_") return;
MessageBox.Show("Победили сте!", "Честитамо");
Reset();
}
else
{
MessageBox.Show("Слово које сте унели се не налази у појму!");
NacrtajDijeloveTijela((DjeloviTijela)kolicina);
kolicina++;
if (kolicina == 9)
{
MessageBox.Show("Изгубили сте, појам је био: " + rec);
Reset();
}
}
curButton = null;
curLabel = null;
}
}
private void pogodirec_Click(object sender, EventArgs e)
{
if (textBox1.Text == rec)
{
MessageBox.Show("Победили сте!", "Честитамо");
Reset();
}
else
{
MessageBox.Show("Реч коју сте унели је погрешна!" , "Грешка!");
NacrtajDijeloveTijela((DjeloviTijela)kolicina);
kolicina++;
if (kolicina == 10)
{
MessageBox.Show("Изгубили сте, појам је био: " + rec);
Reset();
}
}
}
richTextBox1 contains text
I click on a word and Console displays that word for me and it highlights/selects the word i clicked on.
To do this, I keep the index of the character I clicked on then go left and right until I hit a space, -1, or end of file. Now I have the indexes of the beginning and end of the word. The last two lines are supposed to select the word between those two indexes.
However, what happens is that sometimes it highlights the word I want and sometimes it highlights all the words to the right of the character I clicked on.
"omar hello where are you going"
If I click on h in hello, it highlights hello where instead of highlighting hello
If I click on o in going, it will highlight going only as it should
If I click on o in you, it will highlight you going
I used console to check the start and end indexes of the word and they're always right yet for some reason, other words are selected in addition to the word i clicked on
private void richTextBox1_Click(object sender, EventArgs e)
{
int length = richTextBox1.Text.Length;
int rightPart = richTextBox1.SelectionStart;
int leftPart = richTextBox1.SelectionStart - 1;
string rightText = "";
string leftText = "";
while (rightPart != length)
{
if (richTextBox1.Text[rightPart].ToString().CompareTo(" ") != 0)
{
rightText += richTextBox1.Text[rightPart];
rightPart++;
}
else
{
break;
}
}
while (leftPart != -1)
{
if (richTextBox1.Text[leftPart].ToString().CompareTo(" ") != 0)
{
leftText = richTextBox1.Text[leftPart] + leftText;
leftPart--;
}
else
{
break;
}
}
leftPart++;
Console.WriteLine("\nSelected word is " + leftText + rightText + "\n");
richTextBox1.SelectionStart = leftPart;
richTextBox1.SelectionLength = rightPart;
}
The problem appears to be that you are setting the SelectionLength equal to rightPart. Remember this property represents the length of the selection, not the last index of the selection.
Instead, try changing your code to calculate the length by getting the difference between leftPart and rightPart:
richTextBox1.SelectionLength = rightPart - leftPart;
For what it's worth, your code can be shortened a little:
private void richTextBox1_Click(object sender, EventArgs e)
{
if (richTextBox1.TextLength == 0) return;
int rightPart = richTextBox1.SelectionStart;
int leftPart = richTextBox1.SelectionStart - 1;
while (rightPart < richTextBox1.TextLength && richTextBox1.Text[rightPart] != ' ')
{
rightPart++;
}
while (leftPart > -1 && richTextBox1.Text[leftPart] != ' ')
{
leftPart--;
}
leftPart++;
Console.WriteLine($"\nSelected word is " +
richTextBox1.Text.Substring(leftPart, rightPart - leftPart) + "\n");
richTextBox1.SelectionStart = leftPart;
richTextBox1.SelectionLength = rightPart - leftPart;
}
Or even a little more, using IndexOf and LastIndexOf instead of loops to find the spaces:
private void richTextBox1_Click(object sender, EventArgs e)
{
if (richTextBox1.TextLength == 0) return;
// Find the space before this word and after this word
var selStart = Math.Min(richTextBox1.SelectionStart, richTextBox1.TextLength - 1);
var firstSpace = richTextBox1.Text.LastIndexOf(' ', selStart);
var lastSpace = richTextBox1.Text.IndexOf(' ', selStart);
var start = firstSpace + 1;
var length = (lastSpace < 0 ? richTextBox1.TextLength : lastSpace) - start;
Console.WriteLine($"\nSelected word is {richTextBox1.Text.Substring(start, length)} \n");
richTextBox1.SelectionStart = start;
richTextBox1.SelectionLength = length;
}
So I have a Pig Latin Translator that supports multiple words. But whenever I enter some words (For this we'll just use "banana apple shears chocolate Theodore train" for example.) It will spit out the translated words correctly but it makes repeats! Here is my code:
namespace Pig_Latin_Translator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<string> vowels = new List<string>();
List<string> specials = new List<string>();
private void TranslateButton_Click(object sender, EventArgs e)
{
String[] parts = TranslateBox.Text.Split();
foreach (string s in specials)
{
if (TranslateBox.Text.Contains(s) || TranslateBox.Text == "\"")
{
TranslateOutput.Text = "";
MessageBox.Show("No Special Characters!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
break;
}
else
{
foreach (String part in parts)
{
foreach (String v in vowels)
{
if (part.Substring(0, 1) == v)
{
TranslateOutput.Text = TranslateOutput.Text + " " + part + "ay";
break;
}
else
{
if (part.Substring(0, 2) == "sh" || part.Substring(0, 2) == "ch" || part.Substring(0, 2) == "th" || part.Substring(0, 2) == "tr")
{
string SwitchP = part.Substring(2) + part.Substring(0, 2);
TranslateOutput.Text = TranslateOutput.Text + " " + SwitchP + "ay";
break;
}
else
{
string Switch = part.Substring(1) + part.Substring(0, 1);
TranslateOutput.Text = TranslateOutput.Text + " " + Switch + "ay";
break;
}
}
}
}
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
vowels.Add("a");
vowels.Add("e");
vowels.Add("i");
vowels.Add("o");
vowels.Add("u");
specials.Add("`");
specials.Add("1");
specials.Add("2");
specials.Add("3");
specials.Add("4");
specials.Add("5");
specials.Add("6");
specials.Add("7");
specials.Add("8");
specials.Add("9");
specials.Add("0");
specials.Add("-");
specials.Add("=");
specials.Add("[");
specials.Add("]");
specials.Add(#"\");
specials.Add(";");
specials.Add("'");
specials.Add(",");
specials.Add(".");
specials.Add("/");
specials.Add("~");
specials.Add("!");
specials.Add("#");
specials.Add("#");
specials.Add("$");
specials.Add("%");
specials.Add("^");
specials.Add("&");
specials.Add("*");
specials.Add("(");
specials.Add(")");
specials.Add("_");
specials.Add("+");
specials.Add("{");
specials.Add("}");
specials.Add("|");
specials.Add(":");
specials.Add("\"");
specials.Add("<");
specials.Add(">");
specials.Add("?");
}
private void AboutButton_Click(object sender, EventArgs e)
{
MessageBox.Show("Pig Latin is a fake language. It works by taking the first letter (Or two if it's a pair like 'th' or 'ch') and bringing it to the end, unless the first letter is a vowel. Then add 'ay' to the end. So 'bus' becomes 'usbay', 'thank' becomes 'ankthay' and 'apple' becomes 'appleay'.", "About:", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
Which outputs if you typed in "banana apple shears chocolate Theodore train":
"ananabay appleay earsshay ocolatechay heodoreTay aintray" repeating over 10 times.
BTW: Sorry if you can't answer because I know there is LOTS of code. But it doesn't matter because the thing still is useful. It's just that it shouldn't happen and get on my nerves. And I know there is still many glitches and MUCH more to do but I want to get this resolved first.
You are nesting your code in two loops that it shouldn't be nested in
foreach (string s in specials)
and
foreach (String v in vowels)
Your break statements are getting you out of trouble for one, but not the other.
You can avoid these loops entirely is you use the .Any(...) predicate.
Here's what your code could look like:
private void TranslateButton_Click(object sender, EventArgs e)
{
TranslateOutput.Text = "";
if (specials.Any(s => TranslateBox.Text.Contains(s)))
{
MessageBox.Show("No Special Characters!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
String[] parts = TranslateBox.Text.Split();
foreach (var part in parts)
{
var index = 1;
if (vowels.Any(v => part.Substring(0, 1).ToLower() == v))
{
index = 0;
}
else if (new [] { "sh", "ch", "th", "tr", }.Contains(part.Substring(0, 2).ToLower()))
{
index = 2;
}
TranslateOutput.Text += " " + part.Substring(index) + part.Substring(0, index);
}
}
TranslateOutput.Text = TranslateOutput.Text.TrimEnd();
}
This brings it down to the one foreach loop that you actually need.
You can also get your code to initalize vowels and specials down to this:
vowels.AddRange("aeiou".Select(x => x.ToString()));
specials.AddRange(#"`1234567890-=[]\;',./~!##$%^&*()_+{}|:""<>?".Select(x => x.ToString()));
You are iterating through your words once for each special character. Your foreach to go through your words and translate is inside of your foreach to check to see if the textbox contains any special characters.
In otherwords, you are going to do your translation once per special character.
You'll want to move your foreach (String part in parts) out of your foreach (string s in specials)
You have a bit of a logic problem in your loops.
Your outer loop:
foreach( string s in specials ) {
...is looping through all 42 characters in your special characters list.
Your inner loop
foreach( String part in parts ) {
...is then executed 42 times. So for your six word example you're actually doing your pig latin conversion 252 times.
If you extract the inner loop from the outer, your results are better. Like this:
foreach( string s in specials ) {
if( TranslateBox.Text.Contains( s ) || TranslateBox.Text == "\"" ) {
TranslateOutput.Text = "";
MessageBox.Show( "No Special Characters!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning );
return;
}
}
String[] parts = TranslateBox.Text.Split();
foreach( String part in parts ) {
foreach( String v in vowels ) {
if( part.Substring( 0, 1 ) == v ) {
TranslateOutput.Text = TranslateOutput.Text + " " + part + "ay";
break;
}
else {
if( part.Substring( 0, 2 ) == "sh" || part.Substring( 0, 2 ) == "ch" || part.Substring( 0, 2 ) == "th" || part.Substring( 0, 2 ) == "tr" ) {
string SwitchP = part.Substring( 2 ) + part.Substring( 0, 2 );
TranslateOutput.Text = TranslateOutput.Text + " " + SwitchP + "ay";
break;
}
else {
string Switch = part.Substring( 1 ) + part.Substring( 0, 1 );
TranslateOutput.Text = TranslateOutput.Text + " " + Switch + "ay";
break;
}
}
}
}
A somewhat more concise implementation would be:
private void TranslateButton_Click( object sender, EventArgs e )
{
char[] specials = "`1234567890-=[]\";',./~!##$%^&*()_+{}|:\\<>?".ToArray();
char[] vowels = "aeiou".ToArray();
TranslateOutput.Text = String.Empty;
if( TranslateBox.Text.IndexOfAny( specials ) > -1 ) {
MessageBox.Show( "No Special Characters!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning );
return;
}
String[] parts = TranslateBox.Text.Split();
foreach( String part in parts ) {
int firstVowel = part.IndexOfAny( vowels );
if( firstVowel > 0 ) {
TranslateOutput.Text += part.Substring( firstVowel ) + part.Substring( 0, firstVowel ) + "ay ";
}
else {
TranslateOutput.Text += part + "ay ";
}
}
TranslateOutput.Text = TranslateOutput.Text.TrimEnd();
}
In this example, I create two character arrays for the specials and the vowels. I can then leverage the framework's IndexOfAny method to search for any of the characters in the array and return the index of the first occurrence. That will find the first special, if any, in the first loop and the first vowel in the second loop. Once I have that character index from the word I can parse the word into pig Latin. Note that I'm checking for zero as the vowel index since, in pig Latin a leading vowel stays where it is and the "ay" is just appended to the end of the word.
I´m having trouble picking a random word from a list in another file.
Actually I can´t even get it to choose any word. I´m not sure how to connect the 2 files so to say.
Hoping someone can help out, I´m a beginner so please explain as easy as possible:)
I have 2 files, one is called program.cs and the other is called WordList.cs
I´m gonna paste all my code but first the little snip that I´m having problem with. I just can´t figure out how to write the code correct.
Here is the little part which is called Pick word:
//PICK WORD
static string pickWord()
{
string returnword = "";
TextReader file = new StreamReader(words);
string fileLine = file.ReadLine();
Random randomGen = new Random();
returnword = words[randomGen.Next(0, words.Count - 1)];
return returnword;
}
And here is all the code in Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
class Hangman
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Title = "C# Hangman";
Console.WriteLine("Welcome To C# Hangman!");
//MENU
int MenuChoice = 0;
while (MenuChoice != 4)
{
Console.Write("\n\t1) Add words");
Console.Write("\n\t2) Show list of words");
Console.Write("\n\t3) Play");
Console.Write("\n\t4) Quit\n\n");
Console.Write("\n\tChoose 1-4: "); //Choose meny item
MenuChoice = Convert.ToInt32(Console.ReadLine());
WordList showing = new WordList();
switch (MenuChoice)
{
case 1:
Console.Write("\n\tAdd a word\n\n");
var insert = Console.ReadLine();
showing.AddWord(insert);
Console.Write("\n\tList of words\n\n");
showing.ListOfWords();
break;
case 2:
Console.Write("\n\tList of words\n\n");
showing.ListOfWords();
break;
case 3: //Running game
int numGuessesInt = -1;
while (numGuessesInt == -1)
{
/* Sets the number of guesses the user has to guess the word*/
pickNumGuesses(ref numGuessesInt);
}
/* Randomly picks a word*/
string word = pickWord();
/* Creates a list of characters that will show */
List<char> guessedLetters = new List<char>();
bool solved = false;
while (solved == false)
{
/* Displaying a string to the user based on the user's correct guesses.
* If nothing is correct string will return "_ _ _ " */
string wordToDisplay = displayWord(guessedLetters, word);
/* If the string returned contains the "_" character, all the
* correct letters have not been guessed, so checking if user
* has lost, by checking if numGuessesLeft is less than 1.*/
if (!wordToDisplay.Contains("_"))
{
solved = true;
Console.WriteLine("You Win! The word was " + word);
/* Check if the user wants to play again. If they do,
* then solved is set to true, will end the loop,
* otherwise, checkIfPlayAgain will close the program.*/
checkIfPlayAgain();
}
else if (numGuessesInt <= 0)
{
solved = true;
Console.WriteLine("You Lose! The word was " + word);
checkIfPlayAgain();
}
else
{
/* If the user has not won or lost, call guessLetter,
* display the word, minus guesses by 1*/
guessLetter(guessedLetters, word, wordToDisplay, ref numGuessesInt);
}
}
break;
case 4:
Console.WriteLine("\n\tEnd game?\n\n");
break;
default:
Console.WriteLine("Sorry, invalid selection");
break;
}
}
}
// ****** PICK NUMBER OF GUESSES ******
static void pickNumGuesses(ref int numGuessesInt)
{
string numGuessesString = "";
Console.WriteLine("Pick a number of guesses");
numGuessesString = Console.ReadLine();
try
{
numGuessesInt = Convert.ToInt32(numGuessesString);
if (!(numGuessesInt <= 20 & numGuessesInt >= 1))
{
throw new Exception();
}
}
catch (Exception)
{
numGuessesInt = -1;
Console.WriteLine("Error: Invalid Number of Guesses");
}
}
//PICK WORD
static string pickWord()
{
string returnword = "";
TextReader file = new StreamReader(words);
string fileLine = file.ReadLine();
Random randomGen = new Random();
returnword = words[randomGen.Next(0, words.Count - 1)];
return returnword;
}
// ****** Display word ******
static string displayWord(List<char> guessedCharacters, string word)
{
string returnedWord = "";
if (guessedCharacters.Count == 0)
{
foreach (char letter in word)
{
returnedWord += "_ ";
}
return returnedWord;
}
foreach (char letter in word)
{
bool letterMatch = false;
foreach (char character in guessedCharacters)
{
if (character == letter)
{
returnedWord += character + " ";
letterMatch = true;
break;
}
else
{
letterMatch = false;
}
}
if (letterMatch == false)
{
returnedWord += "_ ";
}
}
return returnedWord;
}
// ****** Guess letter ******
static void guessLetter(List<char> guessedCharacters, string word, string wordToDisplay, ref int numGuessesLeft)
{
string letters = "";
foreach (char letter in guessedCharacters)
{
letters += " " + letter;
}
Console.WriteLine("Guess a letter");
Console.WriteLine("Guessed Letters: " + letters);
Console.WriteLine("Guesses Left: " + numGuessesLeft);
Console.WriteLine(wordToDisplay);
string guess = Console.ReadLine();
char guessedLetter = 'a';
try
{
guessedLetter = Convert.ToChar(guess);
if (!Char.IsLetter(guessedLetter))
{
throw new Exception();
}
}
catch (Exception)
{
Console.WriteLine("Error: Invalid Letter Choice");
//guessLetter(guessedCharacters, word, wordToDisplay, ref numGuessesLeft);
}
bool repeat = false;
for (int i = 0; i < guessedCharacters.Count; i++)
{
if (guessedCharacters[i] == guessedLetter)
{
Console.WriteLine("Error: Invalid Letter Choice");
repeat = true;
//guessLetter(guessedCharacters, word, wordToDisplay, ref numGuessesLeft);
}
}
if (repeat == false)
{
guessedCharacters.Add(guessedLetter);
numGuessesLeft -= 1;
}
}
// ****** Check to see if player wants to play again. ******
static void checkIfPlayAgain()
{
Console.WriteLine("Would you like to play again? (y/n)");
string playAgain = Console.ReadLine();
if (playAgain == "n")
{
Environment.Exit(1);
}
}
}
And here is the code for WordList.cs
using System;
using System.Collections.Generic;
class WordList
{
List <string> words = new List<string>();
public void ListOfWords()
{
words.Add("test"); // Contains: test
words.Add("dog"); // Contains: test, dog
words.Insert(1, "shit"); // Contains: test, shit, dog
words.Sort();
foreach (string word in words) // Display for verification
{
Console.WriteLine(word);
}
}
public void AddWord(string value){
words.Add(value);
}
}
I have made some changes to your code. The code works now but is far from perfect.
Your solution has two files Program.cs and Wordlist.cs, which looks like this
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
public class Hangman
{
/*
* Some notes on your code:
* use naming convention for methods and fields, i.e. methods names start with a capital letter
* use modifiers for methods, i.e private, public, protected in your method declarations
* make variables private if you use them on several methods
* and finally: read a book on c#
*
*/
private static WordList words;
private static Random randomGen = new Random();
public static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Title = "C# Hangman";
Console.WriteLine("Welcome To C# Hangman!");
initializeWordList();
//MENU
int MenuChoice = 0;
while (MenuChoice != 4)
{
Console.Write("\n\t1) Add words");
Console.Write("\n\t2) Show list of words");
Console.Write("\n\t3) Play");
Console.Write("\n\t4) Quit\n\n");
Console.Write("\n\tChoose 1-4: "); //Choose meny item
MenuChoice = Convert.ToInt32(Console.ReadLine());
switch (MenuChoice)
{
case 1:
Console.Write("\n\tAdd a word\n\n");
var insert = Console.ReadLine();
words.Add(insert);
Console.Write("\n\tList of words\n\n");
foreach (string w in words) // Display for verification
Console.WriteLine(w);
break;
case 2:
Console.Write("\n\tList of words\n\n");
foreach (string w in words) // Display for verification
Console.WriteLine(w);
break;
case 3: //Running game
int numGuessesInt = -1;
while (numGuessesInt == -1)
{
/* Sets the number of guesses the user has to guess the word*/
pickNumGuesses(ref numGuessesInt);
}
/* Randomly picks a word*/
string word = PickWord();
/* Creates a list of characters that will show */
List<char> guessedLetters = new List<char>();
bool solved = false;
while (solved == false)
{
/* Displaying a string to the user based on the user's correct guesses.
* If nothing is correct string will return "_ _ _ " */
string wordToDisplay = displayWord(guessedLetters, word);
/* If the string returned contains the "_" character, all the
* correct letters have not been guessed, so checking if user
* has lost, by checking if numGuessesLeft is less than 1.*/
if (!wordToDisplay.Contains("_"))
{
solved = true;
Console.WriteLine("You Win! The word was " + word);
/* Check if the user wants to play again. If they do,
* then solved is set to true, will end the loop,
* otherwise, checkIfPlayAgain will close the program.*/
checkIfPlayAgain();
}
else if (numGuessesInt <= 0)
{
solved = true;
Console.WriteLine("You Lose! The word was " + word);
checkIfPlayAgain();
}
else
{
/* If the user has not won or lost, call guessLetter,
* display the word, minus guesses by 1*/
guessLetter(guessedLetters, word, wordToDisplay, ref numGuessesInt);
}
}
break;
case 4:
Console.WriteLine("\n\tEnd game?\n\n");
break;
default:
Console.WriteLine("Sorry, invalid selection");
break;
}
}
}
private static void initializeWordList()
{
words = new WordList();
words.Add("test"); // Contains: test
words.Add("dog"); // Contains: test, dog
words.Insert(1, "shit"); // Contains: test, shit, dog
words.Sort();
}
// ****** PICK NUMBER OF GUESSES ******
private static void pickNumGuesses(ref int numGuessesInt)
{
string numGuessesString = "";
Console.WriteLine("Pick a number of guesses");
numGuessesString = Console.ReadLine();
try
{
numGuessesInt = Convert.ToInt32(numGuessesString);
if (!(numGuessesInt <= 20 & numGuessesInt >= 1))
{
throw new Exception();
}
}
catch (Exception)
{
numGuessesInt = -1;
Console.WriteLine("Error: Invalid Number of Guesses");
}
}
//PICK WORD
private static string PickWord()
{
return words[randomGen.Next(0, words.Count() - 1)];
}
// ****** Display word ******
private static string displayWord(List<char> guessedCharacters, string word)
{
string returnedWord = "";
if (guessedCharacters.Count == 0)
{
foreach (char letter in word)
{
returnedWord += "_ ";
}
return returnedWord;
}
foreach (char letter in word)
{
bool letterMatch = false;
foreach (char character in guessedCharacters)
{
if (character == letter)
{
returnedWord += character + " ";
letterMatch = true;
break;
}
else
{
letterMatch = false;
}
}
if (letterMatch == false)
{
returnedWord += "_ ";
}
}
return returnedWord;
}
// ****** Guess letter ******
static void guessLetter(List<char> guessedCharacters, string word, string wordToDisplay, ref int numGuessesLeft)
{
string letters = "";
foreach (char letter in guessedCharacters)
{
letters += " " + letter;
}
Console.WriteLine("Guess a letter");
Console.WriteLine("Guessed Letters: " + letters);
Console.WriteLine("Guesses Left: " + numGuessesLeft);
Console.WriteLine(wordToDisplay);
string guess = Console.ReadLine();
char guessedLetter = 'a';
try
{
guessedLetter = Convert.ToChar(guess);
if (!Char.IsLetter(guessedLetter))
{
throw new Exception();
}
}
catch (Exception)
{
Console.WriteLine("Error: Invalid Letter Choice");
//guessLetter(guessedCharacters, word, wordToDisplay, ref numGuessesLeft);
}
bool repeat = false;
for (int i = 0; i < guessedCharacters.Count; i++)
{
if (guessedCharacters[i] == guessedLetter)
{
Console.WriteLine("Error: Invalid Letter Choice");
repeat = true;
//guessLetter(guessedCharacters, word, wordToDisplay, ref numGuessesLeft);
}
}
if (repeat == false)
{
guessedCharacters.Add(guessedLetter);
numGuessesLeft -= 1;
}
}
// ****** Check to see if player wants to play again. ******
static void checkIfPlayAgain()
{
Console.WriteLine("Would you like to play again? (y/n)");
string playAgain = Console.ReadLine();
if (playAgain == "n")
{
Environment.Exit(1);
}
}
}
Wordlist.cs
using System;
using System.Collections.Generic;
public class WordList : List<string>
{
}
Here is the very simple solution:
Populate your list just one time, or when ever you add any word, call this method. Code:
private void PopulateTheWordList()
{
Console.Write("\n\tAdd a word\n\n");
WordList.Add(Console.ReadLine());
}
Now just call this method to get random words:
private string PickWord()
{
Random ran = new Random();
return WordList[ran.Next(0, WordList.Count)];
}
If you need to create List of words in another class, then use keyword static:
static List<string> WordList = new List<string>();
Now you can call it by just writing the class name, like YourClassName.WordList
Try creating a Static Class and add a method for returning your List, you will then be able to access your wordlist.
example:
static class WordList
{
static List<string> words = new List<string>();
public static void ListOfWords()
{
words.Add("test"); // Contains: test
words.Add("dog"); // Contains: test, dog
words.Insert(1, "shit"); // Contains: test, shit, dog
words.Sort();
foreach (string word in words) // Display for verification
{
Console.WriteLine(word);
}
}
public static List<string> GetWords()
{
return words;
}
public static void AddWord(string value)
{
words.Add(value);
}
}
You would then change your switch statement to look something like this.
switch (MenuChoice)
{
case 1:
Console.Write("\n\tAdd a word\n\n");
var insert = Console.ReadLine();
WordList.AddWord(insert);
Console.Write("\n\tList of words\n\n");
WordList.ListOfWords();
break;
case 2:
Console.Write("\n\tList of words\n\n");
WordList.ListOfWords();
break;
....
and your pickWord Method would look like this:
static string pickWord()
{
string returnword = "";
Random randomGen = new Random();
returnword = WordList.GetWords()[randomGen.Next(0, WordList.GetWords().Count() - 1)];
return returnword;
}
I modified your Wordlist class so that it can use a file to maintain your Words between uses of your program, just incase that is what was being asked of you.
static class WordList
{
static string filePath = #"C:\temp\Word.txt";
static List<string> words = new List<string>();
private static void CheckFile()
{
//Makes sure our base words are saved to the file
if (!File.Exists(#"C:\temp\Word.txt"))
{
using (TextWriter writer = new StreamWriter(filePath))
{
writer.WriteLine("test");
writer.WriteLine("dog");
writer.WriteLine("shit");
}
}
}
public static void ListOfWords()
{
CheckFile();
words.Clear();
using (TextReader file = new StreamReader(filePath))
{
char[] delineators = new char[] { '\r', '\n' };
string[] tempWords = file.ReadToEnd().Split(delineators, StringSplitOptions.RemoveEmptyEntries);
foreach (string line in tempWords)
{
words.Add(line);
}
}
foreach (string word in words) // Display for verification
{
Console.WriteLine(word);
}
}
public static List<string> GetWords()
{
return words;
}
public static void AddWord(string value)
{
CheckFile();
using (TextWriter writer = new StreamWriter(filePath,true ))
{
writer.WriteLine(value);
}
}
}
I'm working on a code-editor and I want to call the string line into a keyargs event which is inside another void-returning method.
Output should occur when I type enter key, and then the selected-list from ComboBox should append to text held in RichTextBox.
Now to fulfill that, I'd like to ask you, how to call this method:
void Parse()
{
String inputLanguage =
"using System;\n" + "\n" +
"public class Stuff : Form { \n" +
" public static void Main(String args) {\n" +
"\n" + "\n" +
" }\n" +
"}\n";
// Foreach line in input,
// identify key words and format them when adding to the rich text box.
Regex r = new Regex("\\n");
String[] lines = r.Split(inputLanguage);
foreach (string l in lines)
{
ParseLine(l);
}
}
void ParseLine(string line)
{
Regex r = new Regex("([ \\t{}();])");
String[] tokens = r.Split(line);
foreach (string token in tokens)
{
// Set the token's default color and font.
rtb.SelectionColor = Color.Black;
rtb.SelectionFont = new Font("Courier New", 10, FontStyle.Regular);
// Check for a comment.
if (token == "//" || token.StartsWith("//"))
{
// Find the start of the comment and then extract the whole comment.
int index = line.IndexOf("//");
rtb.SelectedText = comment;
break;
}
// Check whether the token is a keyword.
var keywordsDef = new KeyWord();
String[] keywords = keywordsDef.keywords;
for (int i = 0; i < keywords.Length; i++)
{
if (keywords[i] == token)
{
// Apply alternative color and font to highlight keyword.
HighlighType.keywordsType(rtb);
break;
}
}
rtb.SelectedText = token;
}
rtb.SelectedText = "\n";
}
from within this one:
void lb_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
lb.Visible = false;
lb.Items.Clear();
}
if (e.KeyCode == Keys.Enter)
{
//ParseLine(string line);
Parse();
string comment = line.Substring(index, line.Length - index);
rtb.SelectedText = comment + " " + lb.SelectedIndex.ToString();
}
}
I really need help. Big thanks in advance!
You are passing the parameter wrong. You can not pass a type when calling a method. The commented line should read
ParseLine(line);
The variable line must be declared somewhere above ParseLine. What it contains is up to you, but probably you want to set
string line = lb.Text;
So your code could read like this:
void lb_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
lb.Visible = false;
lb.Items.Clear();
}
if (e.KeyCode == Keys.Enter)
{
string line = lb.Text;
ParseLine(line);
//Parse();
string comment = line.Substring(index, line.Length - index);
rtb.SelectionColor = Color.Green;
rtb.SelectionFont = new Font("Courier New", 10, FontStyle.Italic);
rtb.SelectedText = comment + " " + lb.SelectedIndex.ToString();
}
}
Calling the function is not the problem, but you need some way of retrieving the current line in whichever editor you're using. Once you have retrieved it, you can call ParseLine on it, but until you have it you have nothing to work on.