trying to write Connect Four

I'm fairly new to java. Actually, I'm not that new but I forgot everything I ever learned about applets. I'm trying to write Connect Four but I'm having trouble figuring out what parts of my code are supposed to do. From what I understand, start(), starts the execution of my applet. I'm not sure what code I should put in there. I basically want it to say IF the game is not over, keep playing. I'm not entirely sure whatplaying is. I think I need a thread somewhere thats constantly checking if the game is over but I'm not sure how to code that. Thanks for any help.

Here's what I have so far:

import java.applet.*;

import java.awt.*;

import java.util.*;

publicclass GameDisplayextends Appletimplements MouseListener

{

ArrayList pieces;

Image board;

Toolkit tool;

Player red,blue;

Game connect;

String turn;

boolean gameOver;

publicvoid init()

{

pieces =new ArrayList();

tool = Toolkit.getDefaultToolkit();

board = tool.getImage(fileName);

red =new Player("red");

blue =new Player("blue");

gameOver =false;

turn ="red";

connect =new Game();

addMouseListener(this);

}

publicvoid start()

{

while(!gameOver)

{

//Do something

}

stop()

}

publicvoid stop()

{

}

publicvoid destroy()

{

}

publicvoid mouseClicked (MouseEvent me)

{

Location loc =new Location(me.getX(),me.getY());

if(loc.isValid())

{

if(turn.equals("red"))

{

Piece p =new Piece("red",me.getX(),me.getY());

pieces.add(p);

}

elseif(turn.equals("blue"))

{

Piece p =new Piece("blue",me.getX(),me.getY());

pieces.add(p);

}

}

}

publicvoid mouseEntered (MouseEvent me){}

publicvoid mousePressed (MouseEvent me){}

publicvoid mouseReleased (MouseEvent me){}

publicvoid mouseExited (MouseEvent me){}

publicvoid update()

{

paint(g);

}

publicvoid paint(Graphics g)

{

for(int i=0;i<pieces.size();i++)

{

Piece p = pieces.get(i)

g.drawImage(p.getName(), p.getX(), p.getY(),null);

}

g.drawImage(board, 0, 0,null);

}

}

>

[5047 byte] By [JFactor2004a] at [2007-11-27 6:50:47]
# 1
Can't you do it based on turn? The game would be over when someone wins
kdajania at 2007-7-12 18:24:56 > top of Java-index,Java Essentials,New To Java...
# 2
but what if I want the game to report when someone wins? Or what if I want to include the ability to play against the computer? Or something?
JFactor2004a at 2007-7-12 18:24:56 > top of Java-index,Java Essentials,New To Java...
# 3
Well it still has to be turn based then.Someone has to make a winning move for the game to end. So anytime someone makes a move, check if they made they winning combo. I played the game a while back , but I don't remember how u win.
kdajania at 2007-7-12 18:24:56 > top of Java-index,Java Essentials,New To Java...
# 4
So should I just get rid of the start method then? And have the mouseClicked method basically run my game? I guess it makes sense for this game but in the future I'll want to make other games and I guess I'm just trying to have a sort of neat template/convention to work from
JFactor2004a at 2007-7-12 18:24:56 > top of Java-index,Java Essentials,New To Java...
# 5
Well, your start can start the game off. Your mouse click method will have to call other methods depending on who's turn and what the move is. Also has to check if a winning combo has been made.
kdajania at 2007-7-12 18:24:56 > top of Java-index,Java Essentials,New To Java...
# 6
but how would it start the game off? What does the start method do? When does it get called? And if I'm in the start menu for the entire game, how do any of the other methods get called ( mouse clicked)
JFactor2004a at 2007-7-12 18:24:56 > top of Java-index,Java Essentials,New To Java...
# 7

well, we have to do this step by step.

Consider the rules of the game: the game is turn based is it not?

So instead of the start, stop and destroy, maybe use another method to do this. You don't necessarily have to stick to this structure.

If your game is driven by the mouse, then yes. depending on the event or where the mouse is clicked, your mouse clicked method will call other methods to do things.

Usually in a game you want to do this:

- draw the initial board

- allow a player to make a move

- once the move is made, check if it ends the game.

- if it does not, you redraw the board

- next player's turn

- and so on....

Don't worry too much about the actual code until you have figured out all the little bits and pieces one by one. I would start with the display first, and build that way. We can move on from there. Does that work?

kdajania at 2007-7-12 18:24:56 > top of Java-index,Java Essentials,New To Java...
# 8

Ok, I added a couple more things. I'm planning on having a board object which will be a matrix of Piece objects. I think the reason that this was confusing me, was a couple of years ago I tried to right a Monopoly game and I don't remember how I did it, I know I used applets but I also had a thread going and I don't remember why I needed it.

import java.applet.*;

import java.awt.*;

import java.util.*;

public class GameDisplay extends Applet implements MouseListener

{

ArrayList pieces;

Image board;

Toolkit tool;

Player red,blue;

Game connect;

String turn,message;

public void init()

{

pieces = new ArrayList();

tool = Toolkit.getDefaultToolkit();

board = tool.getImage(fileName);

red = new Player("red");

blue = new Player("blue");

turn = "red";

message = "";

connect = new Game();

addMouseListener(this);

}

public void mouseClicked (MouseEvent me)

{

Location loc = new Location(me.getX(),me.getY());

if(loc.isValid())

{

if(turn.equals("red"))

{

Piece p = new Piece("red",me.getX(),me.getY());

pieces.add(p);

update();

}

else if(turn.equals("blue"))

{

Piece p = new Piece("blue",me.getX(),me.getY());

pieces.add(p);

update();

}

}

if(connect.gameOver())//Some recursive check or something to see if game is over.

{

message = "Game Over!";

update();

}

}

public void mouseEntered (MouseEvent me) {}

public void mousePressed (MouseEvent me) {}

public void mouseReleased (MouseEvent me) {}

public void mouseExited (MouseEvent me) {}

public void update()

{

paint(g);

}

public void paint(Graphics g)

{

for(int i=0;i<pieces.size();i++)

{

Piece p = pieces.get(i)

g.drawImage(p.getName(), p.getX(), p.getY(),null);

}

g.drawImage(board, 0, 0, null);

if(connect.gameOver())

{

g.drawString(message,0,0);

//Wait a certain amount of time

init(); //Reset game

}

}

}

>

JFactor2004a at 2007-7-12 18:24:56 > top of Java-index,Java Essentials,New To Java...
# 9
sweet
kdajania at 2007-7-12 18:24:56 > top of Java-index,Java Essentials,New To Java...