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();
}
}

