Stack Problem

This is a major problem I got with stacks...I've been trying for hours and my professor never emailed me back with help so I am turning to the forums for help.

I have to do a few things with stacks & these are my problems:

1. I have to copy the top element in a stack to an INT

2. I have to take the larger number after I compare the numbers of the 2 stacks out (I presume this is done with pop())

3. I have to copy 2 arrays of INTS into 2 stacks

Please help...coding below:

import jmds.Stack;

public class PileGame

{

private Stack pile1, pile2;

private int winner;

private boolean gameOver;

public PileGame()

{

// TO DO: INITIALIZE GAME

}

private void move()

{

int top1 = 0;

int top2 = 0;

///TO DO: SET top1 = NUMBER ON TOP OF pile1

/// SET top2 = NUMBER ON TOP OF pile2

///TO DO: REMOVE TOP ITEM FROM WINNING PILE

/// ("WINNING PILE" IS THE ONE WITH THE LARGER TOP NUMBER)

/// FOR A TIE, REMOVE TOP ITEM FROM BOTH PILES

///TO DO: DECIDE WHETHER GAME IS OVER BECAUSE A PILE IS EMPTY

/// EMPTY PILE IS THAT OF WINNER (IF BOTH EMPTY, TIE).

/// IF DONE, CALL win FOR WINNER (1 OR 2) OR FOR TIE (0)

/// OTHERWISE, CONTINUE GAME

win(0); ///ALWAYS A TIE -- TO DO: REPLACE WITH SOLUTION TO ABOVE

}

private void win(int player)

{

gameOver = true;

winner = player;

}

public int getWinner()

{

if (!gameOver)

play();

return winner;

}

public String toString()

{

String result;

if (gameOver)

{

if (winner == 0)

result = "Tie Game";

else

result = "Player " + winner + " wins!";

}

else

{

result = "Game not played";

}

return result;

}

public void play()

{

// Initialize new game

// Fill both piles with integers between 1 and 20

// (the integers must be "shuffled")

int[] list1 = ShuffledIntegers.shuffle(1, 20);

int[] list2 = ShuffledIntegers.shuffle(1, 20);

// TO DO: PUT CONTENTS OF list1 INTO pile1,

// AND CONTENTS OF list2 INTO pile2

// Play the game

while (!gameOver)

move();

}

}

[2312 byte] By [Centurian] at [2007-9-27 22:43:42]
# 1
http://forum.java.sun.com/thread.jsp?forum=406&thread=313859
Ragnvald at 2007-7-7 13:40:47 > top of Java-index,Other Topics,Java Game Development...
# 2

I am not a stack master but here are the basic functions.

in order to look at the item on top of the stack you use the function peek().

Yes, removing the top item is done with pop().

In order to put items onto the stack you use the function push().

and to Check if it is empty call the function empty(). This will return a boolean.

And that is as basic and as advanced as I am in this subject

drslinky at 2007-7-7 13:40:47 > top of Java-index,Other Topics,Java Game Development...
# 3

Err... what exactly is the problem you're having with the code?Or are you just looking for some hints?

1) int top_element = stack.pop( );

2) int x = stackA.pop( );int y = stackB.pop( ); if ( x > y ) { largest = x } else { largest = y }

3) for ( int i = 0; i < array.length; i++ ) { stack.push( array ); } then repeat for the second array.

TheDavid at 2007-7-7 13:40:47 > top of Java-index,Other Topics,Java Game Development...