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 GameOver As Boolean = False, Hints As Boolean = False Sub Main() Dim Quit As String Dim HintChoice As String Do Console.Write("Do You Want To Enable Hints (Y/N) : ") : HintChoice = Console.ReadLine.ToLower() If HintChoice = "y" Then Hints = True Console.Clear() Call StartOfGame() Console.Write("Do You Wish To Continue (Y/N) : ") : Quit = Console.ReadLine() Loop Until Quit.ToLower() = "n" End Sub Sub StartOfGame() Randomize() Call AssignArray() Dim RndStarter As Integer = CInt(Rnd() * 1) Dim NextMove As Integer Dim Winner As String Console.WriteLine("Player Icon : X Computer Icon : O ") Call ShowBoard() If RndStarter = 0 Then Call GetAIMove() NextMove = 1 Else Call GetPlayerMove() NextMove = 2 End If Call ShowBoard() Do If NextMove = 1 Then Call GetPlayerMove() NextMove = 2 Call CheckWin(AIIcon) If Hints = True Then Call ShowHints() Call ShowBoard() ElseIf NextMove = 2 Then Call GetAIMove() NextMove = 1 Call CheckWin(PlayerIcon) If Hints = True Then Call ShowHints() Call ShowBoard() End If Loop Until GameOver = True If NextMove = 2 Then Winner = "You" Else Winner = "Computer" Console.WriteLine("{0} Won The Game. Congratulations.", Winner) End Sub Sub GetPlayerMove() Dim XMove, YMove As Integer Dim Input As String Dim MoveMade As Boolean = False Do Console.Write("Enter Horizontal Position (A-H) : ") : Input = Console.ReadLine XMove = Asc(Input) - 96 Do Console.Write("Enter Vertical Position (1-8) : ") : Input = Console.ReadLine() YMove = CInt(Input) Loop Until IsNumeric(Input) = True AndAlso CInt(Input) <= 8 AndAlso CInt(Input) >= 1 If ValidMoveCheck(XMove, YMove, PlayerIcon) <> 0 AndAlso IsOnBoard(XMove, YMove) = True Then Call MakeMove(XMove, YMove, PlayerIcon) MoveMade = True End If Loop Until MoveMade = True End Sub Sub GetAIMove() Randomize() Dim BestX, BestY, BestScore, ScoreHolder As Integer Dim StrChar As String 'Determines the best scoring move for the computer ' Call GetAllValidMoves(AIIcon) For i As Integer = 1 To 8 For j As Integer = 1 To 8 If DupeBoard(i, j) = "'" Then ScoreHolder = ValidMoveCheck(i, j, AIIcon) If ScoreHolder > BestScore Then BestScore = ScoreHolder BestX = i BestY = j End If If ScoreHolder = BestScore AndAlso IsCornerSquare(i, j) = True Then BestX = i BestY = j End If End If Next Next Call MakeMove(BestX, BestY, AIIcon) StrChar = Chr(BestX + 96) Console.WriteLine("AI Choose Square X: {0} Y: {1}", StrChar, BestY.ToString()) Console.WriteLine() End Sub Function ValidMoveCheck(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 Return 0 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 AndAlso 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 Function IsOnBoard(ByVal XCord As Integer, ByVal YCord As Integer) As Boolean If YCord > 8 Or YCord < 1 Or XCord > 8 Or XCord < 1 Then Return False Else Return True End If End Function Function IsCornerSquare(ByVal XCord As Integer, ByVal YCord As Integer) As Boolean If (XCord = 8 AndAlso YCord = 8) Or (XCord = 1 AndAlso YCord = 8) Or (XCord = 1 AndAlso YCord = 1) Or _ (XCord = 8 AndAlso YCord = 1) Then Return True Else Return False End If End Function Sub MakeMove(ByVal XCord As Integer, ByVal YCord As Integer, ByVal Icon As String) Dim Count As Integer = 1 If ValidMoveCheck(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 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 Sub ShowBoard() Dim HLine As String = " +--+--+--+--+--+--+--+--+ " Dim XScore As Integer = 0 Dim OScore As Integer = 0 Console.WriteLine() Console.WriteLine(" A B C D E F G H") 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 Console.WriteLine(HLine) For i As Integer = 1 To 8 For j As Integer = 1 To 8 If BoardSquare(i, j) = "X" Then XScore += 1 If BoardSquare(i, j) = "O" Then OScore += 1 Next Next Console.WriteLine() Console.WriteLine(" Computer : {0} Player : {1} ", OScore, XScore) Console.WriteLine() End Sub Sub GetAllValidMoves(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 ValidMoveCheck(i, j, Icon) <> 0 Then DupeBoard(i, j) = "'" End If Next Next End Sub Sub ShowHints() Call GetAllValidMoves(PlayerIcon) For i As Integer = 1 To 8 For j As Integer = 1 To 8 If DupeBoard(i, j) = "'" AndAlso BoardSquare(i, j) = " " Then BoardSquare(i, j) = DupeBoard(i, j) Next Next End Sub Sub CheckWin(ByVal Icon As String) Call GetAllValidMoves(Icon) For i As Integer = 1 To 8 For j As Integer = 1 To 8 If DupeBoard(i, j) = "'" Then GameOver = False Exit Sub End If Next Next GameOver = True End Sub End Module