I'm sure I'm making this much harder than it really is, but my brain hurts....
I have a program that reads a list of baseball teams from a .txt file (called Teams) and displays them into a listbox. I have another .txt file (called WorldSeries) that displays, in order, which teams have won the World Series. The WorldSeries file looks like this (minus the bullet points):
Boston Americans
New York Giants
Chicago White Sox
Chicago Cubs
Chicago Cubs
Pittsburgh Pirates
...
Meaning The Boston Americans won the first World Series, Giants second, White Sox third, etc. The dates range from 1903-2009 accordingly, however there was no World Series in 1904 or 1994.
The program allows you to select a team from the list box, click a button, and a MessageBox tells you how many times that team has won the World Series. I thought it sounded pretty easy, but I'm having a harder time than I thought I would. I think I'm doing it way wrong. Can someone please help. Thanks!
private int numberWins(int[] iArray) // Method to count total number of wins
{
int totalWins = 0; // Accumulator, intitialized to 0.
// Step through the array, adding each element to the Accumulator
for (int index = 0; index < iArray.Length; index++)
{
totalWins += iArray[index];
}
// Return the number of wins
return numberWins;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void worldSeriesApp_Load(object sender, EventArgs e)
{
try
{
string teamName; // To hold the teams name
StreamReader inputTeams; // For file input
inputTeams = File.OpenText("Teams.txt"); // Open the file for team list
teamList.Items.Clear(); // Clear all items currently in the List Box
while (!inputTeams.EndOfStream) // While not at the end of the list of teams
{
teamName = inputTeams.ReadLine(); // Read each line
teamList.Items.Add(teamName); // Add each line to the List Box
}
inputTeams.Close(); // Close the file
}
catch (Exception ex) // Catch any errors
{
MessageBox.Show(ex.Message); // Let the user know there is an error
}
}
private void button1_Click(object sender, EventArgs e)
{
// Local variables
const int SEASONS = 104; // Number of Seasons between 1903 and 2009
int[] championships = new int [SEASONS]; // Array of seasons
StreamReader champFile; // For file input
int index = 0; // Loop counter
string selectedTeam; // Team selected from the List Box
// Open the file and get StreamReader object
champFile = File.OpenText("WorldSeries.txt");
//Read the files contents into the array
while (index < championships.Length && !champFile.EndOfStream)
{
championships[index] = int.Parse(champFile.ReadLine());
index ++;
}
if (teamList.SelectedIndex !=-1) // If a team is selected
{
//Get the selected item.
selectedTeam = teamList.SelectedItem.ToString();
// Determine if the team is in the array.
if (numberWins(champFile, selectedTeam) != 1) // If so...
{
// Display how many times that team has won the World Series
MessageBox.Show("The " + selectedTeam + "has won the World Series"
numberWins + "times!");
}
else // Otherwise..
{
// Let them know their team sucks.
MessageBox.Show("The " + selectedTeam + "sucks because they have never
won the World Series!");
}
Several problems with your code:
The statement if (numberWins(champFile, selectedTeam) != 1) is going to fail if a team won 2, 3, 4, etc. world series. This should be >= 1.
The "numberWins" function takes a list of integers, but you are calling it with two arguments, which will fail compilation.
The "numberWins" function itself doesn't make any sense, you are accumulating every value in the array, not checking for a team name at all. It should look like:
int numberWins(string teamName, string[] allChampions)
{
int numTitles = 0;
for (int i = 0; i < allChampions.Length; i++)
{
if (allChampions[i] == teamName)
numTitles++;
}
return numTitles;
}
The while loop to read the file doesn't make any sense, you are reading a list of team names into an integer array! Make championships a string array, and change your loop to:
while (index < championships.Length && !champFile.EndOfStream)
{
championships[index] = champFile.ReadLine();
index ++;
}
then call numberWins as numberWins(selectedTeam, championships);
Fixing those should get you closer. I will edit if I notice anything else.
Related
A part of my program has the user save their record within a text document along with their name. The way the program works is that if the user submits their own name, their previous record would be brought back and reinserted into the game. For example, if my name was Justin, I would enter "Justin" into the textbox and the program looks through the textfile, and if it finds someone named Justin, then it would look at the next three lines of data and assign those lines to playerwins, computerwins, and ties respectively. However, any examples I could find either dealt with adding those specific numbers up. I was hoping someone here could point me in the right direction with how I am supposed to structure this code.
private void FileReader(string playername, int playerwins, int computerwins, int ties)
{
StreamReader outputfile;
outputfile = File.OpenText("Records.txt");
while (!outputfile.EndOfStream)
{
if (string.Compare(playername, outputfile.ReadLine()) == 0)
{
//ReadLine() and variable assigning code goes here
}
}
}
If the text file is small enough (and most of them are), I prefer to read and write all of the file's lines at once, rather than one at a time. This separates "file handling" from "score updating", rather than having them intertwined. Resources are also cleaned up automatically (with StreamReader, you have to remember to use 'using' or 'Dispose()' to ensure that resources like file handles are properly released after an error occurs).
For the code below, if playername is already in the file, then playername's scores are updated. Otherwise, playername and playername's scores are added to the end of the file. If the file does not already exist, a new file is created.
The last suggestion would be to rename the method, to something like UpdateOrInsertPlayerScores().
private void FileReader(string playername, int playerwins, int computerwins, int ties)
{
string filename = "Records.txt";
string[] lines;
// read entire text file (if any) into lines
if (File.Exists(filename))
{
lines = File.ReadAllLines(filename);
}
else
{
lines = new string[0];
}
// find playername's line (or -1 if not present)
int p = -1;
for (int i = 0; i < lines.Length; ++i)
{
if (lines[i] == playername)
{
p = i;
break;
}
}
// update (or insert) playername's scores in lines[]
if (p == -1)
{
// playername does not have scores yet; append 4 new lines representing playername's scores
List<string> newLines = new List<string>(); // copy lines[] to a List<> so we can add new lines to it
newLines.AddRange(lines);
newLines.Add(playername);
newLines.Add(playerwins.ToString());
newLines.Add(computerwins.ToString());
newLines.Add(ties.ToString());
lines = newLines.ToArray(); // copy expanded List<> back to lines[]
}
else
{
// update the 3 lines after playername's line with playername's updated scores
// verify that the 3 lines to be updated are present after the playername's line P
if ((p + 3) > (lines.Length - 1))
{
throw new Exception("Player scores file is not in the expected format.");
}
// update the 3 lines in place
lines[p + 1] = playerwins.ToString();
lines[p + 2] = computerwins.ToString();
lines[p + 3] = ties.ToString();
}
// re-write entire text file (with updated lines)
File.WriteAllLines(filename, lines);
}
I'm working on something for school, just a basic score calculator. I know it's not the prettiest code, but it works and that's more what the class is focused on here at the beginning.
The only issue that I have is that whenever I click "Display" it prints out 20 0s. 20 is the length of the array. Everything else is working. It adds the number I input into the array and replaces the 0s. But I don't want it to have 0s at all unless I specifically type them in.
Any help is appreciated.
Full code:
// Creates the list that displays the score
List<string> scoreList = new List<string>();
// Array to store up to 20 scores
int[] scoreArray = new int[20];
// class level variable to store current open slot in the array
int openSlot = 0;
public Form1()
{
InitializeComponent();
}
// Initializes variables that hold our math total and count of numbers entered
int total = 0;
int count = 0;
private void btnExit_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void btnAdd_Click(object sender, System.EventArgs e)
{
if (openSlot <= scoreArray.GetUpperBound(0))
{
try
{
// Basic math for conversion of entered number and calculating total numbers entered
// and the averages of those numbers
int score = Convert.ToInt32(txtScore.Text);
total += score;
count += 1;
int average = total / count;
txtScoreTotal.Text = total.ToString();
txtScoreCount.Text = count.ToString();
txtAverage.Text = average.ToString();
txtScore.Focus();
}
catch(System.FormatException) // Makes sure that the user enters valid character into box
{
MessageBox.Show("Please enter valid number into box");
return;
}
// Adds the most recent entered number to the Score List
scoreList.Add(txtScore.Text);
}
// if statement to make sure that there is still room in the array to store the
// new entry
if (openSlot > scoreArray.GetUpperBound(0)) // GetUpperBound(0) returns the index of the last element in the first dimension
{
MessageBox.Show("The array is full! The most recent number was not added.");
txtScore.SelectAll();
txtScore.Focus();
return;
}
// Assigns a variable as an integer from the score text box
// to allow us to numerically sort the numbers in the scoreArray
int scoreParse = Int32.Parse(txtScore.Text);
// move the most recent number to the current open slot in the score array
scoreArray[openSlot] = scoreParse;
// add 1 to openSlot
openSlot += 1;
}
private void btnClear_Click(object sender, EventArgs e)
{
// Clears all input fields and resets variables to 0
openSlot = 0;
total = 0;
count = 0;
txtScore.Text = "";
txtScoreTotal.Text = "";
txtScoreCount.Text = "";
txtAverage.Text = "";
txtScore.Focus();
// Clears the array and list
int[] clearScoreArray = new int[20];
scoreArray = clearScoreArray;
List<string> clearScoreList = new List<string>();
scoreList = clearScoreList;
}
private void btnDisplay_Click(object sender, EventArgs e)
{
// If array has no stored values, display a MessageBox that informs user
if (scoreArray == null || scoreArray.Length == 0)
{
MessageBox.Show("There are no numbers to display");
return;
}
//move focus to the code textbox
txtScore.Focus();
// Creates a blank string variable named scr to input the scores into
// for the MessageBox
string scr = "";
foreach (var scoreAdded in scoreArray)
{
// Adds variable scr as the string to display
scr += scoreAdded + "\n";
}
// Sorts the array from lowest to highest number
Array.Sort(scoreArray);
// Displays a message box with the scores that were added
MessageBox.Show(scr);
}
}
When you declare a set amount of arrays (In your place it's 20), they get some sort of value, normally it's 0. Keep this is mind when you are using myArrayHere.length, cause it will check how many arrays have been declared (int[] array) and initialized (... = new array[]), not how many have been modified (given a value).
Best solution, IMO is creating a function, that would know how many elements in an array you will need or how many of them you are using (just have a function, that returns the amount of used variables, by checking it with a loop, and then modifying it later on... but that is one way to fix this issue, and there are better fixes than these I pointed out, but as I see you are newer to C# (prob.) fixing your issues with ok'ish code is good, as your first project should be meant for learning, later on, you can improve it and heck, if you are looking to become a pro, take some Programming courses on how to improve your code in general).
Good luck!
-Normantas
You can use nullable if you dont want zero as default value.
int? [] array = new int?[20] ;
Welcome to SO!
When an object is initially declared there is a default initialization value. In this case, 0 is the default value for an int in C#. The default value can often be changed at the initialization of the object if there's a supporting constructor.
When the int[] scoreArray = new int[20]; is declared, all 20 variables are assigned the value of 0. This is because C# does not allow uninitialized variables.
This link shows all default initialization values for C#.
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/default-values-table
I have a List structured as:
"Then a sentence woop", 340
"and another one", 256
"in order they appear", 700
"in a linked file", 304
The list contains the highest scored sentence from each paragraph of a text file. I need to output each sentence, but with a track bar reduce how many are shown.
So remove the sentence with the lowest score, the issue is the list is ordered by the sentences appearance in the original text, and the output needs to be in this order. So if I had a track bar for the above list, it would have 4 points. If I move it to point 3 sentence 2 would vanish, point 2 sentence 2 and 4 would vanish.
The code where the list is generated is:
public List<ScoredSentence> buildSummarySentenceList()
{
List<ScoredSentence> ultimateScoreslist = new List<ScoredSentence>();
scoreCoord2 = -1;
for (int x1 = 0; x1 < results.Length; x1++)
{
List<ScoredSentence> paragraphsScorelist = new List<ScoredSentence>();
for (int x2 = 0; x2 < results[x1].Length; x2++)
{
scoreCoord2++;
paragraphsScorelist.Add(new ScoredSentence(results[x1][x2], intersectionSentenceScores[scoreCoord2]));
}
var maxValue = paragraphsScorelist.Max(s => s.score);
string topSentence = paragraphsScorelist.First(s => s.score == maxValue).sentence;
int topScore = paragraphsScorelist.First(s => s.score == maxValue).score;
ultimateScoreslist.Add(new ScoredSentence(topSentence, topScore));
}
return ultimateScoreslist;
}
public class ScoredSentence
{
public string sentence { get; set; }
public int score { get; set; }
public ScoredSentence(string sentence, int score)
{
this.sentence = sentence;
this.score = score;
}
}
This code loops though a jagged array and a list of sentence to sentence scores, it results in a list as shown at the top.
Currently I output every sentence, and set the trackbar to be as long as there are sentences:
protected void summaryOutput()
{
List<ScoredSentence> ultimateScoreslist = buildSummarySentenceList();
trackBSummaryPercent.Maximum = ultimateScoreslist.Count;
lblNoOfLines.Text += trackBSummaryPercent.Maximum.ToString();
//make 2 lists for the reduction????
for (var x = 0; x < ultimateScoreslist.Count; x++)
{
TextboxSummary.Text += ultimateScoreslist[x].sentence + "\n";
}
}
I have thought of on every onchange tick of the trackbar to have a second clone list and remove the lowest value entry. Then when the bar is moved up to somehow move the missing entries back from the clone list. I don't like this method as it may cause program speed issues when for example my current test text is 100 paragraphs long, and moving the trackbar a lot may make it become slow.
Add a displayed property to your ScoredSentence object. Then whenever the list changes, or the track bar selection changes, run this method on it to update the set of displayed elements. The main list should always be sorted in the order you want it displayed. numberToDisplay would be calculated by whatever means you are using to go from your UI to the number of items.
public void OnUpdate()
{
var orderedEnumerable = ScoresList.OrderByDescending (s => s.Score);
foreach (var s in orderedEnumerable.Take (numberToDisplay))
{
s.Displayed = true;
}
foreach (var s in orderedEnumerable.Skip(numberToDisplay))
{
s.Displayed = false;
}
}
Then use the following instead of the list whenever you need to display it
ScoredSentences.Where(s=> s.Displayed);
I'll start by saying I am very much a beginner programmer, this is essentially my first real project outside of using learning material. I've been making a 'Simon Says' style game (the game where you repeat the pattern generated by the computer) using C# and XNA, the actual game is complete and working fine but while creating it, I wanted to also create a 'top 10' scoreboard. The scoreboard would record player name, level (how many 'rounds' they've completed) and combo (how many buttons presses they got correct), the scoreboard would then be sorted by combo score. This led me to XML, the first time using it, and I eventually got to the point of having an XML file that recorded the top 10 scores. The XML file is managed within a scoreboard class, which is also responsible for adding new scores and sorting scores. Which gets me to the point... I'd like some feedback on the way I've gone about sorting the score list and how I could have done it better, I have no other way to gain feedback =(. I know .NET features Array.Sort() but I wasn't too sure of how to use it as it's not just a single array that needs to be sorted. When a new score needs to be entered into the scoreboard, the player name and level also have to be added. These are stored within an 'array of arrays' (10 = for 'top 10' scores)
scoreboardComboData = new int[10]; // Combo
scoreboardTextData = new string[2][];
scoreboardTextData[0] = new string[10]; // Name
scoreboardTextData[1] = new string[10]; // Level as string
The scoreboard class works as follows:
- Checks to see if 'scoreboard.xml' exists, if not it creates it
- Initialises above arrays and adds any player data from scoreboard.xml, from previous run
- when AddScore(name, level, combo) is called the sort begins
- Another method can also be called that populates the XML file with above array data
The sort checks to see if the new score (combo) is less than or equal to any recorded scores within the scoreboardComboData array (if it's greater than a score, it moves onto the next element). If so, it moves all scores below the score it is less than or equal to down one element, essentially removing the last score and then places the new score within the element below the score it is less than or equal to. If the score is greater than all recorded scores, it moves all scores down one and inserts the new score within the first element. If it's the only score, it simply adds it to the first element. When a new score is added, the Name and Level data is also added to their relevant arrays, in the same way. What a tongue twister. Below is the AddScore method, I've added comments in the hope that it makes things clearer O_o. You can get the actual source file HERE. Below the method is an example of the quickest way to add a score to follow through with a debugger.
public static void AddScore(string name, string level, int combo)
{
// If the scoreboard has not yet been filled, this adds another 'active'
// array element each time a new score is added. The actual array size is
// defined within PopulateScoreBoard() (set to 10 - for 'top 10'
if (totalScores < scoreboardComboData.Length)
totalScores++;
// Does the scoreboard even need sorting?
if (totalScores > 1)
{
for (int i = totalScores - 1; i > - 1; i--)
{
// Check to see if score (combo) is greater than score stored in
// array
if (combo > scoreboardComboData[i] && i != 0)
{
// If so continue to next element
continue;
}
// Check to see if score (combo) is less or equal to element 'i'
// score && that the element is not the last in the
// array, if so the score does not need to be added to the scoreboard
else if (combo <= scoreboardComboData[i] && i != scoreboardComboData.Length - 1)
{
// If the score is lower than element 'i' and greater than the last
// element within the array, it needs to be added to the scoreboard. This is achieved
// by moving each element under element 'i' down an element. The new score is then inserted
// into the array under element 'i'
for (int j = totalScores - 1; j > i; j--)
{
// Name and level data are moved down in their relevant arrays
scoreboardTextData[0][j] = scoreboardTextData[0][j - 1];
scoreboardTextData[1][j] = scoreboardTextData[1][j - 1];
// Score (combo) data is moved down in relevant array
scoreboardComboData[j] = scoreboardComboData[j - 1];
}
// The new Name, level and score (combo) data is inserted into the relevant array under element 'i'
scoreboardTextData[0][i + 1] = name;
scoreboardTextData[1][i + 1] = level;
scoreboardComboData[i + 1] = combo;
break;
}
// If the method gets the this point, it means that the score is greater than all scores within
// the array and therefore cannot be added in the above way. As it is not less than any score within
// the array.
else if (i == 0)
{
// All Names, levels and scores are moved down within their relevant arrays
for (int j = totalScores - 1; j != 0; j--)
{
scoreboardTextData[0][j] = scoreboardTextData[0][j - 1];
scoreboardTextData[1][j] = scoreboardTextData[1][j - 1];
scoreboardComboData[j] = scoreboardComboData[j - 1];
}
// The new number 1 top name, level and score, are added into the first element
// within each of their relevant arrays.
scoreboardTextData[0][0] = name;
scoreboardTextData[1][0] = level;
scoreboardComboData[0] = combo;
break;
}
// If the methods get to this point, the combo score is not high enough
// to be on the top10 score list and therefore needs to break
break;
}
}
// As totalScores < 1, the current score is the first to be added. Therefore no checks need to be made
// and the Name, Level and combo data can be entered directly into the first element of their relevant
// array.
else
{
scoreboardTextData[0][0] = name;
scoreboardTextData[1][0] = level;
scoreboardComboData[0] = combo;
}
}
}
Example for adding score:
private static void Initialize()
{
scoreboardDoc = new XmlDocument();
if (!File.Exists("Scoreboard.xml"))
GenerateXML("Scoreboard.xml");
PopulateScoreBoard("Scoreboard.xml");
// ADD TEST SCORES HERE!
AddScore("EXAMPLE", "10", 100);
AddScore("EXAMPLE2", "24", 999);
PopulateXML("Scoreboard.xml");
}
In it's current state the source file is just used for testing, initialize is called within main and PopulateScoreBoard handles the majority of other initialising, so nothing else is needed, except to add a test score.
I thank you for your time!
As a programming tip, you should replace the 3 different arrays with a single array of Score Objects, each of these objects would have all 3 properties you mentioned (Name, Level and Score). You can then create a comparator-like function (based on the score attribute) for it which can be used to sort it via the Arrays.Sort() method.
If you want to keep you current 3 arrays then you can look up sorting algorithms here: http://en.wikipedia.org/wiki/Sorting_algorithm#Comparison_of_algorithms and just make it so that any changes are done to the 3 arrays simultaneously instead of one by one (since you keep the data in them synchronized by index).
You can consider placing your user name, level, and score in a separate class (let's call it ScoreBoardEntry) that inherits from IComparable and then write a comparator. This will allow you to use the built in sort function of List and will be a neater solution than just writing your own.
Your code should look something like this:
1) Create/load a List < ScoreBoardEntry> with all the previous scores
2) push the new score into it
3) Sort List
4) Print only first 10 entries
I wrote some code for you. I've been learning some new-ish type C# stuff like linq, IEnumerable, and yield and kind of wanted to put them together, so hopefully it helps.
you might like the snippet editor i used - linqpad - it's good for learning/experimenting
void Main()
{
string [] playerNames = {"Aaron", "Rick", "Josie"};
var sim = new GameSimulator(playerNames, 1000000, 5000);
var scores = sim.GetScores(5);
var sorted = scores.OrderBy(c=>c.Score).Reverse();
var top = sorted.Take(2);
// uncomment if using linq pad
//scores.Dump("scores");
//sorted.Dump("sorted");
//top.Dump("top");
}
class GameSimulator
{
public GameSimulator(string [] playerNames, int maxScore, int maxLevel)
{
this.playerNames = playerNames;
this.maxScore = maxScore;
this.maxLevel = maxLevel;
}
private string [] playerNames;
private int maxScore;
private int maxLevel;
public IEnumerable<ScoreBoardEntry> GetScores(int numGames)
{
for (int i = 0; i < numGames; i++)
{
string randomName = playerNames[random.Next(0, playerNames.Length-1)];
int randomScore = random.Next(1, maxScore);
int randomLevel = random.Next(1, maxLevel);
yield return new ScoreBoardEntry(randomName, randomLevel, randomScore);
}
}
static private Random random = new Random();
}
class ScoreBoardEntry
{
public ScoreBoardEntry(string playerName, int levenNumber, int score)
{
PlayerName = playerName;
LevelNumber = levenNumber;
Score = score;
}
public string PlayerName { set; get; }
public int LevelNumber { set; get; }
public int Score { set; get; }
}
what i have is a textbox into which the user types a string. The string will look something like this:
G32:04:20:40
Then the user hits the search button. The program must then open a textfile and search for the "nearest" five strings to the one they entered and display them in a listbox.
I'll define "nearest string" as much as i can (most likely using a very long complicated example).
The data contained in the text file looks like this:
G32:63:58:11 JG01
G32:86:98:30 JG01
G33:50:05:11 JG06
G33:03:84:12 JG05
G34:45:58:11 JG07
G35:45:20:41 JG01
G35:58:20:21 JG03
So if the user types in the string :
G33:89:03:20
The five closest results should display in the list box like this:
G33:50:05:11 JG06
G33:03:84:12 JG05
G32:86:98:30 JG01
G32:63:58:11 JG01
G34:45:58:11 JG07
I should probably point out at this point that strings are coordinates and the value after "JG" represents the value of something at that coordinate.
The way i got to those 5 is by going through the string piece by piece. So the user typed in "G33" so i find all those with G33 at the beginning - if there are none then i find the closest to G33. Then it was "89" so i find all those where the next part is "89" if there are none, then the closest to 89 the better and so on.
What i need to know is how do i go about doing this? I've built the visual components and i also have code in place that deals with similar kinds of things but when it comes to this i'm truely stumped. As you can probably tell by now, i'm rather new to C#, but i'm learning :)
EDIT: Search Code
private void btnSearch_Click(object sender, EventArgs e)
{
lstResult.Items.Clear();
if (txtSearch.Text == String.Empty)
{
MessageBox.Show("The textbox is empty, there is nothing to search.",
"Textbox empty", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
this.CheckFormatting();
}
}
private long GetIndexForCoord(string coord)
{
// gets out a numerical value for each coordinate to make it easier to compare
Regex m_regex = new Regex("\\d\\d:\\d\\d:\\d\\d:\\d\\d");
string cleaned = m_regex.Match(coord).Value;
cleaned = cleaned.Replace(':', '0');
return Convert.ToInt64(cleaned);
}
private List<string> GetResults(string coord)
{
// gets out the 5 closest coordinates
long index = GetIndexForCoord(coord);
// First find the 5 closest indexes to the one we're looking for
List<long> found = new List<long>();
while (found.Count < 5)
{
long closest = long.MaxValue;
long closestAbs = long.MaxValue;
foreach (long i in m_indexes)
{
if (!found.Contains(i))
{
long absIndex = Math.Abs(index - i);
if (absIndex < closestAbs)
{
closest = i;
closestAbs = absIndex;
}
}
}
if (closest != long.MaxValue)
{
found.Add(closest);
}
}
// Then use those indexes to get the coordinates from the dictionary
List<string> s = new List<string>();
foreach (long i in found)
{
s.Add(m_dic[i]);
}
return s;
}
private void CheckFormatting()
{
StringReader objReader = new StringReader(txtSearch.Text);
bool FlagCheck = true;
if (!Regex.IsMatch(txtSearch.Text,
"G3[0-9]{1}:[0-9]{2}:[0-9]{2}:[0-9]{2}"))
{
FlagCheck = false;
}
if (FlagCheck == true)
{
this.CheckAndPopulate();
}
else
{
MessageBox.Show("Your search coordinates are not formatted correctly.",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void CheckAndPopulate()
{
StreamReader objReader = new StreamReader("Jumpgate List.JG");
List<String> v = new List<String>();
do
{
v.Add(objReader.ReadLine());
}
while (objReader.Peek() != -1);
objReader.Close();
foreach (string c in v)
{
long index = GetIndexForCoord(c);
m_dic.Add(index, c);
m_indexes.Add(index);
}
List<string> results = GetResults(txtSearch.Text);
foreach (string c in results)
{
lstResult.Items.Add(c);
}
}
Edit: Added suggested code to read in the file. And also an explanation at the end.
Edit2: Added if check around the adding to the dictionary/list to handle duplicates.
Please note, this code is almost certainly quite inefficient (except possibly by accident) and would need to be cleaned up and error handling would need to be added etc, but it might give you a starting point for writing better code at least.
You probably want to take a look at k-nearest neighbor algorithm to find out the proper way of doing this.
I've assumed that the letter in the beginning is always G and that all 4 parts of the coordinates are always 2 digits each.
Add the following 2 to your class/form:
Dictionary<long, string> m_dic = new Dictionary<long, string>();
List<long> m_indexes = new List<long>();
Then initialize them with the following code (I've assumed you've already read in all the coordinates into a string array called v with one coordinate per item):
foreach (string c in v)
{
long index = GetIndexForCoord(c);
if(!m_dic.ContainsKey(index))
{
m_dic.Add(index, c);
m_indexes.Add(index);
}
}
Then add the following 2 methods:
// gets out a numerical value for each coordinate to make it easier to compare
private long GetIndexForCoord(string coord)
{
Regex m_regex = new Regex("\\d\\d:\\d\\d:\\d\\d:\\d\\d");
string cleaned = m_regex.Match(coord).Value;
cleaned = cleaned.Replace(':', '0');
return Convert.ToInt64(cleaned);
}
// gets out the 5 closest coordinates
private List<string> GetResults(string coord)
{
long index = GetIndexForCoord(coord);
// First find the 5 closest indexes to the one we're looking for
List<long> found = new List<long>();
while (found.Count < 5)
{
long closest = long.MaxValue;
long closestAbs = long.MaxValue;
foreach (long i in m_indexes)
{
if (!found.Contains(i))
{
long absIndex = Math.Abs(index - i);
if (absIndex < closestAbs)
{
closest = i;
closestAbs = absIndex;
}
}
}
if (closest != long.MaxValue)
{
found.Add(closest);
}
}
// Then use those indexes to get the coordinates from the dictionary
List<string> s = new List<string>();
foreach (long i in found)
{
s.Add(m_dic[i]);
}
return s;
}
And finally when the user enter the data you send in that data to the method as:
List<string> results = GetResults(lookingFor);
You can then use the results to populate your listbox.
The code works by converting each coordinate to a numerical value called index (since it's easier to work with) and it then adds all the coordinates to a dictionary with the index as the key.
When it's looking up the closest coordinates it compares the difference in value between the index you're looking for and each of the previously stored indexes to find the 5 closest ones (it uses the Math.Abs method so that it can get the difference without having to worry about negative numbers). It's quite inefficient since it loops through each value once for each coordinate you want to find (so if your list contains 1000 coordinates and you want to find the 5 closest, it'll go through the inner loop 5000 times, I'd assume that could probably be cut down to just 1000 times by improving the code, I suggest looking at the wiki link close to the top of this answer for a better algorithm).