c# - Processing int to name of list -
i try write simple console application hanoi towers. stuck @ 1 point.
in program ask person write tower wants put disk, but: have 3 lists towers, i'll ask number gamer , how can build "compare method"? because don't want copy same piece of code 6 times...
class towers : disks { //public towers(int value, int whereitis) : base(value, whereitis) { } public list<disks> tower1 = new list<disks>(); public list<disks> tower2 = new list<disks>(); public list<disks> tower3 = new list<disks>(); public void compare(int dyskfrom, int dyskwhere) { } public void display() { foreach(var in tower1){ console.writeline("where: {0} | value: {1}", i.whereitis, i.value); } } public void build(int towerhigh) { (int = towerhigh; > 0; i--) { tower1.add(new disks {value = i, whereitis = 1 }); } } } class disks { public int value; //wartosc krazka public int whereitis; //na ktorej wiezy sie on znajduje } class program { static void main(string[] args) { towers tower = new towers(); int towerhigh; console.write("podaj wysokość wieży: "); towerhigh = int.parse(console.readline()); tower.build(towerhigh); tower.display(); tower.compare(1, 2); } }
it's easier create array of stack<disk>()
. way can select tower index.
i use stack, because thats typically functionality need.
something like: pseudo (typed in browser, no syntax checked)
class disk { public int size {get; set;} } static void main(string[] args) { // create stack<disk> array stack<disk>[] towers = new stack<disk>[3]; // create each tower for(int i=0;i<towers.length;i++) { towers[i] = new stack<disk>(); // fill towers. for(int j=3;j>0;j--) towers[i].enqueue(new disk { size = j }); } bool isholdingdisk = false; while(true) { displaytowers(towers); if(isholdingdisk) console.writeline("on tower want place it?"); else console.writeline("choose tower pick disk"); var key = console.readkey(true); var chosentowerindex = 0; switch(key) { case(key.escape): break; case(key.d1): chosentowerindex = 0; break; case(key.d1): chosentowerindex = 1; break; case(key.d1): chosentowerindex = 2; break; // etc... } if((chosentowerindex < 1) || (chosentowerindex >= towers.length)) continue; // check here if holding disk if(isholdingdisk) { // logic testing if can place disk here... // using towers[chosentowerindex] // check if completed... if(completed) break; isholdingdisk = false; } else { // check if can pickup disk....(if there any) isholdingdisk = true; } } // final display... displaytowers(towers); console.writeline("thanks playing"); } static void displaytowers(stack<disk>[] towers) { // draw fancy asc-ii art... }
Comments
Post a Comment