Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I was hanging out on irc (freenode, #perl) this morning chatting about this and that, mostly about crappy programming languages and so forth. Someone entering the conversation mentioned that once he sat on a class of VB students who had to write a tic-tac-toe "gui" in VB, no AI, just checking for win conditions. He recounted that all of the students solved the problem by writing 16 or 20 or however many lines of code that looked like:
if( $board[0][0] eq 'x' and $board[0][1] eq 'x' and $board[0][2] eq 'x +' ) { #win! }
Or whatever the VB equivalent happened to be. Naturally, as a Perl programmer who strives to be as concise as possible (while still being clear and understandable of course), I was horrified by all the repeated code. But then when I stopped to think how I would do it, I had problems coming up with a way that was substantially better. So I pose the question to you: Given a datastructure, how do you check for a tic-tac-toe win condition?

Feel free to assume any data structure, I have arbitrarily decided the default datastructure will be a 2d array containing 'x's, 'o's and 'undefs', whose meaning should be obvious. Using a different one is perfectly acceptable, just make sure you describe said datastructure.

(And yes, I promise I'm not going to steal *anybody's* hard work and fraudently use it to gain a job. Not that I'm bitter. This "challenge" has nothing to do with anything in the real world and is merely the result of a late night conversation on IRC.)

In reply to Checking Tic-Tac-Toe Win conditions? by BUU

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-06-13 19:34 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.