Best Projects

Bug Trackingsystem,Books online shopping,college management system,HR management system,Internet banking,Online chat room ,Online exam,Telephone billing system,Banking application,ATM database,airways reservation system,virtual network computing ,calculator,SMTP(simple mail transfer protocol)

A Civilisation Game




//AboutPanel
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
* Title: AboutPanel.java
* Description: This class shows the about box showing who programmed the game
* @author: Shane Grunf
* @version: 1.0
*/
public class AboutPanel extends JPanel implements ActionListener
{
private JButton okButton;
private JPanel buttonPanel;
private JLabel myLabel;
private ImageIcon myIcon;

public AboutPanel()
{
//create the panel containing the OK button
this.setLayout(new BorderLayout());

//set up the image
myIcon = new ImageIcon("images/AboutDialog.png");
myLabel = new JLabel(myIcon);
add(myLabel, BorderLayout.CENTER);

//set up the button
okButton = new JButton("OK");
okButton.setFocusable(false);
okButton.addActionListener(this);

//set up the panel
buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
buttonPanel.setBackground(Color.black);
buttonPanel.add(okButton);
add(buttonPanel, BorderLayout.SOUTH);
}//end constructor

public void actionPerformed(ActionEvent e)
{
this.setVisible(false);
}//end actionPerformed
}//end class



//City
import javax.swing.*;

/**
* Title: City.java
* Description: This class holds city information
* @author: Shane Grund
* @version: 1.0
*/
public class City
{
private ImageIcon cityIcon;
private int pos;

public City(int playerNo)
{
if(playerNo == 1)
cityIcon = new ImageIcon("images/Cities/player1.png");
else if(playerNo == 2)
cityIcon = new ImageIcon("images/Cities/player2.png");
}//end constructor

public ImageIcon getImage()
{
return cityIcon;
}

public void setLocation(int x)
{
pos = x;
}

public int getLocation()
{
return pos;
}
}//end class



//CustomCellRender
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

/**
* Title: CustomCellRenderer
* Description: This class is used for displaying pictures in a JTable
* @author: Shane Grund
* @version: 1.0
*/
class CustomCellRenderer extends JLabel implements TableCellRenderer {
private boolean isSelected;
private boolean hasFocus;
private ImageIcon[] suitImages;

/**
* This constructor sets up all the images to be
* used in the JTable
*/
public CustomCellRenderer()
{
suitImages = new ImageIcon[9];
suitImages[1] = new ImageIcon("images/units/settler.png");
suitImages[2] = new ImageIcon("images/units/warrior.png");
suitImages[3] = new ImageIcon("images/units/spearman.png");
suitImages[4] = new ImageIcon("images/units/archer.png");
suitImages[5] = new ImageIcon("images/units/swordsman.png");
suitImages[6] = new ImageIcon("images/units/catapult.png");
suitImages[7] = new ImageIcon("images/units/galley.png");
suitImages[8] = new ImageIcon("images/units/horseman.png");
}//end constructor

/**
* This method sets up each row for everything inside JTable
* @param table JTable
* @param value Object
* @param isSelected boolean
* @param hasFocus boolean
* @param row int
* @param column int
* @return Component
*/
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
String sText = (String) value;
this.isSelected = isSelected;
this.hasFocus = hasFocus;

//set up the first column
if (column == 0)
{
if (row == 0)
{
setText("Unit");
}
else if (row == 1)
{
setText("Movement");
}
else if (row == 2)
{
setText("Attack");
}
else
{
setText("Defence");
}
} //end if

//set up the setler column
if (column == 1)
{
if (row == 0)
{
setIcon(suitImages[column]);
}
else if (row == 1)
{
setIcon(null);
setText("1");
}
else if (row == 2)
{
setIcon(null);
setText("0");
}
else
{
setIcon(null);
setText("0");
}
}

//set up the warrior column
if (column == 2)
{
if (row == 0)
{
setIcon(suitImages[column]);
}
else if (row == 1)
{
setIcon(null);
setText("1");
}
else if (row == 2)
{
setIcon(null);
setText("1");
}
else
{
setIcon(null);
setText("1");
}
}

//set up the spearman column
if (column == 3)
{
if (row == 0)
{
setIcon(suitImages[column]);
}
else if (row == 1)
{
setIcon(null);
setText("1");
}
else if (row == 2)
{
setIcon(null);
setText("2");
}
else
{
setIcon(null);
setText("1");
}
}

//set up the archer column
if (column == 4)
{
if (row == 0)
{
setIcon(suitImages[column]);
}
else if (row == 1)
{
setIcon(null);
setText("1");
}
else if (row == 2)
{
setIcon(null);
setText("3");
}
else
{
setIcon(null);
setText("2");
}
}

//set up the swordsman column
if (column == 5)
{
if (row == 0)
{
setIcon(suitImages[column]);
}
else if (row == 1)
{
setIcon(null);
setText("2");
}
else if (row == 2)
{
setIcon(null);
setText("2");
}
else
{
setIcon(null);
setText("2");
}
}

//set up the catapolt column
if (column == 6)
{
if (row == 0)
{
setIcon(suitImages[column]);
}
else if (row == 1)
{
setIcon(null);
setText("2");
}
else if (row == 2)
{
setIcon(null);
setText("5");
}
else
{
setIcon(null);
setText("0");
}
}

//set up the ship column
if (column == 7)
{
if (row == 0)
{
setIcon(suitImages[column]);
}
else if (row == 1)
{
setIcon(null);
setText("6");
}
else if (row == 2)
{
setIcon(null);
setText("1");
}
else
{
setIcon(null);
setText("1");
}
}

//set up the horseman column
if (column == 8)
{
if (row == 0)
{
setIcon(suitImages[column]);
}
else if (row == 1)
{
setIcon(null);
setText("3");
}
else if (row == 2)
{
setIcon(null);
setText("2");
}
else
{
setIcon(null);
setText("2");
}
}
return this;
} //end method
} //end class



//CustomDataModel
import javax.swing.*;
import javax.swing.table.*;

/**
* Title: CustomDataModel
* Description: This class sets up the amount of rows and columns in
* my JTable.
* @author: Shane Grund
* @version: 1.0
*/
class CustomDataModel extends AbstractTableModel
{
public Object getValueAt( int iRowIndex, int iColumnIndex )
{
return "" + iColumnIndex + "," + iRowIndex;
}

public void setValueAt( Object aValue, int iRowIndex, int iColumnIndex ){}

public int getColumnCount()
{
return 0;// Return 0 because we handle our own columns
}

public int getRowCount()
{
return 4;
}
}//end class



//gamecore
import java.awt.*;
import javax.swing.*;

/**
Class where the game starts. Sets up the screen and all
subclasses will implement the draw(Graphics2D g) method
*/
public abstract class GameCore
{
private static final DisplayMode POSSIBLE_MODES[] = {
new DisplayMode(800, 600, 32, 0),
new DisplayMode(800, 600, 24, 0),
new DisplayMode(800, 600, 16, 0),
new DisplayMode(640, 480, 32, 0),
new DisplayMode(640, 480, 24, 0),
new DisplayMode(640, 480, 16, 0)};

protected static final int FONT_SIZE = 24;
private boolean isRunning;
protected ScreenManager screen;

/**
Calls init() and gameLoop()
*/
public void run()
{
try
{
init();
gameLoop();
}//end try

finally
{
screen.restoreScreen();
}//end finally
}//end run

/**
Sets full screen mode and initiates and objects.
*/
public void init()
{
screen = new ScreenManager();
DisplayMode displayMode = screen.findFirstCompatibleMode(POSSIBLE_MODES);
screen.setFullScreen(displayMode);

Window window = screen.getFullScreenWindow();
window.setFont(new Font("Dialog", Font.PLAIN, FONT_SIZE));

isRunning = true;
}//end init

public Image loadImage(String fileName)
{
return new ImageIcon(fileName).getImage();
}//end loadImage

/**
Runs through the game loop until stop() is called.
*/
public void gameLoop()
{
long startTime = System.currentTimeMillis();
long currTime = startTime;

while (isRunning)
{
long elapsedTime = System.currentTimeMillis() - currTime;
currTime += elapsedTime;

// update
update(elapsedTime);

// draw the screen
Graphics2D g = screen.getGraphics();
draw(g);
g.dispose();
screen.update();

// take a nap
try
{
Thread.sleep(20);
}//end try
catch (InterruptedException ex){}
}//end while
}//end gameloop

/**
Updates the state of the game/animation based on the
amount of elapsed time that has passed.
*/
public void update(long elapsedTime)
{
// do nothing
}//end update

/**
Draws to the screen. Subclasses must override this
method.
*/
public abstract void draw(Graphics2D g);
}//end class



//GWmap
/**
GWMap -- Generate world class for Game
Copyright (c) Ian Foley, March 2004

You are free to use and modify this code, but you must acknowledge adaptations of this
code in your program. However many actual lines of code you write yourself in the
final submission, this code (and any adaptations of it) will count as 10% of acknowledged
code in your final submission.

The GWMap class is a singleton, that is only one object of this class may exist at a time.
It is initially created by the static create() method.

The static getGWMap() method can be called from any class to obtain a reference to the
existing GWMap object. It will return null if GWMap has not been created.

A code fragment something like the following will create a 100x100 map array. You can work
out how the variables need to be declared

gwMap = GWMap.getGWMap();
if (gwMap == null)
{
GWMap.create(100,100,100,3,(float)50.0,(float)33.0,2,4);
gwMap = GWMap.getGWMap();
}
else
gwMap.reCreate(100,100,100,3,(float)50.0,(float)33.0,2,4);
map = gwMap.Generate3();

Note that 3 increasingly sophisticated map generation algorithms can be called upon.

The map images are loaded from the GWTerrain.png file. Each image is 32x32 pixels.
The images are stored together with 20 images per row and are referenced by a
position offset. Offset 0 (the first image is sea, Offset 1 is a full land square. The
remaining land images enable a nice coastline to be developed. You will work out
exactly how to draw the image yourself. All of the map is stored in layer 0, so the
other layers are free for you to add other things which should appear on top of
the map. The default map has 100x100 squares and has 4 layers.

Use the following Graphics drawImage() method which enables you to specify source position
and destination position to draw the map.

public abstract boolean drawImage(Image img,
int dx1,
int dy1,
int dx2,
int dy2,
int sx1,
int sy1,
int sx2,
int sy2,
ImageObserver observer)

Draws as much of the specified area of the specified image as is currently available,
scaling it on the fly to fit inside the specified area of the destination drawable
surface. Transparent pixels do not affect whatever pixels are already there.
This method returns immediately in all cases, even if the image area to be drawn
has not yet been scaled, dithered, and converted for the current output device.
If the current output representation is not yet complete then drawImage returns false.
As more of the image becomes available, the process that draws the image notifies
the specified image observer.

This method always uses the unscaled version of the image to render the
scaled rectangle and performs the required scaling on the fly. It does not
use a cached, scaled version of the image for this operation.
Scaling of the image from source to destination is performed such that the
first coordinate of the source rectangle is mapped to the first coordinate
of the destination rectangle, and the second source coordinate is mapped to
the second destination coordinate. The subimage is scaled and flipped as needed
to preserve those mappings.


Parameters:
img - the specified image to be drawn
dx1 - the x coordinate of the first corner of the destination rectangle.
dy1 - the y coordinate of the first corner of the destination rectangle.
dx2 - the x coordinate of the second corner of the destination rectangle.
dy2 - the y coordinate of the second corner of the destination rectangle.
sx1 - the x coordinate of the first corner of the source rectangle.
sy1 - the y coordinate of the first corner of the source rectangle.
sx2 - the x coordinate of the second corner of the source rectangle.
sy2 - the y coordinate of the second corner of the source rectangle.
observer - object to be notified as more of the image is scaled and converted.

Returns:
true if the current output representation is complete; false otherwise.
Since:
JDK1.1

*/

import java.util.*;

class GWMap
{
private static GWMap gwMap = null;
private Random random;

// Parameters needed to construct a GWMap object
private int mapWidth = 100; // Width of map in squares
private int mapHeight = 100; // Height of map in squares
private int numIslands = 100; // Number of islands to generate
private int numContinents = 3;// Number of continents to generate
private float percentageContinent = (float)50.0; // % of land which is in continents
private float percentageLand = (float)33.0; // Overall % of land to aim for
private int iNations = 2; // Number of nations in game
private int iLayers = 4; // Number of graphical layers to display

// Arrays which hold map data
private int[][] map; // Array of indexes to tiles in layers
private int[] nationMap; // Which nation does a tile belong to? 0 = sea

// Instance variables used to generate map
private int world; // Variable to hold mapWidth*mapHeight
private int maxSize; // Max size of islands
private int contSize; // Max size of continents
private int[] origPos; // Starting square for an island
private int[] startPos; // Square from which to compute next land square
private int[] loc; // Adjacent location in 8 directions with radius 1
private int[] loc2; // Location 2 squares away
private int[] choice; // Equally near locations
private int[] adjacent; // Adjacent square in 8 directions with radius 1
private int[] adjacent2; // Content of square 2 squares away
private float[] distance; // Distance of adjacent square from original square
private float[] distance2; // Distance of square from original square
private float avDist; // Average distance from original position

public GWMap(int mapWidth,int mapHeight,int numIslands,int numContinents,
float percentageContinent,float percentageLand,int iNations,int iLayers)
{
if (gwMap != null)
{
System.out.println("GWMap already exists");
return;
}
gwMap = this;
this.mapWidth = mapWidth;
this.mapHeight = mapHeight;
this.numIslands = numIslands;
this.numContinents = numContinents;
this.percentageContinent = percentageContinent;
this.percentageLand = percentageLand;
this.iNations = iNations;
this.iLayers = iLayers;
world = mapWidth*mapHeight;
map = new int[iLayers][world];
nationMap = new int[world];
origPos = new int[numIslands];
startPos = new int[numIslands];
loc = new int[8];
loc2 = new int[16];
choice = new int[16];
adjacent = new int[8];
adjacent2 = new int[16];
distance = new float[8];
distance2 = new float[16];
}

public static GWMap getGWMap()
{
return gwMap;
}

public static void create(int mapWidth,int mapHeight,int numIslands,
int numContinents,float percentageContinent,float percentageLand,
int iNations,int iLayers)
{
// everything is taken care of in the constructor
new GWMap(mapWidth,mapHeight,numIslands,numContinents,
percentageContinent,percentageLand,iNations,iLayers);
}

public void reCreate(int mapWidth,int mapHeight,int numIslands,int numContinents,
float percentageContinent,float percentageLand,int iNations,int iLayers)
{
if (gwMap == null)
{
System.out.println("GWMap does not exist");
return;
}
gwMap = this;
this.mapWidth = mapWidth;
this.mapHeight = mapHeight;
this.numIslands = numIslands;
this.numContinents = numContinents;
this.percentageContinent = percentageContinent;
this.percentageLand = percentageLand;
this.iNations = iNations;
this.iLayers = iLayers;
world = mapWidth*mapHeight;
map = new int[iLayers][world];
nationMap = new int[world];
origPos = new int[numIslands];
startPos = new int[numIslands];
loc = new int[8];
loc2 = new int[16];
choice = new int[16];
adjacent = new int[8];
adjacent2 = new int[16];
distance = new float[8];
distance2 = new float[16];
}

public int getNumLayers()
{
return iLayers;
}

public int getMapWidth()
{
return mapWidth;
}

public int getMapHeight()
{
return mapHeight;
}

//Initialize the map
public void InitializeMap()
{
int i, j;

// Clear the map
for (i = 0;i < world;i++)
{
// Base layer gets sea = 0
map[0][i] = 0;
nationMap[i] = 0;
// Other layers get 0
for (j = 1;j < iLayers;j++)
{
map[j][i] = 0;
}
// All Land is > 0
}
}

// Distance in squares between a (original square) and b
// If b has not been wrapped, calculation is straightforward
public float Dist(int a,int b)
{
int xOrig,yOrig,xCur,yCur,x,y,oldXCur;
float d,d1,d2,d3,d4;
xOrig = a%mapWidth;
yOrig = a/mapWidth;
xCur = b%mapWidth;
yCur = b/mapWidth;
x = Math.abs(xOrig-xCur);
y = Math.abs(yOrig-yCur);
d1 = (float)Math.sqrt(x*x + y*y);
d = d1;
// Assume current wrapped. Original cannot be wrapped
// If b wrapped in x direction
oldXCur = xCur;
if (xCur < mapWidth/2)
xCur += mapWidth;
else
xCur -= mapWidth;
x = Math.abs(xOrig-xCur);
d2 = (float)Math.sqrt(x*x + y*y);
if (d2 < d)
d = d2;
// If b wrapped in x and y direction
if (yCur < mapHeight/2)
yCur += mapHeight;
else
yCur -= mapHeight;
y = Math.abs(yOrig-yCur);
d3 = (float)Math.sqrt(x*x + y*y);
if (d3 < d)
d = d3;
// if b wrapped in y direction
xCur = oldXCur;
x = Math.abs(xOrig-xCur);
d4 = (float)Math.sqrt(x*x + y*y);
if (d4 < d)
d = d4;
return (float)d;
}

// Find position of adjacent squares to sPos and store in loc.
// Store contents of these locations in adjacent.
// Store distance of these locations from the original island square
// Compute average distance from the original square
public void FindAdjacent(int j,int sPos)
{
int k;
int iThisRow;
int iNextRow;
float totalDist = (float)0.0;
float no = (float)0.0;

k = 0; // North
loc[k] = sPos;
loc[k] -= mapHeight;
if (loc[k] < 0)
{
loc[k] += world;
}
k = 2; // East
loc[k] = sPos;
iThisRow = loc[k]/mapWidth;
loc[k]++;
iNextRow = loc[k]/mapWidth;
if (iNextRow > iThisRow)
{
loc[k] -= mapWidth;
}
k = 4; // South
loc[k] = sPos;
loc[k] += mapHeight;
if (loc[k] >= world )
{
loc[k] -= world;
}
k = 6; // West
loc[k] = sPos;
iThisRow = loc[k]/mapWidth;
loc[k]--;
iNextRow = loc[k]/mapWidth;
if (iNextRow < iThisRow || loc[k] < 0)
{
loc[k] += mapWidth;
}
k = 1; // NE
loc[k] = loc[2]; // Go East
loc[k] -= mapHeight; // Go North
if (loc[k] < 0)
{
loc[k] += world;
}
k = 3; // SE
loc[k] = loc[2]; // Go East
loc[k] += mapHeight; // Go South
if (loc[k] >= world )
{
loc[k] -= world;
}
k = 5; // SW
loc[k] = loc[6]; // Go West
loc[k] += mapHeight; // Go South
if (loc[k] >= world )
{
loc[k] -= world;
}
k = 7; // NW
loc[k] = loc[6]; // Go West
loc[k] -= mapHeight; // Go North
if (loc[k] < 0)
{
loc[k] += world;
}

for (k = 0;k < 8;k++)
{
adjacent[k] = map[0][loc[k]];
distance[k] = Dist(origPos[j],loc[k]);
if (adjacent[k] != 0) // Not equal to sea
{
totalDist += distance[k];
no += 1.0;
}
}
if (no > 0.0)
avDist = totalDist/no;
else
avDist = (float)0.0;
}

// Find position of squares 2 squares away
// Store contents of these locations in adjacent2.
// Store distance of these locations from the original island square
public void FindAdjacent2(int j)
{
int k,m;
int iThisRow;
int iNextRow;

m = 0;
loc2[m] = loc[0]; // 2 squares north
loc2[m] -= mapHeight;
if (loc2[m] < 0)
{
loc2[m] += world;
}
m = 1;
loc2[m] = loc2[m-1]; // NNE 2 squares
iThisRow = loc2[m]/mapWidth;
loc2[m]++;
iNextRow = loc2[m]/mapWidth;
if (iNextRow > iThisRow)
{
loc2[m] -= mapWidth;
}
m = 2;
loc2[m] = loc2[m-1]; // Go East
iThisRow = loc2[m]/mapWidth;
loc2[m]++;
iNextRow = loc2[m]/mapWidth;
if (iNextRow > iThisRow)
{
loc2[m] -= mapWidth;
}
for (m = 3;m < 7;m++)
{
loc2[m] = loc2[m-1]; // Go South
loc2[m] += mapHeight;
if (loc2[m] >= world )
{
loc2[m] -= world;
}
}
for (m = 7;m < 11;m++)
{
loc2[m] = loc2[m-1]; // Go West
iThisRow = loc2[m]/mapWidth;
loc2[m]--;
iNextRow = loc2[m]/mapWidth;
if (iNextRow < iThisRow || loc2[m] < 0)
{
loc2[m] += mapWidth;
}
}
for (m = 11;m < 15;m++)
{
loc2[m] = loc2[m-1]; // Go North
loc2[m] -= mapHeight;
if (loc2[m] < 0)
{
loc2[m] += world;
}
}
m = 15;
loc2[m] = loc2[m-1]; // SSW+
iThisRow = loc2[m]/mapWidth;
loc2[m]++;
iNextRow = loc2[m]/mapWidth;
if (iNextRow > iThisRow)
{
loc2[m] -= mapWidth;
}

for (k = 0;k < 16;k++)
{
adjacent2[k] = map[0][loc2[k]];
distance2[k] = Dist(origPos[j],loc2[k]);
}
}

// Modify land according to surrounding sea.
public int[][] modifyLand()
{
int i,j,k,iCount;

j = 0; // Value irrelevent
for (i = 0; i < world;i++)
{
if (map[0][i] == 0)
{
if (j >= 100)
System.out.println("Island: "+j+" j too big ");
FindAdjacent(j,i);
iCount = 0;
for (k = 0;k < 8;k++)
{
if (adjacent[k] == 0) // Sea
{
iCount++;
}
}
if (iCount == 0) // location i is completely surrounded by land
map[0][i] = 14;
}
}

for (i = 0; i < world;i++)
{
//j = nationMap[i] - 1;
//if (j < 0)
// j = 0;
if (map[0][i] > 0)
{
FindAdjacent(j,i);
iCount = 0;
for (k = 0;k < 8;k++)
{
if (adjacent[k] == 0) // Sea
{
iCount++;
}
}
if (iCount == 1)
{
if (adjacent[0] == 0)
map[0][i] = 2;
else
if (adjacent[2] == 0)
map[0][i] = 3;
else
if (adjacent[4] == 0)
map[0][i] = 4;
else
if (adjacent[6] == 0)
map[0][i] = 5;
else
if (adjacent[1] == 0)
map[0][i] = 13;
else
if (adjacent[3] == 0)
map[0][i] = 10;
else
if (adjacent[5] == 0)
map[0][i] = 11;
else
if (adjacent[7] == 0)
map[0][i] = 12;
}
else
if (iCount == 2)
{
if (adjacent[0] == 0 && adjacent[2] == 0)
map[0][i] = 6;
else
if (adjacent[2] == 0 && adjacent[4] == 0)
map[0][i] = 7;
else
if (adjacent[4] == 0 && adjacent[6] == 0)
map[0][i] = 8;
else
if (adjacent[6] == 0 && adjacent[0] == 0)
map[0][i] = 9;
else
if (adjacent[3] == 0 && adjacent[7] == 0)
map[0][i] = 21;
else
if (adjacent[1] == 0 && adjacent[5] == 0)
map[0][i] = 23;
else
if (adjacent[7] == 0 && adjacent[1] == 0)
map[0][i] = 25;
else
if (adjacent[1] == 0 && adjacent[3] == 0)
map[0][i] = 20;
else
if (adjacent[3] == 0 && adjacent[5] == 0)
map[0][i] = 22;
else
if (adjacent[5] == 0 && adjacent[7] == 0)
map[0][i] = 24;
else
if (adjacent[0] == 0 && adjacent[3] == 0)
map[0][i] = 32;
else
if (adjacent[0] == 0 && adjacent[5] == 0)
map[0][i] = 33;
else
if (adjacent[2] == 0 && adjacent[5] == 0)
map[0][i] = 34;
else
if (adjacent[2] == 0 && adjacent[7] == 0)
map[0][i] = 35;
else
if (adjacent[4] == 0 && adjacent[7] == 0)
map[0][i] = 36;
else
if (adjacent[4] == 0 && adjacent[1] == 0)
map[0][i] = 37;
else
if (adjacent[6] == 0 && adjacent[1] == 0)
map[0][i] = 30;
else
if (adjacent[6] == 0 && adjacent[3] == 0)
map[0][i] = 31;
else
if (adjacent[0] == 0 && adjacent[4] == 0)
map[0][i] = 38;
else
if (adjacent[2] == 0 && adjacent[6] == 0)
map[0][i] = 39;
else
if (adjacent[0] == 0)
map[0][i] = 2;
else
if (adjacent[2] == 0)
map[0][i] = 3;
else
if (adjacent[4] == 0)
map[0][i] = 4;
else
if (adjacent[6] == 0)
map[0][i] = 5;
}
else
if (iCount == 3)
{
if (adjacent[6] == 0 && adjacent[0] == 0 && adjacent[2] == 0)
map[0][i] = 16;
else
if (adjacent[0] == 0 && adjacent[2] == 0 && adjacent[4] == 0)
map[0][i] = 17;
else
if (adjacent[2] == 0 && adjacent[4] == 0 && adjacent[6] == 0)
map[0][i] = 18;
else
if (adjacent[4] == 0 && adjacent[6] == 0 && adjacent[0] == 0)
map[0][i] = 19;
else
if (adjacent[7] == 0 && adjacent[1] == 0 && adjacent[3] == 0)
map[0][i] = 45;
else
if (adjacent[1] == 0 && adjacent[3] == 0 && adjacent[5] == 0)
map[0][i] = 46;
else
if (adjacent[3] == 0 && adjacent[5] == 0 && adjacent[7] == 0)
map[0][i] = 47;
else
if (adjacent[5] == 0 && adjacent[7] == 0 && adjacent[1] == 0)
map[0][i] = 48;
else
if (adjacent[0] == 0 && adjacent[3] == 0 && adjacent[5] == 0)
map[0][i] = 27;
else
if (adjacent[2] == 0 && adjacent[5] == 0 && adjacent[7] == 0)
map[0][i] = 28;
else
if (adjacent[4] == 0 && adjacent[7] == 0 && adjacent[1] == 0)
map[0][i] = 29;
else
if (adjacent[1] == 0 && adjacent[3] == 0 && adjacent[6] == 0)
map[0][i] = 26;
else
if (adjacent[2] == 0 && adjacent[4] == 0 && adjacent[7] == 0)
map[0][i] = 40;
else
if (adjacent[4] == 0 && adjacent[6] == 0 && adjacent[1] == 0)
map[0][i] = 41;
else
if (adjacent[6] == 0 && adjacent[0] == 0 && adjacent[3] == 0)
map[0][i] = 42;
else
if (adjacent[0] == 0 && adjacent[2] == 0 && adjacent[5] == 0)
map[0][i] = 43;
else
if (adjacent[7] == 0 && adjacent[0] == 0 && adjacent[1] == 0)
map[0][i] = 2;
else
if (adjacent[1] == 0 && adjacent[2] == 0 && adjacent[3] == 0)
map[0][i] = 3;
else
if (adjacent[3] == 0 && adjacent[4] == 0 && adjacent[5] == 0)
map[0][i] = 4;
else
if (adjacent[5] == 0 && adjacent[6] == 0 && adjacent[7] == 0)
map[0][i] = 5;
else
if (adjacent[0] == 0 && adjacent[2] == 0)
map[0][i] = 6;
else
if (adjacent[2] == 0 && adjacent[4] == 0)
map[0][i] = 7;
else
if (adjacent[4] == 0 && adjacent[6] == 0)
map[0][i] = 8;
else
if (adjacent[6] == 0 && adjacent[0] == 0)
map[0][i] = 9;
else
if (adjacent[0] == 0 && adjacent[4] == 0)
map[0][i] = 38;
else
if (adjacent[2] == 0 && adjacent[6] == 0)
map[0][i] = 39;
else
if (adjacent[0] == 0 && adjacent[3] == 0)
map[0][i] = 32;
else
if (adjacent[0] == 0 && adjacent[5] == 0)
map[0][i] = 33;
else
if (adjacent[2] == 0 && adjacent[5] == 0)
map[0][i] = 34;
else
if (adjacent[2] == 0 && adjacent[7] == 0)
map[0][i] = 35;
else
if (adjacent[4] == 0 && adjacent[7] == 0)
map[0][i] = 36;
else
if (adjacent[4] == 0 && adjacent[1] == 0)
map[0][i] = 37;
else
if (adjacent[6] == 0 && adjacent[1] == 0)
map[0][i] = 30;
else
if (adjacent[6] == 0 && adjacent[3] == 0)
map[0][i] = 31;
}
else
if (iCount == 4)
{
if (adjacent[0] == 0 && adjacent[2] == 0 && adjacent[4] == 0 && adjacent[6] == 0)
map[0][i] = 15;
else
if (adjacent[1] == 0 && adjacent[3] == 0 && adjacent[5] == 0 && adjacent[7] == 0)
map[0][i] = 44;
else
if (adjacent[6] == 0 && adjacent[0] == 0 && adjacent[2] == 0)
map[0][i] = 16;
else
if (adjacent[0] == 0 && adjacent[2] == 0 && adjacent[4] == 0)
map[0][i] = 17;
else
if (adjacent[2] == 0 && adjacent[4] == 0 && adjacent[6] == 0)
map[0][i] = 18;
else
if (adjacent[4] == 0 && adjacent[6] == 0 && adjacent[0] == 0)
map[0][i] = 19;
else
if (adjacent[2] == 0 && adjacent[4] == 0 && adjacent[7] == 0)
map[0][i] = 40;
else
if (adjacent[4] == 0 && adjacent[6] == 0 && adjacent[1] == 0)
map[0][i] = 41;
else
if (adjacent[6] == 0 && adjacent[0] == 0 && adjacent[3] == 0)
map[0][i] = 42;
else
if (adjacent[0] == 0 && adjacent[2] == 0 && adjacent[5] == 0)
map[0][i] = 43;
else
if (adjacent[0] == 0 && adjacent[2] == 0)
map[0][i] = 6;
else
if (adjacent[2] == 0 && adjacent[4] == 0)
map[0][i] = 7;
else
if (adjacent[4] == 0 && adjacent[6] == 0)
map[0][i] = 8;
else
if (adjacent[6] == 0 && adjacent[0] == 0)
map[0][i] = 9;
else
if (adjacent[0] == 0 && adjacent[4] == 0)
map[0][i] = 38;
else
if (adjacent[2] == 0 && adjacent[6] == 0)
map[0][i] = 39;
else
if (adjacent[0] == 0 && adjacent[3] == 0)
map[0][i] = 32;
else
if (adjacent[0] == 0 && adjacent[5] == 0)
map[0][i] = 33;
else
if (adjacent[2] == 0 && adjacent[5] == 0)
map[0][i] = 34;
else
if (adjacent[2] == 0 && adjacent[7] == 0)
map[0][i] = 35;
else
if (adjacent[4] == 0 && adjacent[7] == 0)
map[0][i] = 36;
else
if (adjacent[4] == 0 && adjacent[1] == 0)
map[0][i] = 37;
else
if (adjacent[6] == 0 && adjacent[1] == 0)
map[0][i] = 30;
else
if (adjacent[6] == 0 && adjacent[3] == 0)
map[0][i] = 31;
}
else
if (iCount == 5)
{
if (adjacent[0] == 0 && adjacent[2] == 0 && adjacent[4] == 0 && adjacent[6] == 0)
map[0][i] = 15;
else
if (adjacent[6] == 0 && adjacent[0] == 0 && adjacent[2] == 0)
map[0][i] = 16;
else
if (adjacent[0] == 0 && adjacent[2] == 0 && adjacent[4] == 0)
map[0][i] = 17;
else
if (adjacent[2] == 0 && adjacent[4] == 0 && adjacent[6] == 0)
map[0][i] = 18;
else
if (adjacent[4] == 0 && adjacent[6] == 0 && adjacent[0] == 0)
map[0][i] = 19;
else
if (adjacent[2] == 0 && adjacent[4] == 0 && adjacent[7] == 0)
map[0][i] = 40;
else
if (adjacent[4] == 0 && adjacent[6] == 0 && adjacent[1] == 0)
map[0][i] = 41;
else
if (adjacent[6] == 0 && adjacent[0] == 0 && adjacent[3] == 0)
map[0][i] = 42;
else
if (adjacent[0] == 0 && adjacent[2] == 0 && adjacent[5] == 0)
map[0][i] = 43;
else
if (adjacent[0] == 0 && adjacent[2] == 0)
map[0][i] = 6;
else
if (adjacent[2] == 0 && adjacent[4] == 0)
map[0][i] = 7;
else
if (adjacent[4] == 0 && adjacent[6] == 0)
map[0][i] = 8;
else
if (adjacent[6] == 0 && adjacent[0] == 0)
map[0][i] = 9;
else
if (adjacent[0] == 0 && adjacent[4] == 0)
map[0][i] = 38;
else
if (adjacent[2] == 0 && adjacent[6] == 0)
map[0][i] = 39;
else
if (adjacent[0] == 0 && adjacent[3] == 0)
map[0][i] = 32;
else
if (adjacent[0] == 0 && adjacent[5] == 0)
map[0][i] = 33;
else
if (adjacent[2] == 0 && adjacent[5] == 0)
map[0][i] = 34;
else
if (adjacent[2] == 0 && adjacent[7] == 0)
map[0][i] = 35;
else
if (adjacent[4] == 0 && adjacent[7] == 0)
map[0][i] = 36;
else
if (adjacent[4] == 0 && adjacent[1] == 0)
map[0][i] = 37;
else
if (adjacent[6] == 0 && adjacent[1] == 0)
map[0][i] = 30;
else
if (adjacent[6] == 0 && adjacent[3] == 0)
map[0][i] = 31;
}
else
if (iCount == 6)
{
if (adjacent[0] == 0 && adjacent[2] == 0 && adjacent[4] == 0 && adjacent[6] == 0)
map[0][i] = 15;
else
if (adjacent[6] == 0 && adjacent[0] == 0 && adjacent[2] == 0)
map[0][i] = 16;
else
if (adjacent[0] == 0 && adjacent[2] == 0 && adjacent[4] == 0)
map[0][i] = 17;
else
if (adjacent[2] == 0 && adjacent[4] == 0 && adjacent[6] == 0)
map[0][i] = 18;
else
if (adjacent[4] == 0 && adjacent[6] == 0 && adjacent[0] == 0)
map[0][i] = 19;
else
if (adjacent[2] == 0 && adjacent[4] == 0 && adjacent[7] == 0)
map[0][i] = 40;
else
if (adjacent[4] == 0 && adjacent[6] == 0 && adjacent[1] == 0)
map[0][i] = 41;
else
if (adjacent[6] == 0 && adjacent[0] == 0 && adjacent[3] == 0)
map[0][i] = 42;
else
if (adjacent[0] == 0 && adjacent[2] == 0 && adjacent[5] == 0)
map[0][i] = 43;
else
if (adjacent[0] == 0 && adjacent[4] == 0)
map[0][i] = 38;
else
if (adjacent[2] == 0 && adjacent[6] == 0)
map[0][i] = 39;
}
else
if (iCount == 7)
{
if (adjacent[0] == 0 && adjacent[2] == 0 && adjacent[4] == 0 && adjacent[6] == 0)
map[0][i] = 15;
else
if (adjacent[6] == 0 && adjacent[0] == 0 && adjacent[2] == 0)
map[0][i] = 16;
else
if (adjacent[0] == 0 && adjacent[2] == 0 && adjacent[4] == 0)
map[0][i] = 17;
else
if (adjacent[2] == 0 && adjacent[4] == 0 && adjacent[6] == 0)
map[0][i] = 18;
else
if (adjacent[4] == 0 && adjacent[6] == 0 && adjacent[0] == 0)
map[0][i] = 19;
}
else
if (iCount == 8) // location i is surrounded completely by sea
map[0][i] = 15;
}
}

// Count land
iCount = 0;
for (i = 0; i < world;i++)
{
if (map[0][i] > 0)
iCount++;
}
//System.out.println("Percent land "+(iCount*100)/world);
for(int t = 0; t < map[0].length; t++)
{
if(map[0][t] == 1)
{
int temp = (int)Math.round(Math.random() * 12 + 1);
if(temp < 7)
map[0][t] = 48 + temp;
}
}
return map;
}

// Random location of islands
// Build land by choosing NSEW direction for next land square randomly
// and by allowing 50% chance of returning to previous land to promote
// rounder land
// Allow islands to merge
// Continuous world (land wraps at edges)
// Produces worlds with long, thin straggled islands
public int[][] Generate1()
{
int iRandDir;
int i,j;
int iThisRow;
int iNextRow;
int islandSize;
int iCount;//,iPick;
int savePos = 0;
float dist; // minimum distance

// Factor of 2 would have correct if land did not duplicate.
maxSize = (int)(10.0*(float)world*percentageLand/(100.0*(float)numIslands));
System.out.println("Max island size " + maxSize);

// Clear the map
InitializeMap();

// Seed the random-number generator with current time so that
// the numbers will be different every time we run.
random = new Random();

// Randomly create starting locations
boolean tooClose = false;
boolean loop = true;
for (i = 0;i < numIslands;i++)
{
// Set island starting positions
while (loop)
{
startPos[i] = random.nextInt(world);
for (j = 0;j < i;j++)
{
dist = Dist(startPos[i],startPos[j]);
if (dist <= 2.0)
{
tooClose = true;
break;
}
}
if (!tooClose)
loop = false;
tooClose = false;
}
loop = true;
origPos[i] = startPos[i];

// Place land at these positions
map[0][startPos[i]] = 1;
nationMap[startPos[i]] = i + 1;
}

// Move around
for (j = 0;j < numIslands;j++)
{
islandSize = random.nextInt(maxSize);
System.out.println("Island: "+j+" Size "+islandSize+
" Start "+startPos[j]);
i = 0;
iCount = 0;
while (i < islandSize)
{
iRandDir = random.nextInt(4);

// Move up
if (iRandDir == 0)
{
savePos = startPos[j];
startPos[j] -= mapWidth;
if (startPos[j] < 0)
{
startPos[j] += world;
}
}
// Move right
else
if (iRandDir == 1)
{
savePos = startPos[j];
iThisRow = startPos[j]/mapWidth;
startPos[j]++;
iNextRow = startPos[j]/mapWidth;
if (iNextRow > iThisRow)
{
startPos[j] -= mapWidth;
}
}
// Move down
else
if (iRandDir == 2)
{
savePos = startPos[j];
startPos[j] += mapWidth;
if (startPos[j] >= world )
{
startPos[j] -= world;
}
}
// Move left
else
if (iRandDir == 3)
{
savePos = startPos[j];
iThisRow = startPos[j]/mapWidth;
startPos[j]--;
iNextRow = startPos[j]/mapWidth;
if (iNextRow < iThisRow || startPos[j] < 0)
{
startPos[j] += mapWidth;
}
}

// Place land at these positions
if (startPos[j] < 0 || startPos[j] >= 10000)
{
System.out.println("Location "+j+" "+startPos[j]+
" Rand dir "+iRandDir);
}
map[0][startPos[j]] = 1;
nationMap[startPos[j]] = j + 1;
// 50% probability of returning to previous location
// to promote a rounder land area
if (random.nextInt(2) == 0)
startPos[j] = savePos;
i++;
}
}
return modifyLand();
}

// -- TYPE 2 --
// Random location of islands
// Method finds 4 adjacent squares and then selects one of these providing.
// it is a sea square. First criteria for selection is that the sea square is the
// one choice to the original land square. If there is more than one equally near,
// then that square is chosen randomly between them.
// Islands can merge if they have diagonally adjacent land.
// Continuous world (land wraps at edges)
// Produces world with very rounded islands
public int[][] Generate2()
{
int i,j,k,m;
int iThisRow;
int iNextRow;
int islandSize;
int iCount;//,iPick;
int iSize;
float dist; // minimum distance
float newDist;

// Factor of 2 means average size should be about right
maxSize = (int)(2.0*(float)mapWidth*(float)mapHeight*percentageLand/
(100.0*(float)numIslands));
System.out.println("Max island size "+maxSize);

// Clear the map
InitializeMap();

// Seed the random-number generator with current time so that
// the numbers will be different every time we run.
random = new Random();

// Randomly create starting locations
boolean tooClose = false;
boolean loop = true;
for (i = 0;i < numIslands;i++)
{
// Set island starting positions
while (loop)
{
startPos[i] = random.nextInt(world);
for (j = 0;j < i;j++)
{
dist = Dist(startPos[i],startPos[j]);
if (dist <= 6.0)
{
tooClose = true;
break;
}
}
if (!tooClose)
loop = false;
tooClose = false;
}
loop = true;
origPos[i] = startPos[i];

// Place land at these positions
map[0][startPos[i]] = 1;
nationMap[startPos[i]] = i + 1;
}

// Generate islands one by one
for (j = 0;j < numIslands;j++)
{
islandSize = random.nextInt(maxSize);
iSize = 0;
System.out.println("Island: "+j+" Size "+islandSize+
" Start "+startPos[j]);
i = 0;
while (i < islandSize)
{
// Find adjacent 4 locations. Store in loc[k].
// Store content of these squares in adjacent[k]
for (k = 0; k < 4;k++)
{
loc[k] = startPos[j];
if (k == 0) // North
{
loc[k] -= mapHeight;
if (loc[k] < 0)
{
loc[k] += world;
}
}
else
if (k == 1) // East
{
iThisRow = loc[k]/mapWidth;
loc[k]++;
iNextRow = loc[k]/mapWidth;
if (iNextRow > iThisRow)
{
loc[k] -= mapWidth;
}
}
else
if (k == 2) // South
{
loc[k] += mapHeight;
if (loc[k] >= world )
{
loc[k] -= world;
}
}
else
if (k == 3) // West
{
iThisRow = loc[k]/mapWidth;
loc[k]--;
iNextRow = loc[k]/mapWidth;
if (iNextRow < iThisRow || loc[k] < 0)
{
loc[k] += mapWidth;
}
}
adjacent[k] = map[0][loc[k]];
distance[k] = Dist(origPos[j],loc[k]);
}
// Count number of adjacent sea locations
iCount = 0;
for (k = 0;k < 4;k++)
{
if (adjacent[k] == 0) // Sea
iCount++;
}
// If no adjacent sea squares stop generating this island
if (iCount == 0)
{
System.out.println("Break - no adjacent sea squares");
break;
}
// Select locations nearer or further to the original land square
// depending on strategy
m = 0;
boolean isNearest;
int strategy;
strategy = random.nextInt(100);
if (strategy > 19) // Choose nearest 80% of time
{
dist = (float)9999.0;
isNearest = true;
}
else
{
dist = -1;
isNearest = false;
}
iCount = 0;
for (k = 0;k < 4;k++)
{
if (adjacent[k] == 0)
{
newDist = distance[k];
if (isNearest)
{
if (newDist < dist)
{
dist = newDist;
iCount = 1;
choice[0] = loc[k];
m = 0;
}
else
if (newDist == dist)
{
iCount++;
m++;
choice[m] = loc[k];
}
}
else
{
if (newDist > dist)
{
dist = newDist;
iCount = 1;
choice[0] = loc[k];
m = 0;
}
else
if (newDist == dist)
{
iCount++;
m++;
choice[m] = loc[k];
}
}
}
}
if (iCount == 1) // Only 1 choice, choose this one
{
iSize++;
map[0][choice[0]] = 1;
startPos[j] = choice[0];
nationMap[startPos[j]] = j + 1;
}
// Greater than 1 choice, randomly pick 1
else
if (iCount > 1)
{
m = random.nextInt(iCount);
iSize++;
map[0][choice[m]] = 1;
nationMap[startPos[j]] = j + 1;
// Some probability of returning to previous location
// providing that square has an adjacent sea square
//if (rand()%iCount == 0)
if (isNearest)
startPos[j] = choice[m];
}
i++;
}
System.out.println("Island: "+j+" Actual Size "+iSize);
}
return modifyLand();
}

// -- TYPE 3 --
// Random location of islands. Size is randomly chosen.
// Method finds 8 adjacent squares and the 16 next most adjacent squares.
// It then selects one of the adjacent squares providing it is a sea square
// according to the following algorithm
// It determines all squares that are (1) equally near to the original land square
// or (2) equally far from the original land square. The choice between (1) and (2)
// is random with the probability of selecting (1) set to value stored in strategy.
// If (1) is set to 100%, the result is very rounded islands. As this probability
// is reduced, the islands become less orderly which is desired, but they also
// contain more inland lakes and become more anf more stratified. The default is
// 90%. An attempt is also made to keep the islands separated from each other. This
// done by trying to keep the land 2 squares away from the starting square sea if
// it is further away from the original land square of the island then the sea
// being examined as the next possible land square.
//
// Continuous world (land wraps at edges)
// Produces world with somewhat rounded islands, but with interesting deviations
public int[][] Generate3()
{
int i,j,k,m;
int iThisRow;
int iNextRow;
int islandSize;
int iCount;//,iPick;
int iSize;
float dist; // minimum distance

// These are fixed size
contSize = (int)((float)mapWidth*(float)mapHeight*percentageLand*percentageContinent/
(100.0*100.0*(float)numContinents));
// Factor of 2 means average size should be about right
maxSize = (int)(2.0*(float)mapWidth*(float)mapHeight*percentageLand*
(100.0 - percentageContinent)/(100.0*100.0*(float)numIslands));
//System.out.println("Max island size "+maxSize);

// Clear the map
InitializeMap();

// Seed the random-number generator with current time so that
// the numbers will be different every time we run.
random = new Random();

// Randomly create starting locations
boolean tooClose = false;
boolean loop;
boolean land;
int iAdj;
int iAdj2;
for (j = 0;j < numIslands;j++)
{
// Set island starting positions
// Position continents first as far apart as possible
loop = true;
while (loop)
{
land = true;
while (land)
{
startPos[j] = random.nextInt(world);
//System.out.println("Island: "+j+" Start "+startPos[j]);
FindAdjacent(j,startPos[j]);
iAdj = 0;
for (k = 0;k < 8;k++)
{
if (adjacent[k] == 0) // Sea
iAdj++;
}
FindAdjacent2(j);
iAdj2 = 0;
for (k = 0;k < 16;k++)
{
if (adjacent2[k] == 0) // Sea
iAdj2++;
}
if ((map[0][startPos[j]] == 0) && (iAdj == 8) && (iAdj2 == 16))
land = false;
}
for (i = 0;i < j;i++)
{
dist = Dist(startPos[j],startPos[i]);
if ((j < numContinents) && (dist <= mapWidth/(numContinents + 1)))
{
tooClose = true;
break;
}
else
if (dist <= 3.0)
{
tooClose = true;
break;
}
}
if (!tooClose)
loop = false;
tooClose = false;
}
origPos[j] = startPos[j];

// Place land at these positions
map[0][startPos[j]] = 1;
nationMap[startPos[j]] = j + 1;

// Generate islands one by one
if (j < numContinents) // Continents first
{
islandSize = contSize;
}
else
islandSize = random.nextInt(maxSize) + 1;
iSize = 0;
//System.out.println("Island: "+j+" Size "+islandSize+
// " Start "+startPos[j]);
i = 0;
while (i < islandSize)
{
for (k = 0;k < 16;k++)
choice[k] = 0;
// Find adjacent 8 locations. Store in loc[k].
// Store content of these squares in adjacent[k]
FindAdjacent(j,startPos[j]);
// Find squares two away from start position
// Sore these in adjacent2
FindAdjacent2(j);
// Select locations nearer or further to the original land square
// depending on strategy
boolean isNearest;
int strategy;
int boundary;
if (j < numContinents)
boundary = 20;
else
boundary = 10;
strategy = random.nextInt(100);
if (strategy >= boundary)
{
dist = (float)9999.0;
isNearest = true;
}
else
{
dist = -1;
isNearest = false;
}
int early,before,centre,after,late;
boolean OK;
iCount = 0;
m = 0;
k = 0;
while (k < 8)
{
OK = false;
if (k%2 == 0) // Even k (centre squares)
{
if (k == 0)
{
before = 15;
centre = 0;
after = 1;
}
else
{
before = 2*k - 1;
centre = before + 1;
after = centre + 1;
}
if (adjacent[k] == 0) // Sea
{
if ((distance2[centre] > distance[k] && adjacent2[centre] == 0) ||
(distance2[centre] <= distance[k]))
{
if ((distance2[before] > distance[k] && adjacent2[before] == 0) ||
(distance2[before] <= distance[k]))
{
if ((distance2[after] > distance[k] && adjacent2[after] == 0) ||
(distance2[after] <= distance[k]))
{
OK = true;
}
}
}
}
}
else // Odd k (corner squares)
{
if (k == 7)
{
early = 12;
before = 13;
centre = 14;
after = 15;
late = 0;
}
else
{
early = 2*(k - 1);
before = early + 1;
centre = before + 1;
after = centre + 1;
late = after + 1;
}
if (adjacent[k] == 0)
{
if ((distance2[centre] > distance[k] && adjacent2[centre] == 0) ||
(distance2[centre] <= distance[k]))
{
if ((distance2[before] > distance[k] && adjacent2[before] == 0) ||
(distance2[before] <= distance[k]))
{
if ((distance2[after] > distance[k] && adjacent2[after] == 0) ||
(distance2[after] <= distance[k]))
{
if ((distance2[early] > distance[k] && adjacent2[early] == 0) ||
(distance2[early] <= distance[k]))
{
if ((distance2[late] > distance[k] && adjacent2[late] == 0) ||
(distance2[late] <= distance[k]))
{
OK = true;
}
}
}
}
}
}
}
if (OK)
{
//if (dist < avDist)
// dist = avDist;
if (isNearest)
{
if (distance[k] < dist)
{
dist = distance[k];
iCount = 1;
choice[0] = loc[k];
m = 0;
}
else
if (distance[k] == dist)
{
iCount++;
m++;
choice[m] = loc[k];
}
}
else
{
if (distance[k] > dist)
{
dist = distance[k];
iCount = 1;
choice[0] = loc[k];
m = 0;
}
else
if (distance[k] == dist)
{
iCount++;
m++;
choice[m] = loc[k];
}
}
}
k += 1;
}
if (iCount == 0) // No choices. Relax condition and try again
{
// Count number of adjacent sea locations
m = 0;
for (k = 0;k < 8;k++)
{
if (adjacent[k] == 0) // Sea
{
choice[m] = loc[k];
m++;
iCount++;
}
}
}
if (iCount == 0) // No choices. Relax condition more and try again
{
// Count number of sea locations two squares away
m = 0;
for (k = 0;k < 16;k++)
{
if (adjacent2[k] == 0) // Sea
{
choice[m] = loc2[k];
m++;
iCount++;
}
}
}
if (iCount == 0)
{
int loc;
int times;
// Randomly choose adjacent2 square not a corner
m = random.nextInt(12);
if (m < 3) // Choose adjacent2[0] and go north until sea
{
loop = true;
times = 0;
loc = loc2[0];
while (loop)
{
times++;
if (times == mapHeight)
break;
loc -= mapHeight;
if (loc < 0)
{
loc += world;
}
if (map[0][loc] == 0)
loop = false;
}
if (times < mapHeight)
{
choice[0] = loc;
iCount = 1;
}
else
iCount = 0;
}
else
if (m < 6) // Choose adjacent2[4] and go east until sea
{
loop = true;
times = 0;
loc = loc2[4];
while (loop)
{
times++;
if (times == mapHeight)
break;
iThisRow = loc/mapWidth;
loc++;
iNextRow = loc/mapWidth;
if (iNextRow > iThisRow)
{
loc -= mapWidth;
}
if (map[0][loc] == 0)
loop = false;
}
if (times < mapHeight)
{
choice[0] = loc;
iCount = 1;
}
else
iCount = 0;
}
else
if (m < 9) // Choose adjacent2[8] and go south until sea
{
loop = true;
times = 0;
loc = loc2[8];
while (loop)
{
times++;
if (times == mapHeight)
break;
loc += mapHeight;
if (loc >= world )
{
loc -= world;
}
if (map[0][loc] == 0)
loop = false;
}
if (times < mapHeight)
{
choice[0] = loc;
iCount = 1;
}
else
iCount = 0;
}
else
if (m < 12) // Choose adjacent2[12] and go west until sea
{
loop = true;
times = 0;
loc = loc2[12];
while (loop)
{
times++;
if (times == mapHeight)
break;
iThisRow = loc/mapWidth;
loc--;
iNextRow = loc/mapWidth;
if (iNextRow < iThisRow || loc < 0)
{
loc += mapWidth;
}
if (map[0][loc] == 0)
loop = false;
}
if (times < mapHeight)
{
choice[0] = loc;
iCount = 1;
}
else
iCount = 0;
}
}
if (iCount == 0) // No choices. Exit island creation
{
System.out.println("Break - no squares can be found");
break;
}
// Only 1 choice, choose this one
else
if (iCount == 1)
{
iSize++;
map[0][choice[0]] = 1;
startPos[j] = choice[0];
nationMap[startPos[j]] = j + 1;
}
// Greater than 1 choice, randomly pick 1
else
{
m = random.nextInt(iCount);
iSize++;
map[0][choice[m]] = 1;
// Some probability of returning to previous location
// providing that square has an adjacent sea square
//if (rand()%iCount == 0)
//if (isNearest)
startPos[j] = choice[m];
nationMap[startPos[j]] = j + 1;
}
i++;
}
//System.out.println("Island: "+j+" Actual Size "+iSize);
}

return modifyLand();
}

}



