Tic Tac Toe

Пример знаменитой игры "Крестики-нолики". Автор сделал неплохой AI , но это не тупо выигрываюший компьютер, есть возможность выиграть и у него.
Платформы: Windows , Linux
Автор: BasicCoder2 , создано в 2011 году.

TicTacToe

Screenres 320,240,32


'mouse variables
Dim Shared As Integer mx,my,btns,ox,oy

'global variables
Dim Shared As Integer b(3,3) 'current board

'test for win position
'3 horizontal wins
Function TestWin(t As Integer) As Integer

    Dim As Integer win
    win = 0

    If b(0,0)= t And b(1,0)= t And b(2,0)= t Then

        win = t
    End If

    If b(0,1)= t And b(1,1)= t And b(2,1)= t Then

        win = t
    End If

    If b(0,2)= t And b(1,2)= t And b(2,2)= t Then

        win = t
    End If

    '3 vertical wins
    If b(0,0)= t And b(0,1)= t And b(0,2)= t Then

        win = t
    End If

    If b(1,0)= t And b(1,1)= t And b(1,2)= t Then

        win = t
    End If

    If b(2,0)= t And b(2,1)= t And b(2,2)= t Then

        win = t
    End If

    'cross wins
    If b(0,0)= t And b(1,1)= t And b(2,2)= t Then

        win = t
    End If

    If b(2,0)= t And b(1,1)= t And b(0,2)= t Then

        win = t
    End If

    Return win
End Function


Sub InitializeBoard()
    For j As Integer = 0 To 2

        For i As Integer = 0 To 2

            b(i,j)=0

        Next i
    Next j
End Sub



'display board
Sub DisplayBoard()
    Locate 1,1

    Print "+---+---+---+"
    For j As Integer = 0 To 2

        Print "|";
        For i As Integer = 0 To 2

            If b(i,j)=0 Then Print "   |";
            If b(i,j)=1 Then Print " x |";
            If b(i,j)=2 Then Print " o |";
        Next i
        Print

        Print "+---+---+---+"
    Next j
End Sub


Function humanMove() As Integer

    Dim As Integer choice
    DisplayBoard()
    Print

    Print "USE MOUSE TO CLICK SQUARE OF CHOICE"
    choice = -1

    While choice = -1

        Getmouse mx,my,,btns
        While btns<>1 'wait for select button down
            Getmouse mx,my,,btns
        Wend

        mx = (mx-4)\32

        my = (my-4)\16

        If mx>=0 And mx<3 And my>=0 And my<3 Then

            If b(mx,my)=0 Then  'square empty?
                choice = mx+my*3

            End If

        End If

        While btns=1

            Getmouse mx,my,,btns
        Wend

    Wend

    Return choice
End Function


Function RandomMove() As Integer

    Dim As Integer i,j,choice
    choice = Int(Rnd(1)*9)
    j = Int(choice/3)
    i = choice - Int(choice/3)*3

    While b(i,j)<> 0 Or (choice > 8 Or choice < 0)
        choice = Int(Rnd(1)*9)
        j = Int(choice/3)
        i = choice - Int(choice/3)*3

    Wend

    Return j*3+i
End Function



