Results 1 to 5 of 5

Thread: Tic-Tac-Toe: What data structure would you use?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2014
    Posts
    232

    Tic-Tac-Toe: What data structure would you use?

    You could hard code a huge multi- if statement the checks all variations of 3-in-a-row.
    (Check for a win on the click() event) Obviously, that's good for an 8th grader.

    How would you design this game? 2 player, to start.
    9 picture boxes that can show an X or O image when clicks.
    A counter to keep track of which symbol gets printed next click.

    But, how to store the grid and a better algo to check for a win ?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,393

    Re: Tic-Tac-Toe: What data structure would you use?

    here's a clue:

    Code:
    Public Class Form1
    
        ''' <summary>
        ''' 2d array representing cells in tictactoe grid
        ''' </summary>
        ''' <remarks>each element should contain X or 0 or ""</remarks>
        Dim grid(2, 2) As String
    
        Private Function checkWin(ByVal player As String) As String
            Dim rows() As String = { _
                                    grid(0, 0) & grid(0, 1) & grid(0, 2), _
                                    grid(1, 0) & grid(1, 1) & grid(1, 2), _
                                    grid(2, 0) & grid(2, 1) & grid(2, 2), _
                                    grid(0, 0) & grid(1, 0) & grid(2, 0), _
                                    grid(0, 1) & grid(1, 1) & grid(2, 1), _
                                    grid(0, 2) & grid(1, 2) & grid(2, 2), _
                                    grid(0, 0) & grid(1, 1) & grid(2, 2), _
                                    grid(2, 0) & grid(1, 1) & grid(0, 2) _
                                    }
    
            Dim x As Integer = Array.FindIndex(rows, Function(s) s = New String(CChar(player), 3))
    
            If x > -1 Then
                Return String.Format("Player: {0} wins", player)
            Else
                Return "No winning rows"
            End If
    
        End Function
    
    End Class

  3. #3
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Tic-Tac-Toe: What data structure would you use?

    A 3 by 3 array may seem the most natural way of representing this, but the best model is not the most realistic, but the one that most effectively answer the questions of the domain. You may find this write up of a TDD-as-if-you-meant-it exercise on Tic-Tac-Toe interesting: http://gojko.net/2009/08/02/tdd-as-i...-it-revisited/

    By the end of the exercise, almost half the teams were coding towards something that was not a 3×3 char/int grid. We did not have the time to finish the whole thing, but some interesting solutions in making were:
    • a bag of fields that are literally taken by players – field objects start in the collection belonging to the game and move to collections belonging to players, which simply avoids edge cases such as taking an already taken field and makes checking for game end criteria very easy.
    • fields that have logic whether they are taken or not and by whom
    • game with a current state field that was recalculated as the actions were performed on it and methods that could set this externally to make it easy to test

    Most of the game classes had very nice explicit methods for checking different game end criteria. During a short retrospective at the end, some participants said that their code was very nicely isolated and encapsulated, with “less leakage than normal”, and that very explicit specifications ended up in the code making it easy to understand.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,393

    Re: Tic-Tac-Toe: What data structure would you use?

    I've actually written a 'play against your PC' program with a game class, a grid class, and a cell class.
    It's OOP Programming, and very tough to beat, but it's written in Java, and i haven't got round to rewriting it in VB.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,393

    Re: Tic-Tac-Toe: What data structure would you use?

    Here it is rewritten in VB and C#

    https://code.msdn.microsoft.com/wind...actoe-776a8cc6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width