XAMLの全コード
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:self="clr-namespace:WpfMatchingGame"
Title="MainWindow" Height="550" Width="550">
<Grid Background="CornflowerBlue" Name="Grid1">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<WindowsFormsHost Visibility="Hidden">
<self:UserControl1 x:Name="myControl"/>
</WindowsFormsHost>
<Label Content="c" HorizontalAlignment="Center" Name="Label1" VerticalAlignment="Center" Height="127" Width="132" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Background="CornflowerBlue" FontFamily="Webdings" FontSize="72" FontWeight="Bold" />
<Label Background="CornflowerBlue" Content="c" FontFamily="Webdings" FontSize="72" FontWeight="Bold" Height="127" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Margin="0,0,0,1" Name="Label2" VerticalAlignment="Center" VerticalContentAlignment="Center" Width="132" Grid.Column="1" />
<Label Background="CornflowerBlue" Content="c" FontFamily="Webdings" FontSize="72" FontWeight="Bold" Height="127" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Margin="0,0,0,1" Name="Label3" VerticalAlignment="Center" VerticalContentAlignment="Center" Width="132" Grid.Column="2" />
<Label Background="CornflowerBlue" Content="c" FontFamily="Webdings" FontSize="72" FontWeight="Bold" Height="127" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Margin="0,1,0,0" Name="Label4" VerticalAlignment="Center" VerticalContentAlignment="Center" Width="132" Grid.Column="3" />
<Label Background="CornflowerBlue" Content="c" FontFamily="Webdings" FontSize="72" FontWeight="Bold" Height="127" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Name="Label5" VerticalAlignment="Center" VerticalContentAlignment="Center" Width="132" Grid.Row="1" />
<Label Background="CornflowerBlue" Content="c" FontFamily="Webdings" FontSize="72" FontWeight="Bold" Height="127" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Name="Label6" VerticalAlignment="Center" VerticalContentAlignment="Center" Width="132" Grid.Column="1" Grid.Row="1" />
<Label Background="CornflowerBlue" Content="c" FontFamily="Webdings" FontSize="72" FontWeight="Bold" Height="127" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Margin="0,0,0,1" Name="Label7" VerticalAlignment="Center" VerticalContentAlignment="Center" Width="132" Grid.Column="2" Grid.Row="1" />
<Label Background="CornflowerBlue" Content="c" FontFamily="Webdings" FontSize="72" FontWeight="Bold" Height="127" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Name="Label8" VerticalAlignment="Center" VerticalContentAlignment="Center" Width="132" Grid.Column="3" Grid.Row="1" />
<Label Background="CornflowerBlue" Content="c" FontFamily="Webdings" FontSize="72" FontWeight="Bold" Height="127" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Name="Label9" VerticalAlignment="Center" VerticalContentAlignment="Center" Width="132" Grid.Row="2" />
<Label Background="CornflowerBlue" Content="c" FontFamily="Webdings" FontSize="72" FontWeight="Bold" Height="127" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Name="Label10" VerticalAlignment="Center" VerticalContentAlignment="Center" Width="132" Grid.Column="1" Grid.Row="2" />
<Label Background="CornflowerBlue" Content="c" FontFamily="Webdings" FontSize="72" FontWeight="Bold" Height="127" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Name="Label11" VerticalAlignment="Center" VerticalContentAlignment="Center" Width="132" Grid.Column="2" Grid.Row="2" />
<Label Background="CornflowerBlue" Content="c" FontFamily="Webdings" FontSize="72" FontWeight="Bold" Height="127" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Name="Label12" VerticalAlignment="Center" VerticalContentAlignment="Center" Width="132" Grid.Column="3" Grid.Row="2" />
<Label Background="CornflowerBlue" Content="c" FontFamily="Webdings" FontSize="72" FontWeight="Bold" Height="127" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Name="Label13" VerticalAlignment="Center" VerticalContentAlignment="Center" Width="132" Grid.Row="3" />
<Label Background="CornflowerBlue" Content="c" FontFamily="Webdings" FontSize="72" FontWeight="Bold" Height="127" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Name="Label14" VerticalAlignment="Center" VerticalContentAlignment="Center" Width="132" Grid.Column="1" Grid.Row="3" />
<Label Background="CornflowerBlue" Content="c" FontFamily="Webdings" FontSize="72" FontWeight="Bold" Height="127" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Name="Label15" VerticalAlignment="Center" VerticalContentAlignment="Center" Width="132" Grid.Column="2" Grid.Row="3" />
<Label Background="CornflowerBlue" Content="c" FontFamily="Webdings" FontSize="72" FontWeight="Bold" Height="127" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Name="Label16" VerticalAlignment="Center" VerticalContentAlignment="Center" Width="132" Grid.Column="3" Grid.Row="3" />
</Grid>
</Window>
Class MainWindowクラスの全コード
Class MainWindow
Dim random As New Random
Dim temporaryArray() As String = {"!", "!", "N", "N", ",", ",", "k", "k", "b", "b", "v", "v", "w", "w", "z", "z"}
Dim icons As List(Of String) = temporaryArray.ToList
Dim firstClicked As Label = Nothing
Dim secondClicked As Label = Nothing
Public Sub New()
InitializeComponent()
InitializeComponent()
AssignIconsToSquares()
myControl.UC = Me
End Sub
Private Sub AssignIconsToSquares()
For Each control In Grid1.Children
Dim iconLabel As Label = TryCast(control, Label)
If iconLabel IsNot Nothing Then
Dim randomNumber As Integer = random.Next(icons.Count)
iconLabel.Content = icons.ElementAt(randomNumber)
iconLabel.Foreground = iconLabel.Background
icons.RemoveAt(randomNumber)
End If
Next
End Sub
Private Sub CheckForWinner()
For Each control In Grid1.Children
Dim iconLabel As Label = TryCast(control, Label)
If iconLabel IsNot Nothing Then
If (iconLabel.Foreground.Equals(iconLabel.Background)) Then
Return
End If
End If
Next
MessageBox.Show("You matched all the icons!", "Congratulations")
Close()
End Sub
Private Sub Label16_MouseDown(sender As System.Object, e As System.Windows.Input.MouseButtonEventArgs) Handles Label16.MouseDown, Label15.MouseDown, Label14.MouseDown, Label13.MouseDown, Label12.MouseDown, Label11.MouseDown, Label10.MouseDown, Label9.MouseDown, Label8.MouseDown, Label7.MouseDown, Label6.MouseDown, Label5.MouseDown, Label4.MouseDown, Label3.MouseDown, Label2.MouseDown, Label1.MouseDown
If (myControl.Timer1.Enabled = True) Then
Return
End If
Dim Br As New Windows.Media.SolidColorBrush
Br.Color = Color.FromRgb(0, 0, 0)
Dim clickedLabel As Label = TryCast(sender, Label)
If clickedLabel IsNot Nothing Then
If (clickedLabel.Foreground.Equals(Color.FromRgb(0, 0, 0))) Then
Return
End If
If (firstClicked Is Nothing) Then
firstClicked = clickedLabel
firstClicked.Foreground = Br
Return
End If
secondClicked = clickedLabel
secondClicked.Foreground = Br
CheckForWinner()
If (firstClicked.Content = secondClicked.Content) Then
firstClicked = Nothing
secondClicked = Nothing
Return
End If
myControl.Timer1.Start()
End If
End Sub
Public Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
myControl.Timer1.Stop()
firstClicked.Foreground = firstClicked.Background
secondClicked.Foreground = secondClicked.Background
firstClicked = Nothing
secondClicked = Nothing
End Sub
End Class