将picturebox数组转换为字符串或整数?

时间:2016-09-21 19:05:53

标签: arrays vb.net picturebox

Public Class frmMind
        'Game Mastermind
        Dim pbxBoard(3, 9) As PictureBox
        Dim strColors() As String = {"Red", "Orange", "Yellow", "Green", "Blue", "Violet"} 'Array for colors
        Dim intKey(4) As Integer 'Array for the mastermind's key
        Dim intCol As Integer = 0 'Subroutine for columns
        Dim intRow As Integer = 0 'Subroutine for rows
        Dim Red, Orange, Yellow, Green, Blue, Violet As Boolean
        Private Sub frmMind_Load(sender As Object, e As EventArgs) Handles MyBase.Load
                Dim Value As Boolean 'Sets a true and false thing to do stuff
'Gets a random number for a key that the use will not see

    Randomize()
    Dim intRand As Integer = ((Rnd() * 5))
    'Gets random numbers
    intKey(0) = intRand
    intKey(1) = intRand
    intKey(2) = intRand
    intKey(3) = intRand
    'Assigns a random number from 1-6 to each row

    Do Until Value = True 'Checks to make sure a number is not duplicated.
        If intKey(0) = intKey(1) Then
            intKey(1) = ((Rnd() * 5))
        ElseIf intKey(0) = intKey(2) Then
            intKey(2) = ((Rnd() * 5))
        ElseIf intKey(0) = intKey(3) Then
            intKey(3) = ((Rnd() * 5))
        ElseIf intKey(1) = intKey(2) Then
            intKey(2) = ((Rnd() * 5))
        ElseIf intKey(1) = intKey(3) Then
            intKey(3) = ((Rnd() * 5))
        ElseIf intKey(2) = intKey(3) Then
            intKey(3) = ((Rnd() * 5))
        Else
            'All if statements above gets a new number if one has already been used.

            Value = True
            MessageBox.Show(intKey(0) & " " & intKey(1) & " " & intKey(2) & " " & intKey(3))
        End If
    Loop
    'Sets the variables of strColors
    If intRand = 5 Then
        strColors(5) = "Violet"
    ElseIf intRand = 4 Then
        strColors(4) = "Blue"
    ElseIf intRand = 3 Then
        strColors(3) = "Green"
    ElseIf intRand = 2 Then
        strColors(2) = "Yellow"
    ElseIf intRand = 1 Then
        strColors(1) = "Orange"
    Else
        strColors(0) = "Red"
    End If

End Sub
Private Sub btnCheck_Click(sender As Object, e As EventArgs) Handles btnCheck.Click


    If  Then
        MessageBox.Show("You good.")
    Else
        MessageBox.Show(":(")
        End If

End Sub

'Creates border around peg on click. Red 
Private Sub pbxPegR_Click(sender As Object, e As EventArgs) Handles pbxPegR.Click
    Red = True
    If Red = True Then
        pbxPegR.BorderStyle = BorderStyle.FixedSingle
        pbxPegY.BorderStyle = BorderStyle.None
        pbxPegO.BorderStyle = BorderStyle.None
        pbxPegG.BorderStyle = BorderStyle.None
        pbxPegB.BorderStyle = BorderStyle.None
        pbxPegP.BorderStyle = BorderStyle.None
    End If
    Yellow = False
    Green = False
    Violet = False
    Blue = False
    Orange = False
End Sub

Private Sub pbxJ0_Click(sender As Object, e As EventArgs) Handles pbxJ0.Click
    If Red = True Then 'changes the box from an empty square to a square with a red dot/piece
        pbxJ0.Image = Image.FromFile("H:\VB.net\MasterMind\pictures\CboxR.png")
        pbxBoard(0, 9) = strColors(0)
    ElseIf Yellow = True Then
        pbxJ0.Image = Image.FromFile("H:\VB.net\MasterMind\pictures\CboxY.png")
        pbxBoard(0, 9) = strColors(2)
    ElseIf Orange = True Then
        pbxJ0.Image = Image.FromFile("H:\VB.net\MasterMind\pictures\CboxO.png")
        pbxBoard(0, 9) = strColors(1)
    ElseIf Blue = True Then
        pbxJ0.Image = Image.FromFile("H:\VB.net\MasterMind\pictures\CboxB.png")
        pbxBoard(0, 9) = strColors(4)
    ElseIf Green = True Then
        pbxJ0.Image = Image.FromFile("H:\VB.net\MasterMind\pictures\CboxG.png")
        pbxBoard(0, 9) = strColors(3)
    ElseIf Violet = True Then
        pbxJ0.Image = Image.FromFile("H:\VB.net\MasterMind\pictures\CboxP.png")
        pbxBoard(0, 9) = strColors(5)
    End If
End Sub

所以这段代码的重点是重新创建一个名为mastermind的游戏。用户在第一行输入四种不同的颜色并绑定以猜测正确的顺序和颜色作为键(mastermind)。我们有它,所以空白方块将被用户选择的颜色替换。我们无法弄清楚如何将新值/颜色与randomizer / key的值进行比较。或者如何检查没有多次使用颜色。请帮忙。

1 个答案:

答案 0 :(得分:0)

我建议看一下System.Drawing中定义的Color结构,它定义了很多已知的颜色。包括您在strColors中命名的那些。

如果您需要比较颜色相等的比较,它会变得更复杂,但首先要检查颜色的ARGB属性。

简化的控制台程序:

Imports System.Drawing

Module Module1
  Sub Main()
    Dim a As Color = Color.Red
    Dim b As Color = Color.Orange
    Dim c As Color = Color.Turquoise

    CheckColors(a, a)
    CheckColors(a, b)
    CheckColors(c, Color.FromName("Turquoise"))

    Console.ReadKey()
  End Sub

  Private Sub CheckColors(x As Color, y As Color)
    Console.WriteLine("Does " & x.Name & " equal " & y.Name & "? " & (x = y).ToString)
  End Sub
End Module

输出:

Does Red equal Red? True
Does Red equal Orange? False
Does Turquoise equal Turquoise? True