寫過不少次關於八皇後問題的代碼了,不過都是基於標准的控制台層面上的輸入輸出。這次決定采用WPF來實現一個帶有界面的八皇後的小程序
在開始寫代碼之前,首先回顧一下八皇後問題:
八皇後問題,是一個古老而著名的問題,是回溯算法的典型案例。該問題是國際西洋棋棋手馬克斯·貝瑟爾於1848年提出:在8×8格的國際象棋上擺放八個皇後,使其不能互相攻擊,即任意兩個皇後都不能處於同一行、同一列或同一斜線上,問有多少種擺法。 1 <Window x:Class="nQueen.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="Queen" Height="700" Width="800" Icon="img/Queen.jpg" WindowStartupLocation="CenterScreen" UseLayoutRounding="True" ResizeMode="NoResize" xmlns:my="clr-namespace:nQueen"> 5 6 <Grid Background="White" Name="GridMain" Width="560" Height="560" HorizontalAlignment="Left" Margin="10 0 0 0"> 7 <Grid.ColumnDefinitions> 8 <ColumnDefinition /> 9 <ColumnDefinition /> 10 <ColumnDefinition /> 11 <ColumnDefinition Width="70" /> 12 <ColumnDefinition /> 13 <ColumnDefinition /> 14 <ColumnDefinition /> 15 <ColumnDefinition /> 16 </Grid.ColumnDefinitions> 17 <Grid.RowDefinitions> 18 <RowDefinition /> 19 <RowDefinition /> 20 <RowDefinition /> 21 <RowDefinition /> 22 <RowDefinition /> 23 <RowDefinition /> 24 <RowDefinition /> 25 <RowDefinition /> 26 </Grid.RowDefinitions> 27 <Border Background="{x:Null}" BorderBrush="Black" Grid.ColumnSpan="8" Grid.RowSpan="8" Width="560" Height="560" BorderThickness="4" Panel.ZIndex="1"> 28 29 </Border> 30 31 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle1" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="0" Grid.Column="1" /> 32 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle2" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="0" Grid.Column="3" /> 33 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle3" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="1" Grid.Column="0"/> 34 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle4" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="1" Grid.Column="2" /> 35 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle5" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="2" Grid.Column="1"/> 36 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle6" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="2" Grid.Column="3"/> 37 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle7" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="3" Grid.Column="0"/> 38 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle8" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="3" Grid.Column="2"/> 39 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle9" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="0" Grid.Column="5" /> 40 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle10" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="0" Grid.Column="7" /> 41 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle11" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="1" Grid.Column="4"/> 42 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle12" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="1" Grid.Column="6" /> 43 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle13" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="2" Grid.Column="5"/> 44 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle14" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="2" Grid.Column="7"/> 45 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle15" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="3" Grid.Column="4"/> 46 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle16" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="3" Grid.Column="6"/> 47 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle17" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="4" Grid.Column="1" /> 48 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle18" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="4" Grid.Column="3" /> 49 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle19" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="4" Grid.Column="5"/> 50 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle20" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="4" Grid.Column="7" /> 51 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle21" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="5" Grid.Column="0"/> 52 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle22" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="5" Grid.Column="2"/> 53 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle23" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="5" Grid.Column="4"/> 54 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle24" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="5" Grid.Column="6"/> 55 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle25" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="6" Grid.Column="1" /> 56 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle26" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="6" Grid.Column="3" /> 57 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle27" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="6" Grid.Column="5"/> 58 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle28" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="6" Grid.Column="7" /> 59 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle29" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="7" Grid.Column="0"/> 60 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle30" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="7" Grid.Column="2"/> 61 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle31" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="7" Grid.Column="4"/> 62 <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle32" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="7" Grid.Column="6"/> 63 <Grid Height="42" HorizontalAlignment="Center" Margin="70,28,-225,0" Name="grid1" VerticalAlignment="Top" Width="225" Panel.ZIndex="0" Grid.Column="7" Grid.Row="4" > 64 <Button Content="開始求解" Height="30" HorizontalAlignment="Left" Name="startButton" VerticalAlignment="Top" Width="100" Panel.ZIndex="2" Foreground="Black" Margin="76,8,0,0" FontWeight="Normal" FontStretch="Normal" Click="startButton_Click"> 65 66 </Button> 67 </Grid> 68 69 </Grid> 70 71 </Window> View Code
那麼接下來就是完成通過按鈕實現程序的啟動和將結果展示出來了
程序啟動:
我們只需要在按鈕上面添加一個onClick的事件,然後在這個事件中放入原本在控制台版本的main方法的代碼語句就ok了
程序結果展示:
我這裡一方面用MessageBox.Show()方法彈框顯示出每一步的結果,另一方面通過新建了一個皇後圖片的XAML代碼,將每個皇後的位置通過GridMain.Children.Remove()和GridMain.Children.Add()方法來添加和去除皇後。
總結:這是第一次寫WPF的應用程序,在編寫的過程中參考了不少資料,不過網上對於WPF的資料並不是非常的豐富,遇上了很多的問題。本程序寫的也不是非常的好,很多地方寫的並不是非常的好。希望各位能給予指點或者幫助。
提供一下代碼吧(開發工具為vs2015)
http://files.cnblogs.com/files/BoulderH/nQueen.7z