M
Maine Copper
OK we have a new java instructor and he seems to want us on the next level. He has a whole different java vocabulary than the last instructor so it is making it hard.
Here is an assignment we were supposed to do. Create the game nim with three arrays referencing an Object (Piles) so the user can take stones out of the piles last person to pick up a stone loses. He wants all the output done via print or println() methods and input done via JOptionPane.
Can someone look at my code and tell me what I need to reasearch to move on? I am stuck passing? my array and referencing the objects. Thanks
/*
* Nim.java
*
* Created on February 8, 2004, 11:20 PM
*/
package Nim;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Nim
{
int s;
String stonesNum;
int stonesSub;
String playerWins = "You Win!!!";
String computerWins = "You Lose!!!";
/** Creates a new instance of Nim */
public void Nimality()
{
s = 10 + (int) (Math.random() * 30);
//Pile.Piles();
playersMove();
}//end of method Nim
public void playersMove()
{
if(s >= 1)
{
System.out.println("Pile:" +s);
//System.out.println("Pile" + piles[0] + ":" +s );
//System.out.println("Pile" + piles[1] + ":" +s );
//System.out.println("Pile" + piles[2] + ":" +s );
stonesNum = JOptionPane.showInputDialog("Enter the number of stones you would like to remove from the pile");
stonesSub = Integer.parseInt(stonesNum);
//if(stonesSub
s = s - stonesSub;
System.out.println("Player removed " + stonesSub + " stones.");
computersMove();
}
else
{
JOptionPane.showMessageDialog(null,playerWins,"Player Wins!",JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
}//end of method playersMove()
public void computersMove()
{
if(s >= 1)
{
System.out.println("Pile:" +s);
s = s - 1;
System.out.println("Computer removed 1 stone");
playersMove();
}
else
{
JOptionPane.showMessageDialog(null,computerWins,
"Computer Wins!",JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
}//end of method computersMove()
public static void main(String[] args)
{
Nim nim = new Nim();
nim.Nimality();
}//end of method main
}//end of class Nim
class Pile
{
int s;
final int MAX_PILES = 3;
Pile[] piles;
/** Creates a new instance of Stones */
public void Piles()
{
piles = new Pile[MAX_PILES];
for (int i = 2; i < MAX_PILES; i++) // Fill array with references to
piles = new Pile();
}
/* public void Stones()
{
s = 10 + (int) (Math.random() * 30);
}*/
}//end of class piles
class Display
{
int s;
int p;
public void display()
{
}
public int getPiles()
{
return p;
}
public void setPiles(int pile)
{
pile = p;
}
public int getStones()
{
return s;
}
public void setStones(int stones)
{
stones = s;
}
}//end of class display
Here is an assignment we were supposed to do. Create the game nim with three arrays referencing an Object (Piles) so the user can take stones out of the piles last person to pick up a stone loses. He wants all the output done via print or println() methods and input done via JOptionPane.
Can someone look at my code and tell me what I need to reasearch to move on? I am stuck passing? my array and referencing the objects. Thanks
/*
* Nim.java
*
* Created on February 8, 2004, 11:20 PM
*/
package Nim;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Nim
{
int s;
String stonesNum;
int stonesSub;
String playerWins = "You Win!!!";
String computerWins = "You Lose!!!";
/** Creates a new instance of Nim */
public void Nimality()
{
s = 10 + (int) (Math.random() * 30);
//Pile.Piles();
playersMove();
}//end of method Nim
public void playersMove()
{
if(s >= 1)
{
System.out.println("Pile:" +s);
//System.out.println("Pile" + piles[0] + ":" +s );
//System.out.println("Pile" + piles[1] + ":" +s );
//System.out.println("Pile" + piles[2] + ":" +s );
stonesNum = JOptionPane.showInputDialog("Enter the number of stones you would like to remove from the pile");
stonesSub = Integer.parseInt(stonesNum);
//if(stonesSub
s = s - stonesSub;
System.out.println("Player removed " + stonesSub + " stones.");
computersMove();
}
else
{
JOptionPane.showMessageDialog(null,playerWins,"Player Wins!",JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
}//end of method playersMove()
public void computersMove()
{
if(s >= 1)
{
System.out.println("Pile:" +s);
s = s - 1;
System.out.println("Computer removed 1 stone");
playersMove();
}
else
{
JOptionPane.showMessageDialog(null,computerWins,
"Computer Wins!",JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
}//end of method computersMove()
public static void main(String[] args)
{
Nim nim = new Nim();
nim.Nimality();
}//end of method main
}//end of class Nim
class Pile
{
int s;
final int MAX_PILES = 3;
Pile[] piles;
/** Creates a new instance of Stones */
public void Piles()
{
piles = new Pile[MAX_PILES];
for (int i = 2; i < MAX_PILES; i++) // Fill array with references to
piles = new Pile();
}
/* public void Stones()
{
s = 10 + (int) (Math.random() * 30);
}*/
}//end of class piles
class Display
{
int s;
int p;
public void display()
{
}
public int getPiles()
{
return p;
}
public void setPiles(int pile)
{
pile = p;
}
public int getStones()
{
return s;
}
public void setStones(int stones)
{
stones = s;
}
}//end of class display