[code]
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class Stratego extends Applet implements ActionListener, MouseListener, MouseMotionListener
{
// Place instance variables here
/*
*/
//player one's units
int p1_marshal = 10;
int p1_general = 9;
int p1_spy = 1;
int p1_flag = 0;
int p1_colonel[] = new int [2];
int p1_major[] = new int [3];
int p1_captain[] = new int [4];
int p1_lieutenant[] = new int [4];
int p1_sergeant[] = new int [4];
int p1_miner[] = new int [5];
int p1_scout[] = new int [8];
int p1_bomb[] = new int [6];
//player two's units
int p2_marshal = 10;
int p2_general = 9;
int p2_spy = 1;
int p2_flag = 0;
int p2_colonel[] = new int [2];
int p2_major[] = new int [3];
int p2_captain[] = new int [4];
int p2_lieutenant[] = new int [4];
int p2_sergeant[] = new int [4];
int p2_miner[] = new int [5];
int p2_scout[] = new int [8];
int p2_bomb[] = new int [6];
int opponent_army[] = new int [40];
//other variables
int water[] = new int [8];
int map[] [] = new int [10] [10]; //first value is x and second is y
int x_map;
int y_map;
int p1graveyard[] = new int [40];
int p2graveyard[] = new int [40];
//Buttons
Button instructions;
Button close;
//boolean
boolean buttonPressed = false;
boolean win = false;
//mouse
int mousex, mousey;
int modx, mody;
int xlocation, ylocation;
int clickx, clicky;
public void init ()
{
//sets size
setSize (600, 600);
// Place the body of the initialization method here
//take up the space for the blocks of water
map [3] [4] = water [0];
map [2] [4] = water [1];
map [3] [5] = water [2];
map [2] [5] = water [3];
map [6] [4] = water [4];
map [7] [4] = water [5];
map [6] [5] = water [6];
map [7] [5] = water [7];
//this places and activates the buttons
setLayout (null);
instructions = new Button ("Instructions");
instructions.setBounds (50, 20, 100, 30);
add (instructions);
instructions.addActionListener (this);
//allows use of mouse
addMouseListener (this);
addMouseMotionListener (this);
} // init method
public void op_army ()
{
//places all of the units into the variable for the opponents army
opponent_army [0] = p2_flag;
opponent_army [1] = p2_general;
opponent_army [2] = p2_spy;
opponent_army [3] = p2_marshal;
for (int i = 0 ; i < p2_colonel.length ; i++)
{
p2_colonel [i] = 8;
opponent_army [3 + i] = p2_colonel [i];
} //end for
for (int i = 0 ; i < p2_major.length ; i++)
{
p2_major [i] = 7;
opponent_army [5 + i] = p2_major [i];
} //end for
for (int i = 0 ; i < p2_captain.length ; i++)
{
p2_captain [i] = 6;
opponent_army [8 + i] = p2_captain [i];
} //end for
for (int i = 0 ; i < p2_lieutenant.length ; i++)
{
p2_lieutenant [i] = 5;
opponent_army [12 + i] = p2_lieutenant [i];
} //end for
for (int i = 0 ; i < p2_sergeant.length ; i++)
{
p2_sergeant [i] = 4;
opponent_army [16 + i] = p2_sergeant [i];
} //end for
for (int i = 0 ; i < p2_miner.length ; i++)
{
p2_miner [i] = 3;
opponent_army [20 + i] = p2_miner [i];
} //end for
for (int i = 0 ; i < p2_scout.length ; i++)
{
p2_scout [i] = 2;
opponent_army [25 + i] = p2_scout [i];
} //end for
for (int i = 0 ; i < p2_bomb.length ; i++)
{
p2_bomb [i] = 11;
opponent_army [33 + i] = p2_bomb [i];
}
//end for
for (int i = 0 ; i < opponent_army.length ; i++)
{
x_map = (int) ((Math.random () * 9) + 1);
y_map = (int) ((Math.random () * 3) + 1);
if (x_map == map [x_map] [0] || y_map == map [0] [y_map])
{
i--;
} //end if
else
{
map [x_map] [y_map] = opponent_army [i];
} //end else
} //end for
}
public void mouseEntered (MouseEvent e)
{
//When mouse is within the applet
}
public void mouseExited (MouseEvent e)
{
//When mouse is away from applet
}
public void mouseClicked (MouseEvent e)
{
//when mouse is clicked
buttonPressed = true;
}
public void mousePressed (MouseEvent e)
{
//when mouse if pressed
modx = mousex % 25;
mody = mousey % 25;
//modulo used to find selected box
buttonPressed = true;
repaint();
} //End method mouseMoved
public void mouseReleased (MouseEvent e)
{
buttonPressed = false;
} //End method mouseReleased
public void mouseMoved (MouseEvent e)
{
mousex = e.getX ();
mousey = e.getY ();
showStatus (mousex + "," + mousey);
} //End method mouseMoved
public void mouseDragged (MouseEvent e)
{
} //End method mouseDragged
//this is will dictate the user if they win or lose
public void matchconditions ()
{
for (int i = 0 ; i < p1graveyard.length ; i++)
{
if (p1graveyard [i] == p1_flag) //if player flag in graveyard
{
win = false;
} //end if
else if (p2graveyard [i] == p2_flag) //if opponent flag in graveyard
{
win = true;
} //end if
else
{
;
} //end else
} //end for
} //end match conditions
public void paint (Graphics g)
{
// Place the body of the drawing method here
//the following is the font for the title of the applet
Font title = new Font ("Arial", Font.BOLD, 30);
g.setFont (title);
FontMetrics fm = g.getFontMetrics ();
g.setColor (Color.BLACK);
int y = fm.getHeight ();
g.drawString ("Stratego", 250, y);
//the x lines of the grid.
for (int i = 50 ; i <= 550 ; i = i + 50)
{
g.drawLine (i, 50, i, 550);
} //End for
//the y lines of the grid
for (int j = 50 ; j <= 550 ; j = j + 50)
{
g.drawLine (50, j, 550, j);
} //End for
//this draws the grass on the board
g.setColor (Color.green);
for (int i = 50 ; i <= 500 ; i = i + 50)
{
for (int j = 50 ; j <= 500 ; j = j + 50)
{
g.fillRect (1 + i, 1 + j, 49, 49);
}
}
//this draws the first lake/choke-point
g.setColor (Color.blue);
for (int i = 50 ; i <= 100 ; i = i + 50)
{
for (int j = 50 ; j <= 100 ; j = j + 50)
{
g.fillRect (101 + i, 201 + j, 49, 49);
}
}
//this draws the second lake/choke-point
g.setColor (Color.blue);
for (int i = 50 ; i <= 100 ; i = i + 50)
{
for (int j = 50 ; j <= 100 ; j = j + 50)
{
g.fillRect (301 + i, 201 + j, 49, 49);
}
}
//Draws the blue flag
g.setColor (Color.blue);
g.drawLine (240, 7, 240, 43);
g.drawLine (240, 7, 220, 11);
g.drawLine (220, 11, 240, 15);
//Draws the red flag
g.setColor (Color.red);
g.drawLine (380, 7, 380, 43);
g.drawLine (380, 7, 400, 11);
g.drawLine (400, 11, 380, 15);
if (buttonPressed = true)
{
modx = mousex % 50;
mody = mousey % 50;
xlocation = mousex - modx;
ylocation = mousey - mody;
clickx = ((xlocation - 50) / 25) + 1;
clicky = ((ylocation - 100)) / 25 + 1;
g.setColor (Color.red);
g.drawRect (xlocation, ylocation, 50, 50);
}
} // paint method
//this will dictate what the buttons do.
public void actionPerformed (ActionEvent evt)
{
//if instructions is pressed it will open the instructions.
if (evt.getSource () == instructions)
{
JOptionPane.showMessageDialog (null, "Hello");
}
}
} // Stratego class
[/code]