Learn C# step by step, the HexaFour Game (h4-01)
Is it easy to learn C#? Sure, as a beginner you can quickly learn to write a few small console programs. But what’s the next step? You need something more difficult, but it shouldn’t be too difficult either. This is the beginning of a series that uses a graphical game to show how to program C#.
In the following series of articles I explain how to program the computer game HexaFour. This game is a bit more complicated, so I can use it to explain step by step how to program with C# and some other things that are important in software development. This will not be an app to publish yet, that would be too difficult to start. But this game is a good start to explain tools and concepts that are also important for the development of larger programs.
Game idea
First of all, I only have a rough idea for the game I want to program: Two people should be able to play it. Players can take turns dropping hexagonal tokens from the top into a game board. A token drops down until it hits an anchor position. There are two possibilities:
If the anchor position is free (case a), the token remains on the anchor position. If the anchor position is occupied by another token (case b), the other token moves on and the token coming from above takes the anchor position.
The winner is the player who first has four of her tokens in a horizontal or diagonal line. In this example, blue has won on the next move:
The sequence of the next move of blue is marked with blue arrows. The new token thrown in will move the blue token below it. This token will move down one position diagonally and then form a row of four with three other tokens. Thus blue has won.
I already have a name for the game: HexaFour. “Hexa” because the tiles are hexagons and “Four” because you need four in a row to win.
Besides the name and the rough idea of the game, many things are still unclear. But it is not worth to make a concept for everything and only then to start programming. You can make much better progress (and have more fun) if you implement the first things quickly, then try them out and think about how best to proceed from there. In real software projects, this is often done in the same way; it’s called agile software development.
Tools
I use the programming language C# here. C# is perhaps a little bit more difficult for beginners than python, for example. However, when the programs get bigger, I think C# is an optimal way to go. There are many commercial programs that are developed with C#.
For real games, you actually need a gaming engine - you could use Unity, for example. But then, as a beginner, you have to learn a lot of things at the same time: the C# programming language, tools for that programming language, the gaming engine, tools for the gaming engine, and so on. That’s a lot for the beginning. Therefore I start here without a gaming engine and use the graphics library Woopec instead. I’m not completely impartial there, because I developed Woopec myself :-)
The game in this course won’t look as cool as a Unity game, but for that you can get started here with very little prerequisites. You’ll first learn how to develop software with C# and then you can move on to Unity for the next game. C# is also used as the programming language in Unity, so you’ll learn a lot here that you’ll also need later in Unity.
For software development, you need to know more than just a programming language and tools. When a program gets bigger, other things become more important. The first version of a program is quickly assembled. You nail a few boards together and you have a house with a roof where you can sit dry. But then the house needs more rooms and additional floors. Then the house is to be expanded into a small city. You need more houses, then streets, then bigger streets, a hospital, a theater, an airport, … At some point you reach a point where this can get very complicated. At a certain size you lose the overview if the whole thing is not well organized and structured. That’s why, as we develop our program here, I’m also going to pay more and more attention to making sure that the program is well organized and structured. In his book “Clean Code. A Handbook of Agile Software Craftsmanship” Robert C. Martin calls this clean code:
[…] writing clean code ist a lot like painting a picture […] a programmer who writes clean code is an artist who can take a blank screen through a series of transformations until it is an elegantly coded system.
Let’s see how well we will succeed in writing clean code here.
There is one point I must make at the end: While I will explain many things here, the following blog posts are most definitely not a complete introduction to C#, programming practices, or clean code. Many others have already done that much better than I ever can do. I only explain a few necessary or interesting things here and give you links to more detailed information. Based on this, you can search for the websites, videos or books that best fit your needs.
Overview of the posts in this series
The following list contains the posts of this series published so far.
Chronological overview:
- Developing software is just as easy as building airplanes
- Learn C# step by step, the HexaFour game
- The first C# program with graphics
- Draw a hexagon with C#
- Why is this simple C# program not working? Fixing compiler errors
- C# debugging for beginners
- It is never to early to write clean code
- 0 is not equal to null. Definition and use of the appropriate C# types
- First thing about a concept, then take the next coding step
- Create new instances of a C# class and store them in a list
- Introduction to C# methods, and a rule for it: Do not change the values of their parameters
- Clean code with YAGNI, KISS, SRP, SoC and IOSP, an example
- A Beginner’s Guide to downloading Code from GitHub (h4-11)
If you are looking for specific topics, these lists can help you:
C#:
- Statement
- Methods: Using a method and setting parameters, definition of methods, parameters, return values
- Classes: Creating and initializing
- Records: Simple example
- Properties: Using a property
- Variables: Definition, change, check, usage. Definition with var keyword
- Simple data types: int, double, string, null and nullable value types
- Complex data types: lists
- Control: for, while and other loops, foreach loops, if and else commands
- Conditions: in loops
- Math: modulo
Visual Studio:
- Installation, debugger start, debugger stop
- IntelliSense
- Keyboard shortcuts: jump to corresponding brace, and more
- Compiler errors: tips and tricks to fix them
- Debugging: start, stop, breakpoints, step over, step into, window placement, variables, methods, exceptions
- GitHub: clone, fetch, pull, push, undo changes
Woopec graphics library:
- Create a new Visual Studio project with Woopec graphics
- Turtle methods und properties: Left, Right, Forward, ShowTurtle, HideTurtle, PenUp, PenDown, BeginFill, EndFill, Color, Speed
- Pen methods and properties: Rotate, Move, IsDown BeginFill, EndFill, Screen, BeginPoly, EndPoly
- Figure methods and properties: Rotate, Move, IsVisible, Heading
- Shapes: Shapes.Add, Shapes.Get
- Colors: Colors.FromHSV
- Screen methods: TextInput, NumInput, Bye
Clean Code:
- Meaning, example
- Principles: coding conventions, DRY principle, concepts, YAGNI, KISS, SRP, SoC, IOSP, design types
- Tips: Usage of var, immutable method parameters
HexaFour game:
Comment on this post ❤️
I am very interested in what readers think of this post and what ideas or questions they have. The easiest way to do this is to respond to my anonymous survey.