Public Class Form1 Inherits System.Windows.Forms.Form Dim board As Array Dim TILESIZE As Int16 = 30 Public whiteImage As Bitmap Public blackimage As Bitmap 'Public cursorPB As PictureBox Public whosTurn As tile.tilestateenum Dim randomizer As Random = New Random() Dim blackIsAI As Boolean = False Dim whiteIsAI As Boolean = False Structure coords Public Sub New(ByVal xcoord As Int16, ByVal ycoord As Int16) x = xcoord y = ycoord End Sub Dim x As Int16 Dim y As Int16 End Structure #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox Friend WithEvents PictureBox2 As System.Windows.Forms.PictureBox Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem5 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem6 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem7 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem8 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem9 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem10 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem11 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem12 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem13 As System.Windows.Forms.MenuItem Private Sub InitializeComponent() Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1)) Me.Label1 = New System.Windows.Forms.Label() Me.PictureBox1 = New System.Windows.Forms.PictureBox() Me.PictureBox2 = New System.Windows.Forms.PictureBox() Me.Label2 = New System.Windows.Forms.Label() Me.Button1 = New System.Windows.Forms.Button() Me.MainMenu1 = New System.Windows.Forms.MainMenu() Me.MenuItem1 = New System.Windows.Forms.MenuItem() Me.MenuItem3 = New System.Windows.Forms.MenuItem() Me.MenuItem4 = New System.Windows.Forms.MenuItem() Me.MenuItem5 = New System.Windows.Forms.MenuItem() Me.MenuItem2 = New System.Windows.Forms.MenuItem() Me.MenuItem6 = New System.Windows.Forms.MenuItem() Me.MenuItem7 = New System.Windows.Forms.MenuItem() Me.MenuItem8 = New System.Windows.Forms.MenuItem() Me.MenuItem9 = New System.Windows.Forms.MenuItem() Me.MenuItem10 = New System.Windows.Forms.MenuItem() Me.MenuItem11 = New System.Windows.Forms.MenuItem() Me.MenuItem12 = New System.Windows.Forms.MenuItem() Me.MenuItem13 = New System.Windows.Forms.MenuItem() Me.SuspendLayout() ' 'Label1 ' Me.Label1.Location = New System.Drawing.Point(104, 472) Me.Label1.Name = "Label1" Me.Label1.TabIndex = 0 ' 'PictureBox1 ' Me.PictureBox1.Location = New System.Drawing.Point(32, 456) Me.PictureBox1.Name = "PictureBox1" Me.PictureBox1.Size = New System.Drawing.Size(48, 50) Me.PictureBox1.TabIndex = 1 Me.PictureBox1.TabStop = False ' 'PictureBox2 ' Me.PictureBox2.Location = New System.Drawing.Point(272, 456) Me.PictureBox2.Name = "PictureBox2" Me.PictureBox2.Size = New System.Drawing.Size(48, 50) Me.PictureBox2.TabIndex = 3 Me.PictureBox2.TabStop = False ' 'Label2 ' Me.Label2.Location = New System.Drawing.Point(344, 472) Me.Label2.Name = "Label2" Me.Label2.TabIndex = 2 ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(192, 504) Me.Button1.Name = "Button1" Me.Button1.TabIndex = 4 Me.Button1.Text = "Forfeit Turn" ' 'MainMenu1 ' Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1, Me.MenuItem7, Me.MenuItem2}) ' 'MenuItem1 ' Me.MenuItem1.Index = 0 Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem3, Me.MenuItem4, Me.MenuItem5}) Me.MenuItem1.Text = "File" ' 'MenuItem3 ' Me.MenuItem3.Index = 0 Me.MenuItem3.Text = "New Game" ' 'MenuItem4 ' Me.MenuItem4.Index = 1 Me.MenuItem4.Text = "-" ' 'MenuItem5 ' Me.MenuItem5.Index = 2 Me.MenuItem5.Text = "Quit" ' 'MenuItem2 ' Me.MenuItem2.Index = 2 Me.MenuItem2.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem6}) Me.MenuItem2.Text = "Help" ' 'MenuItem6 ' Me.MenuItem6.Index = 0 Me.MenuItem6.Text = "About" ' 'MenuItem7 ' Me.MenuItem7.Index = 1 Me.MenuItem7.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem8, Me.MenuItem11}) Me.MenuItem7.Text = "AI" ' 'MenuItem8 ' Me.MenuItem8.Index = 0 Me.MenuItem8.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem9, Me.MenuItem10}) Me.MenuItem8.Text = "White" ' 'MenuItem9 ' Me.MenuItem9.Checked = True Me.MenuItem9.Enabled = False Me.MenuItem9.Index = 0 Me.MenuItem9.Text = "Off" ' 'MenuItem10 ' Me.MenuItem10.Index = 1 Me.MenuItem10.Text = "On" ' 'MenuItem11 ' Me.MenuItem11.Index = 1 Me.MenuItem11.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem12, Me.MenuItem13}) Me.MenuItem11.Text = "Black" ' 'MenuItem12 ' Me.MenuItem12.Checked = True Me.MenuItem12.Enabled = False Me.MenuItem12.Index = 0 Me.MenuItem12.Text = "Off" ' 'MenuItem13 ' Me.MenuItem13.Index = 1 Me.MenuItem13.Text = "On" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(496, 534) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.PictureBox2, Me.Label2, Me.PictureBox1, Me.Label1}) Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon) Me.Menu = Me.MainMenu1 Me.Name = "Form1" Me.Text = "Reversi" Me.ResumeLayout(False) End Sub #End Region Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim streamPlus As IO.Stream = Me.GetType().Assembly.GetManifestResourceStream("Reversi.blackImage.bmp") blackimage = New Bitmap(streamPlus) streamPlus.Close() streamPlus = Me.GetType().Assembly.GetManifestResourceStream("Reversi.whiteImage.bmp") whiteImage = New Bitmap(streamPlus) streamPlus.Close() TILESIZE = blackimage.Height + 4 PictureBox1.Image = whiteImage PictureBox2.Image = blackimage Me.Height = 8 * TILESIZE + 10 + 25 + 140 Me.Width = 8 * TILESIZE + 10 'cursorPB = New PictureBox() 'cursorPB.Height = TILESIZE 'cursorPB.Width = TILESIZE 'Me.Controls.Add(cursorPB) 'cursorPB.Location = New Point(2000, 2000) initBoard() addPictBoxs() newGame() End Sub Public Sub checkAI() If (whosTurn = tile.tilestateenum.white And whiteIsAI) Or (whosTurn = tile.tilestateenum.black And blackIsAI) Then If False Then Threading.Thread.CurrentThread.Sleep(200) Dim count As Int16 = 0 While True If count > 200 Then checkForForfeit() Exit While End If If tileClicked(board(randomizer.Next(0, 7))(randomizer.Next(0, 7))) Then Exit Sub End If count = count + 1 End While Else '"Smart" AI '4 corners Dim myArray As ArrayList = New ArrayList() Dim i As Int16 Dim j As Int16 myArray.Add(New coords(0, 0)) myArray.Add(New coords(0, 7)) myArray.Add(New coords(7, 0)) myArray.Add(New coords(7, 7)) randomizeArray(myArray) For i = 0 To myArray.Count - 1 If tileClicked(board(myArray(i).x)(myArray(i).y)) Then Exit Sub End If Next 'edges myArray = New ArrayList() myArray.Add(New coords(2, 0)) myArray.Add(New coords(3, 0)) myArray.Add(New coords(4, 0)) myArray.Add(New coords(5, 0)) myArray.Add(New coords(7, 2)) myArray.Add(New coords(7, 3)) myArray.Add(New coords(7, 4)) myArray.Add(New coords(7, 5)) myArray.Add(New coords(2, 7)) myArray.Add(New coords(3, 7)) myArray.Add(New coords(4, 7)) myArray.Add(New coords(5, 7)) myArray.Add(New coords(0, 2)) myArray.Add(New coords(0, 3)) myArray.Add(New coords(0, 4)) myArray.Add(New coords(0, 5)) randomizeArray(myArray) For i = 0 To myArray.Count - 1 If tileClicked(board(myArray(i).x)(myArray(i).y)) Then Exit Sub End If Next 'center block myArray = New ArrayList() For i = 2 To 5 For j = 2 To 5 myArray.Add(New coords(i, j)) Next Next randomizeArray(myArray) For i = 0 To myArray.Count - 1 If tileClicked(board(myArray(i).x)(myArray(i).y)) Then Exit Sub End If Next '1 in from edge, not corners myArray = New ArrayList() myArray.Add(New coords(2, 1)) myArray.Add(New coords(3, 1)) myArray.Add(New coords(4, 1)) myArray.Add(New coords(5, 1)) myArray.Add(New coords(6, 2)) myArray.Add(New coords(6, 3)) myArray.Add(New coords(6, 4)) myArray.Add(New coords(6, 5)) myArray.Add(New coords(2, 6)) myArray.Add(New coords(3, 6)) myArray.Add(New coords(4, 6)) myArray.Add(New coords(5, 6)) myArray.Add(New coords(1, 2)) myArray.Add(New coords(1, 3)) myArray.Add(New coords(1, 4)) myArray.Add(New coords(1, 5)) randomizeArray(myArray) For i = 0 To myArray.Count - 1 If tileClicked(board(myArray(i).x)(myArray(i).y)) Then Exit Sub End If Next 'Last Hope, those surrounding corners myArray = New ArrayList() myArray.Add(New coords(0, 1)) myArray.Add(New coords(1, 1)) myArray.Add(New coords(1, 0)) myArray.Add(New coords(6, 0)) myArray.Add(New coords(6, 1)) myArray.Add(New coords(7, 1)) myArray.Add(New coords(7, 6)) myArray.Add(New coords(6, 6)) myArray.Add(New coords(6, 7)) myArray.Add(New coords(1, 7)) myArray.Add(New coords(1, 6)) myArray.Add(New coords(0, 6)) randomizeArray(myArray) For i = 0 To myArray.Count - 1 If tileClicked(board(myArray(i).x)(myArray(i).y)) Then Exit Sub End If Next 'AI Cant Go If whosTurn = tile.tilestateenum.black Then whosTurn = tile.tilestateenum.white checkAI() Else whosTurn = tile.tilestateenum.black checkAI() End If End If End If End Sub Public Sub randomizeArray(ByRef theArray As ArrayList) Dim i As Int16 For i = 1 To theArray.Count Dim index1 As Int16 = randomizer.Next(0, theArray.Count - 1) Dim index2 As Int16 = randomizer.Next(0, theArray.Count - 1) Dim temp As coords temp = theArray(index1) theArray(index1) = theArray(index2) theArray(index2) = temp Next End Sub Public Sub newGame() whosTurn = tile.tilestateenum.black Dim i As Int16 Dim j As Int16 For i = 0 To 7 For j = 0 To 7 board(i)(j).tilestate = tile.tilestateenum.none Next Next board(3)(4).tilestate = tile.tilestateenum.white board(4)(3).tilestate = tile.tilestateenum.white board(4)(4).tilestate = tile.tilestateenum.black board(3)(3).tilestate = tile.tilestateenum.black setScore() 'cursorPB.Image = New Bitmap(blackimage) End Sub Public Function tileClicked(ByVal sender As tile) If sender.tilestate <> tile.tilestateenum.none Then Return False End If If checkClick(sender) Then sender.tilestate = whosTurn If whosTurn = tile.tilestateenum.black Then whosTurn = tile.tilestateenum.white 'cursorPB.Image = New Bitmap(whiteImage) Else whosTurn = tile.tilestateenum.black 'cursorPB.Image = New Bitmap(blackimage) End If If setScore() Then newGame() Else Application.DoEvents() checkAI() End If Return True Else 'Beep() Return False End If End Function Public Function setScore() Dim whites As Int16 = 0 Dim blacks As Int16 = 0 Dim i As Int16 = 0 Dim j As Int16 = 0 For i = 0 To 7 For j = 0 To 7 If board(i)(j).tilestate = tile.tilestateenum.black Then blacks = blacks + 1 End If If board(i)(j).tilestate = tile.tilestateenum.white Then whites = whites + 1 End If Next Next Label1.Text = whites Label2.Text = blacks Label1.Refresh() Label2.Refresh() If whites + blacks = 64 Then If whites = blacks Then MsgBox("Tie!!!") ElseIf whites > blacks Then MsgBox("White Wins " & whites & " to " & blacks) Else MsgBox("Black Wins " & blacks & " to " & whites) End If Return True End If Return False End Function Enum dir ul = 0 u = 1 ur = 2 r = 3 dr = 4 d = 5 dl = 6 l = 7 End Enum Public Function checkClick(ByVal daTile As tile) If whosTurn = tile.tilestateenum.black Then daTile.Image = blackimage Else daTile.Image = whiteImage End If daTile.Refresh() Threading.Thread.CurrentThread.Sleep(200) Dim xloc As Int16 = daTile.getXLoc Dim yloc As Int16 = daTile.getYLoc Dim foundOne As Boolean = False Dim temp As Int16 Dim numFound As Int16 = 0 If xloc <> 0 And yloc <> 0 Then temp = checkDir(xloc, yloc, dir.ul) If temp > 0 Then numFound = temp + numFound foundOne = True End If End If If yloc <> 0 Then temp = checkDir(xloc, yloc, dir.u) If temp > 0 Then numFound = temp + numFound foundOne = True End If End If If yloc <> 0 And xloc <> 7 Then temp = checkDir(xloc, yloc, dir.ur) If temp > 0 Then numFound = temp + numFound foundOne = True End If End If If xloc <> 7 Then temp = checkDir(xloc, yloc, dir.r) If temp > 0 Then numFound = temp + numFound foundOne = True End If End If If xloc <> 7 And yloc <> 7 Then temp = checkDir(xloc, yloc, dir.dr) If temp > 0 Then numFound = temp + numFound foundOne = True End If End If If yloc <> 7 Then temp = checkDir(xloc, yloc, dir.d) If temp > 0 Then numFound = temp + numFound foundOne = True End If End If If yloc <> 7 And xloc <> 0 Then temp = checkDir(xloc, yloc, dir.dl) If temp > 0 Then numFound = temp + numFound foundOne = True End If End If If xloc <> 0 Then temp = checkDir(xloc, yloc, dir.l) If temp > 0 Then numFound = temp + numFound foundOne = True End If End If If numFound = 0 And foundOne Then MsgBox("Error 0 found") End If daTile.setImage() Return foundOne 'foundOne End Function Public Function checkDir(ByVal xloc As Int16, ByVal yloc As Int16, ByVal direction As dir) Dim xstep As Int16 Dim ystep As Int16 Dim numFound As Int16 = 0 If direction = dir.ul Or direction = dir.l Or direction = dir.dl Then xstep = -1 ElseIf direction = dir.u Or direction = dir.d Then xstep = 0 Else xstep = 1 End If If direction = dir.ul Or direction = dir.u Or direction = dir.ur Then ystep = -1 ElseIf direction = dir.l Or direction = dir.r Then ystep = 0 Else ystep = 1 End If Dim theArray As ArrayList = New ArrayList() Dim currx As Int16 = xloc Dim curry As Int16 = yloc While currx > -1 And currx < 8 And curry > -1 And curry < 8 theArray.Add(board(currx)(curry)) currx = currx + xstep curry = curry + ystep End While Dim opposite As tile.tilestateenum If whosTurn = tile.tilestateenum.black Then opposite = tile.tilestateenum.white Else opposite = tile.tilestateenum.black End If If theArray(1).tilestate <> opposite Then Return 0 End If Dim count As Int16 = 2 While count < theArray.Count If theArray(count).tilestate = opposite Then count = count + 1 Else Exit While End If End While If count = theArray.Count Then Return 0 End If Try If theArray(count).tilestate <> whosTurn Then Return 0 End If Catch e As Exception MsgBox(e.Message) End Try Me.Refresh() 'Success!!! board(xloc)(yloc).tilestate = whosTurn 'Application.DoEvents() 'Threading.Thread.CurrentThread.Sleep(300) Dim i As Int16 For i = 1 To count - 1 theArray(i).tilestate = whosTurn numFound = numFound + 1 'Application.DoEvents() Threading.Thread.CurrentThread.Sleep(50) Next Return numFound End Function Public Sub initBoard() Dim i As Int16 Dim mainArray As ArrayList = New ArrayList(8) For i = 0 To 7 Dim hori As ArrayList = New ArrayList(8) Dim j As Int16 For j = 0 To 7 Dim a As tile = New tile(i, j, TILESIZE, Me) hori.Add(a) Next mainArray.Add(hori.ToArray) Next board = mainArray.ToArray End Sub ' Public Sub mouseMoved(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseMove ' mouseMoved() ' End Sub 'Public Sub mouseMoved() 'cursorPB.Location = New Point(Me.Cursor.Position.X - Me.Location.X - 40, Me.Cursor.Position.Y - Me.Location.Y - 40) 'End Sub Public Sub addPictBoxs() Dim x As Int16 Dim y As Int16 For x = 0 To 7 For y = 0 To 7 Me.Controls.Add(board(x)(y)) Next Next End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If MsgBox("Are you sure you cannot find a turn? if one exists it will be picked for you", MsgBoxStyle.OKCancel) = MsgBoxResult.OK Then If checkForForfeit() Then MsgBox("We Found one for you") End If End If End Sub Public Function checkForForfeit() Dim i As Int16 Dim j As Int16 Dim foundOne As Boolean = False For i = 0 To 7 For j = 0 To 7 If board(i)(j).tileState = tile.tilestateenum.none And Not foundOne Then If tileClicked(board(i)(j)) Then foundOne = True End If End If Next Next If Not foundOne Then If whosTurn = tile.tilestateenum.black Then whosTurn = tile.tilestateenum.white checkAI() Else whosTurn = tile.tilestateenum.black checkAI() End If End If Return foundOne End Function Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click newGame() End Sub Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem5.Click Dispose() End Sub Private Sub MenuItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem6.Click MsgBox("Reversi created by:" & vbNewLine & "Mike Binns" & vbNewLine & "Send Comments to mikebinns@mail.com", MsgBoxStyle.Information, "About") End Sub Private Sub MenuItem9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem9.Click whiteIsAI = False MenuItem9.Checked = True MenuItem9.Enabled = False MenuItem10.Checked = False MenuItem10.Enabled = True checkAI() End Sub Private Sub MenuItem10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem10.Click whiteIsAI = True MenuItem9.Checked = False MenuItem9.Enabled = True MenuItem10.Checked = True MenuItem10.Enabled = False checkAI() End Sub Private Sub MenuItem12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem12.Click blackIsAI = False MenuItem12.Checked = True MenuItem12.Enabled = False MenuItem13.Checked = False MenuItem13.Enabled = True checkAI() End Sub Private Sub MenuItem13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem13.Click blackIsAI = True MenuItem12.Checked = False MenuItem12.Enabled = True MenuItem13.Checked = True MenuItem13.Enabled = False checkAI() End Sub End Class