Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 8.52 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Minmax algorithm only winning when the move is in direct sight. Otherwise always allowing the player to win
  2. Public Structure Board
  3.     Public lbl As Label
  4.     Public owner As String
  5.     Public posX As Integer
  6.     Public posY As Integer
  7. End Structure
  8.  
  9. Public Structure LegalMove
  10.     Public posX As Integer
  11.     Public posY As Integer
  12. End Structure
  13.  
  14. Public Structure Best
  15.     Public Move As LegalMove
  16.     Public Score As Integer
  17. End Structure
  18.  
  19. Public Class Form1
  20.     Public Const PLAYER_PIECE As String = "X"
  21.     Public Const COMPUTER_PIECE As String = "O"
  22.     Public Const HUMAN_WIN As Integer = -1
  23.     Public Const COMPUTER_WIN As Integer = 1
  24.     Public Const TIE As Integer = 0
  25.     Public Const COMPUTER As Boolean = True
  26.     Public Const HUMAN As Boolean = False
  27.     Public Game_Ended As Boolean = False
  28.     Public Turn As String = "Human"
  29.     Public Board(2, 2) As Board
  30.     'Sets all objects up (mostly labels, and the board)
  31. Private Sub On_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  32.     Dim intindex As Integer
  33.     Dim intindex2 As Integer
  34.     For intindex = 0 To 2
  35.         For intindex2 = 0 To 2
  36.             Dim Label As New Label
  37.             Label.Name = "lbl" & intindex & intindex2
  38.             Label.AutoSize = False
  39.             Label.TextAlign = ContentAlignment.MiddleCenter
  40.             Label.Font = New Font("Arial", 48, FontStyle.Bold)
  41.             Label.Size = New System.Drawing.Size(100, 100)
  42.             Label.Location = New System.Drawing.Point(intindex * 100, intindex2 * 100)
  43.             Label.BorderStyle = Windows.Forms.BorderStyle.FixedSingle
  44.             Board(intindex, intindex2).lbl = Label
  45.             Board(intindex, intindex2).posX = intindex
  46.             Board(intindex, intindex2).posY = intindex2
  47.             Me.Controls.Add(Label)
  48.             AddHandler Board(intindex, intindex2).lbl.Click, AddressOf Player_Move
  49.         Next
  50.     Next
  51. End Sub
  52. 'If a player clicks on one of the labels, it will attmpt to put a player piece on that tile, and direct the game to the computer's turn.
  53. Sub Player_Move(ByVal sender As System.Object, ByVal e As System.EventArgs)
  54.     Dim Current_Board As Board = GetBoard(sender)
  55.     Dim Best As Best
  56.     If Current_Board.owner = Nothing Then
  57.         Board(Current_Board.posX, Current_Board.posY).owner = PLAYER_PIECE
  58.         Board(Current_Board.posX, Current_Board.posY).lbl.Text = PLAYER_PIECE
  59.         Call Check_Board(False, Nothing)
  60.         If Game_Ended = False Then
  61.             Turn = "Computer"
  62.             Best = Get_Computer_Move(COMPUTER)
  63.             Board(Best.Move.posX, Best.Move.posY).owner = COMPUTER_PIECE
  64.             Board(Best.Move.posX, Best.Move.posY).lbl.Text = COMPUTER_PIECE
  65.             Call Check_Board(False, Nothing)
  66.         End If
  67.         Game_Ended = False
  68.         Turn = "Human"
  69.     End If
  70. End Sub
  71.  
  72. 'Checks win/tie conditions. If it is a simulation (for ai), then it will return a number. If it is for legitimate checking, it will call the win function, or tie.
  73. Function Check_Board(ByVal simulation As Boolean, ByVal side As Boolean)
  74.     Dim intindex As Integer
  75.     Dim intindex2 As Integer
  76.     'Vertical Check
  77.     For intindex = 0 To 2
  78.         If Board(intindex, 0).owner = Board(intindex, 1).owner And Board(intindex, 1).owner = Board(intindex, 2).owner And Board(intindex, 0).owner <> Nothing Then
  79.             If simulation = False Then
  80.                 Win()
  81.             Else
  82.                 If Board(intindex, 0).owner = COMPUTER_PIECE Then
  83.                     Return 1
  84.                 Else
  85.                     Return -1
  86.                 End If
  87.             End If
  88.         End If
  89.     Next
  90.     'Horizantal Check
  91.     For intindex = 0 To 2
  92.         If Board(0, intindex).owner = Board(1, intindex).owner And Board(1, intindex).owner = Board(2, intindex).owner And Board(0, intindex).owner <> Nothing Then
  93.             If simulation = False Then
  94.                 Win()
  95.             Else
  96.                 If Board(0, intindex).owner = COMPUTER_PIECE Then
  97.                     Return 1
  98.                 Else
  99.                     Return -1
  100.                 End If
  101.             End If
  102.         End If
  103.     Next
  104.     'Diagonal Check
  105.     Dim intoppindex As Integer
  106.     Dim intoppindex2 As Integer
  107.     For intindex = 0 To 2 Step 2
  108.         For intindex2 = 0 To 2 Step 2
  109.             If intindex = 0 Then
  110.                 intoppindex = 2
  111.             Else
  112.                 intoppindex = 0
  113.             End If
  114.             If intindex2 = 0 Then
  115.                 intoppindex2 = 2
  116.             Else
  117.                 intoppindex2 = 0
  118.             End If
  119.             If Board(intindex, intindex2).owner = Board(1, 1).owner And Board(1, 1).owner = Board(intoppindex, intoppindex2).owner And Board(intindex, intindex2).owner <> Nothing Then
  120.                 If simulation = False Then
  121.                     Win()
  122.                 Else
  123.                     If Board(1, 1).owner = COMPUTER_PIECE Then
  124.                         Return 1
  125.                     Else
  126.                         Return -1
  127.                     End If
  128.                 End If
  129.             End If
  130.         Next
  131.     Next
  132.     'Full Board
  133.     Dim movedcount As Integer
  134.     For intindex = 0 To 2
  135.         For intindex2 = 0 To 2
  136.             If Board(intindex, intindex2).owner <> Nothing Then
  137.                 movedcount += 1
  138.             End If
  139.         Next
  140.     Next
  141.     If movedcount = 9 Then
  142.         If simulation = False Then
  143.             MessageBox.Show("It is a tie. Resetting the board.")
  144.             For intindex = 0 To 2
  145.                 For intindex2 = 0 To 2
  146.                     Board(intindex, intindex2).owner = Nothing
  147.                     Board(intindex, intindex2).lbl.Text = Nothing
  148.                 Next
  149.             Next
  150.             Game_Ended = True
  151.         Else
  152.             Return 0
  153.         End If
  154.     End If
  155.     Return Nothing
  156. End Function
  157.  
  158. 'Allows labels to be processed in to the board
  159. Public Function GetBoard(ByVal sender As Label)
  160.     Dim intindex As Integer
  161.     Dim intindex2 As Integer
  162.     For intindex = 0 To 2
  163.         For intindex2 = 0 To 2
  164.             If Board(intindex, intindex2).lbl.Name = sender.Name Then
  165.                 Return Board(intindex, intindex2)
  166.             End If
  167.         Next
  168.     Next
  169.     Return Nothing
  170. End Function
  171.  
  172. 'If a player wins, it will display a message box and reset the board
  173. Sub Win()
  174.     MessageBox.Show(Turn & " has won. Resetting the board.")
  175.     Dim intindex As Integer
  176.     Dim intindex2 As Integer
  177.     For intindex = 0 To 2
  178.         For intindex2 = 0 To 2
  179.             Board(intindex, intindex2).owner = Nothing
  180.             Board(intindex, intindex2).lbl.Text = Nothing
  181.         Next
  182.     Next
  183.     Game_Ended = True
  184. End Sub
  185.  
  186. 'Minmax algorithm that tries to get best possible move by accessing every possible scenario in the game tree. NOT WORKING. Returns a "best" object, that is then used to place the computer's piece.
  187. Public Function Get_Computer_Move(ByVal side As Boolean)
  188.     Dim mybest As New Best
  189.     Dim reply As New Best
  190.     Dim LegalMoveslst As List(Of LegalMove)
  191.     LegalMoveslst = Get_Legal_Moves(Board)
  192.  
  193.     'This allows to look at other's next move.
  194.     Dim oppside As Boolean
  195.     If side = COMPUTER Then
  196.         oppside = HUMAN
  197.     Else
  198.         oppside = COMPUTER
  199.     End If
  200.  
  201.     'At lowest end of a given branch (win, loss, or tie), the current score is returned.
  202.     mybest.Score = Check_Board(True, side)
  203.     If mybest.Score <> Nothing Then
  204.         Return mybest
  205.     End If
  206.  
  207.     'Base values so something is always there.
  208.     If side = COMPUTER Then
  209.         mybest.Score = -2
  210.     Else
  211.         mybest.Score = 2
  212.     End If
  213.  
  214.     For Each LegalMove In LegalMoveslst
  215.         If side = COMPUTER Then
  216.             Board(LegalMove.posX, LegalMove.posY).owner = COMPUTER_PIECE
  217.         Else
  218.             Board(LegalMove.posX, LegalMove.posY).owner = PLAYER_PIECE
  219.         End If
  220.         reply = Get_Computer_Move(oppside)
  221.         Board(LegalMove.posX, LegalMove.posY).owner = Nothing
  222.         If ((side = COMPUTER And reply.Score > mybest.Score) Or (side = HUMAN And reply.Score < mybest.Score)) Then
  223.             mybest.Move = LegalMove
  224.             mybest.Score = reply.Score
  225.         End If
  226.     Next
  227.     Return mybest
  228. End Function
  229.  
  230. 'Returns potential legal moves on the board
  231. Public Function Get_Legal_Moves(ByVal tempBoard(,) As Board)
  232.     Dim intindex As Integer
  233.     Dim intindex2 As Integer
  234.     Dim legalmoves As New List(Of LegalMove)
  235.     For intindex = 0 To 2
  236.         For intindex2 = 0 To 2
  237.             If tempBoard(intindex, intindex2).owner = Nothing Then
  238.                 Dim legalmove As New LegalMove
  239.                 legalmove.posX = intindex
  240.                 legalmove.posY = intindex2
  241.                 legalmoves.Add(legalmove)
  242.             End If
  243.         Next
  244.     Next
  245.     Return legalmoves
  246. End Function
  247. End Class