C# Function that searches an array and creates subgroups based on user input and a prerequisite

C# Function that searches an array and creates subgroups based on user input and a prerequisite



The project is a game called cheating hangman, where the user sets their guess limit, word length, and makes a guess. There large list of words that is read into an array, then narrowed down into a smaller array based upon the given word length. So far I have been able to take in and validate all user input but I have been trying to figure out an algorithm to make subgroups out of the narrowed down array.



What needs to be done- When the user makes a one letter guess, an algorithm that checks each word in the array to see where the letter is, and makes groups out of each "word family". For instance if an 'e' is guessed, then there would be a group made for every word with e at pos[0], one for pos[1], all the way through the end of the wordlength + 1 for words without the letter.


pos[0]


pos[1]


wordlength + 1


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CheatingHangman

class Program

static void Main(string args)

bool rightGuess = false;
int i;
int wordLength;
int guessNumber;
string guessString;
char guessChar;
char madeGuesses = new char[26];
List<string> possibleWords = new List<string>();
List<string> narrowedWords = new List<string>();

//read dictionary.txt, add its contents to an array
string text = System.IO.File.ReadAllText(@"D:WCUCurrent ClassesGame Developmentdictionary.txt");
string words = System.IO.File.ReadAllLines(@"D:WCUCurrent ClassesGame Developmentdictionary.txt");

//ask the player for character length + greeting + read, parse, and validate user input
Console.WriteLine("Welcome to [CHEATING] Hangman, please specify the word legth you would like to play with! (1 - 30)n");
wordLength = Int32.Parse(Console.ReadLine());
while(wordLength < 1

public string Narrow(int wordlength, char a, string orgwordlist, string newWordArray) //narrow the list down based on word families

newWordArray = new string[5000];
for(int i = 0; i < orgwordlist.Length; i++)

while (orgwordlist[i].Length == wordlength)




return newWordArray;








idownvotedbecau.se/nomcve Way too much code here that's unrelated to the specific question. Please clean it up so that we have what you've attempted to work with.

– gilliduck
Sep 17 '18 at 17:04





1 Answer
1



Personnaly i would use a dictionary with the word as a key and the position as a value.
Something like this :


List<String> narrowedWords = new List<String>();
char letter = 'e';
Dictionary<String, Int32> wordFamilies = new Dictionary<String, Int32>();

foreach (String word in narrowedWords.Where(obj => obj.Contains(letter)))

var index = word.IndexOf(letter);

wordFamilies.Add(word, index);



And narrowedWords.Where(obj => !obj.Contains(letter); are the others


narrowedWords.Where(obj => !obj.Contains(letter);



EDIT



You can do this


narrowedWords.Where(word => word.Contains(letter));



It is to filter out all the string that doesn't contains your selected letter.
The .Where return an IEnumarable and the foreach will then use it.



You can do it without pre filtering your list :


List<String> narrowedWords = new List<String>();
char letter = 'e';
Dictionary<String, Int32> wordFamilies = new Dictionary<String, Int32>();

foreach (String word in narrowedWords)

if (word.Contains(letter))

var index = word.IndexOf(letter);
wordFamilies.Add(word, index);




You should read about Data Structure and LINQ






excuse my ignorance, im new to c#, in (obj => obj.Contains(letter)) is obj a keyword? Or is it to be replaced with the string that is being examined? Also, wouldn't this only create two structures, one with words that contain the letter and one with words that do not?

– Nick Ward
Sep 17 '18 at 18:25







see my edit no it is not a keyword it is more like the name you give to the object being iterate on

– Maxime Desgagné
Sep 17 '18 at 18:42




Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