//LaughButton
import java.applet.*;
import java.net.*;
import javax.swing.*;

/**
* Title: LaughButton.java
* Description: This class plays the fight sounds when units clash
* @author: Shane Grund
* @version: 1.0
*/
public class LaughButton implements Runnable
{
AudioClip laugh = null;//new AudioClip();
Thread runner;

/**
* Constructor sets up sound to be played and plays it and
* then stops playing it.
*/
LaughButton()
{
try
{
URL laughIn = new URL("file:sounds/foo.wav");
laugh = JApplet.newAudioClip(laughIn);
startLaughing();
stopLaughing();
} catch (MalformedURLException e) { }

}//end laughButton

void startLaughing()
{
if (runner == null)
{
runner = new Thread(this);
runner.start();
}
}//end start laughing

void stopLaughing()
{
if (runner != null)
{
laugh.stop();
runner = null;
}
}//end stop laughing

public void run()
{
laugh.play();
Thread thisThread = Thread.currentThread();
while (runner == thisThread)
{
try
{
Thread.sleep(5000);
} catch (InterruptedException e) { }
}//end while
}//end run
}//end class



//MainGUI
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

/**
* Title: MainGUI.java
* Description: This is the main class of this application. It starts the game
* and also sets up most other screens ready to be shown. It also
* handles some events of other panels.
* @author: Shane Grund
* @version: 1.0
*/
public class MainGUI extends GameCore implements ActionListener, Runnable
{
private JButton playButton, aboutButton, quitButton;
private JButton createButton, saveButton;
private JButton unitButton, doneButton;
private JFrame frame;
private JPanel eastPanel, buttonPanel, southPanel;

private AboutPanel aboutPanel;
private MapPanel mapPanel;
private MapCreation mapCreation;
private UnitTable unitTable;

private Image bgImage;

private Container contentPane;
private boolean gameStarted = false;
private boolean isFullScreen = true;

/**
* Constructor sets up the screen, gets a full screen to start with and
* sets up all components to be shown on the content pane.
*/
public void init()
{
super.init();

// make sure Swing components don't paint themselves
RepaintManager repaintManager = new RepaintManager();
repaintManager.setDoubleBufferingEnabled(false);
RepaintManager.setCurrentManager(repaintManager);

//load image
bgImage = loadImage("images/background.gif");

// create buttons
quitButton = createButton("quit", "Quit");
playButton = createButton("play", "Start New Game");
aboutButton = createButton("config", "About");
unitButton = createButton("unit","View Units");

frame = super.screen.getFullScreenWindow();
contentPane = frame.getContentPane();

// make sure the content pane is transparent
if (contentPane instanceof JComponent)
{
((JComponent)contentPane).setOpaque(false);
}//end if

//create a border
Border border = BorderFactory.createLineBorder(Color.black);

//create the aboutDialog
aboutPanel = new AboutPanel();
aboutPanel.setBorder(border);
aboutPanel.setVisible(false);
aboutPanel.setSize(aboutPanel.getPreferredSize());

// center the dialog
aboutPanel.setLocation(
(screen.getWidth() - aboutPanel.getWidth()) / 2,
(screen.getHeight() - aboutPanel.getHeight()) / 2);

//create the unit panel
unitTable = new UnitTable();
doneButton = new JButton("Close");
doneButton.addActionListener(this);
unitTable.bottomPanel.add(doneButton);
unitTable.setBorder(border);
unitTable.setVisible(false);
unitTable.setSize(600,360);

//center the panel
unitTable.setLocation(100,130);

//create the mapCreation dialog
mapCreation = new MapCreation();
buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
createButton = new JButton("Generate Map");
createButton.addActionListener(this);
buttonPanel.add(createButton);
mapCreation.add(buttonPanel);
mapCreation.setBorder(border);
mapCreation.setVisible(false);
mapCreation.setSize(aboutPanel.getPreferredSize());

// center the dialog
mapCreation.setLocation(
(screen.getWidth() - aboutPanel.getWidth()) / 2,
(screen.getHeight() - aboutPanel.getHeight()) / 2);

// add the dialog to the "modal dialog" layer of the screen's layered pane.
screen.getFullScreenWindow().getLayeredPane().add(aboutPanel, JLayeredPane.MODAL_LAYER);
screen.getFullScreenWindow().getLayeredPane().add(mapCreation, JLayeredPane.MODAL_LAYER);
screen.getFullScreenWindow().getLayeredPane().add(unitTable, JLayeredPane.MODAL_LAYER);

//set up east panel
eastPanel = new JPanel(new FlowLayout());
eastPanel.setOpaque(false);
eastPanel.add(playButton);
eastPanel.add(unitButton);
eastPanel.add(aboutButton);
eastPanel.add(quitButton);

//set up the north and south panel
southPanel = new JPanel(new BorderLayout());
southPanel.setOpaque(false);
southPanel.add(eastPanel, BorderLayout.NORTH);

// add components to the screen's content pane
contentPane.setLayout(new BorderLayout());
contentPane.add(southPanel, BorderLayout.SOUTH);

// explicitly layout components (needed on some systems)
frame.validate();
}//end constructot

public static void main(String[] args)
{
new MainGUI().run();
}//end main

/**
Extends GameCores functionality to draw all
Swing components.
*/
public void draw(Graphics2D g)
{
g.drawImage(bgImage, 0, 0, null);
frame = super.screen.getFullScreenWindow();
frame.getLayeredPane().paintComponents(g);
}//end draw

/**
Called by the AWT event dispatch thread when a button is
pressed.
*/
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == quitButton)//quit
{
System.exit(0);
}//end if

