NimGUI.java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class NimGUI
{
public static void main(String[] args)
{
Nim game = new Nim();
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JLabel status = new JLabel("Welcome to nim");
MarblePanel marblePanel = new MarblePanel(200, 150, game.getMarbles());
InputPanel input = new InputPanel("Enter the number of marbles", 4);
panel.add(status);
panel.add(input);
frame.setLayout(new GridLayout());
frame.add(panel);
frame.add(marblePanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 400);
frame.setVisible(true);
}
}
MarblePanel.java:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class MarblePanel extends JPanel
{
private int numMarbles;
public static final double MARBLE_SIZE = 10.0;
private Ellipse2D ellipse;
private Color[] colors = {Color.RED, Color.GREEN, Color.BLUE, Color.GRAY, Color.MAGENTA, Color.BLACK};
public MarblePanel(int width, int height, int numMarbles)
{
setSize(width, height);
this.numMarbles = numMarbles;
}
public MarblePanel(int width, int height)
{
setSize(width, height);
numMarbles = (int) (Math.random() * 91) + 10;
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
int width = getWidth();
int height = getHeight();
int marblesLeft = numMarbles;
for(int x = 0; x + MARBLE_SIZE < width; x += 13)
{
for(int y = 0; y + MARBLE_SIZE < height && marblesLeft > 0; y += 13)
{
g2.setColor(colors[(int) (Math.random() * colors.length)]);
ellipse = new Ellipse2D.Double(x, y, MARBLE_SIZE, MARBLE_SIZE);
g2.fill(ellipse);
marblesLeft--;
}
}
marblesLeft = numMarbles;
g2.drawString("" + numMarbles, 200, 200);
}
public void setMarbles(int numMarbles)
{
this.numMarbles = numMarbles;
}
public int getMarbles()
{
return numMarbles;
}
}
Nim.java:
public class Nim
{
private int numMarbles;
private int winner;
public Nim(int amount)
{
numMarbles = amount;
winner = -1;
}
public Nim()
{
numMarbles = (int) (Math.random() * 91) + 10;
winner = -1;
}
public void takeMarbles(int numMarbles, boolean isHuman)
{
// No one has won just yet
if(winner < 0)
{
if(numMarbles > this.numMarbles / 2)
numMarbles = this.numMarbles / 2;
if(numMarbles < 1)
numMarbles = 1;
this.numMarbles -= numMarbles;
if (this.numMarbles < 1)
{
if(isHuman)
winner = 0;
else
winner = 1;
}
}
}
public void setMarbles(int marbles)
{
numMarbles = marbles;
}
public int getMarbles()
{
return numMarbles;
}
public int getWinner()
{
return winner;
}
public boolean isValid(int amount)
{
if (amount < 1)
return false;
if (amount > numMarbles / 2 && numMarbles / 2 > 0)
return false;
return true;
}
public int getMax()
{
if(numMarbles > 1)
return numMarbles / 2;
else
return 1;
}
// Returns the amount for the computer to take from the pile
public int calcAmount(boolean isSmart)
{
int amount = 0, power = 0;
if(isSmart)
{
// Determines the highest power of two below the pile amount
for(int x = 1; (int) Math.pow(2, x) <= numMarbles; x++)
power = x;
amount = (numMarbles - (int) Math.pow(2, power)) + 1;
}
else
amount = (int) (Math.random() * (numMarbles / 2)) + 1;
if(amount > numMarbles / 2)
amount = numMarbles / 2;
if(amount < 1)
amount = 1;
return amount;
}
public String toString()
{
String result = "There are currently " + numMarbles + " in the pile";
return result;
}
}
InputPanel.java:
import javax.swing.*;
public class InputPanel extends JPanel
{
public InputPanel(String inputText, int fieldWidth)
{
add(new JLabel(inputText));
add(new JTextField(fieldWidth));
}
}
This is my first real GUI application, and I know it's some elemental error. What is my problem?

New Topic/Question
Reply




MultiQuote


|