Posted: Wed Jan 11, 2006 10:01 am Post subject: whatdotcolor
For my final project I am making connect four in java. My plan is too use console. I need a command or method similar to whatdot color in java and how to use it. The idea is to check if the four tokens are in a row using this command
Any help would be great thanks alot
Sponsor Sponsor
evogre3n
Posted: Mon Jan 16, 2006 7:56 am Post subject: (No subject)
Hey howard,
Use and array to store the information..
and lets say.. you want to check if there are 4 in a row like you said in your post...
Then just run through a loop that checks 4 array locations in a row etc.
Then draw the things based on the array information.. so lets say value of
'1' means that place is taken.. and '0' means that place isnt taken... then run througha loop.. that checks if the value of the array location is 1, then draw a circle or "token" there.. otherwise, dont draw anything.
good luck[/code]
Hikaru79
Posted: Mon Jan 16, 2006 9:22 pm Post subject: (No subject)
Or, even better, let's realize that we're using JAVA, which is OBJECT-ORIENTED, and taking care of this stuff is EXACTLY why it was designed -- to give you a clean way to model things like this without having to hack away with arrays and whatdotcolor. If you're this far in the year of a Java course and still don't have an almost knee-jerk reaction to make an object-oriented model of this sort of thing, your teacher is likely incompetent.
evogre3n
Posted: Mon Jan 16, 2006 10:20 pm Post subject: (No subject)
Well then
feel free to help out..
not really appreciated if you come in here saying "ooo OOP is amazing.. USe iT TO SOLVE THIS"
and then just leave it at that
please explain how to go about solving his problem using your proposed method
wtd
Posted: Mon Jan 16, 2006 10:28 pm Post subject: (No subject)
You have some data structure (like a two-dimensional array, for instance) that maintsin the state of your game. Then you have code that draws that state.
You also have code which recognizes that a user has pressed some key. This code changes the state of the game, then redraws it.
wtd
Posted: Mon Jan 16, 2006 10:29 pm Post subject: (No subject)
Since you have the state of your game in some kind of data structure, you can easily use that information to check for used spaces, rather than some "whatdotcolor"-ish hack.
Hikaru79
Posted: Mon Jan 16, 2006 10:45 pm Post subject: (No subject)
evogre3n wrote:
Well then
feel free to help out..
not really appreciated if you come in here saying "ooo OOP is amazing.. USe iT TO SOLVE THIS"
and then just leave it at that
please explain how to go about solving his problem using your proposed method
Alright, well, the Swing part of it is too large for me to cover it in one post (in fact, it is more appropriately covered in an entire book), but you can look up what you need about that in http://sun.java.com .
As for the game model, sure, I'll help you a bit with that
Java:
public class GamePiece
{
private boolean color; //true = black, false = red
public GamePiece(boolean color) {
this.color = color;
}
}
Java:
public class GameSlot
{
GamePiece occupyingPiece;
public GameSlot() {
}
public boolean isOccupied() {
return occupyingPiece!=null;
}
public void occupy(GamePiece piece) {
occupyingPiece = piece;
}
}
public GameBoard (int x, int y) {
grid = new GameSlot [x] [y];
for (int r = 0; r < x; r++) {
for (int c = 0; c < y; c++) {
grid [r][c] = new GameSlot();
}
}
pieceList = new ArrayList<GamePiece>();
public int dropPiece(boolean turn, int c) {
int r = grid[0].length;
while (!grid [c][r].isOccupied()) {
r -= 1;
}
grid[c][r+1].occupy(new GamePiece(turn));
return r+1;
}
}
This is very rough, incomplete, and probably has a bug or two. But this is the idea you should be following Let me know if you have any other questions, and sorry for being short earlier. It's not you I was mad it, its your teacher, for giving you guys assignments that BEG to be done properly, but not giving you the right tools.
GoVikingsGo
Posted: Tue Jan 17, 2006 8:52 am Post subject: (No subject)
okay thanks I still think im gonna go about using the array instead of that whatdotcolor - ish thing THANKS!
Sponsor Sponsor
Hikaru79
Posted: Tue Jan 17, 2006 4:02 pm Post subject: (No subject)
GoVikingsGo wrote:
okay thanks I still think im gonna go about using the array instead of that whatdotcolor - ish thing THANKS!
Okay, I'm glad we at least dissuaded you from using whatdotcolor Now keep in mind that the code I posted is pretty much also just an array, but its all kept as objects to make the code easier to write, debug and maintain. But just having an int[][] works too.
Good luck with the project Ask here for more help if you need it.
GoVikingsGo
Posted: Tue Jan 17, 2006 6:29 pm Post subject: (No subject)
Okay thanks alot, my idea is a 2d array storing 1 or 0 to indicate whether or not the spot is filled....
Now i need to figure out how to check if the player has won....
Thanks to all...
Hikaru79
Posted: Tue Jan 17, 2006 6:37 pm Post subject: (No subject)
GoVikingsGo wrote:
Okay thanks alot, my idea is a 2d array storing 1 or 0 to indicate whether or not the spot is filled....
Okay, but then how do you know if the piece filling it belongs to the red player or the black player?
GoVikingsGo
Posted: Tue Jan 17, 2006 10:18 pm Post subject: (No subject)
if complicates things a bit but 1 means player 1 and 2 means player 2
GoVikingsGo
Posted: Tue Jan 17, 2006 10:23 pm Post subject: (No subject)
the idea is to redraw the tokens everytime my loop runs but when it hits the first if statement it stop eventhough row 1, 4 and 5 may be full, Any ideas?
Hikaru79
Posted: Tue Jan 17, 2006 10:34 pm Post subject: (No subject)
the idea is to redraw the tokens everytime my loop runs but when it hits the first if statement it stop eventhough row 1, 4 and 5 may be full, Any ideas?
I may be misunderstanding your question, but if I'm not, all you have to do is remove the "else". Just have disjoint if statements. That way, even if one of them is true, it will still evaluate the rest anyway.