package game_project2;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.util.Enumeration;
import java.util.Vector;
public class MyModel extends GameModel {
private static final int EAST = 0;
private static final int WEST = 1;
private static final int NORTH = 2;
private static final int SOUTH = 3;
private static final int STOP = 4;
private Color color = new Color((int) (Math.random() * 255), (int) (Math
.random() * 255), (int) (Math.random() * 255));
private GameObject player = new RoundObject(Color.blue);
private GameObject coin = new RoundObject(color);
private GameObject goal = new RoundObject(Color.red);
private GameObject wall = new RectangularObject(Color.black);
private GameObject blank = new GameObject();
private Square ball = new Square(dim.width / 2, dim.height - 1);
private Vector walls = new Vector();
private Vector target = new Vector();
private Vector item = new Vector(); // position of the coins
private int dir; // current direction of move
static int score = 10;
public MyModel() {
for (int i = 0; i < dim.width; i++)
for (int j = 0; j < dim.height; j++)
state[i][j] = blank;
for (int i = 1; i < 75; i++)
setGold();
for (int i = 0; i < 1; i++)
setGoal();
for (int i = 0; i < 1; i++)
setWall();
System.out.println(walls);
}
public void setGold() {
Square tmp;
do
tmp = new Square((int) (Math.random() * dim.width), (int) (Math
.random() * dim.height));
while (inCoins(tmp));
state[tmp.x][tmp.y] = coin;
target.addElement(tmp);
}
public void setGoal() {
Square tmp;
tmp = new Square(dir, dir);
state[19][0] = goal;
target.addElement(tmp);
}
public void setWall() {
Square tmp;
tmp = new Square(dir, dir);
state[19][1] = wall;
state[18][1] = wall;
state[17][1] = wall;
state[16][1] = wall;
state[14][0] = wall;
state[14][1] = wall;
state[16][2] = wall;
state[16][3] = wall;
state[15][3] = wall;
state[14][3] = wall;
state[13][3] = wall;
state[12][2] = wall;
state[12][3] = wall;
state[12][1] = wall;
state[11][1] = wall;
state[10][1] = wall;
state[10][2] = wall;
state[10][3] = wall;
state[10][4] = wall;
state[10][5] = wall;
state[10][6] = wall;
state[8][0] = wall;
state[8][1] = wall;
state[8][2] = wall;
state[9][4] = wall;
state[8][4] = wall;
state[7][4] = wall;
state[6][4] = wall;
state[6][3] = wall;
state[6][2] = wall;
state[6][1] = wall;
state[5][1] = wall;
state[3][1] = wall;
state[3][0] = wall;
state[2][0] = wall;
state[1][0] = wall;
state[0][0] = wall;
state[3][2] = wall;
state[3][3] = wall;
state[4][6] = wall;
state[3][6] = wall;
state[2][6] = wall;
state[1][6] = wall;
state[3][4] = wall;
state[4][3] = wall;
state[4][3] = wall;
state[1][5] = wall;
state[1][4] = wall;
state[1][3] = wall;
state[1][2] = wall;
state[0][8] = wall;
state[1][8] = wall;
state[2][8] = wall;
state[3][8] = wall;
state[3][9] = wall;
state[9][19] = wall;
state[9][18] = wall;
state[11][18] = wall;
state[12][18] = wall;
state[13][18] = wall;
state[14][18] = wall;
state[15][18] = wall;
state[16][18] = wall;
state[17][18] = wall;
state[18][18] = wall;
state[18][17] = wall;
state[18][16] = wall;
state[18][15] = wall;
state[18][14] = wall;
state[18][13] = wall;
state[18][12] = wall;
state[18][11] = wall;
state[18][10] = wall;
state[18][9] = wall;
state[17][9] = wall;
state[16][9] = wall;
state[16][8] = wall;
state[16][7] = wall;
state[16][6] = wall;
state[18][7] = wall;
state[19][7] = wall;
state[16][5] = wall;
state[17][5] = wall;
state[18][5] = wall;
state[18][4] = wall;
state[18][3] = wall;
state[14][4] = wall;
state[14][5] = wall;
state[14][6] = wall;
state[14][7] = wall;
state[14][8] = wall;
state[14][9] = wall;
state[14][10] = wall;
state[14][11] = wall;
state[15][11] = wall;
state[16][11] = wall;
state[17][13] = wall;
state[16][13] = wall;
state[15][13] = wall;
state[14][13] = wall;
state[13][13] = wall;
state[12][13] = wall;
state[12][12] = wall;
state[13][10] = wall;
state[11][12] = wall;
state[11][11] = wall;
state[11][10] = wall;
state[11][9] = wall;
state[11][8] = wall;
state[12][8] = wall;
state[12][7] = wall;
state[12][6] = wall;
state[12][5] = wall;
state[9][17] = wall;
state[9][16] = wall;
state[10][16] = wall;
state[11][16] = wall;
state[11][16] = wall;
state[12][16] = wall;
state[13][16] = wall;
state[14][16] = wall;
state[15][16] = wall;
state[16][16] = wall;
state[16][15] = wall;
state[14][14] = wall;
state[12][15] = wall;
state[10][14] = wall;
state[9][14] = wall;
state[9][13] = wall;
state[9][12] = wall;
state[9][11] = wall;
state[9][10] = wall;
state[9][9] = wall;
state[9][8] = wall;
state[8][8] = wall;
state[8][7] = wall;
state[8][6] = wall;
state[6][5] = wall;
state[6][6] = wall;
state[6][7] = wall;
state[6][8] = wall;
state[6][9] = wall;
state[6][10] = wall;
state[7][10] = wall;
state[7][11] = wall;
state[7][12] = wall;
state[7][13] = wall;
state[7][14] = wall;
state[8][14] = wall;
state[7][15] = wall;
state[7][16] = wall;
state[7][17] = wall;
state[7][18] = wall;
state[6][18] = wall;
state[5][18] = wall;
state[4][18] = wall;
state[3][18] = wall;
state[2][18] = wall;
state[1][18] = wall;
state[0][16] = wall;
state[1][16] = wall;
state[2][16] = wall;
state[3][16] = wall;
state[4][16] = wall;
state[5][16] = wall;
state[6][14] = wall;
state[5][14] = wall;
state[4][14] = wall;
state[3][14] = wall;
state[2][14] = wall;
state[1][14] = wall;
state[1][14] = wall;
state[1][13] = wall;
state[1][12] = wall;
state[1][11] = wall;
state[1][10] = wall;
state[3][10] = wall;
state[4][10] = wall;
state[5][8] = wall;
state[3][12] = wall;
state[4][12] = wall;
state[5][12] = wall;
state[6][12] = wall;
state[7][12] = wall;
walls.addElement(tmp);
}
private boolean inCoins(Square sq) {
// relies on the fact that matrix elements share objects.
return state[sq.x][sq.y] == coin;
}
private boolean inGoal(Square sq) {
// relies on the fact that matrix elements share objects.
return state[sq.x][sq.y] == goal;
}
private void updateDir(int key) {
switch (key) {
case KeyEvent.VK_LEFT:
dir = WEST;
break;
case KeyEvent.VK_UP:
dir = NORTH;
break;
case KeyEvent.VK_RIGHT:
dir = EAST;
break;
case KeyEvent.VK_DOWN:
dir = SOUTH;
break;
default:
dir = STOP;
break;
}
}
private Square newPosition() {
Square res = null;
switch (dir) {
default:
res = new Square(ball.x, ball.y);
break;
case EAST:
if (ball.x < 19) {
res = new Square(ball.x + 1, ball.y);
}
else {
res = new Square(ball.x, ball.y);
}
break;
case NORTH:
if (ball.y > 0) {
res = new Square(ball.x, ball.y - 1);
} else {
res = new Square(ball.x, ball.y);
}
break;
case WEST:
if (ball.x > 0) {
res = new Square(ball.x - 1, ball.y);
} else {
res = new Square(ball.x, ball.y);
}
break;
case SOUTH:
if (ball.y < 19) {
res = new Square(ball.x, ball.y + 1);
} else {
res = new Square(ball.x, ball.y);
}
break;
}
return res;
}
protected void executeCommand(int key) {
updateDir(key);
state[ball.x][ball.y] = blank;
ball = newPosition();
state[ball.x][ball.y] = player;
boolean found = target.removeElement(ball);
if (found)
score++;
}
public static int getScore() {
return score;
}
public Vector getWalls() {
return walls;
}
}