Advertisement
Guest User

Now with mouseover move highlights!

a guest
Mar 20th, 2013
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <Window x:Class="Chess.MainWindow"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         xmlns:local="clr-namespace:Chess" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
  5.         Title="ChessBoard" Height="300" Width="300">
  6.     <Window.Resources>
  7.         <DataTemplate DataType="{x:Type local:ChessPiece}">
  8.             <Image Source="{Binding ImageSource}" Width="32" Height="32">
  9.                 <i:Interaction.Triggers>
  10.                     <i:EventTrigger EventName="MouseEnter">
  11.                         <local:ExecuteCommandAction Command="{Binding PieceSelectedCommand}" CommandParameter="{Binding}"/>
  12.                     </i:EventTrigger>
  13.                     <i:EventTrigger EventName="MouseLeave">
  14.                         <local:ExecuteCommandAction Command="{Binding PieceDeselectedCommand}" CommandParameter="{Binding}"/>
  15.                     </i:EventTrigger>
  16.                 </i:Interaction.Triggers>
  17.             </Image>
  18.         </DataTemplate>
  19.     </Window.Resources>
  20.  
  21.     <Grid>
  22.         <ItemsControl x:Name="_theBoard" ItemsSource="{Binding Squares}">
  23.             <ItemsControl.ItemsPanel>
  24.                 <ItemsPanelTemplate>
  25.                     <UniformGrid Rows="8" Columns="8"/>
  26.                 </ItemsPanelTemplate>
  27.             </ItemsControl.ItemsPanel>
  28.             <ItemsControl.ItemTemplate>
  29.                 <DataTemplate>
  30.                     <Grid>
  31.                         <Rectangle Fill="White" x:Name="SquareColor" Opacity=".5"/>
  32.                         <ContentControl Content="{Binding Piece}"/>
  33.                     </Grid>
  34.                     <DataTemplate.Triggers>
  35.                         <DataTrigger Binding="{Binding IsBlack}" Value="True">
  36.                             <Setter TargetName="SquareColor" Property="Fill" Value="Black"/>
  37.                         </DataTrigger>
  38.                         <DataTrigger Binding="{Binding IsHighlighted}" Value="True">
  39.                             <Setter TargetName="SquareColor" Property="Fill" Value="Yellow"/>
  40.                         </DataTrigger>
  41.                     </DataTemplate.Triggers>
  42.                 </DataTemplate>
  43.             </ItemsControl.ItemTemplate>
  44.         </ItemsControl>
  45.     </Grid>
  46. </Window>
  47.  
  48.  
  49.  
  50. using System;
  51. using System.Collections.Generic;
  52. using System.ComponentModel;
  53. using System.Linq;
  54. using System.Threading;
  55. using System.Windows;
  56. using System.Windows.Input;
  57. using System.Windows.Interactivity;
  58. using Microsoft.Expression.Interactivity.Core;
  59.  
  60. namespace Chess
  61. {
  62.     /// <summary>
  63.     /// Interaction logic for MainWindow.xaml
  64.     /// </summary>
  65.     public partial class MainWindow
  66.     {
  67.         public ChessBoard Board { get; set; }
  68.  
  69.         public MainWindow()
  70.         {
  71.             Board = new ChessBoard();
  72.             InitializeComponent();
  73.             DataContext = Board;
  74.             NewGame();
  75.         }
  76.  
  77.         private void NewGame()
  78.         {
  79.             Board[0, 0] = new ChessPiece(Board) { Row = 0, Column = 0, Type = ChessPieceTypes.Tower, IsBlack = true };
  80.             Board[0, 1] = new ChessPiece(Board) { Row = 0, Column = 1, Type = ChessPieceTypes.Knight, IsBlack = true };
  81.             Board[0, 2] = new ChessPiece(Board) { Row = 0, Column = 2, Type = ChessPieceTypes.Bishop, IsBlack = true };
  82.             Board[0, 3] = new ChessPiece(Board) { Row = 0, Column = 3, Type = ChessPieceTypes.Queen, IsBlack = true };
  83.             Board[0, 4] = new ChessPiece(Board) { Row = 0, Column = 4, Type = ChessPieceTypes.King, IsBlack = true };
  84.             Board[0, 5] = new ChessPiece(Board) { Row = 0, Column = 5, Type = ChessPieceTypes.Bishop, IsBlack = true };
  85.             Board[0, 6] = new ChessPiece(Board) { Row = 0, Column = 6, Type = ChessPieceTypes.Knight, IsBlack = true };
  86.             Board[0, 7] = new ChessPiece(Board) { Row = 0, Column = 7, Type = ChessPieceTypes.Tower, IsBlack = true };
  87.  
  88.             Enumerable.Range(0, 8).Select(x => new ChessPiece(Board)
  89.                 {
  90.                     Row = 1,
  91.                     Column = x,
  92.                     IsBlack = true,
  93.                     Type = ChessPieceTypes.Pawn
  94.                 }).ToList().ForEach(p => Board[p.Row, p.Column] = p);
  95.  
  96.             Board[7, 0] = new ChessPiece(Board) { Row = 7, Column = 0, Type = ChessPieceTypes.Tower, IsBlack = false };
  97.             Board[7, 1] = new ChessPiece(Board) { Row = 7, Column = 1, Type = ChessPieceTypes.Knight, IsBlack = false };
  98.             Board[7, 2] = new ChessPiece(Board) { Row = 7, Column = 2, Type = ChessPieceTypes.Bishop, IsBlack = false };
  99.             Board[7, 3] = new ChessPiece(Board) { Row = 7, Column = 3, Type = ChessPieceTypes.Queen, IsBlack = false };
  100.             Board[7, 4] = new ChessPiece(Board) { Row = 7, Column = 4, Type = ChessPieceTypes.King, IsBlack = false };
  101.             Board[7, 5] = new ChessPiece(Board) { Row = 7, Column = 5, Type = ChessPieceTypes.Bishop, IsBlack = false };
  102.             Board[7, 6] = new ChessPiece(Board) { Row = 7, Column = 6, Type = ChessPieceTypes.Knight, IsBlack = false };
  103.             Board[7, 7] = new ChessPiece(Board) { Row = 7, Column = 7, Type = ChessPieceTypes.Tower, IsBlack = false };
  104.  
  105.             Enumerable.Range(0, 8).Select(x => new ChessPiece(Board)
  106.                 {
  107.                     Row = 6,
  108.                     Column = x,
  109.                     IsBlack = false,
  110.                     Type = ChessPieceTypes.Pawn
  111.                 }).ToList().ForEach(p => Board[p.Row, p.Column] = p);
  112.  
  113.             new Thread(() =>
  114.                 {
  115.                     Thread.Sleep(2000);
  116.                     _theBoard.Dispatcher.Invoke((Action) (() =>
  117.                         {
  118.                             var pawn = Board[6, 3];
  119.                             pawn.Row = 5;
  120.                             pawn.Column = 3;
  121.                             Board[5, 3] = pawn;
  122.                             Board[6, 3] = null;
  123.                         }));
  124.                 }).Start();
  125.         }
  126.     }
  127.  
  128.     public class ChessBoard
  129.     {
  130.         public ChessBoard()
  131.         {
  132.             Captured = new[]{ new List<ChessPiece>(), new List<ChessPiece>()};
  133.             Squares = new ChessSquare[64];
  134.             for (int row = 0; row < 8; row++)
  135.             {
  136.                 for (int col = 0; col < 8; col++)
  137.                 {
  138.                     bool isblack = (row + col) % 2 == 0;
  139.                     Squares[row * 8 + col] = new ChessSquare { IsBlack = isblack };
  140.                 }
  141.             }
  142.  
  143.         }
  144.         public List<ChessPiece>[] Captured { get; private set; }
  145.         public ChessSquare[] Squares { get; private set; }
  146.  
  147.         public ChessPiece this[int row, int col]
  148.         {
  149.             get { return Squares[row * 8 + col].Piece; }
  150.             set
  151.             {
  152.                 if (row < 0 || row > 7 || col < 0 || col > 7)
  153.                     return;
  154.                 var square = Squares[row * 8 + col];
  155.                 if (square.Piece != null)
  156.                 {
  157.                     Captured[square.Piece.IsBlack ? 0 : 1].Add(square.Piece);
  158.                 }
  159.                 square.Piece = value;
  160.             }
  161.         }
  162.  
  163.         public void HighlightMoves(ChessPiece piece)
  164.         {
  165.             var directions = new List<Tuple<int, int, int>>();
  166.             switch (piece.Type)
  167.             {
  168.                 case ChessPieceTypes.Pawn:
  169.                     directions.Add(Tuple.Create(piece.IsBlack ? 1 : -1, 0, 2));
  170.                     break;
  171.                 case ChessPieceTypes.Bishop:
  172.                     directions.AddRange(new[]
  173.                         {
  174.                             Tuple.Create(1, 1, 8),
  175.                             Tuple.Create(1, -1, 8),
  176.                             Tuple.Create(-1, 1, 8),
  177.                             Tuple.Create(-1, -1, 8)
  178.                         });
  179.                     break;
  180.                 case ChessPieceTypes.Tower:
  181.                     directions.AddRange(new[]
  182.                         {
  183.                             Tuple.Create(1, 0, 8),
  184.                             Tuple.Create(-1, 0, 8),
  185.                             Tuple.Create(0, 1, 8),
  186.                             Tuple.Create(0, -1, 8)
  187.                         });
  188.                     break;
  189.                 case ChessPieceTypes.Queen:
  190.                     directions.AddRange(new[]
  191.                         {
  192.                             Tuple.Create(1, 0, 8),
  193.                             Tuple.Create(-1, 0, 8),
  194.                             Tuple.Create(0, 1, 8),
  195.                             Tuple.Create(0, -1, 8),
  196.                             Tuple.Create(1, 1, 8),
  197.                             Tuple.Create(1, -1, 8),
  198.                             Tuple.Create(-1, 1, 8),
  199.                             Tuple.Create(-1, -1, 8)
  200.                         });
  201.                     break;
  202.                 case ChessPieceTypes.King:
  203.                     directions.AddRange(new[]
  204.                         {
  205.                             Tuple.Create(1, 0, 1),
  206.                             Tuple.Create(-1, 0, 1),
  207.                             Tuple.Create(0, 1, 1),
  208.                             Tuple.Create(0, -1, 1),
  209.                             Tuple.Create(1, 1, 1),
  210.                             Tuple.Create(1, -1, 1),
  211.                             Tuple.Create(-1, 1, 1),
  212.                             Tuple.Create(-1, -1, 1)
  213.                         });
  214.                     break;
  215.                 case ChessPieceTypes.Knight:
  216.                     directions.AddRange(new[]
  217.                         {
  218.                             Tuple.Create(2, 1, 1),
  219.                             Tuple.Create(2, -1, 1),
  220.                             Tuple.Create(-2, 1, 1),
  221.                             Tuple.Create(-2, -1, 1),
  222.                             Tuple.Create(1, 2, 1),
  223.                             Tuple.Create(-1, 2, 1),
  224.                             Tuple.Create(1, -2, 1),
  225.                             Tuple.Create(-1, -2, 1),
  226.                         });
  227.                     break;
  228.             }
  229.             foreach (var dir in directions)
  230.             {
  231.                 for (int gofer = 1; gofer < 8; gofer++)
  232.                 {
  233.                     if (gofer > dir.Item3) break;
  234.                     var calcRow = piece.Row + (dir.Item1 * gofer);
  235.                     var calcCol = piece.Column + (dir.Item2 * gofer);
  236.                     if (calcCol < 0 || calcCol > 7 || calcRow < 0 || calcRow > 7)
  237.                         continue;
  238.                     var idx = calcRow * 8 + calcCol;
  239.                     Squares[idx].IsHighlighted = true;
  240.                     if (Squares[idx].Piece != null)
  241.                     {
  242.                         break;
  243.                     }
  244.                 }
  245.             }
  246.         }
  247.  
  248.         public void ClearHighlights(ChessPiece piece)
  249.         {
  250.             foreach (var chessSquare in Squares)
  251.             {
  252.                 chessSquare.IsHighlighted = false;
  253.             }
  254.         }
  255.     }
  256.  
  257.     public class ChessSquare : INotifyPropertyChanged
  258.     {
  259.         private ChessPiece _piece;
  260.         private bool _isHighlighted;
  261.  
  262.         public bool IsBlack { get; set; }
  263.         public bool IsHighlighted
  264.         {
  265.             get { return _isHighlighted; }
  266.             set { _isHighlighted = value; OnPropertyChanged("IsHighlighted"); }
  267.         }
  268.         public ChessPiece Piece
  269.         {
  270.             get { return _piece; }
  271.             set { _piece = value; OnPropertyChanged("Piece");}
  272.         }
  273.  
  274.         public event PropertyChangedEventHandler PropertyChanged;
  275.         protected virtual void OnPropertyChanged(string propertyName)
  276.         {
  277.             PropertyChangedEventHandler handler = PropertyChanged;
  278.             if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
  279.         }
  280.     }
  281.  
  282.     public class ChessPiece: INotifyPropertyChanged
  283.     {
  284.         public ChessPiece(ChessBoard board)
  285.         {
  286.             Board = board;
  287.             PieceSelectedCommand = new ActionCommand(param => Board.HighlightMoves(param as ChessPiece));
  288.             PieceDeselectedCommand = new ActionCommand(param => Board.ClearHighlights(param as ChessPiece));
  289.         }
  290.  
  291.         private int _row;
  292.         private int _column;
  293.         private ICommand _pieceSelectedCommand;
  294.         private ICommand _pieceDeselectedCommand;
  295.         private bool _isBlack;
  296.         private ChessPieceTypes _type;
  297.  
  298.         public ChessBoard Board { get; private set; }
  299.         public bool IsBlack
  300.         {
  301.             get { return _isBlack; }
  302.             set { _isBlack = value; OnPropertyChanged("ImageSource"); }
  303.         }
  304.  
  305.         public ChessPieceTypes Type
  306.         {
  307.             get { return _type; }
  308.             set { _type = value; OnPropertyChanged("ImageSource"); }
  309.         }
  310.  
  311.         public ICommand PieceSelectedCommand
  312.         {
  313.             get { return _pieceSelectedCommand; }
  314.             private set { _pieceSelectedCommand = value; OnPropertyChanged("PieceSelectedCommand"); }
  315.         }
  316.  
  317.         public ICommand PieceDeselectedCommand
  318.         {
  319.             get { return _pieceDeselectedCommand; }
  320.             private set { _pieceDeselectedCommand = value; OnPropertyChanged("PieceDeselectedCommand"); }            
  321.         }
  322.  
  323.         public int Row
  324.         {
  325.             get { return _row; }
  326.             set { _row = value; OnPropertyChanged("Row");}
  327.         }
  328.  
  329.         public int Column
  330.         {
  331.             get { return _column; }
  332.             set { _column = value; OnPropertyChanged("Column");}
  333.         }
  334.  
  335.         public event PropertyChangedEventHandler PropertyChanged;
  336.         protected virtual void OnPropertyChanged(string propertyName)
  337.         {
  338.             PropertyChangedEventHandler handler = PropertyChanged;
  339.             if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
  340.         }
  341.  
  342.         public string ImageSource
  343.         {
  344.             get { return "../ChessPieces/" + (IsBlack ? "Black" : "White") + Type.ToString() + ".png"; }
  345.         }
  346.     }
  347.  
  348.     public enum ChessPieceTypes
  349.     {
  350.         Pawn,
  351.         Tower,
  352.         Knight,
  353.         Bishop,
  354.         Queen,
  355.         King,
  356.     }
  357.  
  358.     public class ExecuteCommandAction : TriggerAction<FrameworkElement>
  359.     {
  360.         public ICommand Command
  361.         {
  362.             get { return (ICommand)GetValue(CommandProperty); }
  363.             set { SetValue(CommandProperty, value); }
  364.         }
  365.  
  366.         public static readonly DependencyProperty CommandProperty =
  367.             DependencyProperty.Register("Command", typeof(ICommand), typeof(ExecuteCommandAction), new UIPropertyMetadata(null));
  368.  
  369.         public object CommandParameter
  370.         {
  371.             get { return (object)GetValue(CommandParameterProperty); }
  372.             set { SetValue(CommandParameterProperty, value); }
  373.         }
  374.  
  375.         public static readonly DependencyProperty CommandParameterProperty =
  376.             DependencyProperty.Register("CommandParameter", typeof(object), typeof(ExecuteCommandAction), new UIPropertyMetadata(null));
  377.  
  378.         protected override void Invoke(object parameter)
  379.         {
  380.             if (Command == null) return;
  381.             if (Command is RoutedCommand)
  382.             {
  383.                 var rc = Command as RoutedCommand;
  384.                 if (rc.CanExecute(CommandParameter, base.AssociatedObject))
  385.                 {
  386.                     rc.Execute(CommandParameter, base.AssociatedObject);
  387.                 }
  388.             }
  389.             else
  390.             {
  391.                 if (Command.CanExecute(CommandParameter))
  392.                     Command.Execute(CommandParameter);
  393.             }
  394.         }
  395.     }
  396. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement