- Oct 20, 2004
- 419
- 0
- 0
Im struggling bad in my Java class, mainly due to working overtime. If anyone can make remarks on what is happening in the java class below, that'd be great. Keep in mind its for a tic tac toe program, and keep in mind theres 5 other classes that go with it that aren't posted here.
Any help at all would be appreciated. I need to write a paper on whats happening, not write the actual program.
ugh thank you so much, please help
import java.awt.*;
import java.awt.event.*;
public class Board extends Canvas { //Create graphics for the tic tac toe program
protected Game game;
protected int row, col;
protected int rowWidth, colHeight;
protected int board[][];
protected int maxMoves;
protected int moves;
protected boolean isover = false;
protected int winner = -1;
public Board(Game game, int row, int col) {
this.game = game;
this.row = row;
this.col = col;
maxMoves = row * col;
moves = 0;
board = new int[row][col];
addMouseListener(new MouseHandler());
}
public void paint(Graphics g) { //Call paint method
Dimension d = getSize();
rowWidth = d.width / row;
colHeight = d.height / col;
int i, j;
for (i = 1; i < row; i++)
g.drawLine(i * rowWidth, 0, i * rowWidth, d.height);
for (j = 1; j < col; j++)
g.drawLine(0, j * colHeight, d.width, j * colHeight);
Font font = new Font("Helvetica", Font.BOLD, 48);
g.setFont(font);
for (i = 0; i < row; i++)
for (j = 0; j < col; j++)
if (board[j] != 0) {
int x = i * rowWidth + 12;
int y = j * colHeight + 60;
switch (board[j]) {
case 1:
g.setColor(Color.red);
g.drawString("X", x, y);
break;
case 2:
g.setColor(Color.blue);
g.drawString("O", x, y);
break;
}
}
}
public int getRow() {
return row;
}
public int getCol() {
return col;
}
public boolean isOver() {
return isover;
}
public int getWinner() {
return winner;
}
public boolean makeMove(Move move, int playerId) {
if (isLegalMove(move)) {
moves++;
board[move.row][move.col] = playerId;
repaint();
return true;
} else {
return false;
}
}
public boolean isLegalMove(Move move) {
return (board[move.row][move.col] == 0);
}
protected void checkGame(int playerId) {
if (moves > maxMoves) {
isover = true;
return;
}
boolean win = false;
for (int i = 0; i < row; i++)
if (checkRow(playerId, i)) {
win = true;
break;
}
if (!win)
for (int i = 0; i < col; i++)
if (checkCol(playerId, i)) {
win = true;
break;
}
if (!win)
win = checkDiagnal(playerId);
if (win) {
winner = playerId;
isover = true;
}
if (moves >= maxMoves) {
isover = true;
}
}
protected boolean checkRow(int playerId, int row) {
for (int i = 0; i < col; i++)
if (board[row] != playerId)
return false;
return true;
}
protected boolean checkCol(int playerId, int col) {
for (int i = 0; i < row; i++)
if (board[col] != playerId)
return false;
return true;
}
protected boolean checkDiagnal(int playerId) {
boolean result = true;
for (int i = 0; i < row; i++)
if (board != playerId) {
result = false;
break;
}
if (result)
return true;
result = true;
for (int i = 0; i < row; i++)
if (board[col - i - 1] != playerId) {
result = false;
break;
}
return result;
}
class MouseHandler extends MouseAdapter {
public void mouseClicked(MouseEvent event) {
Point p = event.getPoint();
game.getPlayer().selectCell(p.x / rowWidth, p.y / colHeight);
}
}
}
Any help at all would be appreciated. I need to write a paper on whats happening, not write the actual program.
ugh thank you so much, please help
import java.awt.*;
import java.awt.event.*;
public class Board extends Canvas { //Create graphics for the tic tac toe program
protected Game game;
protected int row, col;
protected int rowWidth, colHeight;
protected int board[][];
protected int maxMoves;
protected int moves;
protected boolean isover = false;
protected int winner = -1;
public Board(Game game, int row, int col) {
this.game = game;
this.row = row;
this.col = col;
maxMoves = row * col;
moves = 0;
board = new int[row][col];
addMouseListener(new MouseHandler());
}
public void paint(Graphics g) { //Call paint method
Dimension d = getSize();
rowWidth = d.width / row;
colHeight = d.height / col;
int i, j;
for (i = 1; i < row; i++)
g.drawLine(i * rowWidth, 0, i * rowWidth, d.height);
for (j = 1; j < col; j++)
g.drawLine(0, j * colHeight, d.width, j * colHeight);
Font font = new Font("Helvetica", Font.BOLD, 48);
g.setFont(font);
for (i = 0; i < row; i++)
for (j = 0; j < col; j++)
if (board[j] != 0) {
int x = i * rowWidth + 12;
int y = j * colHeight + 60;
switch (board[j]) {
case 1:
g.setColor(Color.red);
g.drawString("X", x, y);
break;
case 2:
g.setColor(Color.blue);
g.drawString("O", x, y);
break;
}
}
}
public int getRow() {
return row;
}
public int getCol() {
return col;
}
public boolean isOver() {
return isover;
}
public int getWinner() {
return winner;
}
public boolean makeMove(Move move, int playerId) {
if (isLegalMove(move)) {
moves++;
board[move.row][move.col] = playerId;
repaint();
return true;
} else {
return false;
}
}
public boolean isLegalMove(Move move) {
return (board[move.row][move.col] == 0);
}
protected void checkGame(int playerId) {
if (moves > maxMoves) {
isover = true;
return;
}
boolean win = false;
for (int i = 0; i < row; i++)
if (checkRow(playerId, i)) {
win = true;
break;
}
if (!win)
for (int i = 0; i < col; i++)
if (checkCol(playerId, i)) {
win = true;
break;
}
if (!win)
win = checkDiagnal(playerId);
if (win) {
winner = playerId;
isover = true;
}
if (moves >= maxMoves) {
isover = true;
}
}
protected boolean checkRow(int playerId, int row) {
for (int i = 0; i < col; i++)
if (board[row] != playerId)
return false;
return true;
}
protected boolean checkCol(int playerId, int col) {
for (int i = 0; i < row; i++)
if (board[col] != playerId)
return false;
return true;
}
protected boolean checkDiagnal(int playerId) {
boolean result = true;
for (int i = 0; i < row; i++)
if (board != playerId) {
result = false;
break;
}
if (result)
return true;
result = true;
for (int i = 0; i < row; i++)
if (board[col - i - 1] != playerId) {
result = false;
break;
}
return result;
}
class MouseHandler extends MouseAdapter {
public void mouseClicked(MouseEvent event) {
Point p = event.getPoint();
game.getPlayer().selectCell(p.x / rowWidth, p.y / colHeight);
}
}
}