else if (e.getSource() == aboutButton)//show about dialog
{
aboutPanel.setVisible(true);
}//end if

else if (e.getSource() == playButton)//start game
{
if(gameStarted == false)
{
mapCreation.setVisible(true);
}
gameStarted = true;
}//end if

else if(e.getSource() == unitButton)//view units in a jTable
{
unitTable.setVisible(true);
}//end else if

else if(e.getSource() == doneButton)//close jTable
{
unitTable.setVisible(false);
if(gameStarted == true)
mapPanel.requestFocus();
}//end else if

else if(e.getSource() == createButton)//create new map
{
mapCreation.setVisible(false);
int size = 0;
int style = 0;
if(mapCreation.largeButton.isSelected())
size = 100;
else if(mapCreation.mediumButton.isSelected())
size = 75;
else
size = 50;
if(mapCreation.continentButton.isSelected())
style = 1;
else if(mapCreation.variedButton.isSelected())
style = 2;
else
style = 3;
mapPanel = new MapPanel(size, style);
mapCreation.setVisible(false);
contentPane.add(mapPanel, BorderLayout.CENTER);
mapPanel.setVisible(true);
eastPanel.setBackground(Color.black);
eastPanel.setOpaque(true);
mapPanel.requestFocus();

PlayMidi pm = new PlayMidi("sounds/The_Salt_Flats.mid");
}//end if
}//end actionPerformed

public JButton createButton(String name, String toolTip)//create a new JButton
{
// load the image
String imagePath = "images/menu/" + name + ".png";
ImageIcon iconRollover = new ImageIcon(imagePath);
int w = iconRollover.getIconWidth();
int h = iconRollover.getIconHeight();

// get the cursor for this button
Cursor cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);

// make translucent default image
Image image = screen.createCompatibleImage(w, h,Transparency.TRANSLUCENT);
Graphics2D g = (Graphics2D)image.getGraphics();
Composite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f);
g.setComposite(alpha);
g.drawImage(iconRollover.getImage(), 0, 0, null);
g.dispose();
ImageIcon iconDefault = new ImageIcon(image);

// make a pressed iamge
image = screen.createCompatibleImage(w, h,Transparency.TRANSLUCENT);
g = (Graphics2D)image.getGraphics();
g.drawImage(iconRollover.getImage(), 2, 2, null);
g.dispose();
ImageIcon iconPressed = new ImageIcon(image);

// create the button
JButton button = new JButton();
button.addActionListener(this);
button.setIgnoreRepaint(true);
button.setFocusable(false);
button.setToolTipText(toolTip);
button.setBorder(null);
button.setContentAreaFilled(false);
button.setCursor(cursor);
button.setIcon(iconDefault);
button.setRolloverIcon(iconRollover);
button.setPressedIcon(iconPressed);

return button;
}//end create Button
}//end menu test



//MapCreation
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

/**
* Title: MapCreation.java
* Description: This class allows the user to customize the map size and
* map attributes
* @author: Shane Grund
* @version: 1.0
*/
public class MapCreation extends JPanel
{
private JLabel smallLabel, largeLabel, mediumLabel;
private JLabel continentLabel, variedLabel, islandLabel;
private JLabel mainLabel;
private ImageIcon smallIcon, largeIcon, mediumIcon;
private ImageIcon continentIcon, variedIcon, islandIcon;
protected JRadioButton smallButton, mediumButton, largeButton;
protected JRadioButton continentButton, variedButton, islandButton;
private ButtonGroup sizeGroup, styleGroup;

private JPanel sizePanel, stylePanel;

/**
* Constructor allows for set up of the panel
*/
public MapCreation()
{
//panel set up
this.setLayout(new GridLayout(0,1));

//set up labels
smallIcon = new ImageIcon("images/MapCreation/smallWorld.png");
smallLabel = new JLabel(smallIcon);

mediumIcon = new ImageIcon("images/MapCreation/mediumWorld.png");
mediumLabel = new JLabel(mediumIcon);

largeIcon = new ImageIcon("images/MapCreation/largeWorld.png");
largeLabel = new JLabel(largeIcon);

continentIcon = new ImageIcon("images/MapCreation/Continent.png");
continentLabel = new JLabel(continentIcon);

variedIcon = new ImageIcon("images/MapCreation/ContinentAndIsland.png");
variedLabel = new JLabel(variedIcon);

islandIcon = new ImageIcon("images/MapCreation/Islands.png");
islandLabel = new JLabel(islandIcon);

mainLabel = new JLabel("New Map Creation");
mainLabel.setForeground(new Color(51, 0, 51));
mainLabel.setFont(new Font("Dialog", 1, 32));
mainLabel.setHorizontalAlignment(SwingConstants.CENTER);

//set up the radioButtons
smallButton = new JRadioButton("Small 50x50");
mediumButton = new JRadioButton("Medium 75x75");
largeButton = new JRadioButton("Large 100x100");
largeButton.setSelected(true);

sizeGroup = new ButtonGroup();
sizeGroup.add(smallButton);
sizeGroup.add(mediumButton);
sizeGroup.add(largeButton);

continentButton = new JRadioButton("Continents");
variedButton = new JRadioButton("Varied");
variedButton.setSelected(true);
islandButton = new JRadioButton("Islands");

styleGroup = new ButtonGroup();
styleGroup.add(continentButton);
styleGroup.add(variedButton);
styleGroup.add(islandButton);


//set up the panels
sizePanel = new JPanel(new GridLayout(2,3));
sizePanel.setBorder(BorderFactory.createTitledBorder("Please select map size"));
sizePanel.add(smallLabel);
sizePanel.add(mediumLabel);
sizePanel.add(largeLabel);
sizePanel.add(smallButton);
sizePanel.add(mediumButton);
sizePanel.add(largeButton);

stylePanel = new JPanel(new GridLayout(2,3));
stylePanel.setBorder(BorderFactory.createTitledBorder("Please select land style"));
stylePanel.add(continentLabel);
stylePanel.add(variedLabel);
stylePanel.add(islandLabel);
stylePanel.add(continentButton);
stylePanel.add(variedButton);
stylePanel.add(islandButton);

//add to panel
this.add(mainLabel);
this.add(sizePanel);
this.add(stylePanel);
//this.add(buttonPanel);
}//end constructor
}//end class



