Computer Science Canada whatdotcolor |
Author: | GoVikingsGo [ 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 |
Author: | evogre3n [ Mon Jan 16, 2006 7:56 am ] |
Post 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] |
Author: | Hikaru79 [ Mon Jan 16, 2006 9:22 pm ] |
Post 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. |
Author: | evogre3n [ Mon Jan 16, 2006 10:20 pm ] |
Post 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 |
Author: | wtd [ Mon Jan 16, 2006 10:28 pm ] |
Post 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. |
Author: | wtd [ Mon Jan 16, 2006 10:29 pm ] |
Post 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. |
Author: | Hikaru79 [ Mon Jan 16, 2006 10:45 pm ] | ||||||
Post 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
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. |
Author: | GoVikingsGo [ Tue Jan 17, 2006 8:52 am ] |
Post subject: | |
okay thanks I still think im gonna go about using the array instead of that whatdotcolor - ish thing THANKS! |
Author: | Hikaru79 [ Tue Jan 17, 2006 4:02 pm ] |
Post 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. |
Author: | GoVikingsGo [ Tue Jan 17, 2006 6:29 pm ] |
Post 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... |
Author: | Hikaru79 [ Tue Jan 17, 2006 6:37 pm ] |
Post 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? |
Author: | GoVikingsGo [ Tue Jan 17, 2006 10:18 pm ] |
Post subject: | |
if complicates things a bit but 1 means player 1 and 2 means player 2 |
Author: | GoVikingsGo [ Tue Jan 17, 2006 10:23 pm ] | ||
Post subject: | |||
okay one more question i have this
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? |
Author: | Hikaru79 [ Tue Jan 17, 2006 10:34 pm ] | ||||||
Post subject: | |||||||
GoVikingsGo wrote: okay one more question
i have this
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. Also, a tip:
can just be written as
Easier to understand what's going on, eh? |
Author: | GoVikingsGo [ Tue Jan 17, 2006 10:41 pm ] |
Post subject: | |
no i cant cause its different each time.... thx solved my problem |
Author: | GoVikingsGo [ Tue Jan 17, 2006 10:57 pm ] | ||||
Post subject: | |||||
i know i might be overlookings something cause im tired but i get an ArrayOutofBoundsException on the my array is a 2d memVar [7][6]
thanks alot |
Author: | evogre3n [ Tue Jan 17, 2006 11:20 pm ] |
Post subject: | |
try catchs are your friend |
Author: | evogre3n [ Tue Jan 17, 2006 11:23 pm ] |
Post subject: | |
GoVikingsGo wrote: else if (memVar [col] [6] == 1) You'll notice that your array is [7][6] which means the array has locations 0,1,2,3,4,5 (6 total locations) setting the if statement to 6 would give you out of bounds exception... |
Author: | GoVikingsGo [ Wed Jan 18, 2006 8:15 am ] |
Post subject: | |
6 and 7 thats the 42 position |
Author: | GoVikingsGo [ Wed Jan 18, 2006 10:08 am ] |
Post subject: | |
cancel last thx |
Author: | GoVikingsGo [ Thu Jan 19, 2006 5:05 pm ] | ||
Post subject: | |||
new problem does any1 know how to use the time class so that i can do
Please help ASAP thanks[/code] |
Author: | Hikaru79 [ Fri Jan 20, 2006 12:21 am ] | ||
Post subject: | |||
GoVikingsGo wrote: new problem does any1 know how to use the time class so that i can do
Please help ASAP thanks[/code] http://java.sun.com/j2se/1.4.2/docs/api/java/util/GregorianCalendar.html |