Index - Reversi - Five-a-Row
Reversi
Source:
Public Class Form1
Inherits System.Windows.Forms.Form
#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 PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
Friend WithEvents Button3 As System.Windows.Forms.Button
Private Sub InitializeComponent()
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.Label4 = New System.Windows.Forms.Label
Me.GroupBox1 = New System.Windows.Forms.GroupBox
Me.Button3 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'PictureBox1
'
Me.PictureBox1.Location = New System.Drawing.Point(152, 16)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(208, 192)
Me.PictureBox1.TabIndex = 0
Me.PictureBox1.TabStop = False
'
'Button1
'
Me.Button1.BackColor = System.Drawing.Color.SeaShell
Me.Button1.Location = New System.Drawing.Point(72, 184)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(48, 24)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Väliin"
'
'Button2
'
Me.Button2.BackColor = System.Drawing.Color.SeaShell
Me.Button2.Location = New System.Drawing.Point(16, 184)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(48, 24)
Me.Button2.TabIndex = 4
Me.Button2.Text = "Uusi"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(40, 40)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(56, 16)
Me.Label1.TabIndex = 5
Me.Label1.Text = "Kone: X"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(40, 88)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(48, 16)
Me.Label2.TabIndex = 6
Me.Label2.Text = "SINÄ: O"
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(40, 104)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(48, 16)
Me.Label3.TabIndex = 7
Me.Label3.Text = "2"
Me.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'Label4
'
Me.Label4.Location = New System.Drawing.Point(40, 56)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(48, 16)
Me.Label4.TabIndex = 8
Me.Label4.Text = "2"
Me.Label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'GroupBox1
'
Me.GroupBox1.Location = New System.Drawing.Point(16, 8)
Me.GroupBox1.Name = "GroupBox1"
Me.GroupBox1.Size = New System.Drawing.Size(104, 120)
Me.GroupBox1.TabIndex = 9
Me.GroupBox1.TabStop = False
Me.GroupBox1.Text = "Tilanne"
'
'Button3
'
Me.Button3.BackColor = System.Drawing.Color.SeaShell
Me.Button3.Location = New System.Drawing.Point(16, 144)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(104, 24)
Me.Button3.TabIndex = 10
Me.Button3.Text = "Ohje"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.Color.AntiqueWhite
Me.ClientSize = New System.Drawing.Size(392, 229)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.PictureBox1)
Me.Controls.Add(Me.GroupBox1)
Me.Name = "Form1"
Me.Text = "vb.net Reversi by TNB 2004"
Me.ResumeLayout(False)
End Sub
#End Region
' --------------------------
' RISTINOLLA-PELIN RUUDUKKO
' --------------------------
Const xmax = 8
Const ymax = 8
Private YK As Integer = 200
Private XK As Integer = 200
Private dx As Integer = XK / xmax
Private A(xmax, ymax) As Boolean ' ihmisen pelimerkit
Private B(xmax, ymax) As Boolean ' koneen pelimerkit
Private bmpKuva As New Bitmap(XK, YK)
Private grKuva As Graphics = Graphics.FromImage(bmpKuva)
Private koneX As Integer ' koneen viimeisin sijoitus X
Private koneY As Integer ' koneen viimeisin sijoitus Y
Private KoneOnYmpyrä As Boolean = False
Dim vuoro As Boolean
Dim aloittaja As Boolean
Private Reversi() As Integer = {11, 18, 81, 88, 83, 86, 61, 68, 31, 38, 13, 16, 63, 66, 33, 36, 64, _
65, 35, 34, 43, 56, 46, 53, 14, 15, 84, 85, _
41, 48, 51, 58, 23, 26, 73, 76, 32, 37, 62, 67, 24, 25, 74, 75, 42, _
47, 52, 57, 12, 17, 82, 87, 21, 28, 71, 78, 22, 77, 72, 27}
Private sx() As Integer = {-1, 0, 1, -1, 1, -1, 0, 1}
Private sy() As Integer = {-1, -1, -1, 0, 0, 1, 1, 1}
Private Sub PiirraTilanne()
Dim X As Integer
Dim Y As Integer
'Putsaa
grKuva.Clear(Color.White)
'Piirrä pelimerkit
Dim redPen As New Pen(Color.Red, 3)
Dim blackPen As New Pen(Color.Black, 3)
For Y = 0 To ymax - 1
For X = 0 To xmax - 1
If Y = koneY And X = koneX Then ' koneen viimeisin merkki punaisella
If B(X, Y) Then KoneenMerkki(redPen, X, Y)
Else
If B(X, Y) Then KoneenMerkki(blackPen, X, Y)
End If
If A(X, Y) Then IhmisenMerkki(blackPen, X, Y)
Next
Next
'Piirrä ruudukko
For X = 0 To xmax + 1
grKuva.DrawLine(New Pen(Color.LightBlue), X * dx, 0, X * dx, XK - 1)
grKuva.DrawLine(New Pen(Color.LightBlue), 0, X * dx, XK - 1, X * dx)
Next
'Laita Kuva näkyviin laatikkoon
PictureBox1.Image = CType(bmpKuva, Bitmap)
End Sub
Private Sub KoneenMerkki(ByVal kynä As Pen, ByVal X1 As Integer, ByVal Y1 As Integer)
If KoneOnYmpyrä Then
grKuva.DrawEllipse(kynä, X1 * dx + dx \ 4 + 1, Y1 * dx + (3 * dx) \ 4, dx \ 2 + 1, -1 * dx \ 2 - 1)
Else
grKuva.DrawLine(kynä, X1 * dx + dx \ 4, Y1 * dx + dx \ 4 + 1, X1 * dx + (3 * dx) \ 4, Y1 * dx + (3 * dx) \ 4 + 1)
grKuva.DrawLine(kynä, X1 * dx + dx \ 4, Y1 * dx + (3 * dx) \ 4, X1 * dx + (3 * dx) \ 4, Y1 * dx + dx \ 4)
End If
End Sub
Private Sub IhmisenMerkki(ByVal kynä As Pen, ByVal X1 As Integer, ByVal Y1 As Integer)
If Not KoneOnYmpyrä Then
grKuva.DrawEllipse(kynä, X1 * dx + dx \ 4 + 1, Y1 * dx + (3 * dx) \ 4, dx \ 2 + 1, -1 * dx \ 2 - 1)
Else
grKuva.DrawLine(kynä, X1 * dx + dx \ 4, Y1 * dx + dx \ 4 + 1, X1 * dx + (3 * dx) \ 4, Y1 * dx + (3 * dx) \ 4 + 1)
grKuva.DrawLine(kynä, X1 * dx + dx \ 4, Y1 * dx + (3 * dx) \ 4, X1 * dx + (3 * dx) \ 4, Y1 * dx + dx \ 4)
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PictureBox1.Height = YK
PictureBox1.Width = XK
'vähän pelitilannetta
A(3, 3) = True
A(4, 4) = True
B(4, 3) = True
B(3, 4) = True
koneX = 3
koneY = 4
aloittaja = True
vuoro = aloittaja
PiirraTilanne()
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
'ihminen teki siirtonsa
Dim xpos, ypos As Integer
If e.Button = MouseButtons.Left Then
xpos = e.X \ dx
ypos = e.Y \ dx
If vuoro Then
If JosSallittuIhminen(xpos, ypos) Then
A(xpos, ypos) = True
vuoro = Not vuoro
PiirraTilanne()
Viive(999)
KoneenSiirto()
PiirraTilanne()
Else : Beep()
End If
Else : Beep()
End If
End If
End Sub
Private Sub KoneenSiirto()
Dim i, xpos, ypos As Integer
For i = 0 To 59
xpos = Reversi(i) \ 10 - 1
ypos = Reversi(i) Mod 10 - 1
If JosSallittuKone(xpos, ypos) Then
B(xpos, ypos) = True
koneX = xpos
koneY = ypos
vuoro = Not vuoro
Exit For
End If
Next
If Not vuoro Then vuoro = True ' kone jättää väliin jos ei pysty
End Sub
Private Function JosSallittuKone(ByVal xpos, ByVal ypos) As Boolean
Dim i, k, kk As Integer
Dim x, y As Integer
Dim valimerkkiOK As Boolean
Dim suuntaOK As Boolean
Dim sallittu As Boolean = False
If B(xpos, ypos) Or A(xpos, ypos) Then
Return False
Exit Function
End If
For i = 0 To 7
valimerkkiOK = False
suuntaOK = False
For k = 1 To 7
x = k * sx(i) + xpos
y = k * sy(i) + ypos
If x < xmax And x > -1 And y < ymax And y > -1 Then
If (Not A(x, y)) And (Not B(x, y)) Then Exit For
If Not valimerkkiOK And B(x, y) Then Exit For
If Not valimerkkiOK Then valimerkkiOK = A(x, y)
If valimerkkiOK And Not suuntaOK Then suuntaOK = B(x, y)
If suuntaOK Then
B(xpos, ypos) = True
koneX = xpos : koneY = ypos
PiirraTilanne()
Application.DoEvents()
For kk = k - 1 To 1 Step -1
B(kk * sx(i) + xpos, kk * sy(i) + ypos) = True
A(kk * sx(i) + xpos, kk * sy(i) + ypos) = False
Viive(300)
PiirraTilanne()
Application.DoEvents()
Next
sallittu = True
LaskeTilanne()
Exit For
End If
End If
Next
Next
Return sallittu
End Function
Private Function JosSallittuIhminen(ByVal xpos, ByVal ypos) As Boolean
Dim i, k, kk As Integer
Dim x, y As Integer
Dim valimerkkiOK As Boolean
Dim suuntaOK As Boolean
Dim sallittu As Boolean = False
If B(xpos, ypos) Or A(xpos, ypos) Then
Return False
Exit Function
End If
For i = 0 To 7
valimerkkiOK = False
suuntaOK = False
For k = 1 To 7
x = k * sx(i) + xpos
y = k * sy(i) + ypos
If x < xmax And x > -1 And y < ymax And y > -1 Then
If (Not A(x, y)) And (Not B(x, y)) Then Exit For
If Not valimerkkiOK And A(x, y) Then Exit For
If Not valimerkkiOK Then valimerkkiOK = B(x, y)
If valimerkkiOK And Not suuntaOK Then suuntaOK = A(x, y)
If suuntaOK Then
A(xpos, ypos) = True
koneX = xpos : koneY = ypos
PiirraTilanne()
Application.DoEvents()
For kk = k - 1 To 1 Step -1
A(kk * sx(i) + xpos, kk * sy(i) + ypos) = True
B(kk * sx(i) + xpos, kk * sy(i) + ypos) = False
Viive(300)
PiirraTilanne()
Application.DoEvents()
Next
sallittu = True
LaskeTilanne()
Exit For
End If
End If
Next
Next
Return sallittu
End Function
Sub Viive(ByVal i As Single)
Dim t1 As Date
Dim t2 As Date
Dim ts As TimeSpan
t1 = Now
Do
Application.DoEvents()
t2 = Now
ts = t2.Subtract(t1)
Loop Until ts.TotalMilliseconds > i
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'uusi peli
Dim i, k As Integer
For i = 0 To xmax - 1
For k = 0 To ymax - 1
A(i, k) = False
B(i, k) = False
Next
Next
A(3, 3) = True
A(4, 4) = True
B(4, 3) = True
B(3, 4) = True
koneX = 3
koneY = 4
PiirraTilanne()
aloittaja = Not aloittaja ' aloittaja vaihtuu
vuoro = aloittaja
If Not vuoro Then ' konen vuoro aloittaa
Viive(999) 'max=999
KoneenSiirto()
PiirraTilanne()
LaskeTilanne()
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'jätän väliin
vuoro = Not vuoro
KoneenSiirto()
PiirraTilanne()
End Sub
Sub LaskeTilanne()
Dim i, k, laskA, laskB As Integer
For i = 0 To xmax - 1
For k = 0 To ymax - 1
laskA = laskA - A(i, k)
laskB = laskB - B(i, k)
Next
Next
Label3.Text = laskA
Label4.Text = laskB
Application.DoEvents()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim fr As New Ohje
fr.Show()
End Sub
End Class
Formiin Ohje:
Public Class Ohje
Inherits System.Windows.Forms.Form
#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
Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label1.Location = New System.Drawing.Point(8, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(280, 192)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Peliruudukko on 8 x 8 kokoinen. Pelaajat vuorotellen asettelevat pelinappuloitaan" & _
" ruudukkoon siten, että ne vastustajan nappulat, jotka jäävät kahden oman nappul" & _
"an väliin, käännetään omiksi nappuloiksi. Pelinappula täytyy asettaa ruudukkoon " & _
"siten, että pääsee kääntämään vähintään yhden vastustajan nappulan. Peli päättyy" & _
" kun ruudukko tulee täyteen tai toinen pelaaja menettää kaikki pelinappulansa . " & _
"Luonnollisesti se pelaaja, jolla tällöin on eniten nappuloita ruudukolla, voitta" & _
"a pelin"
'
'Ohje
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 213)
Me.Controls.Add(Me.Label1)
Me.Name = "Ohje"
Me.Text = "Ohje"
Me.ResumeLayout(False)
End Sub
#End Region
End Class