VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "TicTacToe_Game"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'
' File          : TicTacToe_Game.cls
'
' Project       : demo
' Configuration : demo 1
' Phase         : Implementation 1
' System        : TicTacToe 1
'
' The Tic Tac Toe game itself.
Option Explicit


' Inheritance attributes


' Association attributes

Public gui_ As TicTacToe_GUI

Private board_ As New ClassSet


' User defined attributes

Private turnNumber_ As Integer

Private players_ As String
' Has default value "XO"

Private drawText_ As String
' Has default value "It's a draw!!!"

Private winText_ As String
' Has default value " is the winner!!!"


' User defined methods

Public Sub TicTacToe_Game_Constructor()
    players = "XO"
    drawText = "It's a draw!!!"
    winText = " is the winner!!!"
    ' Start user section
    createCells
    connectCells
    ' End user section
End Sub

Private Sub Class_Terminate()
    ' Start user section
    ' End user section
End Sub

' Creates all the cells of the board.
Public Sub createCells()
    Dim acell As TicTacToe_Cell
    Dim i As Integer
  
    For i = 0 To (gui.numberOfCells - 1)
        Set acell = New TicTacToe_Cell
        acell.TicTacToe_Cell_Constructor
        AddBoard acell, i
    Next i
End Sub

' Connects all the cells of the board.
' Each cell is connected to all its neighbours.
Public Sub connectCells()
    Dim N, E, W, S As Integer
    Dim i As Integer
    N = -Sqr(gui.numberOfCells)
    W = -1
    E = -W
    S = -N
    
    For i = 0 To (gui.numberOfCells - 1)
        board(i).AddNeighbour board(i + N), "N"
        If i Mod Sqr(gui.numberOfCells) <> (Sqr(gui.numberOfCells) - 1) Then
            board(i).AddNeighbour board(i + N + E), "NE"
            board(i).AddNeighbour board(i + E), "E"
            board(i).AddNeighbour board(i + S + E), "SE"
        End If
        board(i).AddNeighbour board(i + S), "S"
        If i Mod Sqr(gui.numberOfCells) <> 0 Then
            board(i).AddNeighbour board(i + S + W), "SW"
            board(i).AddNeighbour board(i + W), "W"
            board(i).AddNeighbour board(i + N + W), "NW"
        End If
    Next i
End Sub

' Initialize everything for a new game.
Public Sub startGame()
    turnNumber = 0
    gui.clearBoard
    Me.clearBoard
    gui.enableBoard
    gui.displayResult ("")
End Sub

' Clears all the cells of the board.
Public Sub clearBoard()
    Dim i As Integer
    For i = 0 To (board_.Count - 1)
        board(i).contents = ""
    Next i
End Sub

' Checks if there is a winner or a draw
' and displays this result.
Public Sub checkResult(number As Integer)
    If board(number).getMaxLine = Sqr(gui.numberOfCells) Then
        gui.disableBoard
        gui.displayResult Me.getActivePlayer + winText
    Else
        If turnNumber = gui.numberOfCells - 1 Then
            gui.disableBoard
            gui.displayResult drawText
        End If
    End If
    turnNumber = turnNumber + 1
End Sub

' Marks the selected cell and checks to
' see if there is a result.
Public Sub selectCell(number As Integer)
    board(number).contents = getActivePlayer
    checkResult (number)
End Sub

' Determines the active player.
Public Function getActivePlayer() As String
    getActivePlayer = Mid(players, turnNumber Mod Len(players) + 1, 1)
End Function


' Access methods

Public Property Get turnNumber() As Integer
    turnNumber = turnNumber_
End Property

Public Property Let turnNumber(x As Integer)
    turnNumber_ = x
End Property

Public Property Get players() As String
    players = players_
End Property

Public Property Let players(x As String)
    players_ = x
End Property

Public Property Get drawText() As String
    drawText = drawText_
End Property

Public Property Let drawText(x As String)
    drawText_ = x
End Property

Public Property Get winText() As String
    winText = winText_
End Property

Public Property Let winText(x As String)
    winText_ = x
End Property


' Association methods

Public Property Get gui() As TicTacToe_GUI
    Set gui = gui_
End Property

Public Property Get board(Index As Integer) As TicTacToe_Cell
    If board_.ContainsKey(CStr(Index)) Then
        Set board = board_.Item(CStr(Index))
    Else
        Set board = Nothing
    End If
End Property

Public Sub AddBoard(x As TicTacToe_Cell, Index As Integer)
    If Not (x Is Nothing) Then
        board_.Add x, CStr(Index)
    End If
End Sub

Public Sub RemoveBoard(Index As Integer)
    Dim temp As TicTacToe_Cell
    Set temp = board_.Item(CStr(Index))
    board_.RemoveUsingKey(CStr(Index))
End Sub

