Making Kids Creative with Computers

Hornsea Lights

This seems to be the year that the education system woke up to the fact that getting kids to make things with computers is a good idea. Excellent. I’m sure it was my blog post yesterday that tipped the balance….

Anyhoo, if you’ve got kids and you want to get them into programming here are my tips for tools that you can use. Note that these are just the ones that I’ve heard of and used, there are lots of others. If you have ones that you want me to mention, let me know and I’ll add them.

Making Computers Do What You Tell Them

These systems get kids used to the idea of logic and control. They have strong gaming context, but that is not a bad thing because it is engaging and gets kids thinking in the right ways.

Little Big Planet – making games goes mainstream. A great way to play games with your kids, create stuff and get things out there. Lots of scope for making things and showing off.

Wairo Ware DIY – make your own mini-games on Nintendo DS. Great fun.

Kodu – make games that run on your Xbox 360. All the programming is done using the gamepad and it is very easy to get interesting game behaviours.

Scratch – wonderful, free platform for the PC and Mac that lets students build fun games using their own assets.

Alice – great little scripting language.

Fiddling with Hardware

There is nothing quite like making hardware jump around and lights go on from software control. Kids love this.

Lego Mindstorms – a bit pricey perhaps, but no more than a few computer games. And you get Lego quality components and a really nice development environment that lets you create some very complex behaviours. Can also be used as a stepping stone into real robotics via things like Microsoft Robotics Developer Studio

Arduino – a good way to get your hands dirty of you want to make things out of tiny components. Lots of add-ons and an easy programming environment to get to grips with. Great if you want to learn some electronics along with your programming.

Gadgeteer – just coming onto the market. If you have folks who can write simple programs (Java or C#) and want to give them an easy way onto proper hardware then this is for you.

Writing Proper Programs

This is were we get more serious. These are tools that take you into the world of proper program development. They are not to be taken lightly, but they do let you make things that you probably can sell.

Small Basic – a version of basic that is free, very easy to understand and works a treat.

XNA – a very easy to use game development environment which takes C# skills and lets you write proper games using them.  Start at the create hub and find a wealth of resources for learning and making games and programs.

A Word of Warning

Learning to use computers creatively is hard work and involves lots of practice. Just like learning to play a guitar is hard work and involves lots of practice. I’d hate folks to think that because these tools make it easy to get started (and they do) that making things that are properly impressive and useful is easy. It isn’t.

The ones that are best at writing software are the ones that are prepared to put the time in fiddling with their programs, persisting with them when they don’t work and polishing them to a high gloss when they do.

Be prepared for the moment when your kids get into computers and they find that actually they have to work at it. Good advice in this situation:

  • Too many ideas can kill you. Does your game need helicopters and evil rabbits? And laser guns? And an equipment shop? Kids latch on very quickly to the idea that with a computer program you can do anything. Unfortunately they often then try to make a program that does everything. Then they find they can’t and get disillusioned with the whole business. Suggest a step by step approach, and throwing things away if you don’t really need them just yet. After all, if the player never knows you meant to put evil rabbits into the game they will never miss them.
  • Make your steps as small as possible. Get something small working and build on it. Get the ball to bounce, then add the paddles, then add the targets, then add the boss level and so on.
  • Walk away for a while if you get stuck. Don’t spend more than an hour on any problem. If you haven’t solved it in that time, do something else for a while and go back.
  • Other people’s code is dangerous. It is very tempting to just search for a program routine to do what you want. But this often ends in tears. You are using something from someone you have never met, which might not do exactly what you want, and might not work. If there is one phrase that worries me when someone describes a problem they are having with their program it is “I found some code on the internet…”. If you make the effort to write the code yourself then you have a much better chance of understanding what went wrong. Having said that, don’t be afraid to look at the work of others and get ideas from it. In that context I recommend  CodeProject and StackOverflow – but remember that these are for proper programmers.
  • Remember that this is supposed to be fun. If you are doing something that seems to be a bit of a grind, find a way of making life easier for yourself. Simplify the problem or use your computer skills to make a tool that will help with the work.

Computer Science is Creative. Fact.

Hornsea Wide Beach

The Guardian has just produced an article with the rather depressing title “Britain's computer science courses failing to give workers digital skills”. You can read it here. The gist of the text is that ‘poor quality training’ and ‘sausage factory’ courses are not providing the skilled and useful people that industry needs.

Oh dear. I always feel a bit nervous when I read these things. I start to wonder if what we do at Hull gives our students a good experience and teaches them skills that make our graduates properly useful. I cheer up a bit when I consider that we actually do a pretty good job for our students with things like Three Thing Game and Evil Squash to get their teeth into and a taught Masters that puts them out into industry. Plus lots of other things I haven’t got space to mention here. And then I remember how our students actually end up doing quite well in industry.

However, I also wonder a little bit about the place of Computer Science in the world.  Some time back The Guardian (same paper) did a big feature called “Top Artists reveal how to find Creative Inspiration”. I found this really annoying. Not because the folks that were there weren’t creative, or that their tips were silly. No, the reason this made me cross was the unspoken assumption that only arty types can be creative. I reckon that Computer Science is the most creative thing you can do. A writer can produce a book about an idea. A composer can create a song about it. A playwright can write a play about it. But a Computer Scientist can bring the idea to life. Which would you rather have, a song about Facebook or Facebook?

Computer Science is the most creative thing you can do. One of the few things we know for certain about the future is that it will be built using digital technology. Everything around us is being converted into patterns of bits and connected to everything else. Of course there are some bad Computer Science courses out there and of course there is confusion about what the subject is actually about. But what it is really about is building the future. And you can’t get more creative than that.

Marking Evil Squash

evilsquash Logo
This is a much better logo than my version. Thanks Jamie

I’ve spent the last three and a bit days in the lab marking first year student work. And it has been great fun. There were four of us down there watching students go through 15 minute presentations of their Evil Squash implementations. For those of you who haven’t heard of it, Evil Squash is a board game for up to 4 players. It is a kind of cross between Snakes & Ladders and Ludo. We invented it just for the practical session and we are going to invent another one next year.

Our first year students had to create a program that implemented the game, getting all the arrows on the board to work, along with the “Squash” behaviour that is triggered when one player lands on top of another. We provided a “special” dice sequence which allowed us to test all the game actions and we watched each program run through this. Then we took a look at the code, gave marks for style and any extras (some students added AI players who could take on their human counterparts), checked on user documentation and test reports and finally gave out a mark.

We always do a game development for the first year course, but this is the first time we’ve used a brand new game of our own devising. It has worked rather well. Everyone got into the spirit of the development and we have seen some very impressive implementations of the game, including a few Windows Phone versions in Silverlight and XNA. Expect to find Evil Squash in the Windows Phone marketplace soon.

Once nice side-effect of using an original game was that there was no code out there for people try and use. When people get into trouble with a development there is sometimes a tendency to leap onto a search engine and look for code that solves the problem. This is never a good thing to do. A lot of code out there is buggy and hard to understand and often takes you further away from where you want to be. During the marking we ask for bits of the code to be explained to us, and I found that for the ones I marked everybody knew how their code worked. Even those unlucky souls who hit bugs during the presentation were able to say “Aha! I know what is wrong and how to fix it” and point to the code block that was causing the problem. 

We saw some great work, and gave some great marks out. I’m really looking forward to what they turn out next semester.

Programming Humans with Evil Squash

Main Board Hi Res

I’m gaining a new respect for board game designers. It turns out to be quite difficult to create rules for the games that are unambiguous and easy to understand. Even what I thought was a simple game, Evil Squash, has turned out to have some nasty edge conditions that we have had to think quite hard about.

Of course, a game design is really a “program for humans”. It is a sequence of instructions that involve the processing of data, with decisions and outcomes. The tricky part is that the rules are written in English, which is a language in which it is very hard to make yourself completely clear.

Perhaps as an introduction to programming we could get a bunch of people to design the rules for a game of some kind, and then “debug” it by playing the game. I’m certainly going to invent a new “imaginary” game for each first year programming course from now on, just exploring the rules and getting students to think about how they work has given a nice new dimension to the coursework.

You can find out more about the game itself at www.evilsquash.com.

Whatever Happened to Flickr

DSCF4802

This is a bit of cautionary tale I guess. I’ve been using Flickr for many years as a host for my pictures. I found the service so good that I’ve even been paying for it. Until now. Suddenly uploads are going like treacle, the web editing doesn’t work properly and other photo sharing sites are powering ahead with innovations that make Flickr look pretty long in the tooth. Snag is, I’ve got thousands of pictures up there, many of which are linked through to my blog – like the one above of Scarborough harbour.

So, do I stick with Flickr, and hope that their parent, Yahoo, decides that it is worth investing in, or do I jump ship to another company which might go the same way in five years? The cloud is a great place to put stuff, but I think we need to remember that not all clouds stay the same forever.

Monday Reminders

Leeds Market

A couple of things to remember today:

Optimus Quest 2

If you want to send me some Windows Phone Marketplace stats before the end of the month (i.e. tomorrow) then you might have a chance to win an LG Optimus 7 Windows Phone. Send your screenshots of the Marketplace to optimusQuest2@robmiles.com

Job with Rob

The closing date for applications to the Knowledge Transfer Partnerships (KTP) which I mentioned earlier is quite soon. If you fancy working on a really interesting project in a nice part of the world you should get your applications in before Thursday.

A Job With Rob

Downtown Boston

One of the great things that the university does is Knowledge Transfer Partnerships. These are set up to do two things. Get students into good jobs and spread expertise into business. We are presently setting one up and I’m one of the academics at Hull University who is involved with managing the work. It is a very interesting project in a nice part of the world (not Hull actually) with good pay. If you are a recent graduate who wants to get started in the business you could do a lot worse than apply. Even though you might end up in meetings with me….

There are lots of KTPs out there. The one I’m talking about is here. You can find lots more, including some others from the university here.

What’s Wrong with this Picture Answer

image

Why is this stoopid?

There is nothing wrong with letting you run the program from the last successful build. Perhaps you want to quickly show somebody what your program looks like, but you are in the middle of editing it. In this case you just click Yes and that person can see your program running, even though it won’t build at the moment.

The problem for me (as some of you have already spotted) is the nasty “Do not show this dialog again.” check box. If you check that it means that Visual Studio will always run your last successful build without giving you a message. The scenario where this bites you is as follows:

  1. Run program, notice an error.
  2. Edit source code, fix error but forget to type semi-colon, leading to a compilation error.
  3. Run program again to see if error has been fixed. Program fails to compile, so Visual Studio runs the old old version without telling you. The error appears to be still there.
  4. Go nuts trying to find the error that you have already fixed. Every change you make has no effect. Give up programming and become a beekeeper.

An experienced programmer will be looking out for compiler errors and will know that they are running old code. But not everyone will. You can get a similar effect by working on the wrong source file. In that case you got the fix right, but a file somewhere else is being used in the build. If I suspect a “reality fault” like this I do something that is guaranteed to break the build:

wobble;

This should not compile and run. If it does, something else is happening. If you want to put this back to how it should be (in my opinion) you can go to Visual Studio Tools>Options and find the following entry:

image

Change “Launch old version” to “Prompt to launch”.

image

If you want to get rid of the dialog box completely, and never have the program run when it won’t build, change it to “Do not launch”.

I’ve been racking my brains as to why you would want to hide the dialog box and always run the old version. To be honest, I can’t think of a reason why you’d want to do this, and I’d be a bit happier if the folks at Visual Studio hadn’t.

Stalked by an Oven

image

I’m being stalked by an oven. It’s actually very scary. We are in the process of planning an upgrade to our kitchen. It should be completed this century with a bit of luck. As part of this I’m searching for prices of various kitchen appliances, including the device you see above. However, now pretty much every web page I go to has an advert for this oven appearing on it. Something in the interwebs has cottoned on to the fact that I’m in the market for some cooking equipment and is tailoring what I see to suit. Most interesting.

Web pages are highly aware of the searches I’ve been doing. Last week there was a very good article in the paper about this kind of thing, which made the point that what you see on the web and when you search depends on what you have already looked for/at. This is not something that you might expect. It means that, far from allowing the web to expand your creativity and send you down new avenues, what really happens is that after a while the search tends to contract and focus down onto what the engines think you are interested in.

Discovering this hot in the heels of the presentation from Sir Tim Berners-Lee last Friday on his dream of an open and level playing field for all internet users makes me wonder if somewhere a battle has already been fought and lost.

I’m not sure if anonymous browsing would make a difference, or if search aggregators like duckduckgo.com would help. As someone said last week “If the service is free, you are the product”. That is how it is with search these days.

Positioning Sprites with Rectangles and Vectors in XNA

Prada

Staying with the XNA techie theme, now a word about performing sprite positioning in a game. A sprite is something that has a texture (the image to be displayed when the sprite is drawn) and a position (the place to draw it). Generally sprites map onto objects in a game. If I am creating a game and I need to draw a bat, ball, spaceship, alien or asteroid I will use a sprite to do this.

There are two different ways to position sprites in XNA and each has its advantages and disadvantages. However, it is worth knowing about both.

Rectangle Positioning

With this form you create a rectangle which tells XNA where to draw the item, and how big it is:

Rectangle r = new Rectangle(0, 0, 200, 100);
spriteBatch.Draw(texture, r, Color.White);

This draws our texture in the top left hand corner (0,0) , in a rectangle 200 pixels wide and 100 pixels high.

Using a rectangle like this works well, it also gives you a ready made “bounding box” which you can use to test for collisions:

if (rect1.Intersects(rect2)) {
    // We have a collision between rect1 and rect2
}

However, there are some bad things about using rectangles that make me less keen on them

  • Rectangles are positioned using integers. The x and y properties you use describe where they are do not have a fractional part. This means that if you want to move a rectangle slowly around the screen (i.e. less than a pixel per update) you can’t just use the x and y properties to do this.
  • You can use rectangles to scale drawing, but this gets a bit tedious as you have to work out the size of rectangle you need, and you also need to be mindful of the aspect ratio of the item you are drawing so that it doesn’t end up squashed or stretched.
  • It is impossible to rotate a sprite which is positioned using a rectangle

So, rectangles are good for very simple sprites, but once you have become more familiar with XNA I think it is worth moving on to Vector positioning.

Vector Positioning

A vector is something that has direction and magnitude. That sounds posh. Actually it just contains an X and Y value, just like a coordinate. The “direction and magnitude” bit kicks in if you draw a line from the origin (0,0) to the X, Y position given. This line is the vector. The direction is the way the line points, and the magnitude is how long the line is.  In XNA terms the Vector2 type is the one you can use to position 2D sprites:

Vector2 v = new Vector2(10, 10);
spriteBatch.Draw(texture, v, Color.White);

This code draws the texture at position (10,10) . The texture is drawn in whatever size it happens to be, i.e. if the texture image was 200 pixels by 100 it would be drawn that size.  This means that you might need to scale your textures to fit a particular screen size – but as we shall see later this is not  a huge problem.

There are quite a few good things about vectors though.

  • The X and Y values of a vector are floating point, so you have very good control of sprite speed
  • The XNA framework supports vector mathematics directly. So you can write code like this:

    position = position + speed;

    - where position and speed are both vectors

When it comes to scaling and rotating a sprite positioned using a vector you can use a more complex version of the draw command (find a detailed description here) to do all this. Also, bear in mind that if you targeting Windows Phone you can fix your resolution in the game to a particular value and then make all your to assets fit.

graphics.PreferredBackBufferWidth = 480;
graphics.PreferredBackBufferHeight = 800;

The phone hardware will scale the display automatically to match whatever size you specify, now and in the future.

If you want to detect bounding box collisions you can write code like this:

public bool Contains(Vector2 pos)
{
    if (pos.X < position.X) return false;
    if (pos.X > (position.X + texture.Width)) return false;
    if (pos.Y < position.Y) return false;
    if (pos.Y > (position.Y + texture.Height)) return false;
    return true;
}

This returns true if the area covered by the sprite contains the given position.

For bounding box collisions you can use this:

public bool Intersects(Sprite c)
{
    if (pos.X + texture.Width < c.pos.X) return false;
    if (c.pos.X + c.texture.Width < pos.X) return false;
    if (pos.Y + texture.Height < c.pos.Y) return false;
    if (c.pos.Y + c.texture.Height < pos.Y) return false;
    return true;
}

This test will tell you if the bounding box around the two sprites intersects. However, if the sprites are textures that don’t fill the entire sprite rectangle this test is not a very accurate one. I’ll be covering pixel level collision detection next.

Game Object State Management in XNA

Vegas Building

Hmm. This sounds a bit deep for a Saturday blog post. I suppose it is, but I got asked a question at Mix 11 and I’ve been pondering it ever since then. The question was about some code like this:

class Cloud : CloudGame.ISprite
{
    public Texture2D CloudTexture;
    public Vector2 CloudPosition;
    public Vector2 CloudSpeed;
    public bool Burst = false;

    public void Draw(CloudGame game)
    {
        if (!Burst)
            game.spriteBatch.Draw(CloudTexture, 
                                                                       CloudPosition, 
                                                                       Color.White);
    }
   // rest of cloud code here
}

This is a tiny part of my “Cloud Bursting” game demo, where players touch clouds to burst them. The above code is the definition of the cloud itself. This has a texture, position, speed of movement and a Burst flag as properties.

Burst is how the cloud “knows” if the cloud has been burst. If the cloud is burst it is not drawn. At the start of the game all the clouds have the Burst flag set to false to indicate that they are still part of the game. As the player touches them the Burst flag is set true.

You see this in a lot of game play situations. Anywhere that things are “killed” they have to be able to remember whether they are still alive or not. The code above is simple and works well, at any instant a cloud is either burst or not. If it is burst (i.e. the Burst flag is true) plays no further part in the game and is not drawn.

However, this design gave rise to the question: “Why do we need to have a flag? Surely it would be more efficient to remove a ‘burst’ cloud from the list of things to be drawn?”

This is a very good question. To understand it you need to remember that in a game we will have many objects on the screen at any given time. To manage this the game contains a collection of some kind (usually a List). During Update and Draw the game works through the list acting on each item in it. If an item is removed from the list it will no longer be part of the game.  When the player bursts a cloud, rather than setting a flag you could just remove that cloud from the list of active game objects.

This approach has the advantage that it is more efficient. Instead of a cloud having to decide not to draw itself the game never tries to draw burst clouds at all.  However, it also adds complication. The game must now have a “master” list of clouds and an “active” list of clouds. At the start of a game the master cloud references are copied into the active list. This is to avoid the overheads of creating and destroying objects, something you really don’t want to be doing on a regular basis.

Furthermore, the time that is saved is probably not going to be that much use to us. If the game works fine with all the clouds visible (which it should do) then saving small amounts of processor time when some clouds are removed is not going to make that much difference to performance. In this situation it is the drawing of the objects that takes the time, not deciding whether or not to draw them.

The fundamental principle here is that you should go for simplicity first, then optimise when you discover you have a performance issue.  I like having the flags present, it makes for greater cohesion and easier debugging. I can look at any cloud and decide whether or not it should be drawn simply by inspecting the flag.  If the flag wasn’t there I’d have to check which lists held the cloud, and so on.

So, I go with internal flags, not lists, which is pretty much what I said at the time as I remember.

C# Yield Return Fun

public static IEnumerable YieldFun()
{
    yield return 1;
    yield return 2;
    yield return 3;
}

static void Main(string[] args)
{
    foreach (int i in YieldFun())
    {
        Console.WriteLine(i);
    }
}

If you can tell me what the code above does, you understand how yield return works. If you can’t, read on……

In a C# program you can mark things as implementing the IEnumerable interface (to use this you need to have a using System.Collections; at the top of your program). This means the thing can be enumerated,  i.e. it means that I can get a succession of objects from it.

The best way to work through something that can be enumerated is by using the foreach construction, as shown above. You’ve probably seen foreach when you’ve worked through items in a collection such as a List or an array.  In the above code we are using foreach to work through the enumerable results that the YieldFun method returns. 

The code inside YieldFun looks a bit scary. The yield return keyword combination is followed by the thing that you want to send back for this iteration. In this case I’m just returning a different number each time. What happens when the yield return is reached is that the method stops at that point, returns the result and then, when the foreach asks for the next value, the method wakes up and continues at the next statement. The result of the program is simply the sequence:

1
2
3

If you want to programmatically supply a sequence of items to a consumer then this is  a great way to do it. 

Talking the Talk and Walking the Walk

Lift Mottor.jpg

We had a couple of presentations in the department today. Team Yellow and Team Purple (Tentacle?) gave the initial presentations for their group projects.  To say that the teams had been working together for  a week or so and it was their first stand up together they did very well.

One thing that did stand out though was some of the phrases that were used and this brought home to me how you need to be careful how you talk in front of an audience, particularly if you want to convince them you know what you are doing.

For example take the phrase “User Friendly”. It is all very well to say “We are going to produce a user-friendly solution”. You want to convey that you think this aspect of a system is important. However, saying it like this is pretty much meaningless. The customer is not expecting you to produce something that is “user-hostile”, but the phrase could also be expressed as “We’re not going to make something that acts as if it hates you”. 

It is far better to say what you are actually going to do to solve the problem. “We are going to closely involve the end user in the design and implementation so that they find the system easy to use.” is a much better way to express your intentions.  Take a similar approach when you talk about security. Rather than saying you think something is important you must say what you are going to do about it.

The other thing that came out from the presentations was partly my fault. I’d said earlier that it is very important to make the customer aware of those aspects of the system that you are not going to implement. For example, you might be expecting the customer to back up the data rather than providing data backup as part of your solution. You need get this over, but I’m not sure you should have have a slide with the heading “Things we are not going to do”.  It is far better to say things like “The server infrastructure that you are using will be used to back up our data along with that from other systems”. This puts the responsibility in the right place without sounding like you are avoiding work.

If all this sounds a bit like the dread “marketing speak” then I’m very sorry about that, but I do feel that it is important that you make sure that things you say are backed up with a some kind of action plan and you should avoid sounding negative about your intentions.

Getting Students Started in the Windows Phone Marketplace

Qwest Field

Getting started as a student in Windows Phone marketplace is actually quite easy, but there are one or two issues that you need to be aware of, and best practices to follow to make sure that you get going as quickly as possible. If you know how to do stuff the two issues that you need to be aware of are very simple:

  • The validation of your account only starts once you have submitted an application for approval.
  • You can only unlock a Windows Phone device once you have submitted an application for approval.

The bottom line here is that the first thing you must do when you have registered is submit an application for approval. Think of this as a “placeholder” that will move you through the process. You can remove it from sale later.

If you are an experienced Windows Phone developer this should be no problem. If you are not submitted before the process is simple enough, and to make it even easier I’ve made a tiny screencast that goes through it for you. In this I make a brand new application from scratch and then show how it would be submitted for approval. If you just copy what I do you can be sorted in around half an hour or so.

You can download and view the video here:

Windows Phone Marketplace Walkthrough

Note: The application that I submit during the screencast hasn’t appeared in the Marketplace yet. I’ll let you know when it does…

Reference and Value Types

IC 2007 Fri Trip f10 124

 

I reckon that the day I give a lecture and don’t learn anything is the day that I will give up teaching. I always take something away from a lecture, although sometimes it is only a decision not to use that particular joke again…

Today I was telling the first year about reference and value types in C# and I learnt something as well. For those of you who are not familiar with programming in C# (and why should you be?)  this is all about how data is held in a program.

Once you get beyond programs that do simple sums you find yourself with a need to lump data together. This happens as soon as you have to do some work in the Real World™. For example, you might be creating an account management system for a customer and so you will need to have a way of holding information about a particular customer account. This will include the name of the customer, their address and their account balance, amongst other things.

Fortunately C# lets you design objects that can contain these items, for example a string for the name, a number for the balance, a string for the address and so on.  In fact, C# provides two ways that you can lump data together. One of these is called a struct (short for structure) and the other is called a class (short for class). These two can be hard to tell apart, in that the way that they are created is exactly the same. But they have one very important difference. Structures are managed by value, but classes are managed by reference.

Today is the point in the course where I have to explain the difference between the two.  I’ve got a routine for doing this which I’ve used in the past, and it usually gets there. If an item is managed by value (for example a struct) you can think of it as a box with a name painted on it.  If we move data between two variables managed by value:

destination= source;

- the result is that whatever value is in the source box is copied into the destination box. If my source is a structure value which contains lots of elements all of these are copied into the destination. This is how simple variables such as integers and floating point values are managed.

However, if an item is managed by reference the effect of the assignment above is different. You can think of a reference as a named tag which is tied to an object in memory. If I assign one reference to another:

destination = source;

- the result of this is that both reference tags are now tied to the same object in memory.  No data is actually copied anywhere.

At this point in the explanation I usually have a room full of people wondering why we bother with references. They just seems to be an added piece of confusion. Now that we have references we have the potential for problems when the equals behaviour doesn’t do what we expect.  Why do we have these two ways of working with data? Why can’t we just use values for everything?

My answer to this is that using references allows us to provide different views of data. If I have a list of customers that I want to order by both customer name and account number then this is not possible with a single array of values. But if I use references it is much easier. I can have a list of references which is ordered by name and another list ordered by account number.

So far I’m going by the slides. But then it occurred to me to go a bit further, and think about the use of reference and value types from a design point of view. If I’m designing a data structure for a  sprite in a game (for example a single alien in a Space Invaders game) the sprite will have to contain the image to be used to draw the sprite and the position of the sprite on the screen. I posed the question which of these two elements should be managed by value and which by reference.

After some discussion we came to the conclusion that it is best if the image to be used to draw the sprite is managed by reference. That means that a number of sprites can hold references to the same sprite design. You see this a lot in computer games, where a game has multiple identical elements (soldiers, cars, spaceships etc) it is often the case that they are all sharing a single graphic. However the position of the sprite on the screen is a value that should be unique to each sprite, we are never going to want to share this, and so the position should be a value type.

We then went through a bunch of other situations where an object contains things, and pondered for each thing whether it should be managed by value or by reference. Generally speaking we came to the conclusion that anything you want to share should be managed by reference, but stuff that is unique to you should be a value.

Of course references bring a lot of other benefits too, which we will explore in the next few weeks, but the thing I learnt was that the more you can show a context in which a particular language characteristic is applicable the more chance you have of getting the message across.

As a little puzzle, one thing we talked about was the storage of the address of a customer in our account database. Should that be managed by value or reference, and why?

Save Dalby Forest

Dalby Forest with Horse

Dalby forest is one of my favourite places in the country. We go there a couple of times a year with a packed lunch and just wander round the place. Years ago, when the kids were smaller, we used to go and have barbeques. It’s just a nice place with loads of trees and some lovely walks.

Dalby Forest Bridestones

And if the government have their way I won’t be able to go there much longer. They have this cunning plan to sell off, or lease, or give away, or whatever, the forests in the UK. This will save them some money and avoid them having to levy so much tax on very rich people. Or something.  It will also almost certainly mean that places like Dalby Forest will be out of bounds to folks like you and me.

Dalby Forest Path

I’m not a particularly political person. My theory is that whoever you vote for the government always gets in. I’m also very aware that there are much more important things out there than whether or not Rob has a nice place to go and have his picnics.  But I’m also aware that there are a lot of us packed onto this tiny little island,  and that the few really nice green spaces that we have left should be protected, not sold off for profit.

There is a petition you can sign up to if you want your voice to be heard on this matter. I’ve already done so. You can find it at the Woodland Trust web site:

http://www.woodlandtrust.org.uk

What Computer should I get for University?

Seattle Museum.jpg

We got an email last week asking what kind of computer works best at university. Here are my thoughts on the matter:

Netbook

Netbooks based on the Atom processor are very cheap and great for web surfing, email and writing essays but they are a bit underpowered for the more demanding stuff like image editing and HD video. While you can use large tools like Visual Studio on an Atom powered Netbook it will not be a particularly enjoyable experience, particularly if you only have 1G of RAM in the machine.  However, they are great for taking notes, very portable and their batteries should see out a day on campus if you are careful. And they are so cheap you won’t suffer an enormous loss if you drop or lose yours.

Laptop

If you are buying a laptop I would go for at least a Core 2 of some kind. Machines based on the i3 processor are becoming affordable and are worth a look. If you are buying a laptop make sure that it has (or you can upgrade it to) at least 4G of RAM. If you want to write games with the machine it really needs a separate graphics adapter, those with built in graphics might work, but their performance will not be good. Take a look here for details of requirements to write XNA games:

http://msdn.microsoft.com/en-us/library/bb203925.aspx

Such a machine need not cost too much, I got an Dell Studio 15 with ATI graphics for around the 600 pound mark last year, and I’m sure things have moved on since then. Of course the snag with buying a “proper” portable computer is that it is properly heavy and scarily expensive to cart round with you.   This might mean that it gets left back at your house most of the time, which kind of negates the purpose of a laptop.

You should also look very carefully at the battery life. Bear in mind that although there are some charging stations on campus these are the exception rather than the rule and so a machine that can last all day is a good plan. I used to have a rule of thumb that I would take the manufacturers’ claimed life and halve it, so a machine that was supposed to be good for 3 hours would actually give only 90 minutes. However, I think things are improving a bit. My latest little machine claims 9 hours of use, and pretty much gets there.

Desktop

I’m in the process of returning to my desktop roots at the moment. I moved onto a laptop a while back because I loved the idea of having all my data with me at all times. It meant that I could pretty much work anywhere.  However, I can now have my data anywhere by using Live Mesh and Dropbox, and I fancy having a go with two monitors, so moving back to desktop makes sense. If you are buying a desktop now you should take a look at the new Intel “SandyBridge” I5 processor, which is not that expensive and provides a big leap in performance terms. Such a machine with at least 4G of ram and a 1T hard disk  and a reasonable graphics card should come in at around that magic 600 pounds (if you shop carefully)  and will provide a big leap in performance over a laptop of similar price. 

Some students have a great big hulking desktop at home and carry a tiny cheap netbook around during the day to take notes. This can work very well, particular if you use one of the cloud services (see backup below) to keep everything synchronised.

Apple

Apple seem to have figured out what makes something a pleasure to own and use, and then bottled it and sold it. All their machines run Windows really well, although the native OS X operating system has a lot to commend it and gives you access to wonderful programs like Garageband which come free with each Mac. And of course if you have a Mac you can write programs for the iPhone. 

I would place a slight question mark over the reliability and longevity of their hardware though. My MacBook Pro has been through two batteries, a power supply and a main board since I got it, and my little MacBook is on its second battery. I've bought machines from lots of other suppliers, Dell, Sony, Toshiba and Acer, and never had this failure rate with them.

If you are in academia make sure that you buy using the Apple academic discount scheme, you will save a little money but you will also get three years of Applecare warranty, which is well worth having. 

Software

Don’t forget software when you are pricing your systems. All our students get Microsoft Academic Alliance usernames shortly after they arrive with us and you can get Microsoft Operating systems and development tools for free from this:

http://msdn.microsoft.com/en-gb/academic/default

The only thing that you will miss from this is Microsoft Office, which you can get quite cheaply from here:

http://www.microsoft.com/uk/education/studentoffer/

If you want to try Linux I’d recommend taking a look at Ubuntu, which provides one of the best turnkey Unix experiences.

Backup

It seems that you have to lose a big chunk of work before you appreciate the importance of making backups of your data. One of my project students had their hard disk crash the night after they had just finished writing a very important report. Of course they hadn’t backed up the files…. 

These days, rather than remembering take a backup I use Dropbox and LiveMesh to make sure that files on my computers are all synchronised. During a working day I’ll probably move between two or three different platforms and these technologies make sure that the data on all of them always lines up. They are also provide browser based interfaces, so that you can get at all your important files anywhere you can find a web connection. 

http://explore.live.com/windows-live-mesh

http://dropbox.com/

The main problem with these services is the limited amount of space they offer. Live Mesh will give you 5G of online storage for free, with Dropbox you have to make do with 2G for free, although you can have more if you pay. However, this is not an issue for me. I don’t put any of my music or video on them, I simply use them to store “work in progress”, which for all the taught content and presentations that I gave last year only amounts to around 2 or so gig.

Insurance

If you do buy lots of fancy hardware do make sure that it is insured. Sometimes home insurance needs to be modified to cover expensive single items and if you move away from home you may need to get a policy of your own to cover your gadgets.

Final Words

Don’t spend too much on a computer. You don’t need a huge powerful machine to do our courses at Hull, actually most of the work (apart from 3D game writing) could be performed on a fairly basic system costing less than 300 pounds. We do have machines on campus which you can use, including some really powerful ones in the games lab which are available to students who need a lot of horsepower. Remember that anyone who tells you that you need the most expensive and powerful system they have is probably a computer salesman….

Hull Digital Question Time

HDQT Pic

The view from the audience, from left to right Jon Moss in the chair, Imran Ali, Helen Philpot and Prof. Calie Pistorius, VC of Hull University.

I’ve just been to something really, really good. With free drinks at the end. Hull Digital Question Time was set up by Jon Moss and brought together a panel of experts to discuss the future of digital technology. I wasn’t sure what to expect, but the combination of interesting questions, a range of expertise from the panel and sensible debate from the audience made for a fascinating evening.  And then we all went to the bar..

I think the event has been filmed and it would make an absolutely great podcast, so with a bit of luck it will turn up in a downloadable form at some point in the future. In the meantime, if you get the chance to go to any events like this in the future (and I’ve already asked for another one) then you should jump at it.

One more thing, Jon told us that the date for the next Hull Digital Live event has been set. It is the 4th of November this year. Note it in your diary.

Processing Lots of Files in C#

4346439556

Elliot came to see me today with a need to process a whole bunch of files on a disk. I quite enjoy playing with code and so we spent a few minutes building a framework which would work through a directory tree and allow him to work on each file in turn. Then I thought it was worth blogging, and here we are.

Finding all the files in a directory

The first thing you want to do is find all the files in a directory. Suppose we put the path to the directory into a string:

string startPath = @"c:\users\Rob\Documents";

Note that I’ve used the special version of string literal with the @ in front. This is so my string can contain escape characters (in this case the backslash character) without them being interpreted as part of control sequences. I want to actually use backslash (\) without taking unwanted newlines (\n)

I can find all the files in that directory by using the Directory.GetFiles method, which is in the System.IO namespace. It returns an array of strings with all the filenames in it.

string [] filenames = Directory.GetFiles(startPath);
for (int i = 0; i < filenames.Length; i++)
{
   Console.WriteLine("File : " + filenames[i]);
}

This lump of C# will print out the names of all the files in the startPath directory. So now Elliot can work on each file in turn.

Finding all the Directories in a Directory

Unfortunately my lovely solution doesn’t actually do all that we want. It will pull out all the files in a directory, but we also want to work on the content of the directories in that directory too. It turns out that getting all the directories in a directory is actually very easy too. You use the Directory.GetDirectories method:

string [] directories =
          Directory.GetDirectories(startPath);
for (int i = 0; i < directories.Length; i++)
{
    Console.WriteLine("Directory : " + directories[i]);
}

This lump of C# will print out all the directories in the path that was supplied.

Processing a Whole Directory Tree

I can make a method which will process all the files in a directory tree. This could be version 1.0

static void ProcessFiles(string startPath)
{
   Console.WriteLine("Processing: " + startPath); 
   string [] filenames = Directory.GetFiles(startPath); 
   for (int i = 0; i < filenames.Length; i++)
   {
      // This is where we process the files themselves
      Console.WriteLine("Processing: " + filenames[i]); 
   }
}

I can use it by calling it with a path to work on:

ProcessFiles(@"c:\users\Rob\Documents");

This would work through all the files in my Documents directory. Now I need to improve the method to make it work through an entire directory tree. It turns out that this is really easy too. We can use recursion.

Recursive solutions appear when we define a solution in terms of itself. In this situation we say things like: “To process a directory we must process all the directories in it”.  From a programming perspective recursion is where a method calls itself.  We want to make ProcessFiles call itself for every directory in the start path.

static void ProcessFiles(string startPath)
{
  Console.WriteLine("Processing: " + startPath); 

  string [] directories = 
                  Directory.GetDirectories(startPath); 
  for (int i = 0; i < directories.Length; i++)
  {
    ProcessFiles(directories[i]);
  }

  string [] filenames = Directory.GetFiles(startPath); 
  for (int i = 0; i < filenames.Length; i++)
  { 
    Console.WriteLine("Processing : " + filenames[i]); 
  }
}

The clever, recursive, bit is in red. This uses the code we have already seen, gets a list of all the directory paths and then calls ProcessFiles (i.e. itself) to work on those. If you compile this method (remember to add using System.IO; to the top so that you can get hold of all these useful methods) you will find that it will print out all the files in all the directories.

Console Window Tip:  If you want to pause the listing as it whizzes past in the command window you can hold down CTRL and press S to stop the display, and CTRL+Q to resume it.