Advertisement
Hellboyandso

TileviewModel

May 23rd, 2015
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Reversi.Cells;
  7. using Reversi.Domain;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10.  
  11. namespace ReversiGUI.ViewModels
  12. {
  13.     class TileViewModel
  14.     {
  15.         private readonly ISquare square;
  16.         private readonly ICommand placeStoneCommand;
  17.         private readonly Game game;
  18.         private readonly ICell<String> colorCell;
  19.  
  20.         public TileViewModel(ISquare square, Game game)
  21.         {
  22.             this.square = square;
  23.             this.game = game;
  24.             placeStoneCommand = new PlaceStoneCommando(this);
  25.             colorCell = Cell.Derived(square.Owner, square.IsValidMove, DeriveKleur);
  26.         }
  27.  
  28.         public ICell<String> ColorCell
  29.         {
  30.             get { return colorCell; }
  31.         }
  32.  
  33.         private String DeriveKleur(Player owner, bool isValidMove)
  34.         {
  35.             if ((square.Owner.Value) == null)
  36.             {
  37.                 if (square.IsValidMove.Value)
  38.                 {
  39.                     return "ValidMove";
  40.                 }
  41.                 else
  42.                 {
  43.                     return "NoTile";
  44.                 }
  45.             }
  46.             else
  47.             {
  48.                 if ((square.Owner.Value) == Player.ONE)
  49.                 {
  50.                     return "PlayerOne";
  51.                 }
  52.                 else
  53.                 {
  54.                     return "PlayerTwo";
  55.                 }
  56.             }
  57.         }
  58.  
  59.         public ICell<bool> IsValidMove
  60.         {
  61.             get
  62.             {
  63.                 return square.IsValidMove;
  64.             }
  65.         }
  66.  
  67.         public void PlaceStone()
  68.         {
  69.             if (square.IsValidMove.Value)
  70.             {
  71.                 square.PlaceStone();
  72.             }
  73.         }
  74.  
  75.         public ICommand PlaceStoneCommand
  76.         {
  77.             get
  78.             {
  79.                 return placeStoneCommand;
  80.             }
  81.         }
  82.  
  83.         private class PlaceStoneCommando : ICommand
  84.         {
  85.             private readonly TileViewModel model;
  86.  
  87.             public PlaceStoneCommando(TileViewModel model)
  88.             {
  89.                 this.model = model;
  90.                 model.IsValidMove.PropertyChanged += IsValidMove_PropertyChanged;
  91.             }
  92.  
  93.             void IsValidMove_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  94.             {
  95.                 if (CanExecuteChanged != null)
  96.                     CanExecuteChanged(this, new EventArgs());
  97.             }
  98.  
  99.             public bool CanExecute(object parameter)
  100.             {
  101.                 return model.IsValidMove.Value;
  102.                 //return true;
  103.             }
  104.  
  105.             public event EventHandler CanExecuteChanged;
  106.  
  107.             public void Execute(object parameter)
  108.             {
  109.                 model.PlaceStone();
  110.             }
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement