User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 397,590 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,896 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 3754 | Replies: 0
Reply
Join Date: Oct 2005
Posts: 5
Reputation: OllieFalle is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
OllieFalle OllieFalle is offline Offline
Newbie Poster

Nim Strategy

  #1  
Nov 29th, 2005
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
www.csm.astate.edu/Nim.html
www.cut-the-knot.org/nim_st.shtml

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()[i]; m++) {
Match match = new Match(m);
match.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
removeFromMe((Component) e.getSource());
}
});
matchRow.add(match);
}
this.getContentPane().add(matchRow);
}

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.getContentPane().add(new Label("You won"));
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.getContentPane().add(new Label("I won"));
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[i] = 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[i];

return (sum == 0);
}
}



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

Thanks all
AddThis Social Bookmark Button
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 6:03 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC