WPF 一个训练记忆力的小游戏——记忆联盟
一、简述
记--一个用来训练记忆力的小游戏——记忆联盟。
例子打包:https://wwa.lanzous.com/iM4ZDllmjzi (20210212更新,修复越级挑战计数没有归到对应等级问题)
二、效果
挑战模式:有挑战时间限制,段位制
训练模式: 没有训练时间限制,没有段位制。
训练区2:
三、工程结构
使用VS2015专业版进行开发。
四、源文件
MainWindow.xaml文件
<Window x:Name="mainWindow" x:Class="MemoryAlliance.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MemoryAlliance"
mc:Ignorable="d"
Title="记忆联盟" Height="569" Width="602.934" WindowStartupLocation="CenterOwner" Loaded="Window_Loaded" Closing="Window_Closing" ResizeMode="CanMinimize" Icon="MemoryAlliance.ico">
<Grid Margin="0,0,-0.2,3">
<Button x:Name="button1" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40"/>
<Button x:Name="button2" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="40,0,0,0"/>
<Button x:Name="button3" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="80,0,0,0"/>
<Button x:Name="button4" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="120,0,0,0"/>
<Button x:Name="button5" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="160,0,0,0"/>
<Button x:Name="button6" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="200,0,0,0"/>
<Button x:Name="button7" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="240,0,0,0"/>
<Button x:Name="button8" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="280,0,0,0"/>
<Button x:Name="button9" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="320,0,0,0"/>
<Button x:Name="button10" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="360,0,0,0"/>
<Button x:Name="button11" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="0,39,0,0"/>
<Button x:Name="button12" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="40,39,0,0"/>
<Button x:Name="button13" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="80,39,0,0"/>
<Button x:Name="button14" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="120,39,0,0"/>
<Button x:Name="button15" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="160,39,0,0"/>
<Button x:Name="button16" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="200,39,0,0"/>
<Button x:Name="button17" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="240,39,0,0"/>
<Button x:Name="button18" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="280,39,0,0"/>
<Button x:Name="button19" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="320,39,0,0"/>
<Button x:Name="button20" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="360,39,0,0"/>
<Button x:Name="button21" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="0,78,0,0"/>
<Button x:Name="button22" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="40,78,0,0"/>
<Button x:Name="button23" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="80,78,0,0"/>
<Button x:Name="button24" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="120,78,0,0"/>
<Button x:Name="button25" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="160,78,0,0"/>
<Button x:Name="button26" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="200,78,0,0"/>
<Button x:Name="button27" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="240,78,0,0"/>
<Button x:Name="button28" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="280,78,0,0"/>
<Button x:Name="button29" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="320,78,0,0"/>
<Button x:Name="button30" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="360,78,0,0"/>
<Button x:Name="button31" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="0,117,0,0"/>
<Button x:Name="button32" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="40,117,0,0"/>
<Button x:Name="button33" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="80,117,0,0"/>
<Button x:Name="button34" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="120,117,0,0"/>
<Button x:Name="button35" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="160,117,0,0"/>
<Button x:Name="button36" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="200,117,0,0"/>
<Button x:Name="button37" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="240,117,0,0"/>
<Button x:Name="button38" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="280,117,0,0"/>
<Button x:Name="button39" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="320,117,0,0"/>
<Button x:Name="button40" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="360,117,0,0"/>
<Button x:Name="button41" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="0,156,0,0"/>
<Button x:Name="button42" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="40,156,0,0"/>
<Button x:Name="button43" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="80,156,0,0"/>
<Button x:Name="button44" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="120,156,0,0"/>
<Button x:Name="button45" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="160,156,0,0"/>
<Button x:Name="button46" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="200,156,0,0"/>
<Button x:Name="button47" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="240,156,0,0"/>
<Button x:Name="button48" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="280,156,0,0"/>
<Button x:Name="button49" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="320,156,0,0"/>
<Button x:Name="button50" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="360,156,0,0"/>
<Button x:Name="button51" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="0,195,0,0"/>
<Button x:Name="button52" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="40,195,0,0"/>
<Button x:Name="button53" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="80,195,0,0"/>
<Button x:Name="button54" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="120,195,0,0"/>
<Button x:Name="button55" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="160,195,0,0"/>
<Button x:Name="button56" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="200,195,0,0"/>
<Button x:Name="button57" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="240,195,0,0"/>
<Button x:Name="button58" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="280,195,0,0"/>
<Button x:Name="button59" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="320,195,0,0"/>
<Button x:Name="button60" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="360,195,0,0"/>
<Button x:Name="button61" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="0,234,0,0"/>
<Button x:Name="button62" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="40,234,0,0"/>
<Button x:Name="button63" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="80,234,0,0"/>
<Button x:Name="button64" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="120,234,0,0"/>
<Button x:Name="button65" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="160,234,0,0"/>
<Button x:Name="button66" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="200,234,0,0"/>
<Button x:Name="button67" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="240,234,0,0"/>
<Button x:Name="button68" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="280,234,0,0"/>
<Button x:Name="button69" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="320,234,0,0"/>
<Button x:Name="button70" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="360,234,0,0"/>
<Button x:Name="button71" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="0,272,0,0"/>
<Button x:Name="button72" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="40,272,0,0"/>
<Button x:Name="button73" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="80,272,0,0"/>
<Button x:Name="button74" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="120,272,0,0"/>
<Button x:Name="button75" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="160,272,0,0"/>
<Button x:Name="button76" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="200,272,0,0"/>
<Button x:Name="button77" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="240,272,0,0"/>
<Button x:Name="button78" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="280,272,0,0"/>
<Button x:Name="button79" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="320,272,0,0"/>
<Button x:Name="button80" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="360,272,0,0"/>
<Button x:Name="button81" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="0,311,0,0"/>
<Button x:Name="button82" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="40,311,0,0"/>
<Button x:Name="button83" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="80,311,0,0"/>
<Button x:Name="button84" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="120,311,0,0"/>
<Button x:Name="button85" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="160,311,0,0"/>
<Button x:Name="button86" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="200,311,0,0"/>
<Button x:Name="button87" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="240,311,0,0"/>
<Button x:Name="button88" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="280,311,0,0"/>
<Button x:Name="button89" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="320,311,0,0"/>
<Button x:Name="button90" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="360,311,0,0"/>
<Button x:Name="button91" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="0,350,0,0"/>
<Button x:Name="button92" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="40,350,0,0"/>
<Button x:Name="button93" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="80,350,0,0"/>
<Button x:Name="button94" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="120,350,0,0"/>
<Button x:Name="button95" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="160,350,0,0"/>
<Button x:Name="button96" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="200,350,0,0"/>
<Button x:Name="button97" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="240,350,0,0"/>
<Button x:Name="button98" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="280,350,0,0"/>
<Button x:Name="button99" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="320,350,0,0"/>
<Button x:Name="button100" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="360,350,0,0"/>
<Button x:Name="buttonStartTrain" Content="开始训练" HorizontalAlignment="Left" Margin="481,344,0,0" VerticalAlignment="Top" Width="78" Height="37" Click="buttonStart_Click"/>
<ComboBox x:Name="comboBoxTrainLevel" HorizontalAlignment="Left" Margin="481,319,0,0" VerticalAlignment="Top" Width="59" SelectedIndex="2" RenderTransformOrigin="-0.109,0.628"/>
<Label x:Name="labelTrainLevel" Content="训练难度:" HorizontalAlignment="Left" Margin="417,318,0,0" VerticalAlignment="Top" Width="61"/>
<Border BorderBrush="#FFA39696" BorderThickness="1" HorizontalAlignment="Left" Height="21" Margin="545,318,0,0" VerticalAlignment="Top" Width="21" Background="#FFBAB6B6" CornerRadius="10" ToolTip=""/>
<Label x:Name="labelTrainLevelComment" Content="i" HorizontalAlignment="Left" Margin="548,316,0,0" VerticalAlignment="Top" Width="15" Height="22" FontSize="13.333" FontWeight="Bold" ToolTip="数值越大难度越大,结果不计入段位" RenderTransformOrigin="-0.596,0.394"/>
<Label x:Name="labelCurrentLevel" Content="当前段位:" HorizontalAlignment="Left" Margin="423,21,0,0" VerticalAlignment="Top" Width="64"/>
<Label x:Name="labelCurrentLevelVal" Content="新手" HorizontalAlignment="Left" Margin="486,21,0,0" VerticalAlignment="Top" Width="45"/>
<ComboBox x:Name="comboBoxChallengeLevel" HorizontalAlignment="Left" Margin="481,166,0,0" VerticalAlignment="Top" Width="53" SelectedIndex="0" RenderTransformOrigin="-0.109,0.628"/>
<Label x:Name="labelChallengeLevel" Content="挑战难度:" HorizontalAlignment="Left" Margin="418,164,0,0" VerticalAlignment="Top" Width="60"/>
<Border BorderBrush="#FFA39696" BorderThickness="1" HorizontalAlignment="Left" Height="21" Margin="539,165,0,0" VerticalAlignment="Top" Width="21" Background="#FFBAB6B6" CornerRadius="10" ToolTip=""/>
<Label x:Name="labelChallengeLevelComment" Content="i" HorizontalAlignment="Left" Margin="543,163,0,0" VerticalAlignment="Top" Width="15" Height="22" FontSize="13.333" FontWeight="Bold" ToolTip="" RenderTransformOrigin="36.693,0.564"/>
<Label x:Name="labelTrainMemoryTime" Content="记忆时间:" HorizontalAlignment="Left" Margin="420,259,0,0" VerticalAlignment="Top" Width="61"/>
<Border BorderBrush="#FFA39696" BorderThickness="1" HorizontalAlignment="Left" Height="21" Margin="537,259,0,0" VerticalAlignment="Top" Width="21" Background="#FFBAB6B6" CornerRadius="10" ToolTip=""/>
<Label x:Name="labelTrainMemoryTimeCommnt" Content="i" HorizontalAlignment="Left" Margin="540,256,0,0" VerticalAlignment="Top" Width="15" Height="22" FontSize="13.333" FontWeight="Bold" ToolTip="难度为n则默认记忆时间为n+(n/7)*3秒" RenderTransformOrigin="36.587,0.5"/>
<Label x:Name="labelMaxLevel" Content="最高段位:" HorizontalAlignment="Left" Margin="423,46,0,0" VerticalAlignment="Top" Width="64"/>
<Label x:Name="labelTopLevelVal" Content="新手" HorizontalAlignment="Left" Margin="486,46,0,0" VerticalAlignment="Top" Width="51" ToolTip="曾经达到的最高段位"/>
<Label x:Name="labelTrainMemoryTimeUint" Content="秒" HorizontalAlignment="Left" Margin="566,283,0,0" VerticalAlignment="Top" Width="15" Height="16" RenderTransformOrigin="1.104,0.215" Padding="0"/>
<GroupBox x:Name="groupBox" Header="战绩" HorizontalAlignment="Left" Margin="414,1,0,0" VerticalAlignment="Top" Height="97" Width="172" Panel.ZIndex="-1" Grid.ColumnSpan="2"/>
<Label x:Name="labelChallengeSuccess" Content="挑战成功率:" HorizontalAlignment="Left" Margin="423,72,0,0" VerticalAlignment="Top" Width="74"/>
<Label x:Name="labelChallengeSuccessVal" Content="0%" HorizontalAlignment="Left" Margin="493,72,0,0" VerticalAlignment="Top" Width="40"/>
<Button x:Name="buttonChallengeDetail" Content="详细" HorizontalAlignment="Left" Margin="540,72,0,0" VerticalAlignment="Top" Width="40" Height="20" Click="buttonChallengeDetail_Click" ToolTip="详细的挑战记录"/>
<ComboBox x:Name="comboBoxTrainMemoryTimeVal" HorizontalAlignment="Left" Margin="501,282,0,0" VerticalAlignment="Top" Width="58" SelectedIndex="1" RenderTransformOrigin="-0.109,0.628"/>
<Label x:Name="labelRemainingTime" Content="剩余记忆时间:" HorizontalAlignment="Left" Margin="423,129,0,0" VerticalAlignment="Top" Width="152" Height="30" Background="#FF9DE05D" Padding="5,9,5,5" Grid.ColumnSpan="2"/>
<Label x:Name="labelRemainingTimeVal" Content="00:00:00" HorizontalAlignment="Left" Margin="515,133,0,0" VerticalAlignment="Top" Width="59" Height="24" Grid.ColumnSpan="2"/>
<Button x:Name="buttonStartChallenge" Content="开始挑战" HorizontalAlignment="Left" Margin="481,192,0,0" VerticalAlignment="Top" Width="78" Height="34" Click="buttonStartChallenge_Click" Grid.ColumnSpan="2"/>
<RadioButton x:Name="radioButtonDefaultMemoryTime" Content="默认" HorizontalAlignment="Left" Margin="481,263,0,0" VerticalAlignment="Top" IsChecked="True"/>
<RadioButton x:Name="radioButtonSelfDefMemoryTime" Content="" HorizontalAlignment="Left" Margin="481,287,0,0" VerticalAlignment="Top"/>
<GroupBox x:Name="groupBoxTrain" Header="训练区" HorizontalAlignment="Left" Margin="415,239,0,0" VerticalAlignment="Top" Height="150" Width="172" Panel.ZIndex="-1">
<Button x:Name="buttonMoreTrain" Content="V" Margin="144,111,-0.8,3" RenderTransformOrigin="0.5,0.5" ToolTip="展开训练2区" Padding="0" Click="buttonMoreTrain_Click" FontWeight="Bold" FontSize="9.333" HorizontalAlignment="Left" Width="16">
<Button.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform Y="-0.4"/>
</TransformGroup>
</Button.RenderTransform>
</Button>
</GroupBox>
<GroupBox x:Name="groupBoxChallenge" Header="挑战区" HorizontalAlignment="Left" Margin="414,107,0,0" VerticalAlignment="Top" Height="126" Width="172" Panel.ZIndex="-1" Grid.ColumnSpan="2">
<Button x:Name="buttonFinishMemoryChallenge" Content="完成记忆" HorizontalAlignment="Left" Margin="0,73,0,0" VerticalAlignment="Top" Width="53" ToolTip="提前完成记忆,开始答题" Click="buttonFinishMemoryChallege_Click" IsEnabled="False"/>
</GroupBox>
<Button x:Name="buttonFinishMemory" Content="完成记忆" HorizontalAlignment="Left" Margin="422,353,0,0" VerticalAlignment="Top" Width="54" IsEnabled="False" Click="buttonFinishMemory_Click"/>
<CheckBox x:Name="checkBoxNumber" Content="0-9" HorizontalAlignment="Left" Margin="75,420,0,0" VerticalAlignment="Top" ToolTip="要记忆的字符串包含数字" IsChecked="True"/>
<CheckBox x:Name="checkBoxLowercaseAlphabet" Content="a-z" HorizontalAlignment="Left" Margin="127,420,0,0" VerticalAlignment="Top" ToolTip="要记忆的字符串包含小写字母"/>
<CheckBox x:Name="checkBoxUppercaseAlphabet" Content="A-Z" HorizontalAlignment="Left" Margin="179,420,0,0" VerticalAlignment="Top" ToolTip="要记忆的字符串包含大写字母"/>
<TextBlock x:Name="textBlockMemoryString" HorizontalAlignment="Left" Margin="16,441,0,0" TextWrapping="Wrap" Text="ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ" VerticalAlignment="Top" Height="33" Width="508" Background="#FF9DE05D"/>
<TextBox x:Name="textBoxMemoryString" HorizontalAlignment="Left" Height="38" Margin="16,483,0,0" TextWrapping="Wrap" Text="ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ" VerticalAlignment="Top" Width="509" Background="#FFD5F9D4"/>
<Button x:Name="buttonCheck" Content="验证" HorizontalAlignment="Left" Margin="529,483,0,0" VerticalAlignment="Top" Width="53" Height="36" Click="buttonCheck_Click" ToolTip="验证输入与要记忆的字符串是否一致" IsDefault="True" Grid.ColumnSpan="2"/>
<Button x:Name="buttonHideMemoryString" Content="隐藏" HorizontalAlignment="Left" Margin="529,441,0,0" VerticalAlignment="Top" Width="53" Height="33" Click="buttonHideMemoryString_Click" ToolTip="隐藏要记忆的字符串" Grid.ColumnSpan="2"/>
<ComboBox x:Name="comboBoxMemoryCharCountVal" HorizontalAlignment="Left" Margin="328,415,0,0" VerticalAlignment="Top" Width="53" SelectedIndex="2" RenderTransformOrigin="-0.109,0.628"/>
<Label x:Name="labelMemoryCharCount" Content="记忆字符个数:" HorizontalAlignment="Left" Margin="246,419,0,0" VerticalAlignment="Top" Width="82" Height="19" Padding="0"/>
<Button x:Name="buttonCreateMemoeyString" Content="生成记忆字符串" HorizontalAlignment="Left" Margin="401,415,0,0" VerticalAlignment="Top" Width="123" ToolTip="生成要记忆的字符串" Click="buttonCreateMemoeyString_Click" Height="20"/>
<Label x:Name="labelMemoryCharType" Content="字符类型:" HorizontalAlignment="Left" Margin="16,419,0,0" VerticalAlignment="Top" Width="54" Height="16" Padding="0"/>
<GroupBox x:Name="groupBox1" Header="训练2区" HorizontalAlignment="Left" Margin="5,398,0,0" VerticalAlignment="Top" Height="132" Width="582" Panel.ZIndex="-1" Grid.ColumnSpan="2"/>
</Grid>
</Window>
MainWindow.xaml.cs文件
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Threading;
/*****************************************************
*
根据当前分值进行计算段位
等级 分值
青铜 0-7
白银 8-15
黄金 16-30
铂金 31-60
钻石 61-80
大师 81-90
王者 91-100
往低级挑战 成功不加分 失败-1分
级内挑战 成功+1分 失败-1分
越级挑战 成功+1+级差分数 失败-1分
*
*****************************************************/
namespace MemoryAlliance
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
int mErrIndex;//红框Button索引
int mNowIndex;//当前点击索引1~mValCnt, mNowIndex==mValCnt时 训练/挑战完成
int mValCnt;//数值个数
int mTopVal;//数值上限
int mHideValTimeOut;//定时器超时计数
int mRemainTime;//剩余时间 单位秒
int mSpanTime;//完成用时 单位秒
int mGameMode;//游戏模式:1:挑战模式 2:训练模式
int mProcess;//游戏进度, 0结束 1进行中
int mIsSaveConfigure;//是否保存配置数据,为0时不保存--数据非法
const int mHideTrain2Height = 434;//隐藏训练2区时 的高度
const int mShowTrain2Height = 569;//显示训练2区时 的高度
string mConfFile = "MemoryAlliance.conf";//配置文件
ConfigureInfo mConfigure;//配置
ButtonManager mBtnManager;//按钮统一管理者
List<int> mIndexList;//数值位置索引集合
List<char> mCharList;//训练区2的记忆字符串集合
DispatcherTimer mHideValTimer;//隐藏数值的定时器
DispatcherTimer mRemainTimeTimer;//剩余时间的定时器
public MainWindow()
{
InitializeComponent();
}
/// <summary>
/// 初始化
/// </summary>
public void init()
{
mProcess = 0;
mGameMode = 0;
mErrIndex = 0;
mNowIndex = 0;
mValCnt = 5;
mRemainTime = 5;
mTopVal = 100;
mHideValTimeOut = mValCnt;
mIsSaveConfigure = 1;
mConfigure.challengeLevelInfos = new ChallengeLevelInfo[ConfigureInfo.LEVEL_CNT];
mBtnManager = new ButtonManager();
initBtnManager();
//初始挑战练难度选择列表框
comboBoxChallengeLevel.Items.Add("青铜");//3-9
comboBoxChallengeLevel.Items.Add("白银");//8-15
comboBoxChallengeLevel.Items.Add("黄金");//16-30
comboBoxChallengeLevel.Items.Add("铂金");//31-60
comboBoxChallengeLevel.Items.Add("钻石");//61-80
comboBoxChallengeLevel.Items.Add("大师");//81-90
comboBoxChallengeLevel.Items.Add("王者");//91-100
//初始化记忆时间选择列表框
comboBoxTrainMemoryTimeVal.Items.Add(3);
comboBoxTrainMemoryTimeVal.Items.Add(5);
comboBoxTrainMemoryTimeVal.Items.Add(10);
comboBoxTrainMemoryTimeVal.Items.Add(20);
comboBoxTrainMemoryTimeVal.Items.Add(30);
comboBoxTrainMemoryTimeVal.Items.Add(40);
comboBoxTrainMemoryTimeVal.Items.Add(50);
comboBoxTrainMemoryTimeVal.Items.Add(60);
comboBoxTrainMemoryTimeVal.Items.Add(90);
comboBoxTrainMemoryTimeVal.Items.Add(120);
comboBoxTrainMemoryTimeVal.Items.Add(200);
comboBoxTrainMemoryTimeVal.Items.Add(300);
//初始化训练难度选择列表框
for (int i = 3; i <= mTopVal; i++)
{
comboBoxTrainLevel.Items.Add(i);
comboBoxMemoryCharCountVal.Items.Add(i);
}
//初始化记忆时间定时器
mHideValTimer = new DispatcherTimer();
mHideValTimer.Tick += hideValTimer_Tick;
//初始化Button的点击响应事件
initBtnClickEvent();
//初始化剩余时间定时器
mRemainTimeTimer = new DispatcherTimer();
mRemainTimeTimer.Tick += remainTimeTimer_Tick;
mRemainTimeTimer.Interval = TimeSpan.FromMilliseconds(1 * 1000);
//初始化记忆字符串集合
string allChar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
mCharList = new List<char>();
mCharList.AddRange(allChar);
}
/// <summary>
/// 计算挑战成功率
/// </summary>
/// <param name="rank">0-7, other:all rank</param>
/// <returns>挑战成功率,结果乘以100倍</returns>
private int getSuccessRate(int rank)
{
int rate = 0;
int totalSuccess = 0;
int totalFailed = 0;
if (0 <= rank && ConfigureInfo.LEVEL_CNT > rank)
{
totalSuccess = mConfigure.challengeLevelInfos[rank].challengeLevelSuccess;
totalFailed = mConfigure.challengeLevelInfos[rank].challengeLevelFailed;
}
else//计算总的
{
totalSuccess = mConfigure.getTotalSuccess();
totalFailed = mConfigure.getTotalFailed();
}
if (totalSuccess <= 0)
{
rate = 0;
}
else
{
double total = totalSuccess + totalFailed;
rate = (int)((totalSuccess / total) * 100);
}
return rate;
}
/// <summary>
/// 设置总挑战成功率
/// </summary>
private void setTotalChallengeSuccessRate()
{
int rate = getSuccessRate(ConfigureInfo.LEVEL_CNT);
if (rate > 0)
{
labelChallengeSuccessVal.Content = rate + "%";
}
else
{
labelChallengeSuccessVal.Content = "0%";
}
}
/// <summary>
/// 加载配置
/// </summary>
private int loadConfigure()
{
ConfigureInfo config;
int ret = Common.readConfigure(mConfFile, out config);
if (ret == 1)
{
mConfigure = config;
int level = 0;
//显示当前段位
labelCurrentLevelVal.Content = getLevelString(mConfigure.curLevel, out level);
//根据当前水平设置挑战难度
//comboBoxChallengeLevel.SelectedIndex = level;
//显示最高段位
labelTopLevelVal.Content = getLevelString(mConfigure.topLevel, out level);
//设置挑战成功率
setTotalChallengeSuccessRate();
//设置记忆时间
if (0 >= mConfigure.memoryTime)//默认
{
radioButtonDefaultMemoryTime.IsChecked = true;
}
else
{
radioButtonSelfDefMemoryTime.IsChecked = true;
comboBoxTrainMemoryTimeVal.SelectedValue = mConfigure.memoryTime;
}
//设置训练难度
if (mConfigure.trainLevel < 3)
{
mConfigure.trainLevel = 5;
}
comboBoxTrainLevel.SelectedValue = mConfigure.trainLevel;
//设置字符类型 charType=0-7
if ((mConfigure.charType & 1) == 1)//选择了数字
{
checkBoxNumber.IsChecked = true;
}
else
{
checkBoxNumber.IsChecked = false;
}
if ((mConfigure.charType & 2) == 2)//选择了小写字母
{
checkBoxLowercaseAlphabet.IsChecked = true;
}
else
{
checkBoxLowercaseAlphabet.IsChecked = false;
}
if ((mConfigure.charType & 4) == 4)//选择了大写字母
{
checkBoxUppercaseAlphabet.IsChecked = true;
}
else
{
checkBoxUppercaseAlphabet.IsChecked = false;
}
//设置字符个数
if (mConfigure.charCnt < 3)
{
mConfigure.charCnt = 5;
}
comboBoxMemoryCharCountVal.SelectedValue = mConfigure.charCnt;
}
return ret;
}
/// <summary>
/// 保存配置
/// </summary>
/// <returns></returns>
private bool saveConfigure()
{
//保存当前挑战难度
mConfigure.challengeLevel = comboBoxChallengeLevel.SelectedIndex;
//保存记忆时间
if ((bool)radioButtonDefaultMemoryTime.IsChecked)//默认
{
mConfigure.memoryTime = 0;
}
else
{
if (null != comboBoxTrainMemoryTimeVal.SelectedValue)
{
mConfigure.memoryTime = (int)comboBoxTrainMemoryTimeVal.SelectedValue;
}
mConfigure.memoryTime = 0;
}
//保存训练难度
mConfigure.trainLevel = (int)comboBoxTrainLevel.SelectedValue;
//保存字符类型 charType=0-7
mConfigure.charType = 0;
if ((bool)checkBoxNumber.IsChecked)//选择了数字
{
mConfigure.charType = 1;
}
if ((bool)checkBoxLowercaseAlphabet.IsChecked)//选择了小写字母
{
mConfigure.charType += 2;
}
if ((bool)checkBoxUppercaseAlphabet.IsChecked)//选择了大写字母
{
mConfigure.charType += 4;
}
//保存字符个数
if (mConfigure.charCnt < 3)
{
mConfigure.charCnt = 5;
}
mConfigure.charCnt = (int)comboBoxMemoryCharCountVal.SelectedValue;
Common.saveConfigure(mConfFile, mConfigure);
return true;
}
#region //在左上角的系统菜单添加 "关于"记忆联盟"" 需要调用系统API接口
[DllImport("user32.dll")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);//获取系统菜单
[DllImport("user32.dll")]
private static extern bool InsertMenu(IntPtr hMenu, Int32 wPosition, Int32 wFlags, Int32 wIDNewItem, string lpNewItem);//添加菜单项
//一些系统定义的常量值 (详情请参考Windows API)
public const int WM_SYSCOMMAND = 0x0112;
public const int MF_SEPARATOR = 0x800;
public const int MF_BYPOSITION = 0x400;
//自定义消息一般在0x0400之后
public const int MENU_ABOUT = 0x0470;
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
source.AddHook(WndProc);
}
//处理各种窗口消息(详情请参考Windows API)
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
//分流处理各种消息
switch (msg)
{
case WM_SYSCOMMAND://系统命令
int id = wParam.ToInt32() & 0xFFFF;
switch(id)
{
case MENU_ABOUT: //关于
handled = true;//表示已经处理,不用系统处理了
string text = "【简介】 这是一个用来训练记忆力的小游戏。"
+ "\n\n【游戏玩法】"
+ "\n 挑战区:在挑战时间内按顺序将‘数字’点开。完成挑战,段位提升;否则降低段位。"
+ "\n 训练区:按照顺序将‘数字’点开。没有时间限制,不影响段位。"
+ "\n 训练2区:随机生成一串字符,记忆后隐藏它,然后将它默写出来。"
+ "\n\n【段位等级说明】 (以下由低到高进行排序)"
+ "\n 新手、青铜、白银、黄金、铂金、钻石、大师、王者"
+ "\n 除新手外每级细分为3小级,如青铜级细分为:青铜-3,青铜-2,青铜-1;其它等级类似。3小级中1级比2级要高。";
MessageBox.Show(text, "关于\"记忆联盟\"");
break;
}
break;
}
handled = false;
return IntPtr.Zero;
}
#endregion
/// <summary>
/// 窗体加载完成事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//添加 关于"记忆联盟"
WindowInteropHelper helper = new WindowInteropHelper((Window)sender);
IntPtr sysMenuHandle = GetSystemMenu(helper.Handle, false);
InsertMenu(sysMenuHandle, 5, MF_BYPOSITION | MF_SEPARATOR, 0, string.Empty);//添加分割线
InsertMenu(sysMenuHandle, 6, MF_BYPOSITION, MENU_ABOUT, "关于\"记忆联盟\"");//添加一项菜单项
init();
//加载配置
int ret = loadConfigure();
if (-1 == ret)
{
MessageBox.Show("数据已被损坏!", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
mIsSaveConfigure = 0;
this.Close();//关闭窗体
}
//隐藏训练2区
Height = mHideTrain2Height;
//设置挑战说明:
labelChallengeLevelComment.ToolTip = "1. 成功挑战本段位或高段位难度,段位提升,难度越高提升越多;"
+ "\n2. 成功挑战低于当前段位难度的不提升段位;"
+ "\n3. 任何难度挑战失败会降低段位;";
//设置挑战难度
comboBoxChallengeLevel.SelectedIndex = mConfigure.challengeLevel;
}
/// <summary>
/// mHideValTimer定时器执行内容
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void hideValTimer_Tick(object sender, EventArgs e)
{
//关闭定时器
mHideValTimer.Stop();
//隐藏所有值
hideBtnVal();
//完成记忆按钮虚化
buttonFinishMemory.IsEnabled = false;
buttonFinishMemoryChallenge.IsEnabled = false;
//开始挑战倒计时
if (1 == mGameMode)
{
labelRemainingTime.Content = "剩余挑战时间:";
initRemainTime(2 * mValCnt);
mRemainTimeTimer.Start();
}
else if (2 == mGameMode)//训练模式 用来统计完成用时
{
mSpanTime = 0;
mRemainTimeTimer.Start();
}
//游戏进行中
mProcess = 1;
}
/// <summary>
/// 剩余时间定时器 每秒一次
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void remainTimeTimer_Tick(object sender, EventArgs e)
{
mSpanTime++;
if (1 == mGameMode)
{
if (mSpanTime == mRemainTime)
{
mRemainTimeTimer.Stop();
//更新剩余时间
updateRemainTime(mRemainTime, mSpanTime);
//挑战时间到
buttonStartChallenge_Click(buttonStartChallenge, null);
if (labelRemainingTime.Content.Equals("剩余挑战时间:"))
{
MessageBox.Show("用时超时,挑战失败! 已经完成"+ mNowIndex + "/" + mValCnt +"。", "挑战失败");
}
}
else
{
//更新剩余时间
updateRemainTime(mRemainTime, mSpanTime);
}
}
}
/// <summary>
/// 更新剩余时间
/// </summary>
/// <param name="totalTime">总时间</param>
/// <param name="spanTime">用时</param>
private void updateRemainTime(int totalTime, int spanTime)
{
int time = totalTime - spanTime;
int hour = time / 3600;
int min = (time % 3600) / 60;
int sec = (time % 60);
labelRemainingTimeVal.Content = string.Format("{0:D2}", hour) + string.Format(":{0:D2}", min) + string.Format(":{0:D2}", sec);
}
/// <summary>
/// 隐藏数值
/// </summary>
private void hideBtnVal()
{
if (null != mIndexList)
{
for (int index = 0; index < mIndexList.Count; index++)
{
mBtnManager.setBtnVal(mIndexList[index], "");
mBtnManager.setBtnBackground(mIndexList[index], Brushes.Black);
}
}
}
/// <summary>
/// 初始化Button管理者
/// </summary>
private void initBtnManager()
{
mBtnManager.mDefaultBackground = button12.Background;
mBtnManager.mDefaultBorderBrush = button12.BorderBrush;
mBtnManager.mDefaultThickness = button12.BorderThickness;
mBtnManager.addBtn(button1);
mBtnManager.addBtn(button2);
mBtnManager.addBtn(button3);
mBtnManager.addBtn(button4);
mBtnManager.addBtn(button5);
mBtnManager.addBtn(button6);
mBtnManager.addBtn(button7);
mBtnManager.addBtn(button8);
mBtnManager.addBtn(button9);
mBtnManager.addBtn(button10);
mBtnManager.addBtn(button11);
mBtnManager.addBtn(button12);
mBtnManager.addBtn(button13);
mBtnManager.addBtn(button14);
mBtnManager.addBtn(button15);
mBtnManager.addBtn(button16);
mBtnManager.addBtn(button17);
mBtnManager.addBtn(button18);
mBtnManager.addBtn(button19);
mBtnManager.addBtn(button20);
mBtnManager.addBtn(button21);
mBtnManager.addBtn(button22);
mBtnManager.addBtn(button23);
mBtnManager.addBtn(button24);
mBtnManager.addBtn(button25);
mBtnManager.addBtn(button26);
mBtnManager.addBtn(button27);
mBtnManager.addBtn(button28);
mBtnManager.addBtn(button29);
mBtnManager.addBtn(button30);
mBtnManager.addBtn(button31);
mBtnManager.addBtn(button32);
mBtnManager.addBtn(button33);
mBtnManager.addBtn(button34);
mBtnManager.addBtn(button35);
mBtnManager.addBtn(button36);
mBtnManager.addBtn(button37);
mBtnManager.addBtn(button38);
mBtnManager.addBtn(button39);
mBtnManager.addBtn(button40);
mBtnManager.addBtn(button41);
mBtnManager.addBtn(button42);
mBtnManager.addBtn(button43);
mBtnManager.addBtn(button44);
mBtnManager.addBtn(button45);
mBtnManager.addBtn(button46);
mBtnManager.addBtn(button47);
mBtnManager.addBtn(button48);
mBtnManager.addBtn(button49);
mBtnManager.addBtn(button50);
mBtnManager.addBtn(button51);
mBtnManager.addBtn(button52);
mBtnManager.addBtn(button53);
mBtnManager.addBtn(button54);
mBtnManager.addBtn(button55);
mBtnManager.addBtn(button56);
mBtnManager.addBtn(button57);
mBtnManager.addBtn(button58);
mBtnManager.addBtn(button59);
mBtnManager.addBtn(button60);
mBtnManager.addBtn(button61);
mBtnManager.addBtn(button62);
mBtnManager.addBtn(button63);
mBtnManager.addBtn(button64);
mBtnManager.addBtn(button65);
mBtnManager.addBtn(button66);
mBtnManager.addBtn(button67);
mBtnManager.addBtn(button68);
mBtnManager.addBtn(button69);
mBtnManager.addBtn(button70);
mBtnManager.addBtn(button71);
mBtnManager.addBtn(button72);
mBtnManager.addBtn(button73);
mBtnManager.addBtn(button74);
mBtnManager.addBtn(button75);
mBtnManager.addBtn(button76);
mBtnManager.addBtn(button77);
mBtnManager.addBtn(button78);
mBtnManager.addBtn(button79);
mBtnManager.addBtn(button80);
mBtnManager.addBtn(button81);
mBtnManager.addBtn(button82);
mBtnManager.addBtn(button83);
mBtnManager.addBtn(button84);
mBtnManager.addBtn(button85);
mBtnManager.addBtn(button86);
mBtnManager.addBtn(button87);
mBtnManager.addBtn(button88);
mBtnManager.addBtn(button89);
mBtnManager.addBtn(button90);
mBtnManager.addBtn(button91);
mBtnManager.addBtn(button92);
mBtnManager.addBtn(button93);
mBtnManager.addBtn(button94);
mBtnManager.addBtn(button95);
mBtnManager.addBtn(button96);
mBtnManager.addBtn(button97);
mBtnManager.addBtn(button98);
mBtnManager.addBtn(button99);
mBtnManager.addBtn(button100);
}
/// <summary>
/// 初始化Button的点击事件
/// </summary>
private void initBtnClickEvent()
{
button1.Click += new RoutedEventHandler(button_Click);
button2.Click += new RoutedEventHandler(button_Click);
button3.Click += new RoutedEventHandler(button_Click);
button4.Click += new RoutedEventHandler(button_Click);
button5.Click += new RoutedEventHandler(button_Click);
button6.Click += new RoutedEventHandler(button_Click);
button7.Click += new RoutedEventHandler(button_Click);
button8.Click += new RoutedEventHandler(button_Click);
button9.Click += new RoutedEventHandler(button_Click);
button10.Click += new RoutedEventHandler(button_Click);
button11.Click += new RoutedEventHandler(button_Click);
button12.Click += new RoutedEventHandler(button_Click);
button13.Click += new RoutedEventHandler(button_Click);
button14.Click += new RoutedEventHandler(button_Click);
button15.Click += new RoutedEventHandler(button_Click);
button16.Click += new RoutedEventHandler(button_Click);
button17.Click += new RoutedEventHandler(button_Click);
button18.Click += new RoutedEventHandler(button_Click);
button19.Click += new RoutedEventHandler(button_Click);
button20.Click += new RoutedEventHandler(button_Click);
button21.Click += new RoutedEventHandler(button_Click);
button22.Click += new RoutedEventHandler(button_Click);
button23.Click += new RoutedEventHandler(button_Click);
button24.Click += new RoutedEventHandler(button_Click);
button25.Click += new RoutedEventHandler(button_Click);
button26.Click += new RoutedEventHandler(button_Click);
button27.Click += new RoutedEventHandler(button_Click);
button28.Click += new RoutedEventHandler(button_Click);
button29.Click += new RoutedEventHandler(button_Click);
button30.Click += new RoutedEventHandler(button_Click);
button31.Click += new RoutedEventHandler(button_Click);
button32.Click += new RoutedEventHandler(button_Click);
button33.Click += new RoutedEventHandler(button_Click);
button34.Click += new RoutedEventHandler(button_Click);
button35.Click += new RoutedEventHandler(button_Click);
button36.Click += new RoutedEventHandler(button_Click);
button37.Click += new RoutedEventHandler(button_Click);
button38.Click += new RoutedEventHandler(button_Click);
button39.Click += new RoutedEventHandler(button_Click);
button40.Click += new RoutedEventHandler(button_Click);
button41.Click += new RoutedEventHandler(button_Click);
button42.Click += new RoutedEventHandler(button_Click);
button43.Click += new RoutedEventHandler(button_Click);
button44.Click += new RoutedEventHandler(button_Click);
button45.Click += new RoutedEventHandler(button_Click);
button46.Click += new RoutedEventHandler(button_Click);
button47.Click += new RoutedEventHandler(button_Click);
button48.Click += new RoutedEventHandler(button_Click);
button49.Click += new RoutedEventHandler(button_Click);
button50.Click += new RoutedEventHandler(button_Click);
button51.Click += new RoutedEventHandler(button_Click);
button52.Click += new RoutedEventHandler(button_Click);
button53.Click += new RoutedEventHandler(button_Click);
button54.Click += new RoutedEventHandler(button_Click);
button55.Click += new RoutedEventHandler(button_Click);
button56.Click += new RoutedEventHandler(button_Click);
button57.Click += new RoutedEventHandler(button_Click);
button58.Click += new RoutedEventHandler(button_Click);
button59.Click += new RoutedEventHandler(button_Click);
button60.Click += new RoutedEventHandler(button_Click);
button61.Click += new RoutedEventHandler(button_Click);
button62.Click += new RoutedEventHandler(button_Click);
button63.Click += new RoutedEventHandler(button_Click);
button64.Click += new RoutedEventHandler(button_Click);
button65.Click += new RoutedEventHandler(button_Click);
button66.Click += new RoutedEventHandler(button_Click);
button67.Click += new RoutedEventHandler(button_Click);
button68.Click += new RoutedEventHandler(button_Click);
button69.Click += new RoutedEventHandler(button_Click);
button70.Click += new RoutedEventHandler(button_Click);
button71.Click += new RoutedEventHandler(button_Click);
button72.Click += new RoutedEventHandler(button_Click);
button73.Click += new RoutedEventHandler(button_Click);
button74.Click += new RoutedEventHandler(button_Click);
button75.Click += new RoutedEventHandler(button_Click);
button76.Click += new RoutedEventHandler(button_Click);
button77.Click += new RoutedEventHandler(button_Click);
button78.Click += new RoutedEventHandler(button_Click);
button79.Click += new RoutedEventHandler(button_Click);
button80.Click += new RoutedEventHandler(button_Click);
button81.Click += new RoutedEventHandler(button_Click);
button82.Click += new RoutedEventHandler(button_Click);
button83.Click += new RoutedEventHandler(button_Click);
button84.Click += new RoutedEventHandler(button_Click);
button85.Click += new RoutedEventHandler(button_Click);
button86.Click += new RoutedEventHandler(button_Click);
button87.Click += new RoutedEventHandler(button_Click);
button88.Click += new RoutedEventHandler(button_Click);
button89.Click += new RoutedEventHandler(button_Click);
button90.Click += new RoutedEventHandler(button_Click);
button91.Click += new RoutedEventHandler(button_Click);
button92.Click += new RoutedEventHandler(button_Click);
button93.Click += new RoutedEventHandler(button_Click);
button94.Click += new RoutedEventHandler(button_Click);
button95.Click += new RoutedEventHandler(button_Click);
button96.Click += new RoutedEventHandler(button_Click);
button97.Click += new RoutedEventHandler(button_Click);
button98.Click += new RoutedEventHandler(button_Click);
button99.Click += new RoutedEventHandler(button_Click);
button100.Click += new RoutedEventHandler(button_Click);
}
/// <summary>
/// Button点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_Click(object sender, RoutedEventArgs e)
{
if (mProcess != 1)//未开始游戏,不响应
{
return;
}
Button btn = (Button)sender;
btn.Background = mBtnManager.mDefaultBackground;
string val = btn.Name.Remove(0, 6);//"button" len is 6
int clickIndex = Convert.ToInt32(val);
if (mIndexList != null)
{
int i = mIndexList.IndexOf(clickIndex);
if (i >= 0 && mBtnManager.get(mIndexList[i]).Content.Equals(""))
{
mBtnManager.setBtnVal(mIndexList[i], i + 1);//恢复原值
mBtnManager.get(mIndexList[i]).BorderBrush = new SolidColorBrush(Colors.Green);
if (clickIndex == mIndexList[mNowIndex])//按顺序点对了
{
mNowIndex++;
if (mNowIndex == mValCnt)//完成
{
closeAllTimer();
buttonStartTrain.Content = "开始训练";
buttonStartChallenge.Content = "开始挑战";
buttonStartTrain.IsEnabled = true;
buttonStartChallenge.IsEnabled = true;
buttonFinishMemory.IsEnabled = false;
comboBoxChallengeLevel.IsEnabled = true;
buttonFinishMemoryChallenge.IsEnabled = false;
if (1 == mGameMode)
{
challengeSuccess(mSpanTime);
MessageBox.Show("恭喜您挑战成功!用时" + mSpanTime + "秒。", "挑战成功");
}
else{
MessageBox.Show("恭喜您完成训练!用时" + mSpanTime + "秒。", "训练完成");
}
mProcess = 0;
}
}
else//点错了
{
closeAllTimer();
mErrIndex = mIndexList[i];
mBtnManager.clearAllBtnVal();
mBtnManager.get(mIndexList[i]).BorderBrush = new SolidColorBrush(Colors.Red);
for (i=0;i<mValCnt;i++)//恢复所有原值
{
mBtnManager.setBtnVal(mIndexList[i], i + 1);
}
buttonStartTrain.IsEnabled = true;
buttonStartChallenge.IsEnabled = true;
buttonFinishMemory.IsEnabled = false;
buttonFinishMemoryChallenge.IsEnabled = false;
buttonStartTrain.Content = "开始训练";
buttonStartChallenge.Content = "开始挑战";
comboBoxChallengeLevel.IsEnabled = true;
if (1 == mGameMode)
{
challengeFailed();
MessageBox.Show("啊哦,记忆错误! 已完成"+ mNowIndex+"/"+mValCnt +"。", "挑战失败");
}
else
{
MessageBox.Show("啊哦,记忆错误! 已完成" + mNowIndex + "/" + mValCnt + "。", "训练失败");
}
mProcess = 0;
}
//点到没有值的Button 不响应
}
}
}
/// <summary>
/// 挑战成功
/// </summary>
/// <param name="spanTime">完成用时</param>
private void challengeSuccess(int spanTime)
{
int level = 0;
int challengeLevel = 0;
getLevelString(mConfigure.curLevel, out level);
challengeLevel = comboBoxChallengeLevel.SelectedIndex;//挑战等级
int sucess = 0;
int failed = 0;
int minTime = 0;
mConfigure.getRankInfo(challengeLevel, out sucess, out failed, out minTime);
//更新最短同用时
if (0 != minTime && spanTime > minTime)
{
spanTime = -1;
}
//挑战成功数+1
sucess++;
mConfigure.setRankInfo(challengeLevel, sucess, -1, spanTime);
//加1分 低级不加分,同级加1分, 越级1+级差
if (challengeLevel >= level)
{
//加分
mConfigure.curLevel += 1 + challengeLevel - level;
//更新最高段位
if (mConfigure.curLevel > mConfigure.topLevel)
{
mConfigure.topLevel = mConfigure.curLevel;
labelTopLevelVal.Content = getLevelString(mConfigure.topLevel, out level);
//晋级时间 20191228
string date = DateTime.Now.ToLocalTime().ToString("yyyyMMdd");
mConfigure.topLevelTime = Convert.ToInt32(date);
}
//更新当前段位
labelCurrentLevelVal.Content = getLevelString(mConfigure.curLevel, out level);
//根据当前段位设置挑战难度
comboBoxChallengeLevel.SelectedIndex = level;
}
//更新挑战成功率
setTotalChallengeSuccessRate();
//保存
saveConfigure();
}
/// <summary>
/// 挑战失败
/// </summary>
private void challengeFailed()
{
int level = 0;
int challengeLevel = 0;
challengeLevel = comboBoxChallengeLevel.SelectedIndex;//挑战等级
int sucess = 0;
int failed = 0;
int minTime = 0;
mConfigure.getRankInfo(challengeLevel, out sucess, out failed, out minTime);
//挑战失败数+1
failed++;
mConfigure.setRankInfo(challengeLevel, -1, failed, -1);
//扣一分
mConfigure.curLevel--;
if (mConfigure.curLevel < 0)
{
mConfigure.curLevel = 0;
}
//更新当前段位
labelCurrentLevelVal.Content = getLevelString(mConfigure.curLevel, out level);
//根据当前段位设置挑战难度
comboBoxChallengeLevel.SelectedIndex = level;
//更新挑战成功率
setTotalChallengeSuccessRate();
//保存
saveConfigure();
}
/// <summary>
/// 清除Button的红框和绿框
/// </summary>
private void clearButtonRegAndGreenBorder()
{
//清除上一次红框
mBtnManager.setDefaultStyle(mErrIndex);
if (null != mIndexList)
{
//清除上一次绿框
for (int i = 0; i < mNowIndex && i< mIndexList.Count; i++)
{
mBtnManager.setDefaultStyle(mIndexList[i]);
}
}
}
/// <summary>
/// 开始训练
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonStart_Click(object sender, RoutedEventArgs e)
{
mGameMode = 2;
string text = buttonStartTrain.Content.ToString();
closeAllTimer();
mProcess = 0;
if (text.Equals("开始训练"))
{
buttonFinishMemory.IsEnabled = true;
buttonStartChallenge.IsEnabled = false;
buttonStartTrain.Content = "结束训练";
//清除红绿边框
clearButtonRegAndGreenBorder();
mNowIndex = 0;
//训练难度
mValCnt = (int)comboBoxTrainLevel.SelectedValue;
//获取唯一索引集合
List<int> indexList = Common.genUniqueRandomVal(1, mTopVal, mValCnt);
if (indexList == null)
{
return;
}
if (null != mIndexList)
{
mIndexList.Clear();
}
mBtnManager.clearAllBtnVal();
mIndexList = indexList;
//显示值
for (int index = 0, i = 1; index < mIndexList.Count; index++)
{
mBtnManager.setBtnVal(mIndexList[index], i);
i++;
}
//记忆时间
mHideValTimeOut = mValCnt + (mValCnt / 7) * 4;//默认
if ((bool)radioButtonSelfDefMemoryTime.IsChecked)
{
mHideValTimeOut = (int)comboBoxTrainMemoryTimeVal.SelectedValue;
}
//mHideValTimeOut秒后自动隐藏数值
mHideValTimer.Interval = TimeSpan.FromMilliseconds(mHideValTimeOut * 1000);
mHideValTimer.Start();
mErrIndex = mIndexList[0];
}
else //结束训练
{
closeAllTimer();
buttonStartTrain.Content = "开始训练";
buttonFinishMemory.IsEnabled = false;
buttonStartChallenge.IsEnabled = true;
mErrIndex = mIndexList[mNowIndex];
mBtnManager.get(mIndexList[mNowIndex]).BorderBrush = new SolidColorBrush(Colors.Red);
for (int i = 0; i < mValCnt; i++)//恢复原值
{
mBtnManager.get(mIndexList[i]).Background = mBtnManager.mDefaultBackground;
mBtnManager.setBtnVal(mIndexList[i], i + 1);
}
}
}
/// <summary>
/// 关闭相关的所有定时器
/// </summary>
private void closeAllTimer()
{
if (mRemainTimeTimer.IsEnabled)
{
mRemainTimeTimer.Stop();
}
if (mHideValTimer.IsEnabled)
{
mHideValTimer.Stop();
}
}
/// <summary>
/// 窗体关闭的时候
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
//正在挑战或正在训练的弹出确认框
bool isChallenging = buttonStartChallenge.Content.Equals("结束挑战");
if (isChallenging || buttonStartTrain.Content.Equals("结束训练"))
{
string msg = "正在挑战中,此操作会影响段位,";
if (!isChallenging)
{
msg = "正在训练中,";
}
var result = MessageBox.Show(msg+"确认退出?", "退出", MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
{
//关闭定时器
closeAllTimer();
//正在挑战退出,扣1分
if (buttonStartChallenge.Content.Equals("结束挑战"))//挑战有超时结束,需要实时判断, 不然可能被扣2分。
{
mConfigure.curLevel--;
if (mConfigure.curLevel < 0)
{
mConfigure.curLevel = 0;
}
}
Environment.Exit(0);
}
else if (result == MessageBoxResult.No)
{
e.Cancel = true;
}
}
//保存配置以及数据
if (mIsSaveConfigure != 0)
{
saveConfigure();
}
}
/// <summary>
/// 初始化剩余时间
/// </summary>
private void initRemainTime(int remainTime)
{
mSpanTime = 0;
if (remainTime <= 0)
{
remainTime = 2 * mValCnt;
}
mRemainTime = remainTime;
if (1 == mGameMode)//挑战模式
{
updateRemainTime(mRemainTime, mSpanTime);
}
}
/// <summary>
/// 开始挑战
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonStartChallenge_Click(object sender, RoutedEventArgs e)
{
mGameMode = 1;
string text = buttonStartChallenge.Content.ToString();
closeAllTimer();
mProcess = 0;
if (text.Equals("开始挑战"))
{
buttonStartChallenge.Content = "结束挑战";
buttonStartTrain.IsEnabled = false;
comboBoxChallengeLevel.IsEnabled = false;
buttonFinishMemoryChallenge.IsEnabled = true;
//清除红绿边框
clearButtonRegAndGreenBorder();
mNowIndex = 0;
//获取当前段位
int curLevel = 0;
getLevelString(mConfigure.curLevel, out curLevel);
//挑战难度
int challengeLevel = comboBoxChallengeLevel.SelectedIndex;
Random random = new Random();
if (0 == challengeLevel)//青铜 3-7
{
if(curLevel == challengeLevel)//同级
{
int min = mConfigure.curLevel - 1;
if (min < 3)
{
min = 3;
}
else if (min > 98)
{
min = 98;
}
mValCnt = random.Next(min, min+2);
}
else
{
mValCnt = random.Next(3, 7);
}
}
else if (1 == challengeLevel)//白银 8-15
{
if (curLevel == challengeLevel)//同级
{
int min = mConfigure.curLevel - 1;
if (min < 3)
{
min = 3;
}
else if (min > 98)
{
min = 98;
}
mValCnt = random.Next(min, min + 2);
}
else
{
mValCnt = random.Next(8, 15);
}
}
else if (2 == challengeLevel)//黄金 16-30
{
if (curLevel == challengeLevel)//同级
{
int min = mConfigure.curLevel - 1;
if (min < 3)
{
min = 3;
}
else if (min > 98)
{
min = 98;
}
mValCnt = random.Next(min, min + 2);
}
else
{
mValCnt = random.Next(16, 30);
}
}
else if (3 == challengeLevel)//铂金 30-60
{
if (curLevel == challengeLevel)//同级
{
int min = mConfigure.curLevel - 1;
if (min < 3)
{
min = 3;
}
else if (min > 98)
{
min = 98;
}
mValCnt = random.Next(min, min + 2);
}
else
{
mValCnt = random.Next(30, 60);
}
}
else if (4 == challengeLevel)//钻石 61-80
{
if (curLevel == challengeLevel)//同级
{
int min = mConfigure.curLevel - 1;
if (min < 3)
{
min = 3;
}
else if (min > 98)
{
min = 98;
}
mValCnt = random.Next(min, min + 2);
}
else
{
mValCnt = random.Next(61, 80);
}
}
else if (5 == challengeLevel)//大师 81-90
{
if (curLevel == challengeLevel)//同级
{
int min = mConfigure.curLevel - 1;
if (min < 3)
{
min = 3;
}
else if (min > 98)
{
min = 98;
}
mValCnt = random.Next(min, min + 2);
}
else
{
mValCnt = random.Next(81, 90);
}
}
else//王者 91-100
{
if (curLevel == challengeLevel)//同级
{
int min = mConfigure.curLevel - 1;
if (min < 3)
{
min = 3;
}
else if (min > 98)
{
min = 98;
}
mValCnt = random.Next(min, min + 2);
}
else
{
mValCnt = random.Next(91, 100);
}
}
//获取唯一索引集合
List<int> indexList = Common.genUniqueRandomVal(1, mTopVal, mValCnt);
if (indexList == null)
{
return;
}
if (null != mIndexList)
{
mIndexList.Clear();
}
mBtnManager.clearAllBtnVal();
mIndexList = indexList;
//显示值
for (int index = 0, i = 1; index < mIndexList.Count; index++)
{
mBtnManager.setBtnVal(mIndexList[index], i);
i++;
}
mErrIndex = mIndexList[0];
//开始记忆倒计时
labelRemainingTime.Content = "剩余记忆时间:";
mHideValTimeOut = mValCnt + (mValCnt / 7) * 4;//默认
initRemainTime(mHideValTimeOut);
//mHideValTimeOut秒后自动隐藏数值
mHideValTimer.Interval = TimeSpan.FromMilliseconds(mHideValTimeOut * 1000);
mHideValTimer.Start();
mRemainTimeTimer.Start();
}
else //结束挑战
{
closeAllTimer();
buttonStartChallenge.Content = "开始挑战";
buttonStartTrain.IsEnabled = true;
comboBoxChallengeLevel.IsEnabled = true;
buttonFinishMemoryChallenge.IsEnabled = false;
mErrIndex = mIndexList[mNowIndex];
mBtnManager.get(mIndexList[mNowIndex]).BorderBrush = new SolidColorBrush(Colors.Red);
for (int i = 0; i < mValCnt; i++)//恢复原值
{
mBtnManager.get(mIndexList[i]).Background = mBtnManager.mDefaultBackground;
mBtnManager.setBtnVal(mIndexList[i], i + 1);
}
//主动结束挑战,扣1分
challengeFailed();
}
}
/// <summary>
/// 完成记忆--开始训练
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonFinishMemory_Click(object sender, RoutedEventArgs e)
{
hideBtnVal();
//关闭定时器
mHideValTimer.Stop();
//隐藏Button
buttonFinishMemory.IsEnabled = false;
//计时
mRemainTimeTimer.Start();
//游戏进行中
mProcess = 1;
}
/// <summary>
/// 完成记忆--开始挑战
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonFinishMemoryChallege_Click(object sender, RoutedEventArgs e)
{
//关闭定时器
closeAllTimer();
hideBtnVal();
//隐藏Button
buttonFinishMemoryChallenge.IsEnabled = false;
mProcess = 1;
//开启挑战倒计时
labelRemainingTime.Content = "剩余挑战时间:";
initRemainTime(2*mValCnt);
mRemainTimeTimer.Start();
}
//更多按钮的旋转角度
static int gAngle = 0;
/// <summary>
/// 更多训练
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonMoreTrain_Click(object sender, RoutedEventArgs e)
{
RotateTransform rotateTranform = new RotateTransform();
if (0 == gAngle)
{
gAngle = 180;
rotateTranform.Angle = 180;
buttonMoreTrain.RenderTransform = rotateTranform;
buttonMoreTrain.ToolTip = "隐藏训练2区";
Height = mShowTrain2Height;
}
else
{
gAngle = 0;
rotateTranform.Angle = 0;
buttonMoreTrain.RenderTransform = rotateTranform;
buttonMoreTrain.ToolTip = "展开训练2区";
Height = mHideTrain2Height;
}
}
/// <summary>
/// 隐藏
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonHideMemoryString_Click(object sender, RoutedEventArgs e)
{
string text = (string)buttonHideMemoryString.Content;
if (text.Equals("隐藏"))
{
textBlockMemoryString.Background = new SolidColorBrush(Colors.Black);
buttonHideMemoryString.Content = "显示";
buttonHideMemoryString .ToolTip = "显示要记忆的字符串";
}
else
{
textBlockMemoryString.Background = new SolidColorBrush(Color.FromRgb(0x9D, 0xE0,0x5D));
buttonHideMemoryString.Content = "隐藏";
buttonHideMemoryString.ToolTip = "隐藏要记忆的字符串";
}
}
/// <summary>
/// 生成要记忆的字符串
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCreateMemoeyString_Click(object sender, RoutedEventArgs e)
{
int minVal = 0;
int topVal = 0;//10个数字+26小写字母+26大写字母
int index;
int charType = 0;
if ((bool)checkBoxNumber.IsChecked)
{
charType = 1;
topVal = 9;
}
if ((bool)checkBoxLowercaseAlphabet.IsChecked)
{
if (1 != charType)//没有选数字
{
minVal = 10;
}
charType += 2;
topVal = 35;
}
if ((bool)checkBoxUppercaseAlphabet.IsChecked)
{
if (1 == charType)//选了数字和大写字母
{
minVal = 35;
topVal = 71;
}
else if (2 == charType)//选了小写字母和大写字母
{
minVal = 10;
topVal = 61;
}
else if (3 == charType)//三种都选了
{
minVal = 0;
topVal = 61;
}
else //只选了大写字母
{
minVal = 35;
topVal = 61;
}
charType += 4;
}
if (0 == charType)
{
MessageBox.Show("请至少选择一种字符类型", "提示");
return;
}
int charCnt = (int)comboBoxMemoryCharCountVal.SelectedValue;
string memoryString = "";
Random random = new Random();
for (; charCnt > 0; charCnt--)
{
index = random.Next(minVal, topVal);
memoryString += mCharList[index];
}
textBlockMemoryString.Text = memoryString;
//清空答题区
textBoxMemoryString.Text = "";
}
/// <summary>
/// 验证
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCheck_Click(object sender, RoutedEventArgs e)
{
string str1 = textBlockMemoryString.Text;
string str2 = textBoxMemoryString.Text;
int len1 = str1.Length;
int len2 = str2.Length;
if (str2.Trim().Equals(""))
{
return;
}
if (str1.Equals(str2))
{
//显示记忆字符串
textBlockMemoryString.Background = new SolidColorBrush(Color.FromRgb(0x9D, 0xE0, 0x5D));
buttonHideMemoryString.Content = "隐藏";
buttonHideMemoryString.ToolTip = "隐藏要记忆的字符串";
MessageBox.Show("恭喜您! 输入正确!", "验证正确");
}
else
{
//找到第一个不同的
int i = 0;
for (; i < str1.Length && i < str2.Length; i++)
{
if (str1[i] != str2[i])
{
break;
}
}
if (buttonHideMemoryString.Content.Equals("隐藏"))
{
if (i != str1.Length)
{
//将第一个不一致的字符标红
textBlockMemoryString.Text = "";
textBlockMemoryString.Inlines.Add(new Run(str1.Substring(0, i)));
textBlockMemoryString.Inlines.Add(new Run(str1[i] + "") { Foreground = Brushes.Red });
textBlockMemoryString.Inlines.Add(new Run(str1.Substring(i + 1, str1.Length - 1 - i)));
MessageBox.Show("抱歉! 输入有误! 已经答对" + i + "/" + str1.Length + "。", "验证错误");
}
else //输入对了,但是后面有多余字符
{
MessageBox.Show("抱歉! 输入有误! 已经答对" + i + "/" + str1.Length + "。但输入有多余", "验证错误");
}
}
else//没有对几个的提示
{
MessageBox.Show("抱歉! 输入有误","验证错误");
}
}
}
/*
个等级分三小等级
等级 分值
新手 0 level 0
青铜 1-7 level 0
白银 8-15 level 1
黄金 16-30 level 2
铂金 31-60 level 3
钻石 61-80 level 4
大师 81-90 level 5
王者 91-100 level 6
*/
/// <summary>
/// 根据分值得到段位
/// </summary>
/// <param name="score"></param>
/// <returns></returns>
private string getLevelString(int score, out int level)
{
int rank = 0;//小等级
string levelStr = "";
if (score <= 0)
{
levelStr = "新手";
level = 0;
}
else if (score <= 7)//青铜 1-7 1-2==>3 3-4==>2 5-7==>1
{
//rank = 4 - (score + 1) / 2; if(rank ==0) rank=1;
if (score <= 2)
{
rank = 3;
}
else if (score <= 5)
{
rank = 2;
}
else
{
rank = 1;
}
levelStr = "青铜-" + rank;
level = 0;
}
else if (score <= 15)//白银 8-15
{
if (score <= 9)
{
rank = 3;
}
else if (score <= 12)
{
rank = 2;
}
else
{
rank = 1;
}
levelStr = "白银-" + rank;
level = 1;
}
else if (score <= 30)//黄金 16-30
{
if (score <= 20)
{
rank = 3;
}
else if (score <= 25)
{
rank = 2;
}
else
{
rank = 1;
}
levelStr = "黄金-" + rank;
level = 2;
}
else if (score <= 60)//钻石 31-60
{
if (score <= 40)
{
rank = 3;
}
else if (score <= 50)
{
rank = 2;
}
else
{
rank = 1;
}
levelStr = "钻石-" + rank;
level = 3;
}
else if (score <= 80)//铂金 61-80
{
if (score <= 66)
{
rank = 3;
}
else if (score <= 73)
{
rank = 2;
}
else
{
rank = 1;
}
levelStr = "铂金-" + rank;
level = 4;
}
else if (score <= 90)//大师 81-90
{
if (score <= 83)
{
rank = 3;
}
else if (score <= 86)
{
rank = 2;
}
else
{
rank = 1;
}
levelStr = "大师-" + rank;
level = 5;
}
else//王者 91-100
{
levelStr = "王者-";
if (score <= 93)
{
rank = 3;
}
else if (score <= 96)
{
rank = 2;
}
else if (score <= 100)
{
rank = 1;
}
else
{
levelStr = "至尊-";
rank = score - 100;
}
levelStr += rank;
level = 6;
}
return levelStr;
}
/// <summary>
/// 详细 历史挑战
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonChallengeDetail_Click(object sender, RoutedEventArgs e)
{
int mLevel = 0;
int topLevelTime = 20210212;
int totalSuccess = mConfigure.getTotalSuccess();
int totalFailed = mConfigure.getTotalFailed();
int level0SucessRate = getSuccessRate(0);
int level1SucessRate = getSuccessRate(1);
int level2SucessRate = getSuccessRate(2);
int level3SucessRate = getSuccessRate(3);
int level4SucessRate = getSuccessRate(4);
int level5SucessRate = getSuccessRate(5);
int level6SucessRate = getSuccessRate(6);
if (0 != mConfigure.topLevelTime)
{
topLevelTime = mConfigure.topLevelTime;
}
//保存配置
saveConfigure();
string text = "当前段位: " + getLevelString(mConfigure.curLevel, out mLevel) + " 当前积分: " + mConfigure.curLevel
+ "\n最高段位: " + getLevelString(mConfigure.topLevel, out mLevel) + " 晋级时间: " + topLevelTime
+ "\n挑战总次数: " + (totalSuccess + totalFailed) + " 成功: " + totalSuccess + " 失败: " + totalFailed + " 胜率: " + labelChallengeSuccessVal.Content
+ "\n青铜级 成功: "+ mConfigure.challengeLevelInfos[0].challengeLevelSuccess + " 失败: "+ mConfigure.challengeLevelInfos[0].challengeLevelFailed + " 胜率: "+ level0SucessRate +"% 最短用时: "+ mConfigure.challengeLevelInfos[0].challengeLevelMinTime + "秒"
+ "\n白银级 成功: "+ mConfigure.challengeLevelInfos[1].challengeLevelSuccess + " 失败: "+ mConfigure.challengeLevelInfos[1].challengeLevelFailed + " 胜率: "+ level1SucessRate +"% 最短用时: "+ mConfigure.challengeLevelInfos[1].challengeLevelMinTime + "秒"
+ "\n黄金级 成功: "+ mConfigure.challengeLevelInfos[2].challengeLevelSuccess + " 失败: "+ mConfigure.challengeLevelInfos[2].challengeLevelFailed + " 胜率: "+ level2SucessRate +"% 最短用时: "+ mConfigure.challengeLevelInfos[2].challengeLevelMinTime + "秒"
+ "\n铂金级 成功: "+ mConfigure.challengeLevelInfos[3].challengeLevelSuccess + " 失败: "+ mConfigure.challengeLevelInfos[3].challengeLevelFailed + " 胜率: "+ level3SucessRate +"% 最短用时: "+ mConfigure.challengeLevelInfos[3].challengeLevelMinTime + "秒"
+ "\n钻石级 成功: "+ mConfigure.challengeLevelInfos[4].challengeLevelSuccess + " 失败: "+ mConfigure.challengeLevelInfos[4].challengeLevelFailed + " 胜率: "+ level4SucessRate +"% 最短用时: "+ mConfigure.challengeLevelInfos[4].challengeLevelMinTime + "秒"
+ "\n大师级 成功: "+ mConfigure.challengeLevelInfos[5].challengeLevelSuccess + " 失败: "+ mConfigure.challengeLevelInfos[5].challengeLevelFailed + " 胜率: "+ level5SucessRate +"% 最短用时: "+ mConfigure.challengeLevelInfos[5].challengeLevelMinTime + "秒"
+ "\n王者级 成功: "+ mConfigure.challengeLevelInfos[6].challengeLevelSuccess + " 失败: "+ mConfigure.challengeLevelInfos[6].challengeLevelFailed + " 胜率: "+ level6SucessRate +"% 最短用时: "+ mConfigure.challengeLevelInfos[6].challengeLevelMinTime + "秒";
MessageBox.Show(text, "历史战绩");
}
}
}
Common.cs文件
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
namespace MemoryAlliance
{
//标识为可序列化
[StructLayout(LayoutKind.Sequential), Serializable]
public struct ChallengeLevelInfo
{
public int challengeLevelSuccess;//挑战成功数
public int challengeLevelFailed;//挑战失败数
public int challengeLevelMinTime;//最短用时
}
//标识为可序列化
[StructLayout(LayoutKind.Sequential), Serializable]
public struct ConfigureInfo
{
public const int LEVEL_CNT = 7;//7个等级
public int trainLevel;//训练难度 3-100
public int challengeLevel;//挑战难度 0-6
public int charType;//组合1 | 2 | 4
public int charCnt;//记忆字符个数
public int memoryTime;//记忆时间非负数 0是默认 其它为实际秒数
public int curLevel;//当前段位
public int topLevel;//最高段位
public int topLevelTime;//晋级时间 20191227
//定义7个结构体数组,存放各等级的挑战信息
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = LEVEL_CNT, ArraySubType = UnmanagedType.Struct)]
public ChallengeLevelInfo[] challengeLevelInfos;
/// <summary>
/// 挑战成功总数
/// </summary>
/// <returns></returns>
public int getTotalSuccess()
{
int totalSuccess = 0;
for (int i = 0; i < LEVEL_CNT; i++)
{
totalSuccess += challengeLevelInfos[i].challengeLevelSuccess;
}
return totalSuccess;
}
/// <summary>
/// 挑战失败总数
/// </summary>
/// <returns></returns>
public int getTotalFailed()
{
int totalFailed = 0;
for (int i = 0; i < LEVEL_CNT; i++)
{
totalFailed += challengeLevelInfos[i].challengeLevelFailed;
}
return totalFailed;
}
/// <summary>
/// 设置等级挑战信息
/// </summary> 如果值为负数就不设置
/// <param name="rank">等级0-6</param>
/// <param name="success">挑战成功数</param>
/// <param name="faied">挑战失败数</param>
/// <param name="minTime">最短用时</param>
public void setRankInfo(int rank, int success, int faied, int minTime)
{
if (0 > rank && LEVEL_CNT <= rank)
{
rank = LEVEL_CNT - 1;//LEVEL_CNT必须大于0
}
if (success >= 0)
{
challengeLevelInfos[rank].challengeLevelSuccess = success;
}
if (faied >= 0)
{
challengeLevelInfos[rank].challengeLevelFailed = faied;
}
if (minTime >= 0)
{
challengeLevelInfos[rank].challengeLevelMinTime = minTime;
}
}
/// <summary>
/// 获取等级挑战信息
/// </summary>
/// <param name="rank">等级0-6</param>
/// <param name="success">挑战成功数</param>
/// <param name="faied">挑战失败数</param>
/// <param name="minTime">最短用时</param>
public void getRankInfo(int rank, out int success, out int faied, out int minTime)
{
if (0 > rank && LEVEL_CNT <= rank)
{
rank = LEVEL_CNT - 1;//LEVEL_CNT必须大于0
}
success = challengeLevelInfos[rank].challengeLevelSuccess;
faied = challengeLevelInfos[rank].challengeLevelFailed;
minTime = challengeLevelInfos[rank].challengeLevelMinTime;
}
}
class Common
{
static List<int> mBaseList = null;
/// <summary>
/// 生成指定范围的n个不重复的随机数
/// </summary>
/// <param name="minVal"></param>
/// <param name="topVal"></param>
/// <param name="cnt"></param>
/// <returns></returns>
public static List<int> genUniqueRandomVal(int minVal, int topVal, int cnt)
{
int index;
if (minVal >= topVal)
{
return null;
}
if (null == mBaseList)
{
mBaseList = new List<int>();
}
mBaseList.Clear();
for (int i = minVal; i <= topVal; i++)
{
mBaseList.Add(i);
}
List<int> uniqueValList = new List<int>();
Random random = new Random();
for (; cnt > 0;)
{
index = random.Next(minVal, topVal);
uniqueValList.Add(mBaseList[index - 1]);
mBaseList.RemoveAt(index - 1);
cnt--;
topVal--;
}
return uniqueValList;
}
/// <summary>
/// ConfigureInfo反序列化
/// </summary>
/// <param name="rawdatas"></param>
/// <returns></returns>
public static ConfigureInfo confInfoDeserialize(byte[] rawdatas)
{
Type anytype = typeof(ConfigureInfo);
int rawsize = Marshal.SizeOf(anytype);
if (rawsize > rawdatas.Length)
{
return new ConfigureInfo();
}
IntPtr buffer = Marshal.AllocHGlobal(rawsize);
Marshal.Copy(rawdatas, 0, buffer, rawsize);
object retobj = Marshal.PtrToStructure(buffer, anytype);
Marshal.FreeHGlobal(buffer);
return (ConfigureInfo)retobj;
}
/// <summary>
/// ConfigureInfo序列化
/// </summary>
/// <param name="obj">ConfigureInfo结构体</param>
/// <returns></returns>
public static byte[] confInfoSerialize(object obj)
{
int rawsize = Marshal.SizeOf(obj);
IntPtr buffer = Marshal.AllocHGlobal(rawsize);
Marshal.StructureToPtr(obj, buffer, false);
byte[] rawdatas = new byte[rawsize];
Marshal.Copy(buffer, rawdatas, 0, rawsize);
Marshal.FreeHGlobal(buffer);
return rawdatas;
}
/// <summary>
/// 保存配置
/// </summary>
/// <param name="confFileName">配置文件名</param>
/// <param name="conf">配置</param>
/// <returns></returns>
public static bool saveConfigure(string confFileName, object conf)
{
try
{
FileStream fs = File.OpenWrite(confFileName);
byte[] data = confInfoSerialize(conf);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(data);
//计算校验和
int dateSize = Marshal.SizeOf(conf);
ushort checkSum = calcCheckSum(data, dateSize);
bw.Write(checkSum);
bw.Flush();
bw.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return false;
}
return true;
}
/// <summary>
/// 读取配置
/// </summary>
/// <param name="confFileName">配置文件名</param>
/// <param name="conf">配置</param>
/// <returns></returns>
public static int readConfigure(string confFileName, out ConfigureInfo conf)
{
try
{
FileStream fs = File.OpenRead(confFileName);
BinaryReader br = new BinaryReader(fs);
int size = Marshal.SizeOf(typeof(ConfigureInfo));
byte[] data = br.ReadBytes(size);
//读取校验和
ushort checkSum1 = br.ReadUInt16();
ushort checkSum2 = calcCheckSum(data, size);
br.Close();
fs.Close();
//判断校验和,如果不一样,表示数据被恶意修改
if (checkSum1 != checkSum2)
{
conf = new ConfigureInfo();
return -1;
}
else
{
conf = confInfoDeserialize(data);
return 1;
}
}
catch
{
conf = new ConfigureInfo();
}
return 0;
}
/// <summary>
/// 计算校验和
/// </summary>
/// <param name="data">指定的数据</param>
/// <param name="size">数据长度</param>
/// <returns></returns>
private static ushort calcCheckSum(byte[] data, int size)
{
byte checkSumA = 0;
byte checkSumB = 0;
ushort checkSum = 0;
for (int i = 0; i < size; i++)
{
checkSumA += data[i];
checkSumB ^= data[i];
}
checkSum = (ushort)((Convert.ToUInt16(checkSumA) << 8) + Convert.ToUInt16(checkSumB));
return checkSum;
}
}
}
ButtonManager.cs文件
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace MemoryAlliance
{
/// <summary>
/// Button的布局类
/// </summary>
class ButtonManager
{
public Brush mDefaultBackground;//Button默认的背景样式
public Brush mDefaultBorderBrush;//Button默认的边框样式
public Thickness mDefaultThickness;//Button默认的边框粗细
private List<Button> mBtnList;
public ButtonManager()
{
mBtnList = new List<Button>();
}
/// <summary>
/// 清空Button集合
/// </summary>
public void clearList()
{
mBtnList.Clear();
}
/// <summary>
/// 添加Button
/// </summary>
/// <param name="btn"></param>
public void addBtn(Button btn)
{
if (mBtnList != null && btn != null)
{
mBtnList.Add(btn);
}
}
/// <summary>
/// 获取指定Button对象
/// </summary>
/// <param name="index">[1-100]</param>
/// <returns></returns>
public Button get(int index)
{
if (index >= 1 && index <= mBtnList.Count)
{
return mBtnList[index-1];
}
return null;
}
/// <summary>
/// 设置指定Button数值
/// </summary>
/// <param name="btnIndex">[1,100]</param>
/// <param name="val">[1,100]</param>
/// <returns></returns>
public void setBtnVal(int btnIndex, int val)
{
if (btnIndex > 0 && btnIndex <= mBtnList.Count)
{
mBtnList[btnIndex-1].Content = val;
}
}
/// <summary>
/// 设置指定Button数值
/// </summary>
/// <param name="btnIndex">[1,100]</param>
/// <param name="val">[1,100]</param>
/// <returns></returns>
public void setBtnVal(int btnIndex, string val)
{
if (btnIndex > 0 && btnIndex <= mBtnList.Count)
{
mBtnList[btnIndex - 1].Content = val;
}
}
/// <summary>
/// 设置指定Button背景色
/// </summary>
/// <param name="btnIndex"></param>
/// <param name="brush"></param>
public void setBtnBackground(int btnIndex, Brush brush)
{
if (btnIndex > 0 && btnIndex <= mBtnList.Count)
{
mBtnList[btnIndex - 1].Background = brush;
}
}
/// <summary>
/// 清空所有Button数值
/// </summary>
public void clearAllBtnVal()
{
if (mBtnList != null)
{
foreach (var item in mBtnList)
{
item.Content = "";
item.Background = mDefaultBackground;
}
}
}
/// <summary>
/// 将Butto设置为默认样式
/// </summary>
/// <param name="btnIndex"></param>
/// <returns></returns>
public bool setDefaultStyle(int btnIndex)
{
if (null == mBtnList)
{
return false;
}
if (btnIndex > 0 && btnIndex <= mBtnList.Count)
{
mBtnList[btnIndex - 1].Background = mDefaultBackground;
mBtnList[btnIndex - 1].BorderBrush = mDefaultBorderBrush;
mBtnList[btnIndex - 1].BorderThickness = mDefaultThickness;
}
return false;
}
}
}
五、总结
5.1 修改系统菜单,需要调用系统API。
声明/引用 系统接口
[DllImport("user32.dll")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);//获取系统菜单
[DllImport("user32.dll")]
private static extern bool InsertMenu(IntPtr hMenu, Int32 wPosition, Int32 wFlags, Int32 wIDNewItem, string lpNewItem);//添加菜单项
//一些系统定义的常量值 (详情请参考Windows API)
public const int WM_SYSCOMMAND = 0x0112;
public const int MF_SEPARATOR = 0x800;
public const int MF_BYPOSITION = 0x400;
//自定义消息一般在0x0400之后
public const int MENU_ABOUT = 0x0470;
在窗体加载完成函数进行添加菜单项,在添加新的菜单项是需要设置一个"消息id",后面根据这个"消息id"来识别这个菜单项。
//添加 关于"记忆联盟"
WindowInteropHelper helper = new WindowInteropHelper((Window)sender);
IntPtr sysMenuHandle = GetSystemMenu(helper.Handle, false);
InsertMenu(sysMenuHandle, 5, MF_BYPOSITION | MF_SEPARATOR, 0, string.Empty);//添加分割线
InsertMenu(sysMenuHandle, 6, MF_BYPOSITION, MENU_ABOUT, "关于\"记忆联盟\"");//添加一项菜单项
新添加的菜单项的动作响应 (拦截各种窗口消息,是我们需要的消息时才进行响应)
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
source.AddHook(WndProc);
}
//处理各种窗口消息(详情请参考Windows API)
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
//分流处理各种消息
switch (msg)
{
case WM_SYSCOMMAND://系统命令
int id = wParam.ToInt32() & 0xFFFF;
switch(id)
{
case MENU_ABOUT: //关于
handled = true;//表示已经处理,不用系统处理了
string text = "【简介】 这是一个用来训练记忆力的小游戏。"
+ "\n\n【游戏玩法】"
+ "\n 挑战区:在挑战时间内按顺序将‘数字’点开。完成挑战,段位提升;否则降低段位。"
+ "\n 训练区:按照顺序将‘数字’点开。没有时间限制,不影响段位。"
+ "\n 训练2区:随机生成一串字符,记忆后隐藏它,然后将它默写出来。"
+ "\n\n【段位等级说明】 (以下由低到高进行排序)"
+ "\n 新手、青铜、白银、黄金、铂金、钻石、大师、王者"
+ "\n 除新手外每级细分为3小级,如青铜级细分为:青铜-3,青铜-2,青铜-1;其它等级类似。3小级中1级比2级要高。";
MessageBox.Show(text, "关于\"记忆联盟\"");
break;
}
break;
}
handled = false;
return IntPtr.Zero;
}
效果:
更多接口:
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
private static extern bool RemoveMenu(IntPtr hMenu, uint nPosition, uint nFlags);//移除
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
private static extern bool SetMenuItemBitmaps(IntPtr hMenu, Int32 nPosition, Int32 nFlags, Bitmap pBmpUnchecked, Bitmap pBmpchecked);//设置图标
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
private static extern bool AppendMenu(IntPtr hMenu,Int32 nFlags, Int32 IDNewItem, string lpNewItem);//追加菜单项
5.2 设置textBlock中某一部分字符的颜色
myTextBlock.Inlines.Add(new Run("abc"));//添加"abc"字符串,默认黑色
myTextBlock.Inlines.Add(new Run("d") { Foreground = Brushes.Red });//添加红色的d
myTextBlock.Inlines.Add(new Run("e") { Foreground = Brushes.Green });//添加绿色的e
myTextBlock.Inlines.Add(new Run("f") { Foreground = Brushes.Blue });//添加蓝色的f
myTextBlock.Inlines.Add(new Run("ghi"));添加"ghi"字符串,默认黑色
字符效果: