CS 202 - Computer Science II - Fall 2004
Nim


Loyola College > Department of Computer Science > Dr. James Glenn > CS 202 > Examples and Lecture Notes > Nim

This applet requires Java 2 and so will not work in all browsers.
The quit button does not work for the applet version of this game.

NimControl4.java

import java.util.StringTokenizer;
import java.awt.event.*;
import javax.swing.*;

/**
 * Yet another controller for a game of Nim.  This controller
 * has a text field where the player can enter the number of
 * sticks to take.  There are three buttons to click to indicate
 * which row to take sticks from.
 *
 * @author Jim Glenn
 * @version 0.1 10/6/2004
 */

public class NimControl4 extends JPanel implements ActionListener
{
    private JTextField sticksInput;
    private JButton row1Button;
    private JButton row2Button;
    private JButton row3Button;

    /**
     * Constructs a new control panel for Nim.
     *
     * @param m the model of the game to control
     * @parem v the view to associate with this game
     */

    public NimControl4(NimModel m, NimView v)
    {
	model = m;
	view = v;

	// use a box layout for this panel

	setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

	// first row is a panel with the input for number of sticks

	JPanel sticksPanel = new JPanel();
	sticksPanel.add(new JLabel("Select number of sticks: "));
	sticksInput = new JTextField(3);
	sticksPanel.add(sticksInput);
	add(sticksPanel);

	// bottom row is a panel with the three buttons

	JPanel buttonPanel = new JPanel();
	buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
	row1Button = new JButton("Row 1");
	row1Button.addActionListener(this);
	buttonPanel.add(row1Button);
	row2Button = new JButton("Row 2");
	row2Button.addActionListener(this);
	buttonPanel.add(row2Button);
	row3Button = new JButton("Row 3");
	row3Button.addActionListener(this);
	buttonPanel.add(row3Button);
	add(buttonPanel);
    }

    /**
     * Respond to action events.  This control will receive action events
     * from the three buttons.
     *
     * @param e an action event
     */

    public void actionPerformed(ActionEvent e)
    {
	int numSticks = Integer.parseInt(sticksInput.getText());

	if (e.getSource() == row1Button)
	    takeSticks(0, numSticks);
	else if (e.getSource() == row2Button)
	    takeSticks(1, numSticks);
	else
	    takeSticks(2, numSticks);
    }

    /**
     * Updates the model by removing the given number of sticks from the
     * given row.  If the user enters an invalid number, nothing happens.
     * (although a good user interface would give the user feedback so
     * he would know what went wrong).
     *
     * @param row the row to take sticks from
     * @param sticks the number of sticks to take
     */

    private void takeSticks(int row, int sticks)
    {
	if (sticks > 0 && sticks <= model.countSticks(row))
	    {
		model.playTurn(row, sticks);
		view.update();
	    }
    }

    /**
     * The model associated with this control.
     */

    protected NimModel model;

    /**
     * The view associated with this control.
     */

    protected NimView view;
}
This code can also be downloaded from the file NimControl4.java.