Author: David

Ideas for C Projects when learning it

Ideas for C Projects when learning it

Maze
Image by Gordon Johnson from Pixabay

Personally I always found the best way for me to learn a new language was to take an existing program- maybe something 500 lines or so long and completely convert it to the new language. It forces you to learn how to do things like string and file handling, organising the program, getting input and producing output and so on.

But having seen requests (on the C programming subreddit) for ideas to help someone apply their newly learnt knowledge, here’s a list of ideas of projects that are doable ion C. Nothing silly like database or operating systems!

  1. A simple calendar. Enter a date and show the month. Bonus points if you can use past dates and show the day that a date is on. Hint. Look up Zeller’s Congruence.
  2. Implement John Conway’s Game of Life. A cellular automaton that can be quite fascinating.
  3. Have a go at a Snake Game.  No cheating by looking at the Games Sources (link above on the top menu).
  4. Solve the 8 Queen’s problem. Put 8 queens on a chessboard so no piece can see and attack another.
  5. Create a maze generator. Make sure it includes at least one path from start to finish.

If you get through that lot, you go up a level and should look at this list.

More Thoughts on the Blazor game design

More Thoughts on the Blazor game design

OpenGameArt
OpenGameArt

This is the latest in a series on the design of a web game based loosely on a no-longer-run game called Inselkampf. It won’t be exactly the same as the original game, I have my own ideas. To see other articles in this series, click here or on the category: Blazor on the right hand side.

There are two models with Blazor. Server and WebAssembly. Both are similar but the crucial difference is that WebAssembly can run completely standalone whereas Server needs a connection to a webserver. Server uses SignalR technology.

The danger with using the Server model is that each person playing the game implicitly creates a connection. With the WebAssembly version, there is no direct connection though it makes sense to have a connection of sorts perhaps a Restful type interface.  This means running a query to fetch game data and then running update queries.

So Blazor with WebAssembly it is. Why Blazor? Because it simplifies updating controls on the page.

Looking after Time

The nature of this type of game is any construction (buildings and units) takes a finite length of time. which can range from seconds to a day or so  I want to see how long before my gold mine goes up a level and so on. It’s normal to have all times countdown in the browser. This should put no strain on the server as its running in the browser. Keeping server and client in sync needs a bit of care in case someone figures out how to make the browser code run faster. Once it reaches 0, an update should be sent to the server which should do a quick comparison with its time and if shenanigans has occurred, return an update status and resync the browser to the server. The rule is “If you give em a chance to cheat- they will“.

Procuring Graphics

Much though I like the Inselkampf graphics for all the buildings, I’m not going to use theirs. It’s infringing copyright (reimagining the game is not copyright infringement BTW!) . As well as the free kenney graphics that I’ve mentioned before and various other websites such as opengameart, (shown in the screenshot) there is also the Reddit game assets subreddit so between these and some others I hope I can find what I’m looking for.  My requirements are quite modest.

 

Answer to the C Puzzle

Answer to the C Puzzle

Solution
Image by Gino Crescoli from Pixabay

If you look back at the puzzle, you might think the for loop is a bit odd starting at -1 and that’s the snag. It exits the for loop immediately because -1 is actually more than 5. It’s a rather subtle bug. The type of d is obviously int, but the type of (TOTAL_ELEMENTS – 2) is unsigned int.

So what I believe is happening is the compiler is promoting d to an unsigned int which is 4294967295, and clearly that’s a bit larger than 5. To fix this just put (int) before the (TOTAL_ELEMENTS – 2) in the for loop and it will work.

Some Pretty fractals in C + SDL2

Some Pretty fractals in C + SDL2

Fractals
Code by Misha

If you’ve ever wanted to draw fractals in C, here’s some code for you. These come from Misha on his(her?) GitHub page.  There are three as you can see. Each has their own program – barnsley.c, sierpinski.c and y-fractal.c. All three programs create an SDL2 window then display the image.

Note that this was coded for Linux. I modified the programs so they would run on Windows and the screenshot was done from my machine. I know these images are on the GitHub page.

If you want to compile and run, then on all three programs change the SDL include from

#include <SDL2/SDL.h>

to

#include "SDL.h"

Also with the barnsley.c you need to give initial values to mx and my in the renderFractals() function (lines 88 and 89) and change these two lines: (114 and 115). No other changes are needed to let them compile.

	
mx_c = mx - win_w / 2;
my_c = my - win_h / 2;
  to
mx_c = mx - WIDTH / 2;
my_c = my - HEIGHT / 2;
Beej’s Guide to C programming

Beej’s Guide to C programming

BookBrian Hall (aka Beej) has written or collected guides to several programming languages including one on C. Out of curiosity I pasted the one document HTML version into both Word and a text editor to get a feel for how big it is and it filled 416 pages on Word, almost 13,700 lines in a text editor.

It is quite comprehensive covering most aspects of C. He has a disclaimer that its a work in practice and there may be bits that need correcting but still, if you are looking for an excellent guide to C, I’d definitely recommend it and if you like it, send him a tip on Paypal.

 

A little C Puzzle

A little C Puzzle

Question mark
Image by Gordon Johnson from Pixabay

The following C program doesn’t output anything. Why?

#include<stdio.h>

#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = { 23,34,12,17,204,99,16 };

int main()
{
    int d;

    for (d = -1; d <= (TOTAL_ELEMENTS - 2); d++)
        printf("%d\n", array[d + 1]);

    return 0;
}

 

This came from this site by Gowri Kumar which has a lot more C puzzles. Answer in a day or two…

There’s always someone inventing something better

There’s always someone inventing something better

JSON Logo
JSON Graphical Logo

In this case, I’m talking about better than JSON. JSON was invented as an alternative to XML which was invented as a way for computers to send data in a human readable form. Unfortunately XML is a pretty bloated format. Putting data into XML can make it five or six times larger.

Plus by the time XML was coming into common use in the mid-late 90s, there was a horrible letter soup of associated acronyms. It sort of appeared at the same time as Java and the whole XML ecosystem was lapped up by big business. JSON was born as an alternative method and it became very popular as JavaScript was growing at the same time, JSON is short for JavaScript Object Notation.

Now there’s MessagePack an alternative to JSON. It’s supposed to be faster and smaller than JSON. I came across it while looking at SignalR, a way to send data between clients and servers and used in Blazor and .NET websites.  It must have been around for a while as it was used back in 2011.

There’s now 50 programming language implementations including C at the bottom of the MessagePack home page although there’s over 100 but that includes several implementations for the same language.

If you have to move data between two computers, or maybe two processes on the same computer, you want it to be as small as possible and that’s what MessagePack allows.

 

A mini-project- SDL toolkit

A mini-project- SDL toolkit

Coloured Rectangles
Image by Gerd Altmann from Pixabay

I’ve thought about doing this for a while. Build a small toolkit (a library) of helper routines for any program that uses the SDL2 library. That means it will have functions to do the following:

  1. Draw horizontal and vertical lines in a specified colour.
  2. Draw coloured rectangles both filled in and empty.
  3. Draw Circles of specified radius and colour.
  4. Draw hexagons in either orientation and of a specified size, hollow or filled.

Plus any other things that occur to me. I’ll start on this shortly.

Games in C on GitHub

Games in C on GitHub

Tetris lamp
Image by Tobias Kozlowski from Pixabay

If you visit this site often, you’ll find many many links to projects on GitHub and not just mine. One of the thing I like about GitHub is how easy it is to search for stuff. So I put games in the top left search box and then ticked the C checkbox.

That returned 2,569 results but not all are playable games. Some are one off projects for other hardware such as Wii, GameBoy, Switch and similar. One gem I found was BSDGames. This is quite old and the 43 games are text based including the original Adventure.  I get the feeling many of these are from the Basic Computer Games books but at least these are in C not BASIC.

These will probably compile better on Linux and with difficulty and lots of fixes on Windows. And yes there is a Tetris game in the collection!

 

Web development- learning Blazor

Web development- learning Blazor

Web pages
Image by Mudassar Iqbal from Pixabay

I’ve talked about Inselkampf in a previous post. it was an example of a PBBG (Persistent Browser Based Game).  Currently if you want to write a web application that updates part of a page without refreshing itself, you have to do it via a JavaScript toolkit. People have been doing this going back about 15 years or so initially using a technology called AJAX.

Refreshing an entire web page takes a few seconds- some of the pages have got really big what with trackers and other things that bloat them up. So being able to update just part is a massive time saver plus it looks good.  Browser games couldn’t work without it.

Blazor takes things further by letting you update parts of pages, controls but in C#. There are two types of Blazor. WebAssembly (the machine code of the web) which does everything in the browser. It’s how the trains program works. The other type uses a server and does everything server side (controls and data) then sends it to the browser to be rendered.

For my purposes, the WebAssembly one is better. If you have a lot of people connected to a server then the Blazor server can use a fair whack of server resources. Th Blazor WebAssembly will also connect to the server but just to fetch or update data. So I’ve bought a Udemy course and am soaking up Blazor.

There’s still all sorts of questions. For example keeping the tick in synch. PBBG games often have a clock where new resources, building construction happen after a few seconds or longer. If you do this on the server then it’s not so difficult if the browser gets disconnected. In the browser though, it has to update the server or come up with some scheme so the browser and server don’t drift part. These are all part of the fun designing and programming such games. But I definitely think Blazor offers a lot of potential here.