//MapPanel
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
* Title: MapPanel.java
* Description: This class displays the map and the units, and allows the
* users to scroll it, and move units.
* @author: Shane Grund
* @version: 1.0
*/
public class MapPanel extends JPanel implements ActionListener, KeyListener
{
private int mapPieces[][];
private int mapWidth, mapHeight, zoom, year;
private int keyUp, keyDown, keyLeft, keyRight, keyZoom;

private WorldPanel worldPanel;
private MiniMap miniMap;
private UnitInfoPanel unitPanel;
private JPanel southPanel, eastPanel1;
private GWMap myMap;

private ImageIcon iconDefault;
private Cursor cursor;

private JButton upButton, downButton, leftButton, rightButton;

/**
* Constructor for mapPanel. It grabs a map and then
* passes it to the worldPanel. it sets up actions for
* button presses
*/
public MapPanel(int size, int style)
{
this.setLayout(new BorderLayout());
zoom = 32;//set initail zoom to 32;
year = -4000;

//set up initial key movements
keyUp = KeyEvent.VK_W;
keyLeft = KeyEvent.VK_A;
keyRight = KeyEvent.VK_D;
keyDown = KeyEvent.VK_S;
keyZoom = KeyEvent.VK_Z;

if(size == 100 && style == 2)
{
//create the map
myMap = GWMap.getGWMap();
if (myMap == null)
{
GWMap.create(size, size, 100, 3, (float) 50.0, (float) 33.0, 2, 4);
myMap = GWMap.getGWMap();
}
else
myMap.reCreate(size, size, 100, 3, (float) 50.0, (float) 33.0, 2, 4);
generate();
}

else if(size == 75 && style == 2)
{
//create the map
myMap = GWMap.getGWMap();
if (myMap == null)
{
GWMap.create(size, size, 75, 3, (float) 50.0, (float) 33.0, 2, 4);
myMap = GWMap.getGWMap();
}
else
myMap.reCreate(size, size, 75, 3, (float) 50.0, (float) 33.0, 2, 4);
generate();
}

else if(size == 50 && style == 2)
{
//create the map
myMap = GWMap.getGWMap();
if (myMap == null)
{
GWMap.create(size, size, 50, 2, (float) 50.0, (float) 33.0, 2, 4);
myMap = GWMap.getGWMap();
}
else
myMap.reCreate(size, size, 50, 2, (float) 50.0, (float) 33.0, 2, 4);
generate();
}

if(size == 100 && style == 1)
{
//create the map
myMap = GWMap.getGWMap();
if (myMap == null)
{
GWMap.create(size, size, 10, 1, (float) 50.0, (float) 33.0, 2, 4);
myMap = GWMap.getGWMap();
}
else
myMap.reCreate(size, size, 10, 1, (float) 50.0, (float) 33.0, 2, 4);
generate();
}

else if(size == 75 && style == 1)
{
//create the map
myMap = GWMap.getGWMap();
if (myMap == null)
{
GWMap.create(size, size, 10, 1, (float) 50.0, (float) 33.0, 2, 4);
myMap = GWMap.getGWMap();
}
else
myMap.reCreate(size, size, 10, 1, (float) 50.0, (float) 33.0, 2, 4);
generate();
}

else if(size == 50 && style == 1)
{
//create the map
myMap = GWMap.getGWMap();
if (myMap == null)
{
GWMap.create(size, size, 7, 1, (float) 50.0, (float) 33.0, 2, 4);
myMap = GWMap.getGWMap();
}
else
myMap.reCreate(size, size, 7, 1, (float) 50.0, (float) 33.0, 2, 4);
generate();
}

if(size == 100 && style == 3)
{
//create the map
myMap = GWMap.getGWMap();
if (myMap == null)
{
GWMap.create(size, size, 120, 1, (float) 50.0, (float) 33.0, 2, 4);
myMap = GWMap.getGWMap();
}
else
myMap.reCreate(size, size, 120, 1, (float) 50.0, (float) 33.0, 2, 4);
generate();
}

else if(size == 75 && style == 3)
{
//create the map
myMap = GWMap.getGWMap();
if (myMap == null)
{
GWMap.create(size, size, 100, 1, (float) 50.0, (float) 33.0, 2, 4);
myMap = GWMap.getGWMap();
}
else
myMap.reCreate(size, size, 100, 1, (float) 50.0, (float) 33.0, 2, 4);
generate();
}

else if(size == 50 && style == 3)
{
//create the map
myMap = GWMap.getGWMap();
if (myMap == null)
{
GWMap.create(size, size, 75, 0, (float) 50.0, (float) 33.0, 2, 4);
myMap = GWMap.getGWMap();
}
else
myMap.reCreate(size, size, 75, 0, (float) 50.0, (float) 33.0, 2, 4);
generate();
}

//set up the cursor
cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);

//set up the buttons
leftButton = new JButton();
leftButton.setFocusable(false);
leftButton.setBorder(null);
leftButton.setContentAreaFilled(false);
leftButton.setCursor(cursor);
iconDefault = new ImageIcon("images/left.gif");
leftButton.setIcon(iconDefault);

rightButton = new JButton();
rightButton.setFocusable(false);
rightButton.setBorder(null);
rightButton.setContentAreaFilled(false);
rightButton.setCursor(cursor);
iconDefault = new ImageIcon("images/right.gif");
rightButton.setIcon(iconDefault);

upButton = new JButton();
upButton.setFocusable(false);
upButton.setBorder(null);
upButton.setContentAreaFilled(false);
upButton.setCursor(cursor);
iconDefault = new ImageIcon("images/up.gif");
upButton.setIcon(iconDefault);

downButton = new JButton();
downButton.setFocusable(false);
downButton.setBorder(null);
downButton.setContentAreaFilled(false);
downButton.setCursor(cursor);
iconDefault = new ImageIcon("images/down.gif");
downButton.setIcon(iconDefault);

leftButton.addActionListener(this);
rightButton.addActionListener(this);
upButton.addActionListener(this);
downButton.addActionListener(this);

//set up the panels
worldPanel = new WorldPanel();
this.add(worldPanel, BorderLayout.CENTER);

southPanel = new JPanel(new FlowLayout());
southPanel.setBackground(Color.black);
southPanel.add(leftButton);
southPanel.add(upButton);
southPanel.add(downButton);
southPanel.add(rightButton);
this.add(southPanel, BorderLayout.SOUTH);

miniMap = new MiniMap();
unitPanel = new UnitInfoPanel();

eastPanel1 = new JPanel(new BorderLayout());
eastPanel1.setBackground(Color.black);
eastPanel1.add(miniMap ,BorderLayout.SOUTH);
eastPanel1.add(unitPanel, BorderLayout.CENTER);
this.add(eastPanel1, BorderLayout.EAST);

//create initial map
worldPanel.setInitialMap(mapPieces, mapWidth, mapHeight);
miniMap.setMap(mapPieces, mapWidth, mapHeight, 12, 12);
int x = worldPanel.getFirstX();
int y = worldPanel.getFirstY();
miniMap.setNewXY(x,y,21,15);
addKeyListener(this);
this.setBackground(Color.black);
this.requestFocus();
}//end constructor

/**
* Performs all actions when buttons get pressed
* @param e ActionEvent
*/
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == upButton)
{
upPressed();
}//end if
if(e.getSource() == downButton)
{
downPressed();
}//end if
if(e.getSource() == leftButton)
{
leftPressed();
}//end if
if(e.getSource() == rightButton)
{
rightPressed();
}//end if
}//end action performed

/**
* This moves the map up
*/
public void upPressed()
{
worldPanel.newMapPos(0,-zoom);
miniMap.newMapPos(0,-1);
}//end upPressed

/**
* This moves the map down
*/
public void downPressed()
{
worldPanel.newMapPos(0,zoom);
miniMap.newMapPos(0,1);
}

/**
* This moves the map left
*/
public void leftPressed()
{
worldPanel.newMapPos(-zoom,0);
miniMap.newMapPos(-1,0);
}

/**
* This moves the map right
*/
public void rightPressed()
{
worldPanel.newMapPos(zoom,0);
miniMap.newMapPos(1,0);
}

/**
* This handles all the key presses
* @param e KeyEvent
*/
public void keyPressed(KeyEvent e)
{
int currentPlayer = worldPanel.getCurrentPlayer();
int currentUnit = worldPanel.getCurrentUnit();
int type = 0;
if(currentPlayer == 1)
type = worldPanel.player1.units[currentUnit - 1].getUnitType();
else
type = worldPanel.player2.units[currentUnit - 1].getUnitType();
if(e.getKeyCode() == keyUp)
upPressed();
else if(e.getKeyCode() == keyDown)
downPressed();
else if(e.getKeyCode() == keyLeft)
leftPressed();
else if(e.getKeyCode() == keyRight)
rightPressed();
else if(e.getKeyCode() == KeyEvent.VK_UP)
{
if(type == 7)
moveShipUp();
else
moveUnitUp();
}
else if(e.getKeyCode() == KeyEvent.VK_DOWN)
{
if(type == 7)
moveShipDown();
else
moveUnitDown();
}
else if(e.getKeyCode() == KeyEvent.VK_LEFT)
{
if(type == 7)
moveShipLeft();
else
moveUnitLeft();
}
else if(e.getKeyCode() == KeyEvent.VK_RIGHT)
{
if(type == 7)
moveShipRight();
else
moveUnitRight();
}
else if(e.getKeyCode() == KeyEvent.VK_C)
createNewCity();
else if(e.getKeyCode() == KeyEvent.VK_B)
createNewUnit();
else if(e.getKeyCode() == KeyEvent.VK_SPACE)
spacePressed();
}//end key pressed

public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}

/**
* This moves unit down
*/
private void moveUnitDown()
{
boolean moved = true;
int currentPlayer = worldPanel.getCurrentPlayer();
int currentUnit = worldPanel.getCurrentUnit();

//if firstPlayer move unit down
if(currentPlayer == 1)
{
int currentPos = worldPanel.player1.units[currentUnit - 1].getPosition();
int temp1 = mapWidth * mapHeight - mapWidth;
int temp2 = mapWidth * mapHeight;
if(currentPos >= temp1 && currentPos < temp2)
{
int newPos = currentPos % mapHeight;
int mapTemp = mapPieces[0][newPos];
if(mapTemp == 0 || mapTemp == 14)//if unit tries to move into sea
moved = false;
else
worldPanel.player1.units[currentUnit - 1].setPosition(newPos);
}//end if
else
{
int mapTemp = mapPieces[0][currentPos + mapWidth];
if(mapTemp == 0 || mapTemp == 14)//if unit tries to move into sea
moved = false;
else
worldPanel.player1.units[currentUnit - 1].setPosition(currentPos + mapWidth);
}//end else
}//end if

//if secondPlayer move unit down
if(currentPlayer == 2)
{
int currentPos = worldPanel.player2.units[currentUnit - 1].getPosition();
int temp1 = mapWidth * mapHeight - mapWidth;
int temp2 = mapWidth * mapHeight;
if(currentPos >= temp1 && currentPos < temp2)
{
int newPos = currentPos % mapHeight;
int mapTemp = mapPieces[0][newPos];
if(mapTemp == 0 || mapTemp == 14)//if unit tries to move into sea
moved = false;
else
worldPanel.player2.units[currentUnit - 1].setPosition(newPos);
}
else
{
int mapTemp = mapPieces[0][currentPos + mapWidth];
if(mapTemp == 0 || mapTemp == 14)//if unit tries to move into sea
moved = false;
else
worldPanel.player2.units[currentUnit - 1].setPosition(currentPos + mapWidth);
}//end if
}//end if

//set up new player once unit has moved
playerMoved(currentPlayer,currentUnit,moved);

//set up new mapPosition if player moved
if(moved == true)
setMapPos();
}//end moveUnitDown

/**
* This moves unit left
*/
private void moveUnitLeft()
{
boolean moved = true;
int currentPlayer = worldPanel.getCurrentPlayer();
int currentUnit = worldPanel.getCurrentUnit();

//if firstPlayer move unit left
if(currentPlayer == 1)
{
int currentPos = worldPanel.player1.units[currentUnit - 1].getPosition();
if(currentPos == 0)
{
int mapTemp = mapPieces[0][mapWidth - 1];
if(mapTemp == 0 || mapTemp == 14)//if unit tries to move into sea
moved = false;
else
worldPanel.player1.units[currentUnit - 1].setPosition(mapWidth - 1);
}
else
{
int temp1 = currentPos % mapWidth;
if(temp1 == 0)
{
int mapTemp = mapPieces[0][currentPos + mapWidth - 1];
if(mapTemp == 0 || mapTemp == 14)//if unit tries to move into sea
moved = false;
else
worldPanel.player1.units[currentUnit - 1].setPosition(currentPos + mapWidth - 1);
}//end if
else
{
int mapTemp = mapPieces[0][currentPos - 1];
if(mapTemp == 0 || mapTemp == 14)//if unit tries to move into sea
moved = false;
else
worldPanel.player1.units[currentUnit - 1].setPosition(currentPos - 1);
}//end else
}//end else
}//end if

//if firstPlayer move unit left
if(currentPlayer == 2)
{
int currentPos = worldPanel.player2.units[currentUnit - 1].getPosition();
if(currentPos == 0)
{
int mapTemp = mapPieces[0][mapWidth - 1];
if(mapTemp == 0 || mapTemp == 14)//if unit tries to move into sea
moved = false;
else
worldPanel.player2.units[currentUnit - 1].setPosition(mapWidth - 1);
}
else
{
int temp1 = currentPos % mapWidth;
if(temp1 == 0)
{
int mapTemp = mapPieces[0][currentPos + mapWidth - 1];
if(mapTemp == 0 || mapTemp == 14)//if unit tries to move into sea
moved = false;
else
worldPanel.player2.units[currentUnit - 1].setPosition(currentPos + mapWidth - 1);
}//end if
else
{
int mapTemp = mapPieces[0][currentPos - 1];
if(mapTemp == 0 || mapTemp == 14)//if unit tries to move into sea
moved = false;
else
worldPanel.player2.units[currentUnit - 1].setPosition(currentPos - 1);
}//end else
}//end else
}//end if

//set up new player once unit has moved
playerMoved(currentPlayer,currentUnit,moved);

//set up new mapPosition if player moved
if(moved == true)
setMapPos();
}//end moveUnitLeft

/**
* This moves unit right
*/
private void moveUnitRight()
{
boolean moved = true;
int currentPlayer = worldPanel.getCurrentPlayer();
int currentUnit = worldPanel.getCurrentUnit();

//if firstPlayer move unit right
if(currentPlayer == 1)
{
int currentPos = worldPanel.player1.units[currentUnit - 1].getPosition();
int temp1 = (currentPos + 1) % mapWidth;
if(temp1 == 0)
{
int mapTemp = mapPieces[0][currentPos - mapWidth + 1];
if(mapTemp == 0 || mapTemp == 14)//if unit tries to move into sea
moved = false;
else
worldPanel.player1.units[currentUnit - 1].setPosition(currentPos - mapWidth + 1);
}
else
{
int mapTemp = mapPieces[0][currentPos + 1];
if(mapTemp == 0 || mapTemp == 14)//if unit tries to move into sea
moved = false;
else
worldPanel.player1.units[currentUnit - 1].setPosition(currentPos + 1);
}
}//end if

//if secondPlayer move unit right
if(currentPlayer == 2)
{
int currentPos = worldPanel.player2.units[currentUnit - 1].getPosition();
int temp1 = (currentPos + 1) % mapWidth;
if(temp1 == 0)
{
int mapTemp = mapPieces[0][currentPos - mapWidth + 1];
if(mapTemp == 0 || mapTemp == 14)//if unit tries to move into sea
moved = false;
else
worldPanel.player2.units[currentUnit - 1].setPosition(currentPos - mapWidth + 1);
}//end if
else
{
int mapTemp = mapPieces[0][currentPos + 1];
if(mapTemp == 0 || mapTemp == 14)//if unit tries to move into sea
moved = false;
else
worldPanel.player2.units[currentUnit - 1].setPosition(currentPos + 1);
}//end else
}//end if

//set up new player once unit has moved
playerMoved(currentPlayer,currentUnit,moved);

//set up new mapPosition if player moved
if(moved == true)
setMapPos();
}//end move Unit Right

/**
* This moves unit up
*/
private void moveUnitUp()
{
boolean moved = true;
int currentPlayer = worldPanel.getCurrentPlayer();
int currentUnit = worldPanel.getCurrentUnit();

//if firstPlayer move unit up
if(currentPlayer == 1)
{
int currentPos = worldPanel.player1.units[currentUnit - 1].getPosition();
if(currentPos >= 0 && currentPos < mapWidth)
{
int newPos = mapHeight -1;
int newPos2 = mapHeight * newPos + currentPos;
int mapTemp = mapPieces[0][newPos2];
if(mapTemp == 0 || mapTemp == 14)//if they try to move on water
moved = false;
else
worldPanel.player1.units[currentUnit - 1].setPosition(newPos2);
}
else
{
int mapTemp = mapPieces[0][currentPos - mapWidth];
if(mapTemp == 0 || mapTemp == 14)//if they try to move on water
moved = false;
else
worldPanel.player1.units[currentUnit - 1].setPosition(currentPos - mapWidth);
}
}//end if

//if secondPlayer move unit up
if(currentPlayer == 2)
{
int currentPos = worldPanel.player2.units[currentUnit - 1].getPosition();
if(currentPos >= 0 && currentPos < mapWidth)
{
int newPos = mapHeight -1;
int newPos2 = mapHeight * newPos + currentPos;
int mapTemp = mapPieces[0][newPos2];
if(mapTemp == 0 || mapTemp == 14)//if they try to move on water
moved = false;
else
worldPanel.player2.units[currentUnit - 1].setPosition(newPos2);
}
else
{
int mapTemp = mapPieces[0][currentPos - mapWidth];
if(mapTemp == 0 || mapTemp == 14)//if they try to move on water
moved = false;
else
worldPanel.player2.units[currentUnit - 1].setPosition(currentPos - mapWidth);
}//end else
}//end if

//set up new player once unit has moved
playerMoved(currentPlayer,currentUnit,moved);

//set up new mapPosition if player moved
if(moved == true)
setMapPos();
}//end move unit up

