• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Efficient way of checking for a pattern

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's running fine now. Thanks to everyone who helped out!
 
reply
    Bookmark Topic Watch Topic
  • New Topic