2017-09-08 4 views
-1

目的:ゲームを作成するには、16の異なるオプションからユーザーが選択し、16の一意の応答を受け取ります。これらの回答は、ユーザートレーニングのための簡単なセキュリティ上のヒントです。誰も見てない画面となり、ボタンの収量を予測し、それらを再利用することはできません/シングルゲーム中に応答を複製することができるように複数の変数のVisual Basic乱数

彼らはランダムな順序でなければなりません。

私は、配列または選択された大文字と小文字のどちらかを使用して前後しています。選択されたケースはより有望視されていますが、16個の異なるボタンをランダムに割り当てる方法がわかりません。

悪いコード要求されたとして、私が提供している

 'Create Select Case random value and assign to buttons 
    Do 
     btn2 = CInt(Int((3 * Rnd()) + 1)) 
     'lblDescript so I can see what btn2 is outputting 
     lblDescript.Text = btn2.ToString 
    Loop While btn2 <> btn1 Or btn3 
'*************************************************************************************** 
'Game Buttons 2 - 16 ************************************************************ 
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 
    'Button Style 
    Button2.ForeColor = Color.Transparent 
    Button2.BackColor = Color.Transparent 
    Button2.FlatStyle = FlatStyle.Flat 
    Button2.Text = "" 
    Button2.UseVisualStyleBackColor = False 
    'Button2.Enabled = False 

    'Select Case 
    Select Case btn2 
     Case Is = 1 
      PictureBox1.Image = My.Resources.Login 
      Button2.BackgroundImage = My.Resources.Login 
      lblPicName.Text = "Two - Factor Authentication" 
      lblDescript.Text = "Two - Factor Authentication, or 2FA adds an extra step to a basic log-in procedure." 
     Case Is = 2 
      'Content PaceHolder***** 
      PictureBox1.Image = My.Resources.Cloud 
      Button2.BackgroundImage = My.Resources.Cloud 
      lblPicName.Text = "Two - Factor Authentication" 
      lblDescript.Text = "Two - Factor Authentication, or 2FA adds an extra step to a basic log-in procedure." 
     Case Is = 3 
      'Content PaceHolder***** 
      PictureBox1.Image = My.Resources.Pwmanager 
      Button2.BackgroundImage = My.Resources.Pwmanager 
      lblPicName.Text = "Two - Factor Authentication" 
      lblDescript.Text = "Two - Factor Authentication, or 2FA adds an extra step to a basic log-in procedure." 
     Case Is = 4 
    End Select 

絵は私がアプリケーションの最初のボタンを選択したときに発生する必要があるかを示しています。

画像は説明する:ユーザーが写真をボタンとしてだけでなく、オフに名前と説明を持つ右に表示されるボタンを選択

enter image description here

たら。

'Button2.Enabled = False 

以下のコードで

+0

写真はリンクされていませんか? – cfonte

+0

これを解決するためにより有用な情報がありますか? – cfonte

+1

「間違っている」コードです。 –

答えて

0

は、

Dim rndNum As Random 
Dim randomChoice As Integer = rndNum.Next(1, 4) 

そしてもちろん、それぞれのコードを含む

Select Case randomChoice 
    Case 1 
    Case 2 
    Case 3 
    Case 4 

にごSelect Caseステートメントを追加します。

1

コードの代わりにデータに基づいたソリューションを追求するべきだと思います。あなたのコードは、データ構造にすべての可能性を保存し、それをシャッフルするだけでも、同様のまたは重複したロジックをハードワイヤリングするのが難しいようです。次に、コードを使用してシャッフルされた結果を表示します。

Public Class Form1 

    Class SecurityTip 
     Public PicName As String 
     Public Description As String 
     Public Image As Image 
    End Class 

    Private TipList As SecurityTip() 
    Private ButtonArray As Button() 

    Protected Overrides Sub OnLoad(e As EventArgs) 
     MyBase.OnLoad(e) 
     ButtonArray = New Button() { 
     Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8, 
     Button9, Button10, Button11, Button12, Button13, Button14, Button15, Button16} 
     For I As Integer = 0 To ButtonArray.Length - 1 
     AddHandler ButtonArray(I).Click, AddressOf Button_Click 
     Next 
     InitializeTips() 
     ShuffleTips() 
    End Sub 

    Public Sub InitializeTips() 
     TipList = New SecurityTip() { 
     New SecurityTip() With { 
      .Image = My.Resources.Login, 
      .PicName = "Two - Factor Authentication", 
      .Description = "Two - Factor Authentication, or 2FA adds an extra step to a basic log-in procedure."}, 
     New SecurityTip() With { 
      .Image = My.Resources.Cloud, 
      .PicName = "Cloud Authentication", 
      .Description = "Cloud authentication might also be referred to as federated identity or something."}, 
     New SecurityTip() With { 
      .Image = My.Resources.Pwmanager, 
      .PicName = "Password Manager", 
      .Description = "Password managers help users maintain separate passwords for different sites."} 
     } 
    End Sub 

    Public Sub ShuffleTips() 
     Dim R As New Random() 
     For i As Integer = 0 To TipList.Length - 1 
     Dim SwapIndex = R.Next(TipList.Length) 
     Dim Temp = TipList(SwapIndex) 
     TipList(SwapIndex) = TipList(i) 
     TipList(i) = Temp 
     Next 
    End Sub 

    Public Sub PresentTip(Index As Integer) 
     With TipList(Index) 
     PictureBox1.Image = .Image 
     lblPicName.Text = .PicName 
     lblDescript.Text = .Description 
     ButtonArray(Index).BackgroundImage = TipList(Index).Image 
     End With 
    End Sub 

    Private Sub Button_Click(sender As Object, e As EventArgs) 
     For I As Integer = 0 To ButtonArray.Length - 1 
     If ButtonArray(I) Is sender Then 
      PresentTip(I) 
      Exit Sub 
     End If 
     Next 
    End Sub 
End Class 
+0

、 ます。Public Sub PresentTip(整数としてインデックス) がどのように宣言するだろう "私は" TipList(I).Image – cfonte

+0

@cfonteのために私はTipList(I)はTipList(インデックス)となっているべきだと思います。ごめんなさい。 – BlueMonkMN

+0

あなたの助けていただきありがとうございます。アプリケーションの上部にある私のmenustripが重複 ファイル、編集として表示され、このから生まれた小さな問題は、出口は二回まで表示し、「開始」ボタンの上にオーバーレイされています。 – cfonte

関連する問題