MainWindow.xamlの全コード
<Window x:Class="WpfMatchingGame.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" MouseDown="Label_MouseDown" />
<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" MouseDown="Label_MouseDown"/>
<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" MouseDown="Label_MouseDown"/>
<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" MouseDown="Label_MouseDown"/>
<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" MouseDown="Label_MouseDown"/>
<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" MouseDown="Label_MouseDown" />
<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" MouseDown="Label_MouseDown" />
<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" MouseDown="Label_MouseDown"/>
<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" MouseDown="Label_MouseDown"/>
<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" MouseDown="Label_MouseDown"/>
<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" MouseDown="Label_MouseDown"/>
<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" MouseDown="Label_MouseDown"/>
<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" MouseDown="Label_MouseDown"/>
<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" MouseDown="Label_MouseDown"/>
<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" MouseDown="Label_MouseDown"/>
<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" MouseDown="Label_MouseDown"/>
</Grid>
</Window>
MainWindow.xaml.csの全コード
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace WpfMatchingGame
{
public partial class MainWindow : Window
{
Random random = new Random();
List icons = new List() { "!", "!", "N", "N", ",", ",", "k", "k", "b", "b", "v", "v", "w", "w", "z", "z" };
Label firstClicked = null;
Label secondClicked = null;
public MainWindow()
{
InitializeComponent();
AssignIconsToSquares();
myControl.UC = this;
}
private void AssignIconsToSquares()
{
foreach (var control in this.Grid1.Children)
{
Label iconLabel = control as Label;
if (iconLabel != null)
{
int randomNumber = random.Next(icons.Count);
iconLabel.Content = icons[randomNumber];
iconLabel.Foreground = iconLabel.Background;
icons.RemoveAt(randomNumber);
}
}
}
public void Timer_Tick(object sender, EventArgs e)
{
myControl.Timer1.Stop();
firstClicked.Foreground = firstClicked.Background;
secondClicked.Foreground = secondClicked.Background;
firstClicked = null;
secondClicked = null;
}
private void Label_MouseDown(object sender, MouseButtonEventArgs e)
{
if (myControl.Timer1.Enabled == true) return;
Label clickedLabel = sender as Label;
SolidColorBrush Br = new SolidColorBrush();
Br.Color = Color.FromRgb(0, 0, 0);
if (clickedLabel != null)
{
if (clickedLabel.Foreground.Equals(Color.FromRgb(0, 0, 0))) return;
if (firstClicked == null)
{
firstClicked = clickedLabel;
firstClicked.Foreground = Br;
return;
}
secondClicked = clickedLabel;
secondClicked.Foreground = Br;
CheckForWinner();
if (firstClicked.Content == secondClicked.Content)
{
firstClicked = null;
secondClicked = null;
return;
}
myControl.Timer1.Start();
}
}
private void CheckForWinner()
{
foreach (var control in this.Grid1.Children)
{
Label iconLabel = control as Label;
if (iconLabel != null)
{
if (iconLabel.Foreground.Equals(iconLabel.Background)) return;
}
}
MessageBox.Show("You matched all the icons!", "Congratulations");
Close();
}
}
}