c# - Need to use IEnumerable.except to compare two dimensional dice roll -
i working on homework assignment c# class , have been going crazy last 2 days trying figure out how put random dice rolls (two dice) 2 dimensional collection or array , compare them using ienumerable.except in order show roll combinations occurred in first set of rolls didn't occur in second set of rolls. here entire code includes commented out attempts have made using nested lists, multidimensional arrays, , dictionary. starting go crazy or advice appreciated. here code:
namespace newdicesimulation { public partial class form1 : form { list<dicerolllist> rolllist1 = new list<dicerolllist>(); list<dicerolllist> rolllist2 = new list<dicerolllist>(); list<int> sumlist1 = new list<int>(); list<int> sumlist2 = new list<int>(); public form1() { initializecomponent(); } private void diceroll() { //clear list new roll listview1.items.clear(); //setup random random shooter = new random(); //setup counter for loop (int = 1; <= 20; ++i) { string result; int die1, die2, die1value, die2value, total; //roll both dice die1 = shooter.next(6); die2 = shooter.next(6); //increase both dice 1 dice value die1value = die1 + 1; die2value = die2 + 1; total = die1value + die2value; //sumloop1[total] +=1; sumlist1.add(total); rolllist1.add(new dicerolllist { rollnum = i, dice1 = die1value, dice2 = die2value, dicetotal = total }); //rollloop1[die1, die2] +=1; //rollloop1[die1, die2, total] += 1; //change image each die represent number rolled // lbl_die1.imageindex = die1; // lbl_die2.imageindex = die2; //if statement determine if both dice same number if (die1value == die2value) { result = "on roll number " + i.tostring() + " both dice rolled " + die1value.tostring() + "'s"; string[] row = { i.tostring(), die1value.tostring(), die2value.tostring(), total.tostring(), result }; var listviewitem = new listviewitem(row); listview1.items.add(listviewitem); //force application update application.doevents(); } else { string[] row = { i.tostring(), die1value.tostring(), die2value.tostring(), total.tostring() }; var listviewitem = new listviewitem(row); listview1.items.add(listviewitem); //force application update application.doevents(); } //force thread sleep create pause between each loop system.threading.thread.sleep(600); } (int = 1; <= 20; ++i) { string result; int die1, die2, die1value, die2value, total; //roll both dice die1 = shooter.next(6); die2 = shooter.next(6); //increase both dice 1 dice value die1value = die1 + 1; die2value = die2 + 1; total = die1value + die2value; //sumloop2[total] += 1; rolllist2.add(new dicerolllist { rollnum = i, dice1 = die1value, dice2 = die2value, dicetotal = total }); sumlist2.add(total); //rollloop2[die1, die2] += 1; //rollloop2[i,i,i] = die1 die2 total; //change image each die represent number rolled // lbl_die1.imageindex = die1; // lbl_die2.imageindex = die2; //if statement determine if both dice same number if (die1value == die2value) { result = "on roll number " + i.tostring() + " both dice rolled " + die1value.tostring() + "'s"; string[] row = { i.tostring(), die1value.tostring(), die2value.tostring(), total.tostring(), result }; var listviewitem = new listviewitem(row); listview2.items.add(listviewitem); //force application update application.doevents(); } else { string[] row = { i.tostring(), die1value.tostring(), die2value.tostring(), total.tostring() }; var listviewitem = new listviewitem(row); listview2.items.add(listviewitem); //force application update application.doevents(); } //force thread sleep create pause between each loop system.threading.thread.sleep(600); } } public void querysearch() { string numselect = combobox1.selecteditem.tostring(); int totcount; ienumerable<int> querytotal = int total in sumlist1 total == int32.parse(numselect) select total; var count = querytotal.count(); label1.text = count.tostring() ; ienumerable<int> querytotal2 = int total in sumlist2 total == int32.parse(numselect) select total; var count2 = querytotal2.count(); totcount = count2; label2.text = totcount.tostring(); } public void compare() { ienumerable<dicerolllist> except = rolllist1.except(rolllist2); foreach (var rolllist in except) { string[] row = { rolllist.rollnum.tostring(), rolllist.dice1.tostring(), rolllist.dice2.tostring(), rolllist.dicetotal.tostring() }; var listviewitem = new listviewitem(row); listview3.items.add(listviewitem); } } public class dicerolllist : iequatable<dicerolllist> { public int rollnum { get; set; } public int dice1 { get; set; } public int dice2 { get; set; } public int dicetotal { get; set; } public bool equals(dicerolllist other) { if (object.referenceequals(other, null)) return false; if (object.referenceequals(this, other)) return true; return dice2.equals(other.dice2) && dice1.equals(other.dice1); } public override int gethashcode() { return base.gethashcode(); } } private void button1_click(object sender, eventargs e) { diceroll(); } private void button2_click(object sender, eventargs e) { querysearch(); } private void button3_click(object sender, eventargs e) { compare(); } } }
i fixed class , used make list, adding entire rolllist1 new listview rather taking out ones shared rolllist2. thoughts?
create model class hold dice rolls. have class implement iequatable interface.
class diceroles : iequatable<t> { public int role1 { ; set ; } public int role2 { ; set ; } public bool equals(product other) { //your code compare equity here } public override int gethashcode() { //your code here } }
add dice roles separate lists , can use except find differences.
go see second code example here on how implement equals , gethashcode. https://msdn.microsoft.com/en-us/library/vstudio/bb300779(v=vs.100).aspx
Comments
Post a Comment