All pastes #2001978 Raw Edit

Reversi V1.3

public text v1 · immutable
#2001978 ·published 2010-11-25 13:52 UTC
rendered paste body


Module Module1

    Dim BoardSquare(8, 8) As String, DupeBoard(8, 8) As String
    Dim Flipped(25, 2) As Integer
    Dim PlayerIcon As String = "X", AIIcon As String = "O"
    Dim HintsEnabled As Boolean = False, GameOver As Boolean = False

    Sub Main()
        Dim Quit As String

        Do
            Call StartGame()
            Console.WriteLine("Do You Wish To Continue (Y/N) : ") : Quit = Console.ReadLine()
        Loop Until LCase$(Quit) = "n"
    End Sub

    Sub StartGame()
        Call AssignArray()
        Randomize()
        Dim MoveCount As Integer = 1
        Dim StartPlayer As String
        Dim StartNumber As Integer = CInt(Rnd() * 2)

        Do
            StartNumber = CInt(Rnd() * 2)
            If StartNumber = 1 Then
                StartPlayer = "Computer"
            ElseIf StartNumber = 2 Then
                StartPlayer = "Player"
            End If
        Loop Until StartNumber <> 0

        Console.WriteLine(StartPlayer + " Makes The First Move")

        Do
            Call ShowBoard()
            If MoveCount + StartNumber Mod 2 = 0 Then
                Call GetAIMove()
                Call CheckWin(PlayerIcon)
                Call ShowBoard()
            Else
                Call GetPlayerMove()
                Call CheckWin(AIIcon)
                Call ShowBoard()
            End If

            MoveCount += 1
        Loop Until GameOver = True

    End Sub

    Sub GetPlayerMove()
        Dim XMove, YMove As Integer
        Dim Input, Split1, Split2 As String

        Console.Write("Enter Your Move (1-8, A-H) : ") : Input = Console.ReadLine()
        Split1 = Left$(Input, 1)
        Split2 = Right$(Input, 1)
        YMove = CInt(Split1)
        XMove = Asc(Split2) - 96 'lcase the letter

        Console.WriteLine(XMove.ToString())
        Console.ReadKey()

        If IsValidMove(XMove, YMove, PlayerIcon) <> 0 Then
            Call MakeMove(PlayerIcon, XMove, YMove)
        Else

        End If

    End Sub


    Sub GetAIMove()
        Randomize()
        Dim BestX, BestY, BestScore, ScoreHolder As Integer
        Dim RndChoice, BestOrRnd As Integer

        'Determines the best scoring move for the computer
        'Allow rnd choice of valid moves so computer wont always win?
        '
        BestOrRnd = CInt(Rnd() * 2)

        If BestOrRnd = 1 Then
            Call GetValidMoves(AIIcon)

            For i As Integer = 1 To 8
                For j As Integer = 1 To 8
                    If DupeBoard(i, j) = "*" Then
                        ScoreHolder = IsValidMove(i, j, AIIcon)
                        If ScoreHolder > BestScore Then
                            BestScore = ScoreHolder
                            BestX = i
                            BestY = j
                        End If
                    End If
                Next
            Next

            Call MakeMove(AIIcon, BestX, BestY)
        Else
            Do
                RndChoice = CInt(Rnd() * UBound(Flipped))
            Loop Until Flipped(RndChoice, 1) <> vbNull

            Call MakeMove(AIIcon, Flipped(RndChoice, 1), Flipped(RndChoice, 2))
        End If

    End Sub

    Sub MakeMove(ByVal Icon As String, ByVal XCord As Integer, ByVal YCord As Integer)
        Dim Count As Integer = 1

        If IsValidMove(XCord, YCord, Icon) <> 0 Then
            BoardSquare(XCord, YCord) = Icon

            While Flipped(Count, 1) <> vbNull
                BoardSquare(Flipped(Count, 1), Flipped(Count, 2)) = Icon
                Count += 1
            End While
        End If

    End Sub

    Sub GetValidMoves(ByVal Icon As String)

        For i As Integer = 1 To 8
            For j As Integer = 1 To 8
                DupeBoard(i, j) = " "
            Next
        Next

        For i As Integer = 1 To 8
            For j As Integer = 1 To 8
                If IsValidMove(i, j, Icon) <> 0 Then
                    DupeBoard(i, j) = "*"
                End If
            Next
        Next

    End Sub

    Sub CopyDupeBoard()

        For i As Integer = 1 To 8
            For j As Integer = 1 To 8
                If DupeBoard(i, j) <> "*" Then
                    DupeBoard(i, j) = BoardSquare(i, j)
                End If
            Next
        Next

    End Sub


    Function IsValidMove(ByVal XCord As Integer, ByVal YCord As Integer, ByVal Icon As String) As Integer
        Dim EnemyIcon As String
        Dim Moves(16) As Integer
        Dim XStart, YStart, YCordTemp, XCordTemp As Integer
        Dim Count As Integer

        For rowcount As Integer = 1 To 25
            For colcount As Integer = 1 To 2
                Flipped(rowcount, colcount) = vbNull
            Next
        Next

        If BoardSquare(YCord, XCord) <> " " Or IsOnBoard(YCord, XCord) = False Then
            'Error Message Invalid Move
            'Return False
        End If

        XStart = XCord
        YStart = YCord

        BoardSquare(XCord, YCord) = Icon

        If Icon = "X" Then
            EnemyIcon = "O"
        Else
            EnemyIcon = "X"
        End If

        ' Moves(Odd) Increases X    Moves(Even) Increases Y
        Moves(1) = 1
        Moves(2) = 0
        Moves(3) = 1
        Moves(4) = 1
        Moves(5) = 0
        Moves(6) = 1
        Moves(7) = -1
        Moves(8) = 1
        Moves(9) = -1
        Moves(10) = 0
        Moves(11) = -1
        Moves(12) = -1
        Moves(13) = 0
        Moves(14) = -1
        Moves(15) = 1
        Moves(16) = -1
        Count = 1

        For i As Integer = 1 To 8

            XCordTemp = XCord + Moves((i * 2) - 1)
            YCordTemp = YCord + Moves(i * 2)

            While IsOnBoard(XCordTemp, YCordTemp) = True AndAlso BoardSquare(XCordTemp, YCordTemp) = EnemyIcon

                XCordTemp = XCordTemp + Moves(((i * 2) - 1))
                YCordTemp = YCordTemp + Moves((i * 2))

                If IsOnBoard(XCordTemp, YCordTemp) = False Then
                    Exit While
                End If

                If BoardSquare(XCordTemp, YCordTemp) = Icon Then

                    While True
                        XCordTemp -= Moves((i * 2) - 1)
                        YCordTemp -= Moves(i * 2)

                        If XCordTemp = XStart And YCordTemp = YStart Then
                            Exit While
                        End If

                        Flipped(Count, 1) = XCordTemp 'Assigns values for all flipped pieces
                        Flipped(Count, 2) = YCordTemp
                        Count += 1
                    End While

                End If

            End While

        Next

        BoardSquare(XCord, YCord) = " "

        If Count <> 1 Then
            Return Count
        Else
            Return 0
        End If

    End Function

    Sub GameScore()

        Dim XScore As Integer = 0
        Dim OScore As Integer = 0

        For i As Integer = 1 To 8
            For j As Integer = 1 To 8
                If BoardSquare(i, j) = "X" Then
                    XScore += 1
                ElseIf BoardSquare(i, j) = "O" Then
                    OScore += 1
                End If
            Next
        Next

        Console.WriteLine()
        Console.WriteLine("Computer : {0}    Player : {1}  ", XScore, OScore)

    End Sub

    Sub CheckWin(ByVal Icon As String)

        Call GetValidMoves(Icon)

        For i As Integer = 1 To 8
            For j As Integer = 1 To 8
                If DupeBoard(i, j) = "*" Then
                    GameOver = False
                End If
            Next
        Next

        GameOver = True

    End Sub

    Sub ShowBoard()
        Dim HLine As String = "  +--+--+--+--+--+--+--+--+ "

        Console.Write("   A  B  C  D  E  F  G  H")
        Console.WriteLine()
        If HintsEnabled = False Then
            For i As Integer = 1 To 8
                Console.WriteLine(HLine)
                For j As Integer = 1 To 1
                    Console.Write(i.ToString() + " ")
                    Console.Write("|" + BoardSquare(1, i) + " |" + BoardSquare(2, i) + " |" + BoardSquare(3, i) + " |" + _
                                  BoardSquare(4, i) + " |" + BoardSquare(5, i) + " |" + BoardSquare(6, i) + " |" + _
                                  BoardSquare(7, i) + " |" + BoardSquare(8, i) + " |")
                Next
                Console.WriteLine()
            Next

        ElseIf HintsEnabled = True Then
            For i As Integer = 1 To 8
                Console.WriteLine(HLine)
                For j As Integer = 1 To 1
                    Console.Write(i.ToString() + " ")
                    Console.Write("|" + DupeBoard(1, i) + " |" + DupeBoard(2, i) + " |" + DupeBoard(3, i) + " |" + _
                                  DupeBoard(4, i) + " |" + DupeBoard(5, i) + " |" + DupeBoard(6, i) + " |" + _
                                  DupeBoard(7, i) + " |" + DupeBoard(8, i) + " |")
                Next
                Console.WriteLine()
            Next

        End If
        Console.WriteLine(HLine)

        Call GameScore()
    End Sub

    Sub AssignArray()

        For i As Integer = 1 To 8
            For j As Integer = 1 To 8
                BoardSquare(i, j) = (" ")
            Next
        Next

        BoardSquare(5, 5) = "X"
        BoardSquare(4, 4) = "X"
        BoardSquare(4, 5) = "O"
        BoardSquare(5, 4) = "O"
    End Sub

    Function IsOnBoard(ByVal XCord As Integer, ByVal YCord As Integer) As Boolean

        If YCord > 8 Or YCord < 0 Or XCord > 8 Or XCord < 0 Then
            Return False
            IsOnBoard = False
        Else
            Return True
            IsOnBoard = True
        End If

    End Function

End Module