17 December 2009

XNA 2d animation

Added animation to pieces. They now glide from one square to another smoothly. At the start of the animation I make a note of the game time, so I know when the animation started. I add a second to calculate when it should end. Then I get the x and y coordinates of the start and end positions.

Then later on, in the display function, work out how far the piece should have moved between the start and end points given the current game time and display a piece at that location.

When the animation starts I make the call to move the piece so I have to display over the destination square with a sprite to show one fewer pieces than are actually there until the animation ends, when the moving piece arrives at it's destination.

The animation takes the same length of time no matter how far the piece is going to move, and the movement is linear, there is no easing into and out of the move. I am developing on a netbook, and I think the animation would be smoother on a desktop, but it looks pretty good.

15 December 2009

XNA, the fun bit

I worked on improving the graphics, creating help screens, realising that twiddling with images could extend indefinitely, so have made a list of things to change and will stop when it looks good enough. Also decided that I really must add some animation of pieces so that they glide from one position to another rather than teleporting.

10 December 2009

Changing the rules

I played a few games and realised I wanted to change the rules. Instead of capturing the opponents pieces you can send them back to their start square. This improves the game play enormously, but does mean that the weightings I generated earlier are all invalid.

08 December 2009

XNA game

I hooked up the existing code to generate the computer's move and make it.

There are still no delays yet, so after your turn the dice get rolled and the move is made instantly, then the dice are rolled again instantly and it's your turn. I need to think of a way to introduce a delay, possibly even an animation to move the pieces smoothly and give the human player a chance to appreciate what is happening.

07 December 2009

Highlighting moves

I added highlighting to show the square you are hovering over and the square the piece would move to with the current dice. To do this I am using PNG files with transparent backgrounds as follows

Background



Piece



Highlight



Put it all together



and for the destination cell

Background + Highlight


These only show up if there is a legal move for the piece hovered over using the current dice.

To display them I am adding them to a spriteBatch:

// start collecting sprites
spriteBatch.Begin();
...// where to display the sprite
Vector2 tempVector = new Vector2(x, y);
// display piece (a Texture2D)
spriteBatch.Draw(piece, tempVector, Color.White);
...
// finished collecting sprites
spriteBatch.End();

That's all it takes to display a 2D board game, lots of sprites displayed in the right place. Not too difficult.

05 December 2009

Moving pieces

I captured the mouse position and added the ability to select a piece to move, checking, of course, that it was a legal move.

This was easy to add as all the game logic for deciding legality of moves already existed in the console app. This was partly why I wanted to get the game play sorted out in a console app before trying to tackle XNA.

04 December 2009

Searching the rainforests

If you have been using Bing or Yahoo for search, consider using Ecosia instead.

It's a search engine created by the WWF in conjunction with Yahoo and Bing. It has the advantage that most of the money taken from advertising goes to protect the rainforest. According to the search page they have already managed to protect 108,000 square yards. The more people use the search engine the faster that figure will grow.

Obviously, it isn't enough and what is really needed is for Governments to act. I know small changes are not enough, but it is time to do whatever we can.

XNA Conference

Tomorrow is the first XBLIGUK conference in Birmingham, for XNA developers. I am not going to be there, but I hope it is a success and that it is the first of many. The agenda looks very promising.

02 December 2009

Putting the code into XNA, part 2

I added a GameMode class to handle the succession of different modes that happen within the game, starting with the following :-

Player 1 rolls the dice,
Player 1 enters a move,
Make player 1's first move,
Player 1 enters second move,
Make player 1's second move,
Player 2 rolls the dice,
Player 2 generates 2 moves,
Make player 2's 1st move,
Make player 2's 2nd move

After player 2 has moved the game loops back to player 1's turn.

I found it best to keep to keep the Draw method just for displaying the board and all the changes to the gamemode in the Update method of the game loop.

So the game Update method contains a switch statement similar to this...

switch (GameMode.State)
{
case GameMode.Player0RollingDice:
{
Dice.Roll();
GameMode.State = GameMode.Player0Input1;
break;
}
case...

01 December 2009

Putting the code into XNA


I started by getting XNA to display the board, this is easy
as it is just a static background so I used the spriteBatch.
Vector2 tempVector = new Vector2(0, 0);
spriteBatch.Draw(background, tempVector, Color.White);

I also needed to resize the window in the Initialize() method...
this.graphics.PreferredBackBufferWidth = 224;
this.graphics.PreferredBackBufferHeight = 471;
this.graphics.ApplyChanges();

Then display the pieces on the board in their starting positions and finally the dice. It is only a 2d game but this seems really easy.
I needed to get mouse input. I used Mouse.GetState().
If the game was ever going to run on the XBox then this wouldn't be enough. Apparently they forgot to add a mouse when they designed the XBox. What were they thinking? I'd need to add support for a GamePad. I don't own an XBox so I am not too bothered about this.

KeyboardState keyboard = Keyboard.GetState();
MouseState mouse = Mouse.GetState();

Now, I can use mouse.X, mouse.Y and compare mouse.LeftButton with ButtonState.Pressed.

29 November 2009

Alpha beta tree pruning

A method where you can limit the number of nodes that the computer looks ahead in a game by stopping if you can prove that a particular move would be worse than one you have already evaluated. I think.

The wikipedia article leads me to conclude that some people
have spent a lot of time thinking about games.

Maybe next time I write a game I'll use it.

22 November 2009

XNA game

I tried to improve the genetic programming approach.

Player One uses a set of weightings giving different importance to various ways of evaluating a position. Then generate ten random sets of weightings around Player One's settings for Player Two to use. Get Player One to play twenty games against each of the sets of ten mutated values. If there was a clear winner then let Player One adopt those weightings and continue with ten new mutated sets based around them. If there was no clear winner then increase the variability slightly and try again with new mutations.

I left it running over night to see if it would improve dramatically.

It seems to have varied the weightings a lot but not really in one direction. Looking at the logs I can see one particular value would go up then down, repeatedly. This could mean there are different approaches to playing and that some of them have slight advantages over others, but not a significant advantage.

Or it could be like stone - paper - scissors played as if there was a best choice. Playing stone all the time leads your opponent to play paper, but if you adopt paper as the winning solution then they play scissors. When you play scissors they play stone and together you proceed around and around.

Not sure what to do about this, but I've added "Read a book on genetic algorithms" to my ever increasing to-do list along with "find out about alpha-beta pruning next time" (as recommended by El Walrus).

XBLIG-UK conference

There is an XBLIG-UK conference in Birmingham on Saturday 5th December. It is organised, I think, by nxtgenug.

This looks like it will be a jolly good event, lots of interesting sessions from people who are experienced with XNA.

Sadly, I shall not be there as I have other commitments that day. So, I hope there will be another.

19 November 2009

Is it improving?

Checking the game to see if the computer player improved Switched back to human vs computer mode and played a game. I was pleased that it actually won a game. I wasn't trying hard to win, but this is progress.

So far the approach seems to have worked well. The moves it makes are occasionally sensible ones that I would make myself.

Sometimes it makes moves that I wouldn't.

17 November 2009

XNA game. Evolving a better player

I've changed the code so the computer can play against itself. I gave each of the two computer players different weights for the evaluation. This is like having two human players with different playing styles.

Changed the game loop so that they play matches of twenty games against each other using their current weightings. The loser of a match changes their weightings by some small random amount. Then they play another match to see who wins.

I left it to play 5,000 matches to see if it improves. Over time, it ought to evolve into a better player.

16 November 2009

XNA game development

Trying to improve the computer's game:
I gave the software a way of evaluating a position. So some moves will result in a position that achieves a high score, such as capturing pieces, and some a low score, such as having pieces taken or leaving them exposed.

I am working out what to evaluate by noticing what I think is a good position when I am playing.

I gave random weights to each of the methods of evaluating the position, since I can't really say what priority I actually give to the things I consider.

The approach is to sum the results from each of the weighted methods for each possible position and use those to pick the best move.

The computer still loses every time.