pushing arrays into stacks

is a pointer passed instead of data? how do i force data instead?

Page 1 of 1

4 Replies - 1736 Views - Last Post: 09 March 2010 - 07:30 AM Rate Topic: -----

#1 fenixbtc   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-March 10

pushing arrays into stacks

Posted 07 March 2010 - 05:49 PM

hey there,

so i'm doing an assignment, first class using C#, second project. In the project i am passing a 5x5 int array to a stack. that array is then updated and some values changed and then pushed again onto the stack. what i'm finding is that once the second push occurs, both elements in the stack contain the same data. i'm assuming that a pointer to the array is being pushed instead of the actual data, how do i force data to be passed instead?

if it matters, it's a program to solve the 15 puzzle game using IDDFS*. the 5x5 array stores the state of the game- graphical position of pieces and values of pieces. another array stores the possible movements for the blank piece.

thanks in advance
~david

Is This A Good Question/Topic? 0
  • +

Replies To: pushing arrays into stacks

#2 MentalFloss   User is offline

  • .
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,590
  • Joined: 02-September 09

Re: pushing arrays into stacks

Posted 07 March 2010 - 06:17 PM

What is the reason for using a stack in this implementation? I am having trouble understanding how it benefits this situation.

Aside from all that, please show your code. There may be some reasons behind what you're seeing and it may be due to how you wrote this so I think it's best if you show us the whole thing.
Was This Post Helpful? 0
  • +
  • -

#3 fenixbtc   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-March 10

Re: pushing arrays into stacks

Posted 07 March 2010 - 06:35 PM

the code is currently nearly 350 lines. if you still want me to post it i will...

basically the stack is to store the state of the game pieces so that it can back track if the DFS portion of the IDDFS dead-ends.

the game is a 4x4 square of blocks that are numbered, but missing one block (which i will call 'blank box') so the pieces can be moved around. at most the blank box can move in 4 directions, left right up down. the program stores the original state of the game on the stack, the search algorithm moves the box in one of the possible directions it can go, stores that onto the stack and determines the next possible path it can take and moves again, stores the state on the stack, etc. it's an IDDFS* algorithm, meaning it's basically both a DFS and BFS search, and uses heuristics to improve search time. if the moves taken gets 'too deep,' it backtracks to a previous state (pops the previous game state array off the stack) and tries the next possible direction it can move.

this is what the puzzle looks like if you aren't sure what i mean.
Posted Image
Was This Post Helpful? 0
  • +
  • -

#4 MentalFloss   User is offline

  • .
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,590
  • Joined: 02-September 09

Re: pushing arrays into stacks

Posted 07 March 2010 - 06:43 PM

With a line count that high, I think it'd be much better if you zipped the project and attached it to a reply.

I'm familiar with the game but I am still having a hard time grasping why you'd use a stack. Generally with undo/redo type functionality, the memento pattern is a decent way to go about it: http://www.dofactory...nto.aspx#_self1
Was This Post Helpful? 0
  • +
  • -

#5 T3hC13h   User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: pushing arrays into stacks

Posted 09 March 2010 - 07:30 AM

After you've pushed the array onto the stack you need assign its old reference to a new array before you start making changes.

Try something like this.
            Stack<Array> s = new Stack<Array>();
            int[,] a = new int[4, 4];
            a[0, 0] = 1;
            s.Push(a);
            a = new int[4, 4];
            a[0, 1] = 2;
            s.Push(a);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1