Hi all : )

Im messing around with the game of Nim and i want to create some strategies to make the game more interesting. I have finished all the other code and all i need to do is this.

Basically i want 4 classes

The first strategy will be to play `randomly.Class` called `NimStrategy`.

``````chooseRow: Choose a row of the array randomly
chooseNum: Choose a number of matches randomly
``````

Second.
The next strategy will be to choose a random number of matches from the row with the most matches. class called `NotQuiteRandomNimStrategy`.

``````chooseRow: Select the row with the most number of matches. If more than one row has the same maximum, pick any of them.

chooseNum: Choose a number of matches randomly. Note, you can't take more matches from the row than there are matches in it to begin with and you must take at least 1 match.
``````

Third
The next strategy will be to take a calculated number of matches from the row with the most matches. class called `IntermediateNimStrategy`.

``````chooseRow: Select the row with the most number of matches. If more than one row has the same maximum, pick any of them.

chooseNum: follow this algorithm:

1.determine the row with the most matches besides the current row
2.determine how many matches are left in all other rows besides that row and the current row
3.find the difference between the number of matches in that row and the total just calculated
4.if that difference is less than the number of matches in the current row, remove that many matches from the current row, otherwise remove all the matches in the row
``````

Fourth.
`AdvancedNimStrategy`. using the strategy from here

Can anyone give me any ideas on this ?

This is what i have done so far.

``````import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.URL;
import java.net.MalformedURLException;

/**
*   The classic game of Nim.
*
*  This class provides a frame interface to a Nim game object.  The interface draws the matches in the game
* and lets the human player remove matches to the right of and including the match clicked.
*
* @author PAS
* @version (23/7/03)
*
*/

// Matches extend buttons to be clickable
class Match extends JButton {
private int id;

// If you do not have internet access, comment out this line:
public Match(int i) { super(); try { URL u = new URL("http://www.it.bond.edu.au/inft110/053/assessment/match.gif"); setIcon(new ImageIcon(u, "match")); this.setBorderPainted(false); id = i; } catch (MalformedURLException e) { e.printStackTrace();}}

// If you have internet access, comment out this line:
// public Match(int i) { super(); try { setIcon(new ImageIcon("match.gif", "match")); this.setBorderPainted(false); id = i; } catch (Exception e) { e.printStackTrace();}}

public int getID() { return id; }
}

// Matches are layed out in rows shown in panels
class MatchRow extends JPanel {
private int id;
public MatchRow(int i) { super(new FlowLayout()); id = i; }
public int getID() { return id; }
}

public class MainFrame extends JFrame {
private Nim game;
private NimStrategy strategy;

public MainFrame(Nim g) {
game = g;

// Strategy still needs to be done : )
strategy = new NimStrategy();

this.setTitle("Nim");
this.setSize(new Dimension(700,300));
this.setVisible(true);
this.getContentPane().setLayout(new GridLayout(game.getMatches().length, 1));

drawMatches();
}

/**
* Override parent processWindowEvent to catch the window closing event and terminate the program.
* Otherwise, the event is passed to the parent to handle.
*
* @param e  the window event
*/
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}

private void drawMatches() {
this.getContentPane().removeAll();

for (int i = 0; i < game.getMatches().length; i++) {
MatchRow matchRow = new MatchRow(i);
for (int m = 0; m < game.getMatches(); m++) {
Match match = new Match(m);
public void actionPerformed(ActionEvent e) {
removeFromMe((Component) e.getSource());
}
});
}
}

this.validate();

}

private void removeFromMe(Component me) {
Container p = me.getParent();

game.remove(((MatchRow) p).getID(), p.getComponentCount() - ((Match) me).getID());
drawMatches();

if (game.gameOver()) {
this.validate();
}
else
computerPlay();
}

private void computerPlay() {
int row = strategy.chooseRow(game.getMatches());
int matches = strategy.chooseNum(game.getMatches(), row);

game.remove(row, matches);
drawMatches();

if (game.gameOver()) {
this.validate();
}
}

public static void main(String [] args) {
if (args.length == 2)
new MainFrame(new Nim(Integer.parseInt(args[0]), Integer.parseInt(args[1])));
else
new MainFrame(new Nim());
}
}

/**
* The classic game of Nim
*
* This class represents the state of a game of Nim.  Since Nim is just rows of matches, the game is represented
* by an array showing how many matches are in each row.
*
* Matches are removed from only one row at a time.
*
* The game is over when no matches remain.
*
* @author PAS
* @version (23/7/03)
*/

public class Nim {
private static final int DEFAULT_ROWS = 3;
private static final int DEFAULT_MAX = 10;
private int [] matches;

public Nim(int rows, int max) {
matches = new int[rows];

for (int i = 0; i < matches.length; i++)
matches = 1 + (int) (Math.random()*(max-1));
}

public Nim() { this(DEFAULT_ROWS, DEFAULT_MAX); }

public int [] getMatches() { return matches; }

public void remove(int row, int ms) {
if (ms <= matches[row])
matches[row] -= ms;
else
matches[row] = 0;
}

public boolean gameOver() {
int sum = 0;

for (int i = 0; i < matches.length && sum == 0; i++)
sum += matches;

return (sum == 0);
}
}
``````

I also have a pic of a match i havent included.

Thanks all

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.