Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class Form1
- ' Date made: 1 July 2012
- ' The aim of this program is to determine which method is faster in determining whether a given tic-tac-toe board contains a
- ' win: representing the board as two boolean arrays, one for each player, and checking for wins by simply checking each of the
- ' rows/columns, or representing the board as two integers, one for each player, with each position on the board being a prime
- ' number, and checking for wins by testing for divisibility of the specified number. A number of boards will be generated,
- ' and both methods will test the same boards.
- ' Results: Prime method is far more efficient.
- Dim numberOfGenerations As Integer = 100000
- Dim boardToPrime As New Dictionary(Of Integer, Integer)
- Dim currentZeroToEightList As New List(Of Integer)
- ' The boards to be generated, saved and tested.
- Dim playerOneArrays As New List(Of Array)
- Dim playerTwoArrays As New List(Of Array)
- Dim playerOneNumbers As New List(Of Integer)
- Dim playerTwoNumbers As New List(Of Integer)
- Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
- Randomize()
- ' Make the "board number to prime number" dictionary.
- boardToPrime(1) = 2
- boardToPrime(2) = 3
- boardToPrime(3) = 5
- boardToPrime(4) = 7
- boardToPrime(5) = 11
- boardToPrime(6) = 13
- boardToPrime(7) = 17
- boardToPrime(8) = 19
- boardToPrime(9) = 23
- End Sub
- Private Sub btnGenerateBoards_Click(sender As System.Object, e As System.EventArgs) Handles btnGenerateBoards.Click
- ' Two different types of boards have to be generated, one type for each method.
- ' Reset the overall lists.
- playerOneArrays.Clear()
- playerTwoArrays.Clear()
- playerOneNumbers.Clear()
- playerTwoNumbers.Clear()
- For i = 1 To numberOfGenerations
- ' Declare arrays and player numbers.
- Dim playerOneArray(8) As Boolean
- Dim playerTwoArray(8) As Boolean
- Dim playerOneNumber As Integer = 1
- Dim playerTwoNumber As Integer = 1
- Dim numberOfCrosses, numberOfNoughts As Integer
- ' Reset boolean arrays.
- For j = 0 To 8
- playerOneArray(j) = False
- playerTwoArray(j) = False
- Next
- ' Randomise the number of noughts and crosses (whichever is randomised first has a higher chance of being higher).
- If Rnd() > 0.5 Then
- numberOfCrosses = Math.Floor(Rnd() * 9)
- numberOfNoughts = Math.Floor(Rnd() * (9 - numberOfCrosses))
- Else
- numberOfNoughts = Math.Floor(Rnd() * 9)
- numberOfCrosses = Math.Floor(Rnd() * (9 - numberOfNoughts))
- End If
- ' Reset the currentZeroToEightList.
- currentZeroToEightList.Clear()
- For j = 0 To 8
- currentZeroToEightList.Add(j)
- Next
- ' Randomly place the pieces in the boolean arrays, using a list to avoid picking the same number twice.
- For j = 1 To numberOfCrosses
- Dim randomNumber As Integer = currentZeroToEightList(Math.Floor(Rnd() * currentZeroToEightList.Count))
- currentZeroToEightList.Remove(randomNumber)
- playerOneArray(randomNumber) = True
- Next j
- For j = 1 To numberOfNoughts
- Dim randomNumber As Integer = currentZeroToEightList(Math.Floor(Rnd() * currentZeroToEightList.Count))
- currentZeroToEightList.Remove(randomNumber)
- playerTwoArray(randomNumber) = True
- Next j
- ' Transfer the pieces in the boolean arrays to the numbers.
- For j = 0 To 8
- If playerOneArray(j) = True Then
- playerOneNumber *= boardToPrime(j + 1)
- End If
- If playerTwoArray(j) = True Then
- playerTwoNumber *= boardToPrime(j + 1)
- End If
- Next j
- ' Add these board representations to the overall lists.
- playerOneArrays.Add(playerOneArray)
- playerTwoArrays.Add(playerTwoArray)
- playerOneNumbers.Add(playerOneNumber)
- playerTwoNumbers.Add(playerTwoNumber)
- Next i
- MessageBox.Show("Done.")
- End Sub
- Private Sub btnRunPrimeMethod_Click(sender As System.Object, e As System.EventArgs) Handles btnRunPrimeMethod.Click
- ' Test all the boards for wins using the prime method, and time the process.
- Dim numberOfPlayerOneWins As Integer = 0
- Dim numberOfPlayerTwoWins As Integer = 0
- Dim currentNumber As Integer
- Dim startTime As Date = Now
- For i = 0 To numberOfGenerations - 1
- currentNumber = playerOneNumbers(i)
- ' Check for player one wins.
- ' Rows.
- If currentNumber Mod 30 = 0 Then
- numberOfPlayerOneWins += 1
- Continue For
- End If
- If currentNumber Mod 1001 = 0 Then
- numberOfPlayerOneWins += 1
- Continue For
- End If
- If currentNumber Mod 7429 = 0 Then
- numberOfPlayerOneWins += 1
- Continue For
- End If
- ' Columns.
- If currentNumber Mod 238 = 0 Then
- numberOfPlayerOneWins += 1
- Continue For
- End If
- If currentNumber Mod 627 = 0 Then
- numberOfPlayerOneWins += 1
- Continue For
- End If
- If currentNumber Mod 1495 = 0 Then
- numberOfPlayerOneWins += 1
- Continue For
- End If
- ' Diagonals.
- If currentNumber Mod 506 = 0 Then
- numberOfPlayerOneWins += 1
- Continue For
- End If
- If currentNumber Mod 935 = 0 Then
- numberOfPlayerOneWins += 1
- Continue For
- End If
- currentNumber = playerTwoNumbers(i)
- ' Check for player two wins.
- ' Rows.
- If currentNumber Mod 30 = 0 Then
- numberOfPlayerTwoWins += 1
- Continue For
- End If
- If currentNumber Mod 1001 = 0 Then
- numberOfPlayerTwoWins += 1
- Continue For
- End If
- If currentNumber Mod 7429 = 0 Then
- numberOfPlayerTwoWins += 1
- Continue For
- End If
- ' Columns.
- If currentNumber Mod 238 = 0 Then
- numberOfPlayerTwoWins += 1
- Continue For
- End If
- If currentNumber Mod 627 = 0 Then
- numberOfPlayerTwoWins += 1
- Continue For
- End If
- If currentNumber Mod 1495 = 0 Then
- numberOfPlayerTwoWins += 1
- Continue For
- End If
- ' Diagonals.
- If currentNumber Mod 506 = 0 Then
- numberOfPlayerTwoWins += 1
- Continue For
- End If
- If currentNumber Mod 935 = 0 Then
- numberOfPlayerTwoWins += 1
- Continue For
- End If
- Next i
- Dim runLength As TimeSpan = Now.Subtract(startTime)
- Dim milliseconds As Integer = runLength.TotalMilliseconds
- MessageBox.Show("Done." & vbNewLine & vbNewLine & "Player one wins: " & numberOfPlayerOneWins & vbNewLine & "Player two wins: " & numberOfPlayerTwoWins & vbNewLine & vbNewLine & "Time taken(ms): " & milliseconds)
- End Sub
- Private Sub btnRunBooleanMethod_Click(sender As System.Object, e As System.EventArgs) Handles btnRunBooleanMethod.Click
- ' Test all the boards for wins using the boolean method, and time the process.
- Dim numberOfPlayerOneWins As Integer = 0
- Dim numberOfPlayerTwoWins As Integer = 0
- Dim currentArray As Array
- Dim startTime As Date = Now
- For i = 0 To numberOfGenerations - 1
- currentArray = playerOneArrays(i)
- ' Check for player one wins.
- ' Rows.
- If currentArray(0) And currentArray(1) And currentArray(2) Then
- numberOfPlayerOneWins += 1
- Continue For
- End If
- If currentArray(3) And currentArray(4) And currentArray(5) Then
- numberOfPlayerOneWins += 1
- Continue For
- End If
- If currentArray(6) And currentArray(7) And currentArray(8) Then
- numberOfPlayerOneWins += 1
- Continue For
- End If
- ' Columns.
- If currentArray(0) And currentArray(3) And currentArray(6) Then
- numberOfPlayerOneWins += 1
- Continue For
- End If
- If currentArray(1) And currentArray(4) And currentArray(7) Then
- numberOfPlayerOneWins += 1
- Continue For
- End If
- If currentArray(2) And currentArray(5) And currentArray(8) Then
- numberOfPlayerOneWins += 1
- Continue For
- End If
- ' Diagonals.
- If currentArray(0) And currentArray(4) And currentArray(8) Then
- numberOfPlayerOneWins += 1
- Continue For
- End If
- If currentArray(2) And currentArray(4) And currentArray(6) Then
- numberOfPlayerOneWins += 1
- Continue For
- End If
- currentArray = playerTwoArrays(i)
- ' Check for player two wins.
- ' Rows.
- If currentArray(0) And currentArray(1) And currentArray(2) Then
- numberOfPlayerTwoWins += 1
- Continue For
- End If
- If currentArray(3) And currentArray(4) And currentArray(5) Then
- numberOfPlayerTwoWins += 1
- Continue For
- End If
- If currentArray(6) And currentArray(7) And currentArray(8) Then
- numberOfPlayerTwoWins += 1
- Continue For
- End If
- ' Columns.
- If currentArray(0) And currentArray(3) And currentArray(6) Then
- numberOfPlayerTwoWins += 1
- Continue For
- End If
- If currentArray(1) And currentArray(4) And currentArray(7) Then
- numberOfPlayerTwoWins += 1
- Continue For
- End If
- If currentArray(2) And currentArray(5) And currentArray(8) Then
- numberOfPlayerTwoWins += 1
- Continue For
- End If
- ' Diagonals.
- If currentArray(0) And currentArray(4) And currentArray(8) Then
- numberOfPlayerTwoWins += 1
- Continue For
- End If
- If currentArray(2) And currentArray(4) And currentArray(6) Then
- numberOfPlayerTwoWins += 1
- Continue For
- End If
- Next i
- Dim runLength As TimeSpan = Now.Subtract(startTime)
- Dim milliseconds As Integer = runLength.TotalMilliseconds
- MessageBox.Show("Done." & vbNewLine & vbNewLine & "Player one wins: " & numberOfPlayerOneWins & vbNewLine & "Player two wins: " & numberOfPlayerTwoWins & vbNewLine & vbNewLine & "Time taken(ms): " & milliseconds)
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement