Getting access to an ArrayList within an ArrayList - c#

I'm sure the first response will be 'wtf, don't use arraylists!', but I'm really just trying to get this working in any way that I can.
It's basically the 'match looker' for a match 3 game. I'm having trouble getting access to the match data within the match list. See below..
public void FindAndRemoveMatches() {
ArrayList foundMatches = LookForMatches();
//just checking if we're getting the right amount for now
Debug.Log("We found " + foundMatches.Count + " 'Match 3's");
foreach(Object el in foundMatches){
// Debug.Log(el.ToString());
}
}
ArrayList LookForMatches(){
//List<int> matchList = new List<int>();
ArrayList matchList = new ArrayList();
// search for horizontal matches
// note that we're subtracting two rows here.
// We don't need to check the last two rows because we're matching 3.
for (int i = 0; i < BOARD_WIDTH; i++){
for (int j = 0; j < BOARD_HEIGHT-2; j++){
ArrayList match = GetMatchHoriz(i,j);
if (match.Count > 2) {
matchList.Add(match);
i += match.Count-1;
}
}
}
// search for vertical matches
for (int i = 0; i < BOARD_WIDTH; i++){
for (int j = 0; j < BOARD_HEIGHT-2; j++){
ArrayList match = GetMatchVert(i,j);
if (match.Count > 2) {
matchList.Add(match);
j += match.Count-1;
}
}
}
return matchList;
}
// look for horizontal matches starting at this point
ArrayList GetMatchHoriz(int col,int row){
ArrayList match = new ArrayList();
match.Add(mBoard[col,row]);
for(int i = 1; (col+i)<8; i++) {
if (mBoard[col,row] == mBoard[col+i,row]) {
if(mBoard[col+i,row] > mPieces.GetNumPieceTypes()) match.Add(mBoard[col+i,row]);
} else {
return match;
}
}
return match;
}
// look for horizontal matches starting at this point
ArrayList GetMatchVert(int col,int row){
ArrayList match = new ArrayList();
match.Add(mBoard[col,row]);
for(int i = 1; (row+i)<8; i++) {
if (mBoard[col,row] == mBoard[col,row+i]) {
if(mBoard[col,row+i] > mPieces.GetNumPieceTypes()) match.Add(mBoard[col,row+i]);
} else {
return match;
}
}
return match;
}
Good news is, I know that it's finding the matches correctly. The number of matches it's finding using the debug log correlates with what's happening on the screen. Yay! But, I need to get access to that data so I can compare it to the board (mBoard[col,row]) and remove those objects from the game board.
The 'foreach' loop in findandremovematches gives an error about casting. Any help with this?
thanks!

I hope i understand your question correctly
foreach(Objecct obj in list)
{
ArrayList inner = obj as ArrayList;
if(inner != null)
{
//this is what you are looking for
//you can then iterate the inner array list and get the int[,] you saved
}
}
However, as suggested, use List<ArrayList> or List<List<int[,]>> if I guessed it right.

If you're using ArrayLists because you think they're easier, don't. Use List.
If you're using ArrayLists because you have to and there is no way for you to use List (which I doubt) then you'll have something like this:
foreach(ArrayList innerList in foundMatches)
{
foreach(SomeClassThatYouAddToInnerLists item in innerlist)
{
//use item;
}
}
Replace SomeClassThatYouAddToInnerLists with whatever the type is that you're adding to the inner ArrayLists. (The type of mBoard.)
If you use Lists then it's very clear what the type is of everything inside of the list. A List<List<int>> is easy to work with. It's a list of lists of numbers.

Related

C# InvalidArgument = Value of '2' is not valid for 'index'

I am new to C# and I have encountered an error stating that: InvalidArgument=Value of '2' is not valid for 'index'.
I want to set the items in checkedlistbox checked if there is a match in listbox.
Can anyone help me with this problem.
This the part of my code where the problems appear.
for (int i = 0; i < checklistbox.Items.Count; i++)
{
if (checklistbox.Items[i].ToString() == listbox.Items[i].ToString())
{
//Check only if they match!
checklistbox.SetItemChecked(i, true);
}
}
You just need to use nested for loop. Here is the code.
for (int i = 0; i < listbox.Items.Count; i++)
{
for (int j = 0; j < checkedlistbox.Items.Count; j++)
{
if (listbox.Items[i].ToString() == checkedlistbox.Items[j].ToString())
{
//Check only if they match!
checkedlistbox.SetItemChecked(i, true);
}
}
}
The reason you are getting this error is because you are looping through the count of checklistbox items. So, for example if there are 3 items in that array, and listbox only has 2 items, then on the third loop (when i = 2), you are trying to reference an item in the listbox array that does not exist.
Another way to do it would be like this:
foreach (var item in listbox.Items)
{
if (Array.Exists(checklistbox.Items, lbitem => lbitem.ToString() == item.ToString()))
{
//They match!
checklistbox[item].MarkAsChecked()
}
}
Update: answer updated to add MarkAsChecked() and loop through user inputted values held within checklist array.

