Here is modified code, fixing errors in the posted code, and with the
Random coordinate generation and setting.
import java.awt.*;
import java.applet.*;
public class five extends Applet implements Runnable {
Thread runner;
Graphics offscreen;
Image image;
static int board[][] = new int[11][11];
//int okmove[]=new int[225];
static final int STOPGAME = 0,
USERFIRST = 1,
PLAY = 2,
PCFIRST = 3,
GAMEOVER = 4;
static final int black = 1,
white = 2,
human = 1,
cpu = 2,
red = 3,
rock = 3;
static final int BLOCKX = 7,
BLOCKY = 7,
BLOCKHEIGHT = 16,
BLOCKWIDTH = 16,
width = 240,
height = 240;
static int INITP, player, done, gamestate, win;
Panel buttons; //the panel for the buttons
Panel main_panel; //a main panel to put buttons and textarea on
//Initialize the applet
public void init() {
//System.out.print(">> init <<");
image = createImage(width, height);
offscreen = image.getGraphics();
win = 0;
gamestate = PLAY;
//player=rock;
player = human;
INITP = 0;
for (int j = 0; j < 11; j++) {
for (int i = 0; i < 11; i++) {
board[i][j] = 0;
}
}
int rockX = getRandomCoordinate(10);
int rockY = getRandomCoordinate(10);
/*
double a = Math.floor(1+Math.random()*11);
String b = String.valueOf(a);
int ind = b.indexOf(".");
int c = Integer.parseInt(b.substring(0,ind));
double a1 = Math.floor(1+Math.random()*11);
String b1 = String.valueOf(a1);
int ind1 = b1.indexOf(".");
int c1 = Integer.parseInt(b1.substring(0,ind1));
*/
board[rockX][rockY]=rock;
//set up the panels, is simple, so not given own class
setLayout(new BorderLayout()); //set the layout
main_panel = new Panel(); //get the main panel
Panel buttons = new Panel();
main_panel.setLayout(new BorderLayout()); //want a border layout on
buttons = new Panel(); //create the panel
buttons.setLayout(new GridLayout(1, 2)); //arrange buttons in a grid
buttons.add(new Button("User First")); //add the buttons
buttons.add(new Button("PC First"));
//buttons.add(new Button("Stop Game"));
main_panel.add("North", buttons); //add the button panel to top of
//main_panel.add("South", main_panel);
//add buttons and window to screen
}
public void start() {
//System.out.print(">> start <<");
if (runner == null) {
runner = new Thread(this);
runner.start();
}
}
public boolean action(Event evt, Object arg) { //process buttons
String label = (String) arg; //converts the button to a string
if (label.equals("User First")) { //if request for more colors
gamestate = USERFIRST;
} else if (label.equals("PC First")) {
gamestate = PCFIRST;
}
//else if (label.equals("Stop Game")){
// gamestate=STOPGAME;
//}
repaint(); //copy new board to screen
return true;
}
public boolean mouseDown(Event e, int x, int y) {
//System.out.print(">> mouseDown <<");
if (player == human && gamestate == PLAY) {
x >>= 4;
y >>= 4;
if (board[y][x] == 0) {
board[y][x] = human;
player = cpu;
repaint();
}
}
return true;
}
public void legalmove(int black, int white) {
//System.out.print(">> legalmove <<");
int b, w, z, zbw, zbw1, zbw2, zbw3, zbw4, zbw5, zbw6, ii, jj,
connect;
for (int j = 0; j < 11; j++) {
for (int i = 0; i < 11; i++) {
for (int l = -1; l < 2; l++) {
for (int k = -1; k < 2; k++) {
b = 0;
w = 0;
z = 0;
zbw = 0;
zbw1 = 0;
zbw2 = 0;
zbw3 = 0;
zbw4 = 0;
zbw5 = 0;
zbw6 = 0;
ii = i;
jj = j;
connect = 0;
if (!(k == 0 && l == 0)) {
for (int m = 1; m < 6; m++) {
if ((ii > -1 && ii < 11)
&& (jj > -1 && jj < 11)) {
if (board[jj][ii] == black) {
b++;
z++;
} else if (board[jj][ii] == white) {
w++;
z++;
} else {
zbw++;
z++;
if (zbw == 1) {
zbw1 = jj;
zbw2 = ii;
zbw3 = m;
} else if (zbw == 2) {
zbw5 = jj;
zbw6 = ii;
zbw4 = m;
}
}
}
ii += k;
jj += l;
}
if (z == 5) {
if (w == 5) {
win = human;
}
if ((zbw3 == 1 && (zbw4 == 2 || zbw4 == 5))
|| (zbw3 == 4 && zbw4 == 5)) {
connect = 1;
}
if (b == 0) {
if (w == 4) {
board[zbw1][zbw2] = 1644;
} else if (w == 3) {
//if(connect==1){
if (zbw3 == 1
&& zbw4 == 2
&& board[zbw5][zbw6] < 1634) {
board[zbw5][zbw6] = 1634;
} else if (board[zbw1][zbw2] < 1634) {
board[zbw1][zbw2] = 1634;
}
//}else if(board[zbw1][zbw2]<1632){
// board[zbw1][zbw2]+=204;
//}
}
}
if (w == 0) {
if (b == 4 && board[zbw1][zbw2] < 1645) {
board[zbw1][zbw2] = 1645;
if (win != human) {
win = cpu;
}
} else if (
b == 3 && board[zbw1][zbw2] < 1633) {
board[zbw1][zbw2] = 1635;
} else if (
b == 2 && board[zbw1][zbw2] < 1624) {
board[zbw1][zbw2] += 203;
} else if (
b == 1 && board[zbw1][zbw2] < 200) {
board[zbw1][zbw2] += 25;
} else if (b == 0 && board[j][i] < 24) {
board[j][i] += 3;
}
}
}
}
}
}
}
}
}
public void paint(Graphics g) {
//System.out.print(">> paint <<");
int k = 0, l;
if (INITP == 0) {
offscreen.setColor(Color.orange);
offscreen.fillRect(0, 0, 177, 177);
offscreen.setColor(Color.black);
for (int i = BLOCKY; i < 168; i += BLOCKWIDTH) {
offscreen.drawLine(i, BLOCKY, i, 168);
offscreen.drawLine(BLOCKX, i, 168, i);
}
INITP = 1;
}
for (int j = 0; j < 11; j++) {
l = 0;
for (int i = 0; i < 11; i++) {
if (board[j][i] == human) {
offscreen.setColor(Color.black);
offscreen.fillOval(l, k, 16, 16);
} else if (board[j][i] == cpu) {
offscreen.setColor(Color.white);
offscreen.fillOval(l, k, 16, 16);
} else if (board[j][i] == rock) {
offscreen.setColor(Color.red);
offscreen.fillOval(l, k, 16, 16);
}
l += 16;
}
k += 16;
}
if (gamestate == GAMEOVER) {
offscreen.setColor(new Color(0, 0, 0));
Font fov = new Font("TimeRoman", Font.BOLD, 28);
offscreen.setFont(fov);
offscreen.setColor(Color.red);
if (win == cpu) {
offscreen.drawString("You Lost!", 35, 114);
} else if (win == human) {
offscreen.drawString("You Win!", 35, 114);
}
offscreen.drawString("Game Over!", 20, 144);
}
g.drawImage(image, 0, 0, this);
}
public void update(Graphics g) {
//System.out.print(">> update <<");
paint(g);
}
public void run() {
//System.out.print(">> run <<");
int okmove, okmove1, okmove2;
while (gamestate != STOPGAME) {
okmove = 2;
okmove1 = 7;
okmove2 = 7;
if (gamestate == PLAY) {
//player=rock;
if (player == human) {
//System.out.print(">> human <<");
} else if (player == cpu) {
//System.out.print(">> cpu <<");
legalmove(cpu, human);
for (int j = 0; j < 11; j++) {
for (int i = 0; i < 11; i++) {
if (board[j][i] > okmove) {
okmove = board[j][i];
okmove1 = j;
okmove2 = i;
}
if (board[j][i] > 2) {
board[j][i] = 0;
}
}
}
if (win != 0) {
if (win == cpu) {
board[okmove1][okmove2] = cpu;
}
player = 0;
gamestate = GAMEOVER;
} else {
board[okmove1][okmove2] = cpu;
player = human;
}
repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
} else if (gamestate == GAMEOVER) {
} else if (gamestate == USERFIRST) {
//init();
repaint();
win = 0;
gamestate = PLAY;
player = human;
INITP = 0;
for (int j = 0; j < 11; j++) {
for (int i = 0; i < 11; i++) {
board[i][j] = 0;
}
}
} else if (gamestate == PCFIRST) {
//init();
repaint();
win = 0;
gamestate = PLAY;
player = cpu;
INITP = 0;
for (int j = 0; j < 11; j++) {
for (int i = 0; i < 11; i++) {
board[i][j] = 0;
}
}
}
}
}
public void stop() {
//System.out.print(">> stop <<");
if (runner != null) {
runner.stop();
runner = null;
}
}
public void destroy() {
//System.out.print(">> destroy <<");
}
public static int getRandomCoordinate(int maxValue){
long randValue = 0;
randValue = Math.round (Math.floor (maxValue * Math.random ()));
System.out.println("RAND VALUE= " +randValue);
return Integer.parseInt(randValue+"");
} |