• Post Reply Bookmark Topic Watch Topic
  • New Topic

Making a player in a connect 4 game  RSS feed

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I have getCloumn, getColumnCount, getRowCount, getSlot, getIsFull, getRowCount, getIsFilled, getIsRed, addRed, addYellow methods to use and not sure how to implement this stuff.
 
Saloon Keeper
Posts: 7994
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Aaron,

Start with the simpler methods, such as iCanWin() and theyCanWin(). How would you determine whether somebody can win in real life? For each column, you would imagine what the game would look like if a piece of a certain color was dropped down, and then you need to determine if that leads to a situation where there are 4 pieces in a sequence. You can break the logic down by making a method isWinningMove(Color color, int column) that simulates dropping (but doesn't actually!) a piece of the given color into the given column, and see if it connects with three other pieces of the same color for any direction. This task can be done in another separate method.

The crux is to break every down into bite-size chunks that are easy to implement and then reuse.
 
aaron butcher
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Hi Aaron,

Start with the simpler methods, such as iCanWin() and theyCanWin(). How would you determine whether somebody can win in real life? For each column, you would imagine what the game would look like if a piece of a certain color was dropped down, and then you need to determine if that leads to a situation where there are 4 pieces in a sequence. You can break the logic down by making a method isWinningMove(Color color, int column) that simulates dropping (but doesn't actually!) a piece of the given color into the given column, and see if it connects with three other pieces of the same color for any direction. This task can be done in another separate method.

The crux is to break every down into bite-size chunks that are easy to implement and then reuse.


like this


 
Stephan van Hulst
Saloon Keeper
Posts: 7994
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like that, except you're not checking horizontal or diagonal sequences. The logic of your iCanWin() and theyCanWin() methods is actually exactly the same, so you can remove one of them and rename the other to getWinningColumn(boolean isChipRed).

You can make the sequence checking more dynamic by using loops. For a horizontal sequence, start at x-3 until you reach x+3. For each chip of the intended color, add 1 to a counter. If a slot doesn't have a chip of the intended color, reset the counter. If the counter reaches 4, the move is winning.
 
Stephan van Hulst
Saloon Keeper
Posts: 7994
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This task is actually a lot easier to think about if the game used a Color enum, instead of working with booleans:


 
aaron butcher
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


had to change it i have to have the icanwin and theycanwin methods but not sure where to go aftering checking all the positions
 
Saloon Keeper
Posts: 3332
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't return more than one thing from a method.

won't compile. If you need to return more that one thing (e.g. i & j) make a small class to hold them and return a new instance of that class.
 
aaron butcher
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:You can't return more than one thing from a method.

won't compile. If you need to return more that one thing (e.g. i & j) make a small class to hold them and return a new instance of that class.




there this is close but can get it to work all the way
 
Carey Brown
Saloon Keeper
Posts: 3332
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tweak to match Java convention. When creating getters use 'is' prefix for boolean values. E.g. isRed() instead of getIsRed().
 
aaron butcher
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Tweak to match Java convention. When creating getters use 'is' prefix for boolean values. E.g. isRed() instead of getIsRed().


that is a set method already
 
Stephan van Hulst
Saloon Keeper
Posts: 7994
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaron, you still have a lot of code duplication. Your code is also very static, meaning you can simplify your conditionals by using loops like I explained earlier.

Try to implement the following method (I removed the Color so you can keep using booleans instead). You're only allowed to use one for-loop, and you may write getColumn() only once.
 
aaron butcher
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Aaron, you still have a lot of code duplication. Your code is also very static, meaning you can simplify your conditionals by using loops like I explained earlier.

Try to implement the following method (I removed the Color so you can keep using booleans instead). You're only allowed to use one for-loop, and you may write getColumn() only once.
 
Stephan van Hulst
Saloon Keeper
Posts: 7994
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that doesn't really do anything, does it?
 
aaron butcher
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Well, that doesn't really do anything, does it?



ok so i have this and i need to make it into a for loop with a static variable for the 3 distance
 
Stephan van Hulst
Saloon Keeper
Posts: 7994
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These are not all possible combinations of where a sequence of tokens may appear.

Hint: You don't have to check specific combinations (such as 3 to the right, and 2 to the right and 1 to the left). If you're checking a horizontal sequence, you should check all slots from -3 to 3, and if at any point there are at least 4 tokens of the same color following one another, you're done.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!