Implementing an intersection merge of 2 array lists

For homework assignment we have to program a Intersection merge of 2 ArrayLists. I have done it using the following code
public void Intersection()
{
foreach (object obj1 in Developed)
{
Apps xApp = (Apps)obj1;
foreach (object obj2 in DPloyed)
{
Apps yApp = (Apps)obj2;
if (xApp.CompareName(yApp) == 0)
{
Inter.Add(yApp);
}
}
}
}
I would like to implement it rather using the while loop but the following code seems to keep missing elements in the list. It puts the first elements in the new intersection list but once the length of developed is increased from 1 element to 5 elements or more it does not add the new elements.
public void Intersection()
{
int i = 0;
int j = 0;
while (i < Developed.Count && j < DPloyed.Count)
{
Apps curA = (Apps)Developed[i];
Apps curB = (Apps)DPloyed[j];
if (curA.CompareName(curB) == 0)
{
Inter.Add(curA);
i++;
j++;
}
else if (curA.CompareName(curB) < 0)
{
i++;
}
else
j++;
}
}
Any help as to why the while loop does not work would be appreciated.
Thanks
Do this
while (i < Developed.Count || j < DPloyed.Count)
because may be both list may be having different Count.
and you need to put extra checks inside loop for indexes so that you don't get Index out of Range Exception.
Problem was not in the actual code for the merges. Problem found in my compare methods.

C# - Looping through pairs and matching

So I'm stuck on a program where I have a series of pairs that may or may not be able to connect together to form a complete path through the pairs. I need to be able to check if the second item in a pair can match the first item in another pair and so on until there are no pairs left. For example, my pairs might be:
(1,5)
(2,4)
(3,2)
(5,3)
(4,3)
I would need to be able to somehow iterate through the pairs and check if I can get a complete path that travels through each one, based on if the second digit of a pair matches the first digit of the next pair. In this example, the output would be: (1,5), (5,3), (3,2), (2,4), (4,3) forming a complete match. If a match can't be formed, I need to report a failure. The input is based on a text file. So far I've been able to read the file with a Streamreader and split the pairs based on newline, then iterate through and split each pair into its items based on the comma. I'm pretty much clueless on how to proceed, if anyone has some ideas I would appreciate it.
StreamReader sr = new StreamReader("inputs.txt");
string line = null;
line = sr.ReadToEnd();
var str = line.Trim().Split('\n');
int length = str.Length;
int index=1;
while (index < length)
{
var pair = str[index].Split(',');
var item1 = pair[0];
var item2 = pair[1];
}
The problem you described can be converted to another form; a graph.
Here's what it looks like for the example you gave.
I drew an arrow from 1 to 5 since there was the pair (1,5), etc.
A path on a graph like this can only go the directions of the arrows.
What you want to know is: "is there a path in this graph that uses every pair, i.e. goes over every edge?"
Such a path is known as an Eulerian Directed Path
Wikipedia lists two algorithms for finding such paths, Fleury's and Hierholzer's, both of which were discovered in the late 1800's. Hopefully, this gives you an idea of where to start in solving this problem.
First, you need to strip the parenthesis - if they are present in your input file. See string.Trim method for that.
The brute force approach:
public class Pair
{
public string First;
public string Second;
}
List<Pair> pairs = new List<Pair>();
for (int index = 0; iter < str.Length; index++)
{
var pair = str[index].Split(',');
pairs.Add(new Pair(){First = pair[0], Second = pair[1]});
}
List<Pair> ordered = new List<Pair>();
ordered.Add(pairs[0]);
pairs.RemoveAt(0);
while (pairs.Count > 0)
{
bool found = false;
for (int iter = 0; iter < pairs.Count; iter++)
{
if (ordered[ordered.Count - 1].Second == pairs[iter].First)
{
ordered.Add(pairs[iter]);
pairs.RemoveAt(iter);
found = true;
break;
}
}
if (!found)
{
<report error>
break;
}
}
Error checking is left as an exercise for the reader.
WARNING: This is not tested!!
using System;
using System.IO;
class t1{
public static void Main(String[] args)
{
StreamReader sr = new StreamReader("inputs.txt");
string line = null;
line = sr.ReadToEnd();
var str = line.Trim().Split('\n');
int length = str.Length;
int[][] arr=new int[10][];//[str.Length][2];
int index=0;
while (index < length)
{
var pair = str[index].Split(',');
var item1 = Convert.ToInt32(pair[0]);
var item2 = Convert.ToInt32(pair[1]);
arr[index]=new int[]{item1,item2};
index++;
}
for (int i=0;i<arr.Length;i++)
{
for (int j=i+1;j<arr.Length;j++)
{
if (arr[i][1] == arr[j][0])
{
//MATCH
}
else
{
//FAILURE
}
}
}
}
}

