AI – Artificial Intelligence – PBP Challenge For The Fittest


Closed Thread
Results 1 to 21 of 21

Hybrid View

  1. #1
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Post VB Source Portion C

    Code:
    Public Sub Check_For_Win()
       '-----------------------------------------------------------------------------
       '*Someone win game?, check all possibilities line ups of 3 both X & O
       '-----------------------------------------------------------------------------
       Dim Count_Correct     As Long    'Total of correct in line up / sequence
       Dim Wining_Line(3, 3) As Long    'Store coords of matrix
       Dim i                 As Long    'General working var
       Dim j                 As Long    '""
       Dim k                 As Long    '""
       Dim e                 As Long    '""
       Dim XO_Wins(1)        As Boolean 'X & O flgas set true if game won
       '//
       For j = 0 To 1
           If Not XO_Wins(0) And Not XO_Wins(1) Then
              For k = 0 To 2                           'This array allows us to blink the wining line
                  For i = 0 To 2                       '
                      Wining_Line(i, k) = 0            'Reset for next check
                  Next
              Next
              Count_Correct = 0
       '//
       'Check rows ply1, ply2 or CPU
              For e = 0 To 2
                  For i = 0 To 2
                      If Grid_Set(e, i) = (j + 1) Then 'Inc
                         Count_Correct = Count_Correct + 1
                         Wining_Line(e, i) = 1         'Set
                      End If
                  Next
                  XO_Wins(j) = (Count_Correct = 3)     'Set flag if 3 in a row
                  '//
                  If Not XO_Wins(0) And Not XO_Wins(1) Then
                     For k = 0 To 2                    'Reset for next check
                         For i = 0 To 2                '
                             Wining_Line(i, k) = 0     '
                         Next
                     Next
                     Count_Correct = 0
                  Else
                     Exit For                          'Bail out we have a winner
                  End If
              Next
           End If
       '//
           If Not XO_Wins(0) And Not XO_Wins(1) Then
              For k = 0 To 2                           'Reset for next check
                  For i = 0 To 2                       '
                      Wining_Line(i, k) = 0            '
                  Next
              Next
              Count_Correct = 0
       '//
       'Cols
              For e = 0 To 2
                  For i = 0 To 2
                      If Grid_Set(i, e) = (j + 1) Then 'Inc
                         Count_Correct = Count_Correct + 1
                         Wining_Line(i, e) = 1         'Set
                      End If
                  Next
                  XO_Wins(j) = (Count_Correct = 3)     'Set flag if 3 in a row
                  '//
                  If Not XO_Wins(0) And Not XO_Wins(1) Then
                     For k = 0 To 2                    'Reset for next check
                         For i = 0 To 2                '
                             Wining_Line(i, k) = 0     '
                         Next
                     Next
                     Count_Correct = 0
                  Else
                     Exit For                          'Bail out we have a winner
                  End If
              Next
           End If
       '//
       'Test diagonal \
           If Not XO_Wins(0) And Not XO_Wins(1) Then
              For k = 0 To 2                           'Reset for next check
                  For i = 0 To 2                       '
                      Wining_Line(i, k) = 0            '
                  Next
              Next
              Count_Correct = 0
              '//
              For i = 0 To 2
                  If Grid_Set(i, i) = (j + 1) Then
                     Count_Correct = Count_Correct + 1 'Inc
                     Wining_Line(i, i) = 1             'Set
                  End If
              Next
              XO_Wins(j) = (Count_Correct = 3)         'Set flag if 3 in a row
           End If
       '//
           If Not XO_Wins(0) And Not XO_Wins(1) Then
              For k = 0 To 2                           'Reset for next check
                  For i = 0 To 2                       '
                      Wining_Line(i, k) = 0            '
                  Next
              Next
              Count_Correct = 0
              k = 0
       '//
       'Diagonal /
              For i = 2 To 0 Step -1
                  If Grid_Set(k, i) = (j + 1) Then
                     Count_Correct = Count_Correct + 1 'Inc
                     Wining_Line(k, i) = 1             'Set
                  End If                               '
                  k = k + 1                            'Inc offset for / check
              Next
              k = 0
              XO_Wins(j) = (Count_Correct = 3)         'Set flag if 3 in a row
           End If
       Next
       '//
       Dim Player_Won As Byte
       If XO_Wins(0) Or XO_Wins(1) Then                'X or O Win flags set?
          '//                                          '
          Select Case XO_Wins(1)                       'Which one?
                 '//                                   '
                 Case True                             '
                      If Players_CHR(0) = 2 Then       'Wining player using 0 or X?
                         Player_Won = 0                'Add to stats
                         Total_Losses(1) = Total_Losses(1) + 1
                      Else
                         Player_Won = 1                'Add to stats
                         Total_Losses(0) = Total_Losses(0) + 1
                      End If
                 '//
                 Case Else
                 '//
                      If Players_CHR(0) = 2 Then
                         Player_Won = 1                'Add to stats
                         Total_Losses(0) = Total_Losses(0) + 1
                      Else
                         Player_Won = 0                'Add to stats
                         Total_Losses(1) = Total_Losses(1) + 1
                      End If
          End Select
      '//
      'Inform who won
          Game_Msg.Caption = Players_Name(Player_Won) & " WON!"
          Total_Wins(Player_Won) = Total_Wins(Player_Won) + 1
      '//
          If Settings_Play_Sound.Checked Then
             i = sndPlaySound(App.Path & "\Win.wav", CSNDaSync)
          End If
      '//
      'Player's or CPU's best time?
          If Game_Duration < Best_Time(Player_Won) Or Best_Time(Player_Won) = 0 Then
             Best_Time(Player_Won) = Game_Duration
          End If
      '//
          Game_Rounds = Game_Rounds + 1                'Inc round count
          Call LED_Displays                            'Refresh displays
          Game_Over = True                             'Flag set
      '//
          If Not Terminate_App Then                    'Object still loaded?
             For e = 0 To 10
                 For i = 0 To 2
                     For j = 0 To 2
                  'Scrn_Render all current
                         BitBlt Play_Area.hDC, 9 + (i * 60), 9 + (j * 60), 45, 45, X0(Grid_Set(j, i)).hDC, 0, 0, SrcCopy
       '//
                  'Flash render wining
                         If k And Wining_Line(j, i) Then
                            BitBlt Play_Area.hDC, 9 + (i * 60), 9 + (j * 60), 45, 45, X0(0).hDC, 0, 0, SrcCopy
                         End If
                     Next
                 Next
       '//
                 Call Delay(250)                       '250mS
                 Play_Area.Cls                         'Wipe screen
                 k = k + 1                             'Inc K which allows the winning line to be flashed
                 k = k Mod 2                           'Reset K
                 DoEvents                              'Yeild to OS
       '//
       'Player select new round or request app term?, bail out of here is so...
                 If Game_Duration = 0 Or Terminate_App Then Exit For
             Next
          End If
       '//
       'Music playing?
          If Not Terminate_App Then
             If Settings_Play_Music.Checked Then
                Music ("stop med")                     'Cease music!
             End If
          End If
       End If
    End Sub

  2. #2
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Post

    You'll need a big PIC with a lot of code space to properly do this project.

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    I can do it on a 16F877.
    <br>
    DT

  4. #4
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Post

    I spent about a month studying Tic-Tac-Toe and developing the Windows-based software. Had a lot of fun doing this project as it gave me a greater sense for logic and a bit of an insight as to how the human mind thinks.

    I tried many different protocols for AI. Including one that favors blocking the players move over winning the actual game itself. I was unable to produce anything successful with this. I think this is actually impossible, you just can't play the game with sole intentions of blocking. You've gotta play to win.

    Interesting stuff - if it's your cup of tea.

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    OK,

    Well, I'll take that as there wasn't really a "Challenge" to begin with.

    Nevermind.
    <br>
    DT

  6. #6
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Post

    Oh I don't know about that. Coding a project like this in PBP would be twice as involved as doing it in Visual Basic. In fact, I don't know of any perfected Tic-Tac-Toe project that relies solely on an embedded system.

  7. #7
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    You keep telling me how it's such a difficult task, and would take months to figure out.

    And I keep telling you I've done it with a 16F and less than 4K words of code.

    He said, she said.

    Prove it, or go home.
    <br>
    DT

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

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