TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
Game modeling
1. Game Modeling
• Requirement: A game has two teams. Each
team has soldiers that move in the game area
and fire at soldiers of opposite team. Each
team can install watch towers that watches
over a given area. If enemy solider steps in the
area that is watched, the soldier at watch
tower fires at them. Create a software model
of this game.
3. Team
A team comprises of soldiers and watchtowers.
public class Team {
private Soldier[] live_soldiers;
private WatchTower[] wt;
private String name;
public Team(Soldier[] s, WatchTower[] wt) {
//assign
}
}
4. Soldier
A soldier is modeled as an independent thread
that moves as per the directions of player. It
supports operations for moving left, right up and
down the game area. Game area is modeled as
composed of several square grids. A soldier can
move from one grid to another grid. It is assumed
that each grid is of size 10 pixels. Once a solider is
on a grid, the grid object is notified of the same.
Grids (plural) is a helper class for accessing the
grid(singular) object.
5. Soldier
public class Soldier implements Runnable {
boolean is_shot;
int x; // current x coordinate
int y; // current
Team t;
public Soldier(Team t) {
this.t=t;
}
public void run() {
while (!is_shot) {
// move along grids in random/predefined fashion
}
}
//other methods on next slide
}
6. Soldier
public void fire(int x, int y) {
}
public void left(int grids) {
int target_x = x - grids;
for (int i = x - 1; i >= target_x; i--) {
move(i, y);
Grid g = Grids.getGrid(i, y);
g.notify();
}
}
public void right(int pixels) {
// similar to left
}
public void up(int pixels) {
// similar to left
}
7. Soldier
public void down(int pixels) {
//similar to left
}
public void move(int x, int y) {
this.x = x;
this.y = y;
Graphics.paintSoldier(x * 10, y * 10, 10); // assuming paint soldier
// takes bottom left screen pixel coordinate and number of screen pixels
// side. Graphic library paints the soldier in that square area by suitably
//coloring the pixels.
}
public void gotHit() {
is_shot = true;
}
8. Watch Tower
Watch tower watches over a square area surrounding it.
Any soldier walking into the square area is shot at by soldier at watchtower.
public class WatchTower {
int x;
int y;
// number of grids for this square area where watch tower watches.
int side;
boolean in;
Team t;
public WatchTower(int x, int y, int side, Team t) {
//assign
}
// other methods in next slide.
}
9. Watch Tower
When watchtower is notified by Grid of an enemy soldier, it calls notfy In, When soldier walks out,
it calls notify out. When notify in is called, watchtower creates its on thread for firing so that calling
thread is not blocked.
public void notifyIn(final int x, final int y) {
in = true;
Thread t = new Thread() {
public void run() {
while (in) {
fire(x, y);
}
}
};
}
public void notifyOut(int x, int y) {
in = false;
}
10. Watch Tower
public void fire(int x, int y) {
}
public void registerWatchtowerWithGrids() {
Grid[] gA = Grids.getGrid(x, y, side);
for (int i = 0; i < gA.length; i++) {
Grid g = gA[i];
g.registerWatchTower(this);
}
}
11. Grid
Game area is divided into square grids.
Grids observe if a soldier is over its square and notify monitoring
watchtowers.
public class Grid {
// coordinate for pixel
int x;
int y;
List<WatchTower> l = new ArrayList<WatchTower>();
public void grid(int x, int y) {
}
12. Grid
public void registerWatchTower(WatchTower w) {
l.add(w);
}
public void NotifyIn() {
for (int i = 0; i < l.size(); i++) {
WatchTower w = l.get(i);
w.notifyIn(x, y);
}
}
public void NotifyOut() {
for (int i = 0; i < l.size(); i++) {
WatchTower w = l.get(i);
w.notifyOut(x, y);
}
}
13. Grids
Assuming area to rectangular, let x be number
of grids length wise and y be number of grids
breadth wise.
Another assumption is that each grid side has
10 screen pixels. So application grid (1,1) will
have (10,10) as pixel coordinate at its bottom-
left.
14. Grids
public class Grids {
static Grid[][] gA ;
static {
int x,y;
x=y=100;
gA = new Grid[x][y];
for(int i=0;i<x;i++) {
for(int j=0;j<y;j++){
Grid g = new Grid(i,j);
gA[i][j]=g;
}
}
}
15. Grids
public static Grid getGrid(int i, int y) {
//implementation
}
public static Grid[] getGrid(int x, int y, int side)
{
//implementation
}