on-the-fly search algorithm in listbox

So, I have a listbox with x number of items. On top of the listbox I have a TextBox (this is the search field). I try do develop an algorithm that removes items from the listbox, if it doesn't contain the searchword (variable keyword in the code). This is supposed to happen for each key the user types (on-the-fly). So, the code:
private void _keywordTextBox_TextChanged(object sender, EventArgs e)
{
string keyword = _keywordTextBox.Text;
if (keyword == searchtext || isSpace) // do nothing if space is typed - searchtext is a templatetext in the textbox ("type here to search...")
return; // ignore
else if (keyword == "")
{
listBox.Items.Clear();
foreach (string s in originalList)
listBox.Items.Add(s);
}
else
{
List<string> selection = new List<string>();
foreach (string s in originalList) // originalList is the listbox at startup
selection.Add(s);
listBox.BeginUpdate();
string[] keywordSplit = keyword.Split(' ');
try
{
for (int i = originalList.Count - 1; i >= 0; i--)
{
string[] selectionSplit = selection[i].Split(' ');
int l = 0; // number of hits
for (int j = 0; j < selectionSplit.Length; j++)
{
for (int k = 0; k < keywordSplit.Length; k++)
{
if (selectionSplit[j].ToLower().Contains(keywordSplit[k].ToLower()))
{
l++;
break;
}
}
}
if (l < keywordSplit.Length) // Not hit on all keywords
selection.RemoveAt(i);
}
}
finally
{
listBox.Items.Clear();
foreach (string s in selection) // Add selection in listbox
listBox.Items.Add(s);
if (listBox.Items.Count > 0)
listBox.SetSelected(0, true); // Select first item in listbox
listBox.EndUpdate();
}
}
}
The problem is hard to describe, other than it doesn't work as intended. The behavour is, as far as I can see, sporadic.
If I search for "ck flow", I should get a hit for stackoverflow. More importantly, it should also work if I deletes chars (delete key of backspace). Anybody?
Edit: more details:
The listbox should shrink and grow on each keystroke, based on what the user searches for. The listbox should keep every item that matches the keyword typed in by the user, and filter away that doesn't match.
Or you could try to work out a Regular Expression:
private void textBox1_TextChanged(object sender, EventArgs e)
{
string keyword = textBox1.Text;
if (string.IsNullOrEmpty(keyword.Trim()))
{
listBox1.Items.Clear();
listBox1.Items.AddRange(_originalList.ToArray());
}
else
{
Regex regex = new Regex(GetRegexPatternFromKeyword(keyword));
List<string> selection =
_originalList.Where(s => regex.IsMatch(s)).ToList();
listBox1.Items.Clear();
listBox1.Items.AddRange(selection.ToArray());
}
}
private static string GetRegexPatternFromKeyword(string keyword)
{
string[] words =
keyword.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(word => "(?=.*" + word.Replace(")", #"\)") + ")").ToArray();
return string.Join("", words);
}
disclaimer: there could be some cases where an input would 'destroy' the regex pattern
Your code increases l too often. For instance;
the text 'aaa aaa aaa' with searchword 'aaa bbb' will give an l of 3 because every time you find 'aaa' you increase l. So this will be a match even though 'bbb' is never found.
You can fix this (among others) by deleting found parts of keywordsplit and recreating keywordsplit anew before every search of a new selectionline.
l++;
break;
becomes
l++
keywordSplit.RemoveAt[k];
break;
and move
string[] keywordSplit = keyword.Split(' ');
to just before you start the k loop
Altough I feel there might be better ways to achieve what you want with a bit cleaner and faster code it should work.
I got it to work. #IvoTops helped me in the right direction. Just loop trough all the keyword first, and then the selections.
for (int j = 0; j < keywordSplit.Length; j++)
{
for (int k = 0; k < selectionSplit.Length; k++)
{
if (selectionSplit[k].ToLower().Contains(keywordSplit[j].ToLower()))
{
l++;
break;
}
}
}
Seems to work ok now.

mergesort - with an insignificant change throws SystemInvalidOperationException

A very strange thing occured in my program. Here is the simplified code.
class Program
{
static void Main(string[] args)
{
ArrayList numbers = new ArrayList();
numbers.Add(1);
numbers.Add(3);
numbers.Add(4);
numbers.Add(2);
var it = Sorts.MergeSort((ArrayList)numbers.Clone());
Sorts.PrintArray(it, "mergesort");
Console.WriteLine("DONE");
Console.ReadLine();
}
}
public static class Sorts
{
public static ArrayList BubbleSort(ArrayList numbers)
{
bool sorted = true;
for (int i = 0; i < numbers.Count; i++)
{
for (int j = 1; j < numbers.Count; j++)
{
if ((int)numbers[j - 1] > (int)numbers[j])
{
int tmp = (int)numbers[j - 1];
numbers[j - 1] = numbers[j];
numbers[j] = tmp;
sorted = false;
}
}
if (sorted)
{
return numbers;
}
}
return numbers;
}
public static ArrayList MergeSort(ArrayList numbers, int switchLimit = 3)
{
//if I use this if - everything works
if (numbers.Count <= 1)
{
// return numbers;
}
//the moment I use this condition - it throws SystemInvalidOperationException in function Merge in the line of a "for"-loop
if (numbers.Count <=switchLimit)
{
return Sorts.BubbleSort(numbers);
}
ArrayList ret = new ArrayList();
int middle = numbers.Count / 2;
ArrayList L = numbers.GetRange(0, middle);
ArrayList R = numbers.GetRange(middle, numbers.Count - middle);
L = MergeSort(L);
R = MergeSort(R);
return Merge(L, R);
}
private static ArrayList Merge(ArrayList L, ArrayList R)
{
ArrayList ret = new ArrayList();
int l = 0;
int r = 0;
for (int i = 0; i < L.Count + R.Count; i++)
{
if (l == L.Count)
{
ret.Add(R[r++]);
}
else if (r == R.Count)
{
ret.Add(L[l++]);
}
else if ((int)L[l] < (int)R[r])
{
ret.Add(L[l++]);
}
else
{
ret.Add(R[r++]);
}
}
return ret;
}
//---------------------------------------------------------------------------------
public static void PrintArray(ArrayList arr, string txt = "", int sleep = 0)
{
Console.WriteLine("{1}({0}): ", arr.Count, txt);
for (int i = 0; i < arr.Count; i++)
{
Console.WriteLine(arr[i].ToString().PadLeft(10));
}
Console.WriteLine();
System.Threading.Thread.Sleep(sleep);
}
}
There is a problem with my Sorts.MergeSort function.
When I use it normally (take a look at the first if-condition in a function - all works perfectly.
But the moment when I want it to switch to bubblesort with smaller input (the second if-condition in the function) it throws me an SystemInvalidOperationException. I have no idea where is the problem.
Do you see it?
Thanks. :)
Remark: bubblesort itself works - so there shouldn't be a problem in that sort...
The problem is with your use of GetRange:
This method does not create copies of the elements. The new ArrayList is only a view window into the source ArrayList. However, all subsequent changes to the source ArrayList must be done through this view window ArrayList. If changes are made directly to the source ArrayList, the view window ArrayList is invalidated and any operations on it will return an InvalidOperationException.
You're creating two views onto the original ArrayList and trying to work with both of them - but when one view modifies the underlying list, the other view is effectively invalidated.
If you change the code to create copies of the sublists - or if you work directly with the original list within specified bounds - then I believe it'll work fine.
(As noted in comments, I'd also strongly recommend that you use generic collections.)
Here's a short but complete program which demonstrates the problem you're running into:
using System;
using System.Collections;
class Program
{
static void Main()
{
ArrayList list = new ArrayList();
list.Add("a");
list.Add("b");
ArrayList view1 = list.GetRange(0, 1);
ArrayList view2 = list.GetRange(1, 1);
view1[0] = "c";
Console.WriteLine(view2[0]); // Throws an exception
}
}
on this line R = MergeSort(R); you alter the range of numbers represented by L. This invalidates L. Sorry I have to go so can't explain any further now.

Categories