刚开始学了1周scjp教材写了个俄罗斯方块,去考试有几层把握?
代码如下:game.java(前半)
import java.awt.*;
import java.awt.event.*;
class cube {
public final static int w = 20, h = 20;
protected int x, y;
protected Color color;
protected int style_id;
public final static int offset = 60;
public static int [][] buf;
public int [][] style;
public void setColor(Color c) {
color = c;
}
public void drawMain(Graphics g) {
g.setColor(color);
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
if (style[i][j] == 0)
continue;
g.fillRect(x+j*w+offset, y+i*h, w, h);
}
}
}
public void drawNext(Graphics g) {
g.setColor(color);
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
if (style[i][j] == 0)
continue;
g.fillRect(x+j*w, y+i*h, w, h);
}
}
}
public void setStyle(int id) {
style = new int[4][4];
style_id = id;
switch (id) {
case 0:
style[0][0] = 1;
style[0][1] = 1;
style[1][0] = 1;
style[1][1] = 1;
break;
case 1:
style[0][0] = 1;
style[1][0] = 1;
style[2][0] = 1;
style[3][0] = 1;
break;
case 2:
style[0][0] = 1;
style[1][0] = 1;
style[0][1] = 1;
style[0][2] = 1;
break;
case 3:
style[0][0] = 1;
style[1][0] = 1;
style[1][1] = 1;
style[1][2] = 1;
break;
case 4:
style[0][0] = 1;
style[0][1] = 1;
style[0][2] = 1;
style[1][1] = 1;
break;
case 5:
style[0][0] = 1;
style[1][0] = 1;
style[1][1] = 1;
style[2][1] = 1;
break;
case 6:
style[0][1] = 1;
style[1][1] = 1;
style[1][0] = 1;
style[2][0] = 1;
break;
}
}
public int down() {
for (int i =0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
if (style[i][j] == 1) {
try {
if (buf[(y+h*(i+1))/h][(x+w*j)/w] == 1)
return -1;
} catch (ArrayIndexOutOfBoundsException e) {
if ((y+h*(i+1))/h >= 14)
return -1;
else {
if ((x+w*j)/w < 0)
x+=w;
else
x-=w;
}
}
}
}
}
return y+=h;
}
public int up() {
if (y-1 >= 0)
return y-=h;
return -1;
}
public int left() {
for (int j = 3; j >= 0; --j) {
for (int i = 0; i < 4; ++i) {
if (style[i][j] == 1) {
try {
if (buf[(y+h*i)/h][(x+w*(j-1))/w] == 1)
return -1;
} catch (ArrayIndexOutOfBoundsException e) {
return -1;
}
}
}
}
return x-=w;
}
public int right() {
for (int j = 0; j < 4; ++j) {
for (int i = 0; i < 4; ++i) {
if (style[i][j] == 1) {
try {
if (buf[(y+h*i)/h][(x+w*(j+1))/w] == 1)
return -1;
} catch (ArrayIndexOutOfBoundsException e) {
return -1;
}
}
}
}
return x+=w;
}
public void setBuf() {
int left, top;
left = x/w;
top = y/h;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
if (style[i][j] == 1) {
try {
buf[top+i][left+j]=style[i][j];
} catch (ArrayIndexOutOfBoundsException e) {
}
}
}
}
}
public boolean reset() {
if (x == 0 && y == 0)
return false;
x = 0;
y = 0;
return true;
}
public void rotate() {
int tmp[][];
int cw = 4;
int left, top;
left = x/w;
top = y/h;
tmp = style;
style = new int[4][4];
for (int i = 0; i < cw; ++i)
for (int j = 0; j < cw; ++j)
style[cw-1-j][i] = tmp[i][j];
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
if (style[i][j] == 1) {
try {
if (buf[top+i][left+j] == 1)
style = tmp;
} catch (ArrayIndexOutOfBoundsException e) {
style = tmp;
}
}
}
}
}
{
x = 0;
y = 0;
style = new int[4][4];
buf = new int[14][10];
}
}