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);
}
}
>
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?
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
}
}
}
>