Skip to main content

A board game in MVVM Part 4: The ViewModel

This is a continuation of my experience creating a quick board game in silverlight 5
Original Post
Part 1 - The Model
Part 2 - The AI
Part 3 - The View
Part 3b - Commands and Converters
Play the Game

In MVVM the viewmodel mediates between the view and the model exposing the model or encapsulates interaction with the model that the view requires but shouldn't be performing on its own such as the user interaction.

The way I created the ViewModel here in Viking is fairly simple. It creates and exposes the main Game object to the view, and tells the game object when the user is trying to perform an action. Aside from that, it's dedicated to the user interaction. In the case of Viking, it's primary user interaction comes into play when the user:

  • Selects a piece is move (or cancels a current selection)
  • Selects a square to move an active piece to
  • Changes the player types between human and AI
  • Restarts the game

These can be seen in the following code:

  1.     public class MainViewModel : BasePropertyChanged
  2.     {
  3.         private DelegateCommand<Piece> activatePieceCommand;
  4.  
  5.         private Game game;
  6.  
  7.         private DelegateCommand<Square> makeMoveCommand;
  8.  
  9.         private DelegateCommand<object> restartGameCommand;
  10.  
  11.         private DelegateCommand<Player> togglePlayerTypeCommand;
  12.  
  13.         public MainViewModel()
  14.         {
  15.             Game = new Game();
  16.         }
  17.  
  18.         public DelegateCommand<Piece> ActivatePieceCommand
  19.         {
  20.             get
  21.             {
  22.                 if (this.activatePieceCommand == null)
  23.                 {
  24.                     this.activatePieceCommand = new DelegateCommand<Piece>(ActivatePiece);
  25.                 }
  26.  
  27.                 return this.activatePieceCommand;
  28.             }
  29.         }
  30.  
  31.         public Player AttackerPlayer
  32.         {
  33.             get
  34.             {
  35.                 return Player.Attacker;
  36.             }
  37.         }
  38.  
  39.         public Player DefenderPlayer
  40.         {
  41.             get
  42.             {
  43.                 return Player.Defender;
  44.             }
  45.         }
  46.  
  47.         public Game Game
  48.         {
  49.             get
  50.             {
  51.                 return this.game;
  52.             }
  53.  
  54.             set
  55.             {
  56.                 if (value != game)
  57.                 {
  58.                     game = value;
  59.                     RaisePropertyChanged(() => Game);
  60.                 }
  61.             }
  62.         }
  63.  
  64.         public DelegateCommand<Square> MakeMoveCommand
  65.         {
  66.             get
  67.             {
  68.                 if (this.makeMoveCommand == null)
  69.                 {
  70.                     this.makeMoveCommand = new DelegateCommand<Square>(MakeMove);
  71.                 }
  72.  
  73.                 return this.makeMoveCommand;
  74.             }
  75.         }
  76.  
  77.         public DelegateCommand<object> RestartGameCommand
  78.         {
  79.             get
  80.             {
  81.                 if (this.restartGameCommand == null)
  82.                 {
  83.                     this.restartGameCommand = new DelegateCommand<object>(RestartGame);
  84.                 }
  85.  
  86.                 return this.restartGameCommand;
  87.             }
  88.         }
  89.  
  90.         public DelegateCommand<Player> TogglePlayerTypeCommand
  91.         {
  92.             get
  93.             {
  94.                 if (this.togglePlayerTypeCommand == null)
  95.                 {
  96.                     this.togglePlayerTypeCommand = new DelegateCommand<Player>(TogglePlayerType);
  97.                 }
  98.  
  99.                 return this.togglePlayerTypeCommand;
  100.             }
  101.         }
  102.  
  103.         public void ActivatePiece(Piece targetPiece)
  104.         {
  105.             if (Game.IsActive)
  106.             {
  107.                 if (targetPiece.IsMoveable && !targetPiece.IsActive)
  108.                 {
  109.                     targetPiece.IsActive = true;
  110.                     Game.Board.ActivePiece = targetPiece;
  111.                 }
  112.                 else if (targetPiece.IsMoveable)
  113.                 {
  114.                     targetPiece.IsActive = false;
  115.                     Game.Board.ActivePiece = null;
  116.                     Game.Board.SetMoveablePieces(Game.CurrentPlayer);
  117.                 }
  118.             }
  119.         }
  120.  
  121.         public void MakeMove(Square targetSquare)
  122.         {
  123.             if (Game.IsActive)
  124.             {
  125.                 Game.MovePiece(targetSquare);
  126.             }
  127.         }
  128.  
  129.         public void RestartGame(object parameter = null)
  130.         {
  131.             Game.Restart();
  132.         }
  133.  
  134.         public void TogglePlayerType(Player playerToToggle)
  135.         {
  136.             if (playerToToggle == Player.Attacker)
  137.             {
  138.                 Game.NextAttacker = Game.NextAttacker == PlayerType.Human ? PlayerType.AI : PlayerType.Human;
  139.                 if (Game.NextAttacker == PlayerType.AI && Game.NextDefender == PlayerType.AI)
  140.                 {
  141.                     Game.NextDefender = PlayerType.Human;
  142.                 }
  143.             }
  144.             else
  145.             {
  146.                 Game.NextDefender = Game.NextDefender == PlayerType.Human ? PlayerType.AI : PlayerType.Human;
  147.                 if (Game.NextAttacker == PlayerType.AI && Game.NextDefender == PlayerType.AI)
  148.                 {
  149.                     Game.NextAttacker = PlayerType.Human;
  150.                 }
  151.             }
  152.         }
  153.     }

Comments

great one

Different web browsers have different features for writing help uk encoding various programs.Many web developers do not know what exactly do in this situation. is one of their desired destinations to make the project successful just in time

I experienced every one

I experienced every one of your progressions and this looks exceptionally cool. Paperdome I like the progressions you have included. I can see that you have cleared every one of the bugs as well. Continue enhancing your product. Tell me when you refresh it once more.

BlackMen

Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can't wait to read lots of your posts. cccam server

Vape and ecig

Thanks for the valuable information and insights you have so provided here and They are running a 33% off site wide sale. Check it our here:Vape and ecig

Wow what a Great Information

Wow what a Great Information about World Day its very nice informative post. thanks for the post. best smelling body wash

I love this blog!! The flash

I love this blog!! The flash up the top is awesome!!Best diets work

I like this post,And I guess

I like this post,And I guess that they having fun to read this post,they shall take a good site to make a information,thanks for sharing it to me.bettaka.com

Most of the time I don’t make

Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post! Hundred Palms Residences Hougang

Good article knowledge

Good article knowledge gaining article. This post is really the best on this valuable topic.top fat burners

I admit, I have not been on

I admit, I have not been on this web page in a long time... however it was another joy to see It is such an important topic and ignored by so many, even professionals. I thank you to help making people more aware of possible issues. At the Open Reviews

RE:

In the meantime, this is just a little investigation of what's conceivable with MVVM and Android testing. With the assistance of capable Android engineers like you, this venture can turn out to be boundlessly better. So if it's not too much trouble leave reactions, begin an exchange, and I'd be extremely respected and complimented to see this venture get iterated and Essay Editing Service help future ventures!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <asp>, <c>, <cpp>, <cs>, <css>, <drupal5>, <drupal6>, <html4strict>, <java>, <javascript>, <jquery>, <php>, <python>, <ruby>, <sql>, <xml>. The supported tag styles are: <foo>, [foo]. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.