I have long wanted to start learning Silverlight, I began to read literature, tried to delve into concepts, patterns, but more and more somehow in the abstract, in theory. In practice, I did not try to write a real Silverlight program.
But recently, in the vastness of Habra, the article “programming Reversi on Python” slipped. Looking at the code, I
was horrified, I immediately got a motivator involved. Not that I don't like Python, I just love C # very much.
What you see was written in 1 working day by a person who has zero practical experience in developing applications in WPF / Silverlight.
Introduction
For those who do not want to delve into how the program works, and wants to see it live, scroll down the article down - there is a link. The game requires Silverlight 3, I hope you are not included in those 43% of Internet users who have not installed or supported the plugin (statistics are taken from
riastats.com ).
')
Training
To develop the game, I used VS.NET 2010 RC with Silverlight Tools 4 (there is one hack, how to force the tools to be installed, the link is easily googled). The project itself is written in Silverlight 3, so as not to strain those who are not in a hurry to install all the beta versions. Those. VS.NET 2008 + Silverlight Tools 3 + Expression Blend 3. Everything you need and not only can be downloaded from here:
silverlight.net/getstartedFeatures
The game involves the following principles of Silverlight 3 and C # in particular:
- Decision logic is in a separate abstract class. The idea with the table of the importance of the cells was adopted from the Python implementation.
- UI synchronization - since the game logic is running in the background thread, all delegates and events must be properly synchronized into the UI thread.
- Synchronization primitives - when the game requires user input, the synchronization primitives of the threads are used - AutoResetEvent.
- LINQ - search for the best AI move is implemented using LINQ extensions.
- Out-of-Browser - the game can be installed locally and play without an internet connection.
- Automatic Updates - if you have an internet connection, the game is updated. The size of the game is currently 14 kilobytes.
- MVVM - game status, score, etc. attached to the model of the game on the principle of Model-View-ViewModel.
- VisualStateManager is used to give a certain life to the interface by animating chips.
- Localization - all game messages are localized using resources in Russian and English. All texts are directly linked to resources, i.e. If you have Russian regional settings, the interface will be displayed in Russian.
How the game works
There is a class Game - a game that starts a background thread in which the moves of players - objects of the GamePlayer class - will be polled. This is an abstract class, it is developed by two others - GameAI - the game decision-making module and GamePC - the logic of accepting a turn from a player.
At the start of a new game, depending on the selected item from the combo, a new game object is constructed, to which various combinations of GameAI and GamePC player objects are transferred. For the first time watch the battle of two AI - transcendental, if not more :)
GameAI makes decisions instantly, so he has a standard delay of 100 ms. At first I wrote the game without using background processes, but it began to strain me that AI was making a move so quickly that I did not have time to notice my moves. When I inserted Thread.Sleep into the main UI stream, the animation hit. I am not a guru in Silverlight, Google didn’t google the answer, so it was decided to carry all the logic of the game into the background thread, and unload the UI stream for good.
When transferring events to the background stream - Silverlight began to kick, just like WinForms. I had to skip everything through the global UI dispatcher.
GameAI is looking for the best move, first by the importance of the cells, then by the win. When there are several optimal options, the randomness factor is connected, the so-called. soul. :) By the way, without this “soul”, White always won. Now AI vs AI is the result of winning 50 to 50.
Out-of-Browser application is done with one mouse click in the options of the project. Automatic updating of the application is also elementary - the only challenge in the application's designer is that if a new version of the game is posted on the server, it will be downloaded to the local cache, the second launch of the application will pick up an already updated version. Quadratisch, praktisch, gut :)
Animation
With the animation at first it was not all smooth. I wanted to make the Ellipse class take on various visual states. But it turned out that Ellipse is not inherited from the Control class, therefore it is not affected by the visual state. I had to make my own control of Cell. By attaching two dependency properties Stroke and Fill to this class, which simply managed the nested ellipse, I was able to make it look like an ellipse, while remaining control capable of animating myself through VisualStateManager.
In Blend, I added two states to the Cell, Normal and Visible classes. In Visible, a visual transition is added - the scale for a second changes from 0.5 to 1 using the elastic smoothing function. It turned out funny.
I will say this, in Blend-e, I was busy most of all, until I figured out a bit with StoryBoards, States, Transitions. I even had to watch a couple of video clips from the Blend for Dummies series. It really helped.
Localization
In the blog, Tim Heuer found a
post on how to link directly to strings from resources (resx). Half an hour trying to figure out why it does not work for me.
It turned out that it is necessary to read more closely. It says that this is a known bug. When the visibility of a class of resources changes, the constructor’s visibility does not change, if the class is public and the constructor is not, then it is impossible to create such a class through the Activator in Silverlight. But my friend Tim says that the bug will be fixed, but for now manually we climb in the C # class of resources and change internal to public. Do not forget that after adding new lines to resources - the file will be generated in a new way, and the operation “public constructor” will have to be repeated.
findings
I learned a lot of important practical experience. I enjoyed the
pampering of working with Blend (I, alas, was no longer destined to become a designer). It turned out to be quite convenient to develop. Of course, WinForms is far away, but worth it.
The nice thing is that a lot of the “big” framework turned out to be available, some methods were simplified, but on the whole - everything is the same.
Chrome is my standard browser, it’s not good friends with the studio in debug mode. Therefore, to debug Silverlight IE8 - what the doctor ordered.
It was also pleasantly struck by the size of the finished application - 14 KB.
According to MS - the game should work under Windows XP, Vista, 7, Mac OS X, Xbox, Windows Phone 7 Series. We’ll see what MIX brings next week.
Result
Play:
tytsSources:
TydytsThe code is more or less commented, but if you have any questions, feel free to ask in the comments.
So, what is next?
The plans are to improve the animation, to make the field proportionally stretching across the entire browser, to sew personalized game tricks into AI ...
Well, what you offer,% username%
UPD: I figured out a bit with Silverlight, added a new game dialogue, twirled chip patterns, it looks much better. Cut the source code into classes.