1. Point.java ¶
2 계 .
~cpp
package suwlee.game.nqueen;
public class Point
{
private int x;
private int y;
public Point() {}
public Point( int x, int y )
{
setX( x );
setY( y );
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public void setX( int x )
{
this.x = x;
}
public void setY( int y )
{
this.y = y;
}
public void setPoint( int x, int y )
{
setX( x );
setY( y );
}
public boolean isSamePoint( Point another )
{
if( x == another.x && y == another.y ) return true;
return false;
}
}
2. Chessman.java ¶
. 기 doIHurtYou() 게 .
~cpp
package suwlee.game.nqueen;
public abstract class Chessman extends Point
{
public Chessman()
{
super();
}
public Chessman( int x, int y )
{
super( x, y );
}
public abstract boolean doIHurtYou( Chessman another );
}
3. Queen.java ¶
. ,,/ 각 .
~cpp
package suwlee.game.nqueen;
import suwlee.game.nqueen.Point;
public class Queen extends Chessman
{
public Queen()
{
super();
}
public Queen( int x, int y )
{
super( x, y );
}
public boolean doIHurtYou( Chessman another )
{
if( getX() == another.getX() ) return true;
if( getY() == another.getY() ) return true;
double angle = ((double)another.getY() - (double)getY()) / ((double)another.getX() - (double)getX());
if( angle == 1.0 || angle == -1.0 ) return true;
return false;
}
}
4. Board.java ¶
2 . 구 draw() 구게 구게 .
~cpp
package suwlee.game.nqueen;
import java.util.Enumeration;
import java.util.Vector;
public abstract class Board
{
private Vector board;
private int sizeOfBoard;
private Board() {}
public Board( int sizeOfBoard ) throws Exception
{
if( sizeOfBoard < 1 ) throw new Exception( Board.class.getName() + "- size_of_board must be greater than 0." );
this.sizeOfBoard = sizeOfBoard;
board = new Vector();
}
public int getSizeOfBoard()
{
return sizeOfBoard;
}
public int countChessman()
{
return board.size();
}
public boolean setChessman( Chessman chessman )
{
if( countChessman() == (sizeOfBoard^2) ) return false;
Chessman prevOne;
for( Enumeration e=board.elements(); e.hasMoreElements(); ) {
prevOne = (Chessman)e.nextElement();
if( chessman.doIHurtYou( prevOne ) || prevOne.doIHurtYou( chessman )) return false;
}
board.addElement( chessman );
return true;
}
public Chessman getChessman( int x, int y )
{
return getChessman( new Point( x, y ));
}
public Chessman getChessman( Point point )
{
Chessman prevOne;
for( Enumeration e=board.elements(); e.hasMoreElements(); ) {
prevOne = (Chessman)e.nextElement();
if( prevOne.isSamePoint( point )) return prevOne;
}
return null;
}
public boolean removeChessman( int x, int y )
{
return removeChessman( new Point( x, y ));
}
public boolean removeChessman( Point point )
{
Chessman prevOne;
for( Enumeration e=board.elements(); e.hasMoreElements(); ) {
prevOne = (Chessman)e.nextElement();
if( prevOne.isSamePoint( point )) return removeChessman( prevOne );
}
return false;
}
public boolean removeChessman( Chessman chessman )
{
return board.removeElement( chessman );
}
public void clearBoardBelowYPosition( int y )
{
Chessman prevOne;
for( Enumeration e=board.elements(); e.hasMoreElements(); ) {
prevOne = (Chessman)e.nextElement();
if( y <= prevOne.getY() ) removeChessman( prevOne );
}
}
public abstract void draw();
}
5. ConsolBoard.java ¶
~cpp
package suwlee.game.nqueen;
import suwlee.game.nqueen.Board;
public class ConsolBoard extends Board
{
public ConsolBoard( int sizeOfBoard ) throws Exception
{
super( sizeOfBoard );
}
/**
* @see Board#draw()
*/
public void draw()
{
Chessman chessman;
for( int i=0; i<getSizeOfBoard(); i++ ) {
for( int j=0; j<getSizeOfBoard(); j++ ) {
chessman = getChessman( j, i );
if( chessman instanceof Queen ) System.out.print( "Q" );
else System.out.print( "." );
}
System.out.println();
}
}
}
6. NQueenPlayer.java ¶
n-Queens Problem 결 . 각 고(기 play ) , 고 .
~cpp
package suwlee.game.nqueen;
public class NQueensPlayer
{
private Board board;
private int numberOfSolutions;
public NQueensPlayer() {}
public void prepareBoard( int sizeOfBoard ) throws Exception
{
board = new ConsolBoard( sizeOfBoard );
}
public void play()
{
numberOfSolutions = 0;
setQueen( 0 );
}
public int getNumberOfSolutions()
{
return numberOfSolutions;
}
private void setQueen( int y )
{
if( y == board.getSizeOfBoard() ) {
if( board.countChessman() == board.getSizeOfBoard() ) {
numberOfSolutions ++;
board.draw();
}
return;
}
Chessman chessman = new Queen();
for( int x=0; x<board.getSizeOfBoard(); x++ ) {
chessman.setPoint( x, y );
if( board.setChessman( chessman )) {
setQueen( y + 1 );
board.clearBoardBelowYPosition( y );
}
}
}
public static void main( String [] args )
{
NQueensPlayer player = new NQueensPlayer();
try {
if( args.length != 1 ) throw new Exception();
player.prepareBoard( Integer.parseInt( args[0] ));
player.play();
System.out.println( "I found " + player.getNumberOfSolutions() + " solutions." );
}
catch( Exception e ) {
System.out.println( "Usage: java NQueensPlayer size_of_board" );
}
}
}










