Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <Window x:Class="Chess.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:Chess" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
- Title="ChessBoard" Height="300" Width="300">
- <Window.Resources>
- <DataTemplate DataType="{x:Type local:ChessPiece}">
- <Image Source="{Binding ImageSource}" Width="32" Height="32">
- <i:Interaction.Triggers>
- <i:EventTrigger EventName="MouseEnter">
- <local:ExecuteCommandAction Command="{Binding PieceSelectedCommand}" CommandParameter="{Binding}"/>
- </i:EventTrigger>
- <i:EventTrigger EventName="MouseLeave">
- <local:ExecuteCommandAction Command="{Binding PieceDeselectedCommand}" CommandParameter="{Binding}"/>
- </i:EventTrigger>
- </i:Interaction.Triggers>
- </Image>
- </DataTemplate>
- </Window.Resources>
- <Grid>
- <ItemsControl x:Name="_theBoard" ItemsSource="{Binding Squares}">
- <ItemsControl.ItemsPanel>
- <ItemsPanelTemplate>
- <UniformGrid Rows="8" Columns="8"/>
- </ItemsPanelTemplate>
- </ItemsControl.ItemsPanel>
- <ItemsControl.ItemTemplate>
- <DataTemplate>
- <Grid>
- <Rectangle Fill="White" x:Name="SquareColor" Opacity=".5"/>
- <ContentControl Content="{Binding Piece}"/>
- </Grid>
- <DataTemplate.Triggers>
- <DataTrigger Binding="{Binding IsBlack}" Value="True">
- <Setter TargetName="SquareColor" Property="Fill" Value="Black"/>
- </DataTrigger>
- <DataTrigger Binding="{Binding IsHighlighted}" Value="True">
- <Setter TargetName="SquareColor" Property="Fill" Value="Yellow"/>
- </DataTrigger>
- </DataTemplate.Triggers>
- </DataTemplate>
- </ItemsControl.ItemTemplate>
- </ItemsControl>
- </Grid>
- </Window>
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Threading;
- using System.Windows;
- using System.Windows.Input;
- using System.Windows.Interactivity;
- using Microsoft.Expression.Interactivity.Core;
- namespace Chess
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow
- {
- public ChessBoard Board { get; set; }
- public MainWindow()
- {
- Board = new ChessBoard();
- InitializeComponent();
- DataContext = Board;
- NewGame();
- }
- private void NewGame()
- {
- Board[0, 0] = new ChessPiece(Board) { Row = 0, Column = 0, Type = ChessPieceTypes.Tower, IsBlack = true };
- Board[0, 1] = new ChessPiece(Board) { Row = 0, Column = 1, Type = ChessPieceTypes.Knight, IsBlack = true };
- Board[0, 2] = new ChessPiece(Board) { Row = 0, Column = 2, Type = ChessPieceTypes.Bishop, IsBlack = true };
- Board[0, 3] = new ChessPiece(Board) { Row = 0, Column = 3, Type = ChessPieceTypes.Queen, IsBlack = true };
- Board[0, 4] = new ChessPiece(Board) { Row = 0, Column = 4, Type = ChessPieceTypes.King, IsBlack = true };
- Board[0, 5] = new ChessPiece(Board) { Row = 0, Column = 5, Type = ChessPieceTypes.Bishop, IsBlack = true };
- Board[0, 6] = new ChessPiece(Board) { Row = 0, Column = 6, Type = ChessPieceTypes.Knight, IsBlack = true };
- Board[0, 7] = new ChessPiece(Board) { Row = 0, Column = 7, Type = ChessPieceTypes.Tower, IsBlack = true };
- Enumerable.Range(0, 8).Select(x => new ChessPiece(Board)
- {
- Row = 1,
- Column = x,
- IsBlack = true,
- Type = ChessPieceTypes.Pawn
- }).ToList().ForEach(p => Board[p.Row, p.Column] = p);
- Board[7, 0] = new ChessPiece(Board) { Row = 7, Column = 0, Type = ChessPieceTypes.Tower, IsBlack = false };
- Board[7, 1] = new ChessPiece(Board) { Row = 7, Column = 1, Type = ChessPieceTypes.Knight, IsBlack = false };
- Board[7, 2] = new ChessPiece(Board) { Row = 7, Column = 2, Type = ChessPieceTypes.Bishop, IsBlack = false };
- Board[7, 3] = new ChessPiece(Board) { Row = 7, Column = 3, Type = ChessPieceTypes.Queen, IsBlack = false };
- Board[7, 4] = new ChessPiece(Board) { Row = 7, Column = 4, Type = ChessPieceTypes.King, IsBlack = false };
- Board[7, 5] = new ChessPiece(Board) { Row = 7, Column = 5, Type = ChessPieceTypes.Bishop, IsBlack = false };
- Board[7, 6] = new ChessPiece(Board) { Row = 7, Column = 6, Type = ChessPieceTypes.Knight, IsBlack = false };
- Board[7, 7] = new ChessPiece(Board) { Row = 7, Column = 7, Type = ChessPieceTypes.Tower, IsBlack = false };
- Enumerable.Range(0, 8).Select(x => new ChessPiece(Board)
- {
- Row = 6,
- Column = x,
- IsBlack = false,
- Type = ChessPieceTypes.Pawn
- }).ToList().ForEach(p => Board[p.Row, p.Column] = p);
- new Thread(() =>
- {
- Thread.Sleep(2000);
- _theBoard.Dispatcher.Invoke((Action) (() =>
- {
- var pawn = Board[6, 3];
- pawn.Row = 5;
- pawn.Column = 3;
- Board[5, 3] = pawn;
- Board[6, 3] = null;
- }));
- }).Start();
- }
- }
- public class ChessBoard
- {
- public ChessBoard()
- {
- Captured = new[]{ new List<ChessPiece>(), new List<ChessPiece>()};
- Squares = new ChessSquare[64];
- for (int row = 0; row < 8; row++)
- {
- for (int col = 0; col < 8; col++)
- {
- bool isblack = (row + col) % 2 == 0;
- Squares[row * 8 + col] = new ChessSquare { IsBlack = isblack };
- }
- }
- }
- public List<ChessPiece>[] Captured { get; private set; }
- public ChessSquare[] Squares { get; private set; }
- public ChessPiece this[int row, int col]
- {
- get { return Squares[row * 8 + col].Piece; }
- set
- {
- if (row < 0 || row > 7 || col < 0 || col > 7)
- return;
- var square = Squares[row * 8 + col];
- if (square.Piece != null)
- {
- Captured[square.Piece.IsBlack ? 0 : 1].Add(square.Piece);
- }
- square.Piece = value;
- }
- }
- public void HighlightMoves(ChessPiece piece)
- {
- var directions = new List<Tuple<int, int, int>>();
- switch (piece.Type)
- {
- case ChessPieceTypes.Pawn:
- directions.Add(Tuple.Create(piece.IsBlack ? 1 : -1, 0, 2));
- break;
- case ChessPieceTypes.Bishop:
- directions.AddRange(new[]
- {
- Tuple.Create(1, 1, 8),
- Tuple.Create(1, -1, 8),
- Tuple.Create(-1, 1, 8),
- Tuple.Create(-1, -1, 8)
- });
- break;
- case ChessPieceTypes.Tower:
- directions.AddRange(new[]
- {
- Tuple.Create(1, 0, 8),
- Tuple.Create(-1, 0, 8),
- Tuple.Create(0, 1, 8),
- Tuple.Create(0, -1, 8)
- });
- break;
- case ChessPieceTypes.Queen:
- directions.AddRange(new[]
- {
- Tuple.Create(1, 0, 8),
- Tuple.Create(-1, 0, 8),
- Tuple.Create(0, 1, 8),
- Tuple.Create(0, -1, 8),
- Tuple.Create(1, 1, 8),
- Tuple.Create(1, -1, 8),
- Tuple.Create(-1, 1, 8),
- Tuple.Create(-1, -1, 8)
- });
- break;
- case ChessPieceTypes.King:
- directions.AddRange(new[]
- {
- Tuple.Create(1, 0, 1),
- Tuple.Create(-1, 0, 1),
- Tuple.Create(0, 1, 1),
- Tuple.Create(0, -1, 1),
- Tuple.Create(1, 1, 1),
- Tuple.Create(1, -1, 1),
- Tuple.Create(-1, 1, 1),
- Tuple.Create(-1, -1, 1)
- });
- break;
- case ChessPieceTypes.Knight:
- directions.AddRange(new[]
- {
- Tuple.Create(2, 1, 1),
- Tuple.Create(2, -1, 1),
- Tuple.Create(-2, 1, 1),
- Tuple.Create(-2, -1, 1),
- Tuple.Create(1, 2, 1),
- Tuple.Create(-1, 2, 1),
- Tuple.Create(1, -2, 1),
- Tuple.Create(-1, -2, 1),
- });
- break;
- }
- foreach (var dir in directions)
- {
- for (int gofer = 1; gofer < 8; gofer++)
- {
- if (gofer > dir.Item3) break;
- var calcRow = piece.Row + (dir.Item1 * gofer);
- var calcCol = piece.Column + (dir.Item2 * gofer);
- if (calcCol < 0 || calcCol > 7 || calcRow < 0 || calcRow > 7)
- continue;
- var idx = calcRow * 8 + calcCol;
- Squares[idx].IsHighlighted = true;
- if (Squares[idx].Piece != null)
- {
- break;
- }
- }
- }
- }
- public void ClearHighlights(ChessPiece piece)
- {
- foreach (var chessSquare in Squares)
- {
- chessSquare.IsHighlighted = false;
- }
- }
- }
- public class ChessSquare : INotifyPropertyChanged
- {
- private ChessPiece _piece;
- private bool _isHighlighted;
- public bool IsBlack { get; set; }
- public bool IsHighlighted
- {
- get { return _isHighlighted; }
- set { _isHighlighted = value; OnPropertyChanged("IsHighlighted"); }
- }
- public ChessPiece Piece
- {
- get { return _piece; }
- set { _piece = value; OnPropertyChanged("Piece");}
- }
- public event PropertyChangedEventHandler PropertyChanged;
- protected virtual void OnPropertyChanged(string propertyName)
- {
- PropertyChangedEventHandler handler = PropertyChanged;
- if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- public class ChessPiece: INotifyPropertyChanged
- {
- public ChessPiece(ChessBoard board)
- {
- Board = board;
- PieceSelectedCommand = new ActionCommand(param => Board.HighlightMoves(param as ChessPiece));
- PieceDeselectedCommand = new ActionCommand(param => Board.ClearHighlights(param as ChessPiece));
- }
- private int _row;
- private int _column;
- private ICommand _pieceSelectedCommand;
- private ICommand _pieceDeselectedCommand;
- private bool _isBlack;
- private ChessPieceTypes _type;
- public ChessBoard Board { get; private set; }
- public bool IsBlack
- {
- get { return _isBlack; }
- set { _isBlack = value; OnPropertyChanged("ImageSource"); }
- }
- public ChessPieceTypes Type
- {
- get { return _type; }
- set { _type = value; OnPropertyChanged("ImageSource"); }
- }
- public ICommand PieceSelectedCommand
- {
- get { return _pieceSelectedCommand; }
- private set { _pieceSelectedCommand = value; OnPropertyChanged("PieceSelectedCommand"); }
- }
- public ICommand PieceDeselectedCommand
- {
- get { return _pieceDeselectedCommand; }
- private set { _pieceDeselectedCommand = value; OnPropertyChanged("PieceDeselectedCommand"); }
- }
- public int Row
- {
- get { return _row; }
- set { _row = value; OnPropertyChanged("Row");}
- }
- public int Column
- {
- get { return _column; }
- set { _column = value; OnPropertyChanged("Column");}
- }
- public event PropertyChangedEventHandler PropertyChanged;
- protected virtual void OnPropertyChanged(string propertyName)
- {
- PropertyChangedEventHandler handler = PropertyChanged;
- if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
- }
- public string ImageSource
- {
- get { return "../ChessPieces/" + (IsBlack ? "Black" : "White") + Type.ToString() + ".png"; }
- }
- }
- public enum ChessPieceTypes
- {
- Pawn,
- Tower,
- Knight,
- Bishop,
- Queen,
- King,
- }
- public class ExecuteCommandAction : TriggerAction<FrameworkElement>
- {
- public ICommand Command
- {
- get { return (ICommand)GetValue(CommandProperty); }
- set { SetValue(CommandProperty, value); }
- }
- public static readonly DependencyProperty CommandProperty =
- DependencyProperty.Register("Command", typeof(ICommand), typeof(ExecuteCommandAction), new UIPropertyMetadata(null));
- public object CommandParameter
- {
- get { return (object)GetValue(CommandParameterProperty); }
- set { SetValue(CommandParameterProperty, value); }
- }
- public static readonly DependencyProperty CommandParameterProperty =
- DependencyProperty.Register("CommandParameter", typeof(object), typeof(ExecuteCommandAction), new UIPropertyMetadata(null));
- protected override void Invoke(object parameter)
- {
- if (Command == null) return;
- if (Command is RoutedCommand)
- {
- var rc = Command as RoutedCommand;
- if (rc.CanExecute(CommandParameter, base.AssociatedObject))
- {
- rc.Execute(CommandParameter, base.AssociatedObject);
- }
- }
- else
- {
- if (Command.CanExecute(CommandParameter))
- Command.Execute(CommandParameter);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement