Hellboyandso

TileviewModel2

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