/**
* This changes the map position after a player has
* moved so that the map will be focusing on the
* next player
*/
private void setMapPos()
{
try
{
Thread.sleep(1000);//so that you can see players move
}
catch(Exception e){}
int currentPlayer = worldPanel.getCurrentPlayer();
int currentUnit = worldPanel.getCurrentUnit();
int unitPos = 0;
int firstX = 0;
int firstY = 0;
if (currentPlayer == 1)
{
unitPos = worldPanel.player1.units[currentUnit - 1].getPosition();
}
if (currentPlayer == 2)
{
unitPos = worldPanel.player2.units[currentUnit - 1].getPosition();
}
int tempX = unitPos % mapWidth;
int tempY = unitPos - tempX;
if (tempY == 0) {}
else
tempY = tempY / mapHeight;
tempX = tempX - 11;
tempY = tempY - 7;
if (tempX >= 0)
firstX = tempX;
else
firstX = tempX + mapWidth;
if (tempY >= 0)
firstY = tempY;
else
firstY = tempY + mapWidth;

int drawWidth = worldPanel.getDrawWidth();
int drawHeight = worldPanel.getDrawHeight();
worldPanel.setNewXYPos(firstX, firstY);
miniMap.setNewXY(firstX, firstY, drawWidth, drawHeight);
}//end set mapPos

/**
* This creates a new city and displays it on the map
*/
private void createNewCity()
{
int currentPlayer = worldPanel.getCurrentPlayer();
int currentUnit = worldPanel.getCurrentUnit();
boolean canBuild = false;

if(currentPlayer == 1)
{
int unitType = worldPanel.player1.units[currentUnit - 1].getUnitType();
if(unitType == 1)//if unit is a settler
canBuild = true;
}

else if(currentPlayer == 2)
{
int unitType = worldPanel.player1.units[currentUnit - 1].getUnitType();
if(unitType == 1)//if unit is a settler
canBuild = true;
}

//if the unit who presses build is a settler
if(canBuild == true)
{
if(currentPlayer == 1)
{
int playerLoc = worldPanel.player1.units[currentUnit - 1].getPosition();
worldPanel.player1.addCity(playerLoc);
currentUnit++;
worldPanel.setCurrentUnit(currentUnit);
}//end if
else if(currentPlayer == 2)
{
int playerLoc = worldPanel.player2.units[currentUnit - 1].getPosition();
worldPanel.player2.addCity(playerLoc);
currentUnit++;
worldPanel.setCurrentUnit(currentUnit);
}//end elseif
}//end if

//set new player and unit if necessary
int temp = worldPanel.getNumUnits(currentPlayer);
if(currentUnit > temp)
{
if(currentPlayer == 1)
worldPanel.setCurrentPlayer(2);
else
worldPanel.setCurrentPlayer(1);
worldPanel.setCurrentUnit(1);
}//end if

//set up new mapPosition if player moved
if(canBuild == true)
setMapPos();
setInfoPanel();
}//end createNewCity

/**
* This creates a new unit and displays it on screen
*/
private void createNewUnit()
{
int currentPlayer = worldPanel.getCurrentPlayer();
int currentUnit = worldPanel.getCurrentUnit();
int randomNumber = (int)Math.ceil(Math.random() * 8);
boolean canBuild = false;

if(currentPlayer == 1)
{
int unitType = worldPanel.player1.units[currentUnit - 1].getUnitType();
if(unitType == 1)//if unit is a settler
canBuild = true;
}

else if(currentPlayer == 2)
{
int unitType = worldPanel.player2.units[currentUnit - 1].getUnitType();
if(unitType == 1)//if unit is a settler
canBuild = true;
}

//if the unit who presses build is a settler
if(canBuild == true)
{
if(currentPlayer == 1)
{
int playerLoc = worldPanel.player1.units[currentUnit - 1].getPosition();
worldPanel.player1.addUnit2(randomNumber);
int noUnits = worldPanel.player1.getNumUnits();
if(randomNumber == 7)//if unit is a ship
worldPanel.player1.units[noUnits].setPosition(getSeaLoc(playerLoc));
else
worldPanel.player1.units[noUnits].setPosition(playerLoc);
worldPanel.player1.setNumUnits(noUnits + 1);
currentUnit++;
worldPanel.setCurrentUnit(currentUnit);
}//end if
else if(currentPlayer == 2)
{
int playerLoc = worldPanel.player2.units[currentUnit - 1].getPosition();
worldPanel.player2.addUnit2(randomNumber);
int noUnits = worldPanel.player2.getNumUnits();
if(randomNumber == 7)//if unit is a ship
worldPanel.player2.units[noUnits].setPosition(getSeaLoc(playerLoc));
else
worldPanel.player2.units[noUnits].setPosition(playerLoc);
worldPanel.player2.setNumUnits(noUnits + 1);
currentUnit++;
worldPanel.setCurrentUnit(currentUnit);
}//end elseif
}//end if

//set new player and unit if necessary
int temp = worldPanel.getNumUnits(currentPlayer);
if(currentUnit > temp)
{
if(currentPlayer == 1)
worldPanel.setCurrentPlayer(2);
else
worldPanel.setCurrentPlayer(1);
worldPanel.setCurrentUnit(1);
}//end if

//set up new mapPosition if player moved
if(canBuild == true)
setMapPos();
setInfoPanel();//set up information panel
}//end buildNewUnit

/**
* This handles all info once player has moved like setting its
* movement rate down one
*/
private void playerMoved(int currentPlayer, int currentUnit, boolean moved)
{
if(moved == true)//if player has made a legitimate move
{
worldPanel.repaint();
int movesLeft = 0;
if(currentPlayer == 1)
{
int myPos = worldPanel.player1.units[currentUnit - 1].getPosition();

//check to see if a player challenges another player
for(int i = 0; i < worldPanel.player2.getNumUnits(); i++)
{
int pos = worldPanel.player2.units[i].getPosition();
if(myPos == pos)
{
fight(1, currentUnit - 1, i);
return;
}
}//end for

//check to see if a player captures a city
for(int i = 0; i < worldPanel.player2.getNumCities(); i++)
{
int pos = worldPanel.player2.cities[i].getLocation();
if(myPos == pos)
{
captureCity(1,i);
return;
}
}//end for
movesLeft = worldPanel.player1.units[currentUnit - 1].getMovementsLeft();
int temp = worldPanel.player1.units[currentUnit - 1].getPosition();
int temp1 = mapPieces[0][temp];
if(temp1 == 54 || temp1 == 51 || temp1 == 52 || temp1 == 53)//if unit moves on rock or something
worldPanel.player1.units[currentUnit - 1].setMovementsLeft(movesLeft - 2);
else
worldPanel.player1.units[currentUnit - 1].setMovementsLeft(movesLeft - 1);
movesLeft = worldPanel.player1.units[currentUnit - 1].getMovementsLeft();
}//end if
else if(currentPlayer == 2)
{
int myPos = worldPanel.player2.units[currentUnit - 1].getPosition();

//check to see if a player challenges another player
for(int i = 0; i < worldPanel.player1.getNumUnits(); i++)
{
int pos = worldPanel.player1.units[i].getPosition();
if(myPos == pos)
{
fight(2, currentUnit - 1, i);
return;
}
}

//check to see if a player captures a city
for(int i = 0; i < worldPanel.player1.getNumCities(); i++)
{
int pos = worldPanel.player1.cities[i].getLocation();
if(myPos == pos)
{
captureCity(2,i);
return;
}
}//end for
movesLeft = worldPanel.player2.units[currentUnit - 1].getMovementsLeft();
int temp = worldPanel.player2.units[currentUnit - 1].getPosition();
int temp1 = mapPieces[0][temp];
if(temp1 == 54 || temp1 == 51 || temp1 == 52 || temp1 == 53)//if unit moves on rock or something
worldPanel.player2.units[currentUnit - 1].setMovementsLeft(movesLeft - 2);
else
worldPanel.player2.units[currentUnit - 1].setMovementsLeft(movesLeft - 1);
movesLeft = worldPanel.player2.units[currentUnit - 1].getMovementsLeft();

//worldPanel.player2.units[currentUnit - 1].setMovementsLeft(movesLeft - 1);
movesLeft = worldPanel.player2.units[currentUnit - 1].getMovementsLeft();
}

if(movesLeft <= 0)//if unit has run out of moves
{
if(currentPlayer == 1)
{
worldPanel.player1.units[currentUnit - 1].resetMovement();
}
if(currentPlayer == 2)
{
worldPanel.player2.units[currentUnit - 1].resetMovement();
}
currentUnit++;
}//end if
worldPanel.setCurrentUnit(currentUnit);
}//end if

int temp = worldPanel.getNumUnits(currentPlayer);
if(currentUnit > temp)
{
if(currentPlayer == 1)
worldPanel.setCurrentPlayer(2);
else
{
worldPanel.setCurrentPlayer(1);
year = unitPanel.getYear() + 20;//add 20 years to the game
}
worldPanel.setCurrentUnit(1);
}//end if

setInfoPanel();//set up the information panel
}//end playerMoved

/**
* This generates a new map using the provided
* GWMap class
*/
private void generate()
{
mapPieces = myMap.Generate3();
mapWidth = myMap.getMapWidth();
mapHeight = myMap.getMapHeight();
}//end generate

/**
* This handles when units clash. It determines who wins and deals
* with disposing of the losing unit.
* @param playerNo int
* @param fighter int
* @param challenger int
*/
private void fight(int playerNo, int fighter, int challenger)
{
LaughButton lt = new LaughButton();
if(playerNo == 1)
{
int attack = worldPanel.player1.units[fighter].getAttack();
int defence = worldPanel.player2.units[challenger].getDefence();
double attack1 = Math.random() * attack;
double defence1 = Math.random() * defence;
if(attack1 >= defence1)//player1 wins
worldPanel.player2.removeUnit(challenger);
else
{
worldPanel.player1.removeUnit(fighter);
worldPanel.setCurrentUnit(1);
}
}//end if

if(playerNo == 2)
{
int attack = worldPanel.player2.units[fighter].getAttack();
int defence = worldPanel.player1.units[challenger].getDefence();
double attack1 = Math.random() * attack;
double defence1 = Math.random() * defence;
if(attack1 >= defence1)//player2 wins
worldPanel.player1.removeUnit(challenger);
else
{
worldPanel.player2.removeUnit(fighter);
worldPanel.setCurrentUnit(1);
}
}//end if
setMapPos();
setInfoPanel();
}//end fight

/**
* This handles when a city has been captured, it sets the
* nation to recognise that it has got one less city, and it
* also disposese of that city on the map.
* @param playerNo int
* @param city int
*/
private void captureCity(int playerNo, int city)
{
if(playerNo == 1)
{
worldPanel.player2.removeCity(city);
}

if(playerNo == 2)
{
worldPanel.player1.removeCity(city);
}
}

/**
* This allows for a unit to skip a turn without
* actually moving anywhere
*/
private void spacePressed()
{
boolean moved = true;
int currentPlayer = worldPanel.getCurrentPlayer();
int currentUnit = worldPanel.getCurrentUnit();
playerMoved(currentPlayer,currentUnit,moved);
setMapPos();
}//end space pressed

/**
* This sets the information panel so the user can
* see who much movement his unit has and also how
* strong and weak the units are.
*/
private void setInfoPanel()
{
//set up info panel
unitPanel.setPlayer(worldPanel.getCurrentPlayer());
unitPanel.setYear(year);
int tempUnit = worldPanel.getCurrentUnit();

if(worldPanel.getCurrentPlayer() == 1)
{
unitPanel.setImageIcon(worldPanel.player1.units[tempUnit - 1].getImage());
unitPanel.setAttack(worldPanel.player1.units[tempUnit - 1].getAttack());
unitPanel.setDefence(worldPanel.player1.units[tempUnit - 1].getDefence());
unitPanel.setMovement(worldPanel.player1.units[tempUnit - 1].getMovementsLeft());
}
else
{
unitPanel.setImageIcon(worldPanel.player2.units[tempUnit - 1].getImage());
unitPanel.setAttack(worldPanel.player2.units[tempUnit - 1].getAttack());
unitPanel.setDefence(worldPanel.player2.units[tempUnit - 1].getDefence());
unitPanel.setMovement(worldPanel.player2.units[tempUnit - 1].getMovementsLeft());
}
}//end set info panel

/**
* This handles when a boat has been built it will place it
* on the ocean
* @param playerLoc int
* @return the sea location to place the ship
*/
private int getSeaLoc(int playerLoc)
{
boolean test = false;
int mapTemp = 0;
//sea how far the sea location is to the left
while(test == false)
{
int temp1 = playerLoc % mapWidth;
if(temp1 == 0)
{
mapTemp = mapPieces[0][playerLoc + mapWidth - 1];
playerLoc = playerLoc + mapWidth;
}
else
{
mapTemp = mapPieces[0][playerLoc - 1];
}
if(mapTemp == 0)//seaLoc
test = true;
playerLoc--;
}
return playerLoc;
}

/*
* This moves a ship left
*/
private void moveShipLeft()
{
boolean moved = true;
int currentPlayer = worldPanel.getCurrentPlayer();
int currentUnit = worldPanel.getCurrentUnit();

//if firstPlayer move unit left
if(currentPlayer == 1)
{
int currentPos = worldPanel.player1.units[currentUnit - 1].getPosition();
if(currentPos == 0)
{
int mapTemp = mapPieces[0][mapWidth - 1];
if(mapTemp != 0)//if unit tries to move onto land
moved = false;
else
worldPanel.player1.units[currentUnit - 1].setPosition(mapWidth - 1);
}
else
{
int temp1 = currentPos % mapWidth;
if(temp1 == 0)
{
int mapTemp = mapPieces[0][currentPos + mapWidth - 1];
if(mapTemp != 0)//if unit tries to move onto land
moved = false;
else
worldPanel.player1.units[currentUnit - 1].setPosition(currentPos + mapWidth - 1);
}//end if
else
{
int mapTemp = mapPieces[0][currentPos - 1];
if(mapTemp != 0)//if unit tries to move onto land
moved = false;
else
worldPanel.player1.units[currentUnit - 1].setPosition(currentPos - 1);
}//end else
}//end else
}//end if

//if firstPlayer move unit left
if(currentPlayer == 2)
{
int currentPos = worldPanel.player2.units[currentUnit - 1].getPosition();
if(currentPos == 0)
{
int mapTemp = mapPieces[0][mapWidth - 1];
if(mapTemp != 0)//if unit tries to move onto land
moved = false;
else
worldPanel.player2.units[currentUnit - 1].setPosition(mapWidth - 1);
}
else
{
int temp1 = currentPos % mapWidth;
if(temp1 == 0)
{
int mapTemp = mapPieces[0][currentPos + mapWidth - 1];
if(mapTemp != 0)//if unit tries to move onto land
moved = false;
else
worldPanel.player2.units[currentUnit - 1].setPosition(currentPos + mapWidth - 1);
}//end if
else
{
int mapTemp = mapPieces[0][currentPos - 1];
if(mapTemp != 0)//if unit tries to move onto land
moved = false;
else
worldPanel.player2.units[currentUnit - 1].setPosition(currentPos - 1);
}//end else
}//end else
}//end if

//set up new player once unit has moved
playerMoved(currentPlayer,currentUnit,moved);

//set up new mapPosition if player moved
if(moved == true)
setMapPos();
}

/*
* This moves a ship right
*/
private void moveShipRight()
{
boolean moved = true;
int currentPlayer = worldPanel.getCurrentPlayer();
int currentUnit = worldPanel.getCurrentUnit();

//if firstPlayer move unit right
if(currentPlayer == 1)
{
int currentPos = worldPanel.player1.units[currentUnit - 1].getPosition();
int temp1 = (currentPos + 1) % mapWidth;
if(temp1 == 0)
{
int mapTemp = mapPieces[0][currentPos - mapWidth + 1];
if(mapTemp != 0)//if unit tries to move onto land
moved = false;
else
worldPanel.player1.units[currentUnit - 1].setPosition(currentPos - mapWidth + 1);
}
else
{
int mapTemp = mapPieces[0][currentPos + 1];
if(mapTemp != 0)//if unit tries to move onto land
moved = false;
else
worldPanel.player1.units[currentUnit - 1].setPosition(currentPos + 1);
}
}//end if

//if secondPlayer move unit right
if(currentPlayer == 2)
{
int currentPos = worldPanel.player2.units[currentUnit - 1].getPosition();
int temp1 = (currentPos + 1) % mapWidth;
if(temp1 == 0)
{
int mapTemp = mapPieces[0][currentPos - mapWidth + 1];
if(mapTemp != 0)//if unit tries to move onto land
moved = false;
else
worldPanel.player2.units[currentUnit - 1].setPosition(currentPos - mapWidth + 1);
}//end if
else
{
int mapTemp = mapPieces[0][currentPos + 1];
if(mapTemp != 0)//if unit tries to move onto land
moved = false;
else
worldPanel.player2.units[currentUnit - 1].setPosition(currentPos + 1);
}//end else
}//end if

//set up new player once unit has moved
playerMoved(currentPlayer,currentUnit,moved);

//set up new mapPosition if player moved
if(moved == true)
setMapPos();
}