Function SmartMove(t As Integer) As Integer

    Dim As Integer i,j,choice,t2
    choice = -1 'signal no choice made yet

    'get opponents token t2
    If t = 1 Then t2 = 2 Else t2 = 1


    'test for center square
    If b(1,1)=0 Then

        choice = 4

    End If


    'test for win
    If choice = -1 Then
        If b(0,0)= 0 And b(1,0)= t And b(2,0)= t Then

            choice = 0

        End If

        If b(0,0)= t And b(1,0)= 0 And b(2,0)= t Then

            choice = 1

        End If

        If b(0,0)= t And b(1,0)= t And b(2,0)= 0 Then

            choice = 2

        End If


        If b(0,1)= 0 And b(1,1)= t And b(2,1)= t Then

            choice = 3

        End If

        If b(0,1)= t And b(1,1)= 0 And b(2,1)= t Then

            choice = 4

        End If

        If b(0,1)= t And b(1,1)= t And b(2,1)= 0 Then

            choice = 5

        End If



        If b(0,2)= 0 And b(1,2)= t And b(2,2)= t Then

            choice = 6

        End If

        If b(0,2)= t And b(1,2)= 0 And b(2,2)= t Then

            choice = 7

        End If

        If b(0,2)= t And b(1,2)= t And b(2,2)= 0 Then

            choice = 8

        End If


        '3 vertical blocks
        If b(0,0)= 0 And b(0,1)= t And b(0,2)= t Then

            choice = 0

        End If

        If b(0,0)= t And b(0,1)= 0 And b(0,2)= t Then

            choice = 3

        End If

        If b(0,0)= t And b(0,1)= t And b(0,2)= 0 Then

            choice = 6

        End If


        If b(1,0)= 0 And b(1,1)= t And b(1,2)= t Then

            choice = 1

        End If

        If b(1,0)= t And b(1,1)= 0 And b(1,2)= t Then

            choice = 4

        End If

        If b(1,0)= t And b(1,1)= t And b(1,2)= 0 Then

            choice = 7

        End If


        If b(2,0)= 0 And b(2,1)= t And b(2,2)= t Then

            choice = 2

        End If

        If b(2,0)= t And b(2,1)= 0 And b(2,2)= t Then

            choice = 5

        End If

        If b(2,0)= t And b(2,1)= t And b(2,2)= 0 Then

            choice = 8

        End If


        'cross blocks
        If b(0,0)= 0 And b(1,1)= t And b(2,2)= t Then

            choice = 0

        End If

        If b(0,0)= t And b(1,1)= 0 And b(2,2)= t Then

            choice = 4

        End If

        If b(0,0)= t And b(1,1)= t And b(2,2)= 0 Then

            choice = 8

        End If


        If b(2,0)= 0 And b(1,1)= t And b(0,2)= t Then

            choice = 2

        End If

        If b(2,0)= t And b(1,1)= 0 And b(0,2)= t Then

            choice = 4

        End If
        If b(2,0)= t And b(1,1)= t And b(0,2)= 0 Then

            choice = 6

        End If

    End If


    'test for block
    If choice = -1 Then
        If b(0,0)= 0 And b(1,0)= t2 And b(2,0)= t2 Then

            choice = 0

        End If

        If b(0,0)= t2 And b(1,0)= 0 And b(2,0)= t2 Then

            choice = 1

        End If

        If b(0,0)= t2 And b(1,0)= t2 And b(2,0)= 0 Then

            choice = 2

        End If


        If b(0,1)= 0 And b(1,1)= t2 And b(2,1)= t2 Then

            choice = 3

        End If

        If b(0,1)= t2 And b(1,1)= 0 And b(2,1)= t2 Then

            choice = 4

        End If

        If b(0,1)= t2 And b(1,1)= t2 And b(2,1)= 0 Then

            choice = 5

        End If



        If b(0,2)= 0 And b(1,2)= t2 And b(2,2)= t2 Then

            choice = 6

        End If

        If b(0,2)= t2 And b(1,2)= 0 And b(2,2)= t2 Then

            choice = 7

        End If

        If b(0,2)= t2 And b(1,2)= t2 And b(2,2)= 0 Then

            choice = 8

        End If


        '3 vertical blocks
        If b(0,0)= 0 And b(0,1)= t2 And b(0,2)= t2 Then

            choice = 0

        End If

        If b(0,0)= t2 And b(0,1)= 0 And b(0,2)= t2 Then

            choice = 3

        End If

        If b(0,0)= t2 And b(0,1)= t2 And b(0,2)= 0 Then

            choice = 6

        End If


        If b(1,0)= 0 And b(1,1)= t2 And b(1,2)= t2 Then

            choice = 1

        End If

        If b(1,0)= t2 And b(1,1)= 0 And b(1,2)= t2 Then

            choice = 4

        End If

        If b(1,0)= t2 And b(1,1)= t2 And b(1,2)= 0 Then

            choice = 7

        End If


        If b(2,0)= 0 And b(2,1)= t2 And b(2,2)= t2 Then

            choice = 2

        End If

        If b(2,0)= t2 And b(2,1)= 0 And b(2,2)= t2 Then

            choice = 5

        End If

        If b(2,0)= t2 And b(2,1)= t2 And b(2,2)= 0 Then

            choice = 8

        End If


        'cross blocks
        If b(0,0)= 0 And b(1,1)= t2 And b(2,2)= t2 Then

            choice = 0

        End If

        If b(0,0)= t2 And b(1,1)= 0 And b(2,2)= t2 Then

            choice = 4

        End If

        If b(0,0)= t2 And b(1,1)= t2 And b(2,2)= 0 Then

            choice = 8

        End If


        If b(2,0)= 0 And b(1,1)= t2 And b(0,2)= t2 Then

            choice = 2

        End If

        If b(2,0)= t2 And b(1,1)= 0 And b(0,2)= t2 Then

            choice = 4

        End If
        If b(2,0)= t2 And b(1,1)= t2 And b(0,2)= 0 Then

            choice = 6

        End If

    End If


    If choice = -1 Then

        If b(0,0)=0 Then

            choice = 0

        End If

        If b(2,0)=0 Then

            choice = 2

        End If

        If b(0,2)=0 Then

            choice = 6

        End If

        If b(2,2)=0 Then

            choice = 8

        End If

    End If


    'no choice make random choice
    If choice = -1 Then

        choice = Int(Rnd(1)*9)
        j = Int(choice/3)
        i = choice - Int(choice/3)*3

        'find empty square
        While b(i,j)<> 0

            choice = Int(Rnd(1)*9)
            j = Int(choice/3)
            i = choice - Int(choice/3)*3

        Wend

    End If


    Return choice

End Function


InitializeBoard()

DisplayBoard()

Dim As Integer player,moveCount,gameCount,move,totalGames,result

player = 1

moveCount = 0

gameCount = 0

move = 0


'set number of games to play
totalGames = 10


Dim As Integer i,j

Do

    'alternate players
    If player = 1 Then player = 2 Else player = 1


    'select type of move for each player
    If player = 1 Then

        move = humanMove()
    Else

        move = smartMove(2)
    End If

    'print "move =";move
    'print "player =";player

    'convert choice to board coordinates i,j
    j = Int(move/3)
    i = move - (j*3)
    b(i,j) = player 'enter players token 1 or 2

    result = TestWin(player) 'test if player has won

    DisplayBoard()


    moveCount = moveCount + 1


    '==============================================
    'test for game end and/or a win result
    '==============================================
    If moveCount = 9 Or result <> 0 Then

        displayBoard()
        If result = 0 Then

            Print "DRAW"
        End If

        If result = 1 Then

            Print "x WON"
        End If

        If result = 2 Then

            Print "o WON"
        End If

        Print

        Print "HIT SPACE A BAR TO START ANOTHER GAME OR ESC KEY TO EXIT"
        Sleep

        Cls

        InitializeBoard() 'reset board
        moveCount = 0

        gameCount = gameCount + 1

    End If


Loop Until Multikey(&H01)

End