I really need help with Java... Can you remark on whats happening in this class?

Chris2wire

Senior member
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);
}
}

}
 

AgaBoogaBoo

Lifer
Feb 16, 2003
26,108
5
81
What exactly are you confused with? If everything, pick some things out you want explained.
 

Chris2wire

Senior member
Oct 20, 2004
419
0
0
I need help with what various parts of the code are doing... Not necessarily every line, but every major method or calling action, anything like that... I basically need descriptions of various important lines. So maybe like remarking on the lines with a short sentance of what its doing.
 

amdskip

Lifer
Jan 6, 2001
22,530
13
81
So the program is there and you just need to comment for each section what it is doing?
 

drinkmorejava

Diamond Member
Jun 24, 2004
3,567
7
81
lol, if you color it for me, I'll comment it for you. And no, I'm not going to drop it into netbeans.
 

EvilYoda

Lifer
Apr 1, 2001
21,198
9
81
What exactly do you understand then? Most of the program is simple getters & setters along with basic loops to implement the game...
 

ngvepforever2

Golden Member
Oct 19, 2003
1,269
0
0
dude...WTF? this isn't even programming or JAVA, just look at the code and figure out what it does. Basically, all you have to do is comment it. Really man! just look at names of the methods. They basically tell you what they do.

Regards

ng
 

Chris2wire

Senior member
Oct 20, 2004
419
0
0
I know its easy, im just so burnt out from work and school, and panicking because this is all due thursday. Ok ill try to get it all done myself, i was just looking for a little help.

Im not one to EVER ask for school help, its just the circumstances. Sorry if this seemed dumb.
 

purbeast0

No Lifer
Sep 13, 2001
53,664
6,546
126
here

----jGRASP exec: javac -g C:\Documents and Settings\Drew\My Documents\Board.java

Board.java:6: cannot find symbol
symbol : class Game
location: class Board
protected Game game;
^
Board.java:17: cannot find symbol
symbol : class Game
location: class Board
public Board(Game game, int row, int col) {
^
Board.java:74: cannot find symbol
symbol : class Move
location: class Board
public boolean makeMove(Move move, int playerId) {
^
Board.java:85: cannot find symbol
symbol : class Move
location: class Board
public boolean isLegalMove(Move move) {
^
Board.java:41: incomparable types: int[] and int
if (board[j] != 0) {
^
Board.java:44: incompatible types
found : int[]
required: int
switch (board[j]) {
^
Board.java:122: incomparable types: int[] and int
if (board[row] != playerId)
^
Board.java:129: incomparable types: int[] and int
if (board[col] != playerId)
^
Board.java:137: incomparable types: int[][] and int
if (board != playerId) {
^
Board.java:145: incomparable types: int[] and int
if (board[col - i - 1] != playerId) {
^
10 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

enjoy.
 

Chris2wire

Senior member
Oct 20, 2004
419
0
0
lol, of course it wont compile without the other classes on your system...

I meant what is happening at various lines within the program But yeah...
 

purbeast0

No Lifer
Sep 13, 2001
53,664
6,546
126
Originally posted by: Chris2wire
lol, of course it wont compile without the other classes on your system...

I meant what is happening at various lines within the program But yeah...

run it through a debugger ... you will be able to see it line by line whats going on.
 

LordSnailz

Diamond Member
Nov 2, 1999
4,821
0
0
seems like the folks are willing to help but you're not putting in any effort at all, 3rd post and still no specific question aside from 'what's happening in various lines of the code' ... be more specific and I'm sure ppl. would help out.
 

Chris2wire

Senior member
Oct 20, 2004
419
0
0
Well see what I was asking for is for people to make remarks within the code about what lines in the code are doing.

So if a line says "public class bob extends thread" then they would put at the end "//this new class bob is creating a new thread"

Or something like that... And basically to make remarks on whatever lines they choose, since I need all the help I can get.

I mean I dont mean to be a hassle to anyone, so if anyone has time and doesn't mind then I'd appreciate the help, but I don't mean to bug anyone about it.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
If a line says "public class bob extends thread",

WTF do you want us to write except "This defines a new class named 'bob' that is publicly accesible and extends the 'thread' class."

You can't figure that out yourself?
 

Chris2wire

Senior member
Oct 20, 2004
419
0
0
Let me ask a more specific question... What does this snippet mean:

public void init()
{
String gametype = getParameter("type");
if (gametype.equals("human-human"))
{
players[0] = new HumanPlayer(this, 1);
players[1] = new HumanPlayer(this, 2);
}
else if (gametype.equals("machine-machine"))
{
players[0] = new MachinePlayer(this, 1);
players[1] = new MachinePlayer(this, 2);
} else { players[0] = new HumanPlayer(this, 1);
players[1] = new MachinePlayer(this, 2);
}
players[0].start();
players[1].start();
players[0].setNext(players[1]);
players[1].setNext(players[0]);
players[0].turn();
}
 
Jun 4, 2005
19,723
1
0
If it's a human vs human game, create players 1 and 2.
If it's a machine vs machine game, create machine 1 and 2.
Else, create player 1, player 2.

Run game with player 1 and player 2, or machine 1 and machine 2 depending on game type.
Take turns, if it's player 1's turn, player 2's turn will be next. If it's player 2's turn, player 1's turn will be next.
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
If you can't understand tic-tac-toe code, it's time to look at another industry. And why isn't this locked yet?
 

Chris2wire

Senior member
Oct 20, 2004
419
0
0
ok really, im sorry to bug you all and cause commotion. consider this thread locked now, even though its not literally locked...