/*
* This moves a ship up
*/
private void moveShipUp()
{
boolean moved = true;
int currentPlayer = worldPanel.getCurrentPlayer();
int currentUnit = worldPanel.getCurrentUnit();

//if firstPlayer move unit up
if(currentPlayer == 1)
{
int currentPos = worldPanel.player1.units[currentUnit - 1].getPosition();
if(currentPos >= 0 && currentPos < mapWidth)
{
int newPos = mapHeight -1;
int newPos2 = mapHeight * newPos + currentPos;
int mapTemp = mapPieces[0][newPos2];
if(mapTemp != 0)//if unit tries to move onto land
moved = false;
else
worldPanel.player1.units[currentUnit - 1].setPosition(newPos2);
}
else
{
int mapTemp = mapPieces[0][currentPos - mapWidth];
if(mapTemp != 0)//if unit tries to move onto land
moved = false;
else
worldPanel.player1.units[currentUnit - 1].setPosition(currentPos - mapWidth);
}
}//end if

//if secondPlayer move unit up
if(currentPlayer == 2)
{
int currentPos = worldPanel.player2.units[currentUnit - 1].getPosition();
if(currentPos >= 0 && currentPos < mapWidth)
{
int newPos = mapHeight -1;
int newPos2 = mapHeight * newPos + currentPos;
int mapTemp = mapPieces[0][newPos2];
if(mapTemp != 0)//if unit tries to move onto land
moved = false;
else
worldPanel.player2.units[currentUnit - 1].setPosition(newPos2);
}
else
{
int mapTemp = mapPieces[0][currentPos - mapWidth];
if(mapTemp != 0)//if unit tries to move onto land
moved = false;
else
worldPanel.player2.units[currentUnit - 1].setPosition(currentPos - mapWidth);
}//end else
}//end if

//set up new player once unit has moved
playerMoved(currentPlayer,currentUnit,moved);

//set up new mapPosition if player moved
if(moved == true)
setMapPos();
}

/*
* This moves a ship down
*/
private void moveShipDown()
{
boolean moved = true;
int currentPlayer = worldPanel.getCurrentPlayer();
int currentUnit = worldPanel.getCurrentUnit();

//if firstPlayer move unit down
if(currentPlayer == 1)
{
int currentPos = worldPanel.player1.units[currentUnit - 1].getPosition();
int temp1 = mapWidth * mapHeight - mapWidth;
int temp2 = mapWidth * mapHeight;
if(currentPos >= temp1 && currentPos < temp2)
{
int newPos = currentPos % mapHeight;
int mapTemp = mapPieces[0][newPos];
if(mapTemp != 0)//if unit tries to move onto land
moved = false;
else
worldPanel.player1.units[currentUnit - 1].setPosition(newPos);
}//end if
else
{
int mapTemp = mapPieces[0][currentPos + mapWidth];
if(mapTemp != 0)//if unit tries to move onto land
moved = false;
else
worldPanel.player1.units[currentUnit - 1].setPosition(currentPos + mapWidth);
}//end else
}//end if

//if secondPlayer move unit down
if(currentPlayer == 2)
{
int currentPos = worldPanel.player2.units[currentUnit - 1].getPosition();
int temp1 = mapWidth * mapHeight - mapWidth;
int temp2 = mapWidth * mapHeight;
if(currentPos >= temp1 && currentPos < temp2)
{
int newPos = currentPos % mapHeight;
int mapTemp = mapPieces[0][newPos];
if(mapTemp != 0)//if unit tries to move onto land
moved = false;
else
worldPanel.player2.units[currentUnit - 1].setPosition(newPos);
}
else
{
int mapTemp = mapPieces[0][currentPos + mapWidth];
if(mapTemp != 0)//if unit tries to move onto land
moved = false;
else
worldPanel.player2.units[currentUnit - 1].setPosition(currentPos + mapWidth);
}//end if
}//end if

//set up new player once unit has moved
playerMoved(currentPlayer,currentUnit,moved);

//set up new mapPosition if player moved
if(moved == true)
setMapPos();
}
}//end class



//MiniMap
import javax.swing.*;
import java.awt.*;

/**
* Title: MiniMap.java
* Description: This class displays a miniture version of the map so the
* user can see whats going on without having to scroll the
* large map
* @author: Shane Grund
* @version: 1.0
*/
public class MiniMap extends JPanel
{
private int[][] myMap;
private int mapWidth;
private int mapHeight;
private int startX, startY;
private int iconsToDrawWidth, iconsToDrawHeight;

private ImageIcon[] myIcons;

/**
* The constructot sets up the images to be drawn
*/
public MiniMap()
{
this.setPreferredSize(new Dimension(120,120));

//set up the images
myIcons = new ImageIcon[55];
for(int i = 0; i < myIcons.length; i++)
{
myIcons[i] = new ImageIcon("images/Map Pieces/Map" + i + ".png");
}
}//end constructor

/**
* This method paints the mini map to the panel
* @param g Graphics
*/
public void paint(Graphics g)
{
if(myMap == null){}
else
{
int tempX = 0;
int tempY = 20;
int drawY = 0;
for(int i = 0; i < mapHeight; i++)
{
tempX = 0;
for(int b = 0; b < mapWidth; b++)
{
int arrayPos = drawY + tempX;
int mapPieceToDraw = myMap[0][arrayPos];
Image tempIcon = myIcons[mapPieceToDraw].getImage();
g.drawImage(tempIcon, tempX + 20, tempY, 1, 1, null);
tempX++;
}//end for
tempY++;
drawY +=mapHeight;
}//end for

g.drawRect(startX + 20,startY + 20,iconsToDrawWidth,iconsToDrawHeight);
}//end else
}//end paint

/**
* This method gets called once the original map is created
* and can just clone the exact version of the normal map
* @param map int[][]
* @param mapW int
* @param mapH int
* @param width int
* @param height int
*/
public void setMap(int map[][], int mapW, int mapH, int width, int height)
{
myMap = map;
mapWidth = mapW;
mapHeight = mapH;
iconsToDrawWidth = 21;
iconsToDrawHeight = 15;
repaint();
}

/**
* This method sets players x y coordinates and represents the visible
* portion in the mini map
* @param x int
* @param y int
* @param width int
* @param height int
*/
public void setNewXY(int x, int y, int width, int height)
{
startX = x;
startY = y;
iconsToDrawWidth = width;
iconsToDrawHeight = height;
repaint();
}//end set new XY

/**
* This set a new map position once a player has moved
* @param x int
* @param y int
*/
public void newMapPos(int x, int y)
{
startX +=x;
if(startX == -1)
startX = startX + mapWidth;
if(startX == mapWidth)
startX = startX - mapWidth;
startY +=y;
if(startY == -1)
startY = startY + mapHeight;
if(startY == mapHeight)
startY = startY - mapHeight;
repaint();
}
}//end class



//Nation
/**
* Title: Nation.java
* Description: This class is used so nations can be created
* Note that each nation is created in a seperate
* thread
* @author: Shane Grund
* @version: 1.0
*/
public class Nation
{
protected City[] cities;
protected Unit[] units;
private int playerNumber, count, cityCount;
private int numUnits;
protected boolean started = false;
private String name;

/**
* This constuctor just sets up which player is to be set up
* @param playerNo int
*/
public Nation(int playerNo)
{
playerNumber = playerNo;
count = 0;
name = "Player" + playerNumber;
cityCount = 1;

if(playerNumber == 1)
{
cities = new City[20];
cities[0] = new City(1);
units = new Unit[50];
started = true;
}//end player1
if(playerNumber == 2)
{
cities = new City[20];
cities[0] = new City(2);
units = new Unit[50];
started = true;
}//end player2
}//end constructor

public int getNumUnits()
{
return numUnits;
}//end getNumUnits

public void setNumUnits(int i)
{
numUnits = i;
}//end setNumUnits

public City[] getCities()
{
return cities;
}//end getCities

public int getNumCities()
{
return cityCount;
}//end getNumCities

/**
* This method adds a city to the current players cities.
* @param pos int
*/
public void addCity(int pos)
{
if(cityCount < 20)
{
cities[cityCount] = new City(playerNumber);
cities[cityCount].setLocation(pos);
cityCount++;
}//end if
}//end addCity

/**
* This method adds a unit to the current players units
* @param type int
*/
public void addUnit(int type)
{
if(count < 50)
{
units[count] = new Unit(type, playerNumber);
count++;
}//end if
}//end addUnit

/**
* This method adds a unit to the current players units
* @param type int
*/
public void addUnit2(int type)
{
if(count < 50)
{
units[numUnits] = new Unit(type, playerNumber);
count++;
}//end if
}//end addUnit2


public Unit[] getUnits()
{
return units;
}//end getUnits

/**
* This removes a unit from the current players units
* @param no int
*/
public void removeUnit(int no)
{
for(int i = no; i < numUnits; i++)
{
units[i] = units[i + 1];
}//end for
numUnits--;
if(numUnits == 0)
{
System.out.println("Player" + playerNumber + " LOSES");
System.exit(0);
}//end if
}//end removeUnit

/**
* This removes a city from the current players cities
* @param no int
*/
public void removeCity(int no)
{
for(int i = no; i < cityCount; i++)
{
cities[i] = cities[i + 1];
}//end for
cityCount--;
if(cityCount == 0)
{
System.out.println("Player" + playerNumber + " LOSES");
System.exit(0);
}//end if
}//end removeCity
}//end class




//PlayMidi
import javax.sound.midi.*;
import java.io.File;

/**
* Title: PlayMidi.java
* Description: This plays the background music of ther game
* @author: Shane Grund
* @version: 1.0
*/
public class PlayMidi
{
PlayMidi(String song)
{
MidiPanel midi = new MidiPanel(song);
}

public static void main(String[] arguments)
{
PlayMidi pm = new PlayMidi("sounds/The_Salt_Flats.mid");
}
}

/**
* Title: MidiPanel.java
* Description: This class plays the background music for the game
* @author: Shane Grund
* @version: 1.0
*/
class MidiPanel implements Runnable
{
Thread runner;
Sequence currentSound;
Sequencer player;
String songFile;

/**
* Constructor set up the song ready to play and starts playing it
* @param song String
*/
MidiPanel(String song)
{
super();
songFile = song;
if (runner == null)
{
runner = new Thread(this);
runner.start();
}
}

/**
* This will loop over the song again and again, until
* the game has finished
*/
public void run()
{
try
{
File file = new File(songFile);
currentSound = MidiSystem.getSequence(file);
player = MidiSystem.getSequencer();

while(true)
{
player.open();
player.setSequence(currentSound);
player.start();
while (player.isRunning())
{
try
{
Thread.sleep(1000);
}//end try
catch (InterruptedException e) { }
}//end while
player.close();
}//end while
}//end try
catch (Exception ex)
{
System.out.println(ex.toString());
}//end catch
}//end run
}//end class



//ScreenManager
import java.awt.*;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JFrame;

/**
The ScreenManager class manages initializing and displaying
full screen graphics modes.
*/
public class ScreenManager
{
protected GraphicsDevice device;

/**
Creates a new ScreenManager object.
*/
public ScreenManager()
{
GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
device = environment.getDefaultScreenDevice();
}//end screen manager

/**
Returns a list of compatible display modes for the
default device on the system.
*/
public DisplayMode[] getCompatibleDisplayModes()
{
return device.getDisplayModes();
}//end getCompatibleMode

/**
Returns the first compatible mode in a list of modes.
Returns null if no modes are compatible.
*/
public DisplayMode findFirstCompatibleMode(DisplayMode modes[])
{
DisplayMode goodModes[] = device.getDisplayModes();
for (int i = 0; i < modes.length; i++)
{
for (int j = 0; j < goodModes.length; j++)
{
if (displayModesMatch(modes[i], goodModes[j]))
{
return modes[i];
}//end if
}//end for
}//end for

return null;
}//end findFirstCompatibleMode

/**
Returns the current display mode.
*/
public DisplayMode getCurrentDisplayMode()
{
return device.getDisplayMode();
}// end getCurrentDisplayMode

/**
Determines if two display modes "match". Two display
modes match if they have the same resolution, bit depth,
and refresh rate. The bit depth is ignored if one of the
modes has a bit depth of DisplayMode.BIT_DEPTH_MULTI.
Likewise, the refresh rate is ignored if one of the
modes has a refresh rate of
DisplayMode.REFRESH_RATE_UNKNOWN.
*/
public boolean displayModesMatch(DisplayMode mode1, DisplayMode mode2)
{
if (mode1.getWidth() != mode2.getWidth() || mode1.getHeight() != mode2.getHeight())
{
return false;
}//end if

if (mode1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI &&
mode2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI &&
mode1.getBitDepth() != mode2.getBitDepth())
{
return false;
}//end if

if (mode1.getRefreshRate() !=
DisplayMode.REFRESH_RATE_UNKNOWN &&
mode2.getRefreshRate() !=
DisplayMode.REFRESH_RATE_UNKNOWN &&
mode1.getRefreshRate() != mode2.getRefreshRate())
{
return false;
}//end if

return true;
}//displayModesMatch

/**
Enters full screen mode and changes the display mode.
If the specified display mode is null or not compatible
with this device, or if the display mode cannot be
changed on this system, the current display mode is used.
The display uses a BufferStrategy with 2 buffers.
*/
public void setFullScreen(DisplayMode displayMode)
{
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.setIgnoreRepaint(true);
frame.setResizable(false);

device.setFullScreenWindow(frame);

if (displayMode != null && device.isDisplayChangeSupported())
{
try
{
device.setDisplayMode(displayMode);
}//end try

catch (IllegalArgumentException ex){}

// fix for mac os x
frame.setSize(displayMode.getWidth(), displayMode.getHeight());
}//end if

// avoid potential deadlock in 1.4.1_02
try
{
EventQueue.invokeAndWait(new Runnable()
{
public void run()
{
frame.createBufferStrategy(2);
}//end run
});
}//end try

catch (InterruptedException ex){}
catch (InvocationTargetException ex){}
}//end set full screen

/**
Gets the graphics context for the display. The
ScreenManager uses double buffering, so applications must
call update() to show any graphics drawn.
The application must dispose of the graphics object.
*/
public Graphics2D getGraphics()
{
Window window = device.getFullScreenWindow();

if (window != null)
{
BufferStrategy strategy = window.getBufferStrategy();
return (Graphics2D)strategy.getDrawGraphics();
}//end if

else
{
return null;
}//end else
}//end getGraphics

/**
Updates the display.
*/
public void update()
{
Window window = device.getFullScreenWindow();
if (window != null)
{
BufferStrategy strategy = window.getBufferStrategy();
if (!strategy.contentsLost())
{
strategy.show();
}//end if
}//end if
Toolkit.getDefaultToolkit().sync();
}//end update

/**
Returns the window currently used in full screen mode.
Returns null if the device is not in full screen mode.
*/
public JFrame getFullScreenWindow()
{
return (JFrame)device.getFullScreenWindow();
}//getFullScreenWindow

/**
Returns the width of the window currently used in full
screen mode. Returns 0 if the device is not in full
screen mode.
*/
public int getWidth()
{
Window window = device.getFullScreenWindow();

if (window != null)
{
return window.getWidth();
}//end if

else
{
return 0;
}//end else
}//end getWidth

/**
Returns the height of the window currently used in full
screen mode. Returns 0 if the device is not in full
screen mode.
*/
public int getHeight()
{
Window window = device.getFullScreenWindow();

if (window != null)
{
return window.getHeight();
}//end if

else
{
return 0;
}//end else
}//end getHeight

/**
Restores the screen's display mode.
*/
public void restoreScreen()
{
Window window = device.getFullScreenWindow();

if (window != null)
{
window.dispose();
}//end if

device.setFullScreenWindow(null);
}//end restoreScreen

/**
Creates an image compatible with the current display.
*/
public BufferedImage createCompatibleImage(int w, int h,int transparancy)
{
Window window = device.getFullScreenWindow();
if (window != null)
{
GraphicsConfiguration gc =
window.getGraphicsConfiguration();
return gc.createCompatibleImage(w, h, transparancy);
}//end if

return null;
}//end createCompatibleImage
}//end class




//Unit.java
import javax.swing.*;

/**
* Title: Unit.java
* Description: This class set up a unit for a particular player
* @author: Shane Grund
* @version: 1.0
*/
public class Unit
{
private ImageIcon settlerIcon, warriorIcon, swordsmanIcon, spearmanIcon;
private ImageIcon archerIcon, catapultIcon, galleyIcon, horsemanIcon;
private ImageIcon settlerIcon1, warriorIcon1, swordsmanIcon1, spearmanIcon1;
private ImageIcon archerIcon1, catapultIcon1, galleyIcon1, horsemanIcon1;
private ImageIcon myIcon;

private int unitType, attack, defence, movement, position;
private int playerNo, movementsLeft;

/**
* This constructor sets up all the different unit images
* it also sets up movement rates attack, defence ect...
* @param type int
* @param playNo int
*/
public Unit(int type, int playNo)
{
unitType = type;
playerNo = playNo;

//set up image icons;
settlerIcon = new ImageIcon("images/Units/settler2.png");
warriorIcon = new ImageIcon("images/Units/warrior2.png");
swordsmanIcon = new ImageIcon("images/Units/swordsman2.png");
spearmanIcon = new ImageIcon("images/Units/spearman2.png");
archerIcon = new ImageIcon("images/Units/archer2.png");
catapultIcon = new ImageIcon("images/Units/catapult2.png");
galleyIcon = new ImageIcon("images/Units/galley2.png");
horsemanIcon = new ImageIcon("images/Units/horseman2.png");

settlerIcon1 = new ImageIcon("images/Units/settler1.png");
warriorIcon1 = new ImageIcon("images/Units/warrior1.png");
swordsmanIcon1 = new ImageIcon("images/Units/swordsman1.png");
spearmanIcon1 = new ImageIcon("images/Units/spearman1.png");
archerIcon1 = new ImageIcon("images/Units/archer1.png");
catapultIcon1 = new ImageIcon("images/Units/catapult1.png");
galleyIcon1 = new ImageIcon("images/Units/galley1.png");
horsemanIcon1 = new ImageIcon("images/Units/horseman1.png");

//set up this players icon
if(type == 1)
{
if(playerNo == 1)
myIcon = settlerIcon1;
else
myIcon = settlerIcon;
movement = 1;
attack = 0;
defence = 0;
}//end if
else if(type == 2)
{
if(playerNo == 1)
myIcon = warriorIcon1;
else
myIcon = warriorIcon;
movement = 1;
attack = 1;
defence = 1;
}//end if
else if(type == 3)
{
if(playerNo == 1)
myIcon = swordsmanIcon1;
else
myIcon = swordsmanIcon;
movement = 2;
attack = 2;
defence = 2;
}//end if
else if(type == 4)
{
if(playerNo == 1)
myIcon = spearmanIcon1;
else
myIcon = spearmanIcon;
movement = 1;
attack = 2;
defence = 1;
}//end if
else if(type == 5)
{
if(playerNo == 1)
myIcon = archerIcon1;
else
myIcon = archerIcon;
movement = 1;
attack = 3;
defence = 2;
}//end if
else if(type == 6)
{
if(playerNo == 1)
myIcon = catapultIcon1;
else
myIcon = catapultIcon;
movement = 2;
attack = 5;
defence = 0;
}//end if
else if(type == 7)
{
if(playerNo == 1)
myIcon = galleyIcon1;
else
myIcon = galleyIcon;
movement = 6;
attack = 1;
defence = 1;
}//end if
else if(type == 8)
{
if(playerNo == 1)
myIcon = horsemanIcon1;
else
myIcon = horsemanIcon;
movement = 3;
attack = 2;
defence = 2;
}//end if
movementsLeft = movement;
}//end constructor

public ImageIcon getImage()
{
return myIcon;
}//end getImage

public void setPosition(int x)
{
position = x;
}//end setPosition

public int getPosition()
{
return position;
}//end getPosition

public int getMovement()
{
return movement;
}//end getMovement

public int getAttack()
{
return attack;
}//end getAttack

public int getDefence()
{
return defence;
}//end getDefence

public int getUnitType()
{
return unitType;
}//end getUnitType

public void setMovementsLeft(int i)
{
movementsLeft = i;
}//end setMovementLeft

public int getMovementsLeft()
{
return movementsLeft;
}//end getMovementsLeft

public void resetMovement()
{
movementsLeft = movement;
}//end resetMovement
}//end class





//UnitInfoPanel
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;

/**
* Title: UnitInfoPanel.java
* Description: This panel allows players to view unit statistics
* @author: Shane Grund
* @version: 1.0
*/
public class UnitInfoPanel extends JPanel
{
private JLabel playerLabel, unitLabel, attack, defence, moves, yearLabel;
private ImageIcon playerIcon;
private int year;

/**
* This constructor sets up the panel
*/
public UnitInfoPanel()
{
this.setLayout(new GridLayout(0,1));
this.setBackground(Color.black);
this.setBorder(BorderFactory.createEtchedBorder(0,Color.blue,Color.blue));
year = -4000;

//set up labels
yearLabel = new JLabel(" Year: 4000 bc ");
yearLabel.setForeground(Color.white);
playerLabel = new JLabel(" Player 1 ");
playerLabel.setForeground(Color.white);

playerIcon = new ImageIcon("images/units/settler1.png");
unitLabel = new JLabel(playerIcon);

attack = new JLabel(" Attack: 0 ");
attack.setForeground(Color.white);
defence = new JLabel(" Defence: 0 ");
defence.setForeground(Color.white);
moves = new JLabel(" Moves Left: 1 ");
moves.setForeground(Color.white);

this.add(yearLabel);
this.add(playerLabel);
this.add(unitLabel);
this.add(attack);
this.add(defence);
this.add(moves);
}//end constructor

public void setImageIcon(ImageIcon i)
{
playerIcon = i;
unitLabel.setIcon(playerIcon);
}//end set imageIcon

public void setPlayer(int i)
{
playerLabel.setText(" Player " + i+ " ");
}//end set player

public void setMovement(int i)
{
moves.setText(" MovesLeft: " + i+ " ");
}//end set movement

public void setAttack(int i)
{
attack.setText(" Attack: " + i+ " ");
}//end set attack

public void setDefence(int i)
{
defence.setText(" Defence: " + i+ " ");
}//end set defence

public void setYear(int i)
{
year = i;
if(year < 0)
{
int temp = Math.abs(year);
yearLabel.setText(" Year: " + temp + " bc ");
}
else
yearLabel.setText(" Year: " + year + " ad ");
}

public int getYear()
{
return year;
}
}//end class






//UnitTable.java
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

/**
* Title: UnitTable.java
* Description: This sets up a JTable in a JPanel and displays a JTable
* with units and there pics
* @author: Shane Grund
* @version: 1.0
*/
class UnitTable extends JPanel
{
protected JPanel bottomPanel, centerPanel;
protected JTable table;
protected JLabel lblMain;
protected JScrollPane scrollPane;

private String columnNames[];
private String dataValues[][];

/**
* This sets a a JTable in a JPanel
*/
public UnitTable()
{
// Set the Panel characteristics
this.setLayout(new BorderLayout());
this.setBackground(Color.white);

lblMain = new JLabel("Civilisation Conquest Units");
lblMain.setForeground(new Color(51, 167, 91));
lblMain.setHorizontalAlignment(SwingConstants.CENTER);
lblMain.setFont(new Font("Dialog", 1, 32));
this.add(lblMain, BorderLayout.NORTH);

// Create a panel to hold all other components
bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
bottomPanel.setBackground(Color.white);
this.add(bottomPanel, BorderLayout.SOUTH);

// Create the custom data model
CustomDataModel customDataModel = new CustomDataModel();

// Create a new table instance
table = new JTable(customDataModel);
table.setRowHeight(65);
CreateColumns();

// Configure some of JTable's paramters
table.setShowHorizontalLines( true );

centerPanel = new JPanel(new BorderLayout());
centerPanel.setBackground(Color.white);

// Add the table to a scrolling pane
scrollPane = table.createScrollPaneForTable(table);
centerPanel.add(scrollPane, BorderLayout.CENTER);
this.add(centerPanel);
}//end constructor

/**
* This method sets up all the columns of the JTable
*/
public void CreateColumns()
{
// Say that we are manually creating the columns
table.setAutoCreateColumnsFromModel(false);

for( int iCtr = 0; iCtr < 9; iCtr++ )
{
// Manually create a new column
TableColumn column = new TableColumn( iCtr );
if(iCtr == 0)
column.setHeaderValue( (Object)("") );
if(iCtr == 1)
column.setHeaderValue( (Object)("Setler") );
if(iCtr == 2)
column.setHeaderValue( (Object)("Warrior") );
if(iCtr == 3)
column.setHeaderValue( (Object)("Spearman") );
if(iCtr == 4)
column.setHeaderValue( (Object)("Archer") );
if(iCtr == 5)
column.setHeaderValue( (Object)("Swordman") );
if(iCtr == 6)
column.setHeaderValue( (Object)("Catapolt") );
if(iCtr == 7)
column.setHeaderValue( (Object)("Ship") );
if(iCtr == 8)
column.setHeaderValue( (Object)("horseman") );

// Add a cell renderer for this class
column.setCellRenderer( new CustomCellRenderer() );

// Add the column to the table
table.addColumn( column );
}//end for
}//end create Column
}//end class




//WorldPanel
import javax.swing.*;
import java.awt.*;

/**
* Title: WorldPanel.java
* Description: This class sets displays the game map and handles when
* the map needs to change position or zoom, it handles the
* drawing of what should be drawn to the screen
* @author: Shane Grund
* @version: 1.0
*/
public class WorldPanel extends JPanel
{
private int firstX;
private int firstY;

private int zoom;
private int currentPlayer, currentUnit;

private int mapWidth;
private int mapHeight;

private int[][] myMap;

private ImageIcon[] myIcons;

protected Nation player1, player2;
protected int iconsToDrawWidth, iconsToDrawHeight;

/**
* The constructor for the world panel. Sets up
* some instance varibles, and declares the panels
* attributes
*/
public WorldPanel()
{
this.setBackground(Color.black);

zoom = 32; // set up the initial zoom

//set up the images
myIcons = new ImageIcon[55];
for(int i = 0; i < myIcons.length; i++)
{
myIcons[i] = new ImageIcon("images/Map Pieces/Map" + i + ".png");
}

//set initial values
firstX = 0;
firstY = 0;
}//end constructor

/**
* Paints the map depending on the position and map size
* @param g the graphics object
*/
public void paint(Graphics g)
{
//set initial drawingPositions to 0 will be incremented in the loop
int drawPosX = 0;
int drawPosY = 0;

int arrayPos = 0;

//get screen res info
int width = this.getWidth();
int height = this.getHeight();

int leftOverWidth = this.getWidth() % zoom;
int leftOverHeight = this.getHeight() % zoom;

iconsToDrawWidth = (width - leftOverWidth) / zoom;
iconsToDrawHeight = (height - leftOverHeight) / zoom;

int tempX = firstX;
int tempY = firstY;
for(int i = 0; i < iconsToDrawHeight; i++)
{
if(tempY == mapHeight)
tempY = tempY - mapHeight;
tempX = firstX;
drawPosX = 0;
for(int b = 0; b < iconsToDrawWidth; b++)
{
if(tempX == mapWidth)
tempX = tempX - mapWidth;
arrayPos = tempY * mapWidth + tempX;
int mapPieceToDraw = myMap[0][arrayPos];
Image tempIcon = myIcons[mapPieceToDraw].getImage();

//see if player 1 cities fall on the array position to draw
for(int c = 0; c < player1.getCities().length; c++)
{
if(player1.cities[c] != null)
{
if (arrayPos == player1.cities[c].getLocation())
{
tempIcon = player1.cities[c].getImage().getImage();
}
}
}//end for

//see if player 1 units fall on the array position to draw
for(int c = 0; c < player1.getUnits().length; c++)
{
if(player1.units[c] != null)
{
if (arrayPos == player1.units[c].getPosition())
{
tempIcon = player1.units[c].getImage().getImage();
}
}
}//end for

//see if player 2 cities fall on the array position to draw
for(int c = 0; c < player2.getCities().length; c++)
{
if(player2.cities[c] != null)
{
if(arrayPos == player2.cities[c].getLocation())
{
tempIcon = player2.cities[c].getImage().getImage();
}
}
}//end for

//see if player 2 cities fall on the array position to draw
for(int c = 0; c < player2.getUnits().length; c++)
{
if(player2.units[c] != null)
{
if(arrayPos == player2.units[c].getPosition())
{
tempIcon = player2.units[c].getImage().getImage();
}
}
}//end for

g.drawImage(tempIcon, drawPosX, drawPosY, null);
tempX++;
drawPosX = drawPosX + zoom;
}//end for
drawPosY = drawPosY + zoom;
tempY++;
}//end for

//highlight current moving unit
g.setColor(Color.red);
g.drawRect(352,223,32,32);
}//end paint

/**
* Paints the map in The x = 0 y = 0 position
* It will also load in the image files;
* @param map the double array of map pieces
*/
public void setInitialMap(int[][] map, int mapW, int mapH)
{
//set map dynamics
mapWidth = mapW;
mapHeight = mapH;
myMap = map;

//set up initial player cities
player1 = new Nation(1);
player1.addUnit(1);//settler unit
player1.addUnit(2);//warrior unit
player1.setNumUnits(2);
player2 = new Nation(2);
player2.addUnit(1);//settler unit
player2.addUnit(2);//warrior unit
player2.setNumUnits(2);
currentPlayer = 1;
currentUnit = 1;

int totalPieces = mapWidth * mapHeight;
boolean b = false;
while(b == false)
{
int temp = (int)Math.round(Math.random() * totalPieces);
if(myMap[0][temp] == 1)
{
City[] city = player1.getCities();
city[0].setLocation(temp);
b = true;
}
Unit[] unit = player1.getUnits();
unit[0].setPosition(temp - 1);
unit[1].setPosition(temp + 1);
}//end while

b = false;
while(b == false)
{
int temp = (int)Math.round(Math.random() * totalPieces);
if(myMap[0][temp] == 1)
{
City[] city = player2.getCities();
city[0].setLocation(temp);
b = true;
}//end if
Unit[] unit = player2.getUnits();
unit[0].setPosition(temp - 1);
unit[1].setPosition(temp + 1);
}//end while

//set up the initial starting point on the map
int unitPos = player1.units[0].getPosition();
int tempX = unitPos % mapWidth;
int tempY = unitPos - tempX;
if(tempY == 0){}
else
tempY = tempY / mapHeight;
tempX = tempX - 11;
tempY = tempY - 7;
if(tempX >=0)
firstX = tempX;
else
firstX = tempX + mapWidth;
if(tempY >=0)
firstY = tempY;
else
firstY = tempY + mapWidth;

//paint the initial starting point with player cities and units
repaint();
}//end setInitialMap

public int getFirstX()
{
return firstX;
}//end getFirstY

public int getFirstY()
{
return firstY;
}//end getFirstX

/**
* Set a new map position. It gets passed in new positions
* depending on what button has been pressed. It basically
* tells the map to move 32 pixels in a particular direction.
* @param x - the new x location
* @param y - the new y pos
*/
public void newMapPos(int x, int y)
{
if(x != 0)
{
firstX = firstX + (x / zoom);
if(firstX == -1)
firstX = firstX + mapWidth;
if(firstX == mapWidth)
firstX = firstX - mapWidth;
}//end if
if(y != 0)
{
firstY = firstY + (y / zoom);
if(firstY == -1)
firstY = firstY + mapHeight;
if(firstY == mapHeight)
firstY = firstY - mapHeight;
}//end if
repaint();
}//end newMapPos

public int getCurrentPlayer()
{
return currentPlayer;
}//end getCurrentPlayer

public void setCurrentPlayer(int i)
{
currentPlayer = i;
}//end setCurrentPlayer

public int getCurrentUnit()
{
return currentUnit;
}//end getCurrentUnit

public void setCurrentUnit(int i)
{
currentUnit = i;
}//end setCurrentUnit

public int getNumUnits(int i)
{
if(i == 1)
return player1.getNumUnits();
else
return player2.getNumUnits();
}//end getNumUnits

public void setNewXYPos(int x, int y)
{
firstX = x;
firstY = y;
repaint();
}//end setNewXYPos

public int getDrawWidth()
{
return iconsToDrawWidth;
}//end getDrawWidth

public int getDrawHeight()
{
return iconsToDrawHeight;
}//end getDrawHeight
}//end class

No comments: