• Post Reply Bookmark Topic Watch Topic
  • New Topic

Efficient way of checking for a pattern  RSS feed

 
Wouter Hermans
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

I'm trying to make a simple noughts-and-crosses game using a 5x5 grid (String[][]; I considered ArrayLists based on my last topic but will leave those alone for now), and am now trying to write the code that checks whether the last move made a row of three noughts or three crosses (which of these two isn't important at the moment; I just need to know if there are three adjacent identical characters). But I'm having a hard time coming up with an elegant way to do the check. The options I've thought of so far are:

- Check the whole board each time. Pro: easy to code using for-loops; con: hugely wasteful.

- Starting from the last character entered, check in all directions to see if it completes a line of three identical characters. Pro: slightly less wasteful; con: code looks like this:



- Or something else

What's the best way to go about this?
 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would definitely check only in the directions around the last placed piece. you should be able to perform this with a for loop as well.


Note that for clarity I compared the fields with the equals operator. You shouldn't do this for strings. Consider using an enum instead.
 
Wouter Hermans
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


What does this do, if you don't mind me asking?

EDIT: and what is the function of the 'break' in the if-clause?

EDIT2, last one I promise: is isWithingBounds() a custom function or can I import it? Google isn't helping.

Thanks for the help so far
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Hermans wrote:

What does this do, if you don't mind me asking?

ternary operator

EDIT: and what is the function of the 'break' in the if-clause?

The break statement

EDIT2, last one I promise: is isWithingBounds() a custom function or can I import it? Google isn't helping.

Looks like something you will need to implement. Sometimes we give only parts of solutions. Other parts are "black boxes", only given as a call to an undefined function. This appears to be such a case.
 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, isWithinBounds() is something you will have to write yourself. Its purpose is to check whether the given column and row are within the field.
 
Wouter Hermans
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have something similar (a simple isValid(a,b)), I just wondered whether there was an 'official' function for it. It didn't occur to me too look into enumeration, it's not something we've covered so far in class.





 
Wouter Hermans
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's running fine now. Thanks to everyone who helped out!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!