Develop a (chess) program in Visual Studio 2022 (C# NET 6.0) [Huo Chess example]

Huo Chess DV (GUI Edition)

Interested in chess programming? You are interested in philosophy too! What is thinking? Can AI be conscious? What does it mean to do something? Can the AI understand that it does play chess? Explore the AI related articles in Harmonia Philosophica and understand why you are already a philosopher!

Goal

Some years have passed since the latest major update of Huo Chess. The goal of this article is to catch up with the developments of the latest years and show how an application built in previous versions of the Visual Studio can be ported into Visual Studio 2022. We will also show what is new with the newest .NET 6.0 Platform and what are the implications of the latest developments in the tools provided by Microsoft for building applications with C#. We will use the Huo Chess micro-chess engine published in this site as an example to show all the above in a short and concise way.

Important Notes: The new versions of the Huo Chess application in Visual Studio 2022 C# NET 6.0 are called ‘Huo Chess DV’ (from Huo Chess Delta Version).

Overview

Things in computers evolve over time. In the ‘old days’ we had BASIC. Then we had .NET Framework. Now we have .NET Platform (see also here), the latest weapon in the arsenal of Microsoft that allows developers to develop cross-platform applications with a single framework.

.NET frameworks and libraries (source)

And yes, .NET Platform is not the same with .NET Framework. In fact the latter is almost dead, as its support will soon end, whereas .NET is the latest framework (without the word… Framework in its tile, computer world always finds ways to confuse the uninitiated as alchemy used to do in the old days) you should choose. It has been around for some years now and its latest version is .NET 6.0. The .NET platform contains all the necessary tools, drivers and libraries to allow the modern programmer develop their programs.

And just to add more complexity to the already complex landscape, one needs to know that .NET Framework was initially succeeded by .NET Core. It was only after .NET Core 3.1 that it was decided to name the new framework .NET (without the ‘Core’) and that is what we now have .NET. Net (pun intended). [1] [2] [3].

The new .NET Platform allows for easier development of applications for various platforms, from Android to Mac. Details on how this is done is beyond the limited scope of this article.

The purpose of this article is to show how the open source micro-chess engine of Huo Chess was ported into Visual Studio 2022 and .NET 6.0. The next sections will present the problems and obstacles one might face when using the new platform and how they can be used to minimize the size of the application as much as possible.

Tutorials for building a chess program

There are many available tutorials on how to build a chess program. These are all based on Huo Chess open source code and will come in handy when trying to create your own first program. The logic of the program is explained, the functions used and the way the program thinks.

Tutorials on how to develop a chess program include:

This article only covers the porting of Huo Chess into Visual Studio 2022 and .NET 6.0, so in case you are lacking the basic understanding on how to program a chess application please visit these articles first. In any case, you are more than welcomed to continue reading and use the source code provided below as it is. Many times learning to program entails starting directly to… program. That is how I used to learn programming in Commodore and that is always the best way to start. Learning should always go hand in hand with practice.

After all remember: There is no way to learn without experimenting and making mistakes (but keeping a backups is always useful).

Huo Chess source code

The Huo Chess chess program is an open source micro-chess engine specifically designed for simplicity for educational purposes. One can view the code and easily understand the basic structure of a chess engine using Mini-Max algorithm for its thinking process. Reusing the code is allowed by all means (and even encouraged), as long as the source of the code is cited properly.

You can download the latest files for previous Visual Studio versions via the relative sites:

Download the code from the sites above, open them in your local Visual Studio 2022 and… let the fun begin.

In this article, I will describe how the Visual Studio 2019 Huo Chess based on .NET Framework was ported into Visual Studio 2022 and .NET 6.0. In the process I will describe some changes that were necessary and you will be asked to apply those changes to the program you downloaded yourself.

If you are impatient or find that difficult, you can always download the ported (‘fixed’) program from the last chapter at the end of this article.

First things First: Fixing dpi issues

Modern world is different than the world during the Commodore era in many ways. One of them is higher dpis. A modern program should handle that higher dpi of the screens effectively, otherwise it will look blurry. If you try to compile and run the program you already downloaded, you can see that for yourself.

Huo Chess previous version had an issue with how high-dpi screens were handled, making the UI of the chessboard look blurry without the fix. The changes described below are the changes needed to fix the Huo Chess issues for laptops with high dpi [28].

Please do try to apply those changes yourself in the project you already downloaded and see how they can improve the UI look and feel of the program.

Change 1: The application needs to add a compatibility declaration for Windows 10 in the manifest file:

<compatibility xmlns="urn:schemas-microsoft.com:compatibility.v1">
  <application>
    <!-- Windows 10 compatibility -->
    <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
  </application>
</compatibility>

Note: If you cannot find the manifest, add it to the solution.

Change 2: We also need to enable per-monitor DPI awareness in the solution’s app.config:

<System.Windows.Forms.ApplicationConfigurationSection>
  <add key="DpiAwareness" value="PerMonitorV2" />
</System.Windows.Forms.ApplicationConfigurationSection>  

Note: If you cannot find the app.config of your application read here for more.

With these changes, the application will handle high dpi of a modern screen fine. However you will still have a problem: the pieces in the chessboard will look small. Try to figure out how to fix that yourself (if you are impatient, just download the source files at the end of this article).

Tip: Try to change the zoom option in the pictureBoxes in the chessboard.

Porting in Visual Studio 2022…

The first thing to do when developing a new application is of course to download and open the IDE. So go and get the latest Visual Studio, open it and then load the program of Huo Chess. In that way you will load the application and you will also be able to build it and run it.

However, that will not make your application a .NET application. Instead, the existing (old) .NET Framework will still be used.

In order to make your application an application that utlizes the latest .NET Platform, you must create a new project and select to create…

  • Console C# application .NET for the Huo Chess console application (DO NOT select the .NET Framework console application, this is the old framework) [4] [5]
  • WinForms C# NET application for the Huo Chess GUI application (DO NOT select the .NET Framework console application, this is the old framework) [6]

After you create the applications, ‘all’ you need to do is copy the code of the old Huo Chess applications into the new projects. Do it your self and learn something through that process. You will learn that copying is not as simple as it seems. Some times you will forget times, some other times you will see that the new .NET application needs something additional in order to compile.

Follow the Visual Studio Intellisense suggestions to walk your way through the hurdles.

For example, you might be missing a ‘using’ statement in the beginning of the program for elements of the application. If that happens (and it WILL happen since the structure of the application in the new project is somewhat different that the old one), first take some time to understand the problem.

Consider for example the following example…

The problem seems to be with the StreamWriter that is highlighted accordingly. If you hover over the Intellisense lightbulb on the left, you will see some suggestions on how to fix the problem.

We will follow the first suggestion and simply add the missing ‘using’ directive in the beginning of the program. Essentially we were using the StreamWriter function without adding the relevant reference to the library containing that functionality. By simply adding the reference as below, the program compiled successfully.

Hopefully with some effort (depending on your level) you will be able to transfer Huo Chess from previous Visual Studio versions to the latest Visual Studio 2022.

So let’s move to the next and crucial step: Publishing!

Publishing the application

Once we created the application and successfully compiled it, we need to create the executable of our application that we will share with others when distributing our chess program. That is where the new .NET Platform has many options for publishing to various systems.

We will explore the best possible parameters to use so as to achieve the major goal for Huo Chess when it comes to size. And that is only one thing: Minimum size!

In summary, the following parameters are used:

  • Target framework: Net 6.0 – Windows 7.0
  • Deployment mode: Framework dependent
  • Target runtime: win-x86 for the console application / win-x64 for the winForms application
  • Other details: Set to produce a single file with no manifest

So essentially we are publishing for not the latest version of Windows, but we are using the latest framework .NET 6.0.

The publishing is conducted in Framework Dependent mode, meaning that the publishing will generate the minimum needed size of the application that will expect the host system to contain the .NET framework in order to run. Of course that is logical to expect, since the .NET framework is developed to be shared in as many computers as possible so that developers can use it – the was we used to us Java that was again supposed to be installed everywhere.

If we choose the other option to produce a Self-contained application, Visual Studio will publish a huge-sized application (counting in MBs) that will contain all needed .NET libraries for it to run. Useful to make sure your application will run even in a system without the .NET, but not useful for Huo Chess.

It is also important to select a specific target runtime for the publishing to generate code for that specific runtime. Try choosing other runtimes to see how easy it is and check out what will happen.

Last but not least, we choose to publish one single file. Doing so provides us with the true size of the application. There is no point in choosing to publish in multiple files and ‘fool ourselves’ that our application is small just because we choose to look at the size only of the .exe file in the folder…

Application trimming

Because of the changes brought by .NET, the size of the applications is somewhat (or significantly) increased vis-à-vis previous versions of the .NET Framework. Being modern, flexible and cross-platform has a price after all.

For Huo Chess this means that the console edition is now 159 KB (from 44 KB that it used to be) and the GUI edition is now 239 KB (from 96 KB that it used to be).

In order to fix that problem, Microsoft added functionalities to trim the code and optimize its size. There are many articles for application trimming in the References at the end of this article that you can read [17-20] [22] [23].

One basic thing to do in order to reduce the size of your application is to disable the implicit usings. ‘Implicit usings’ essentially tell the Visual Studio to add additional needed references to your project as it sees fit based on your code. Disabling this and selecting on your own the ‘using’ statements to have (like the one for the IO we added above) is the way to go when you need minimum size.

There is also an option to allow application trimming, however this applies only to specific publishing configurations and not to the ones we have selected above for Huo Chess. The trimming options look something like…

<PublishSingleFile>true</PublishSingleFile>
<PublishTrimmed>true</PublishTrimmed>
<TrimMode>Link</TrimMode>

However as already said, you cannot use the trimming options when publishing in Framework dependent mode. If we had selected to publish a Self-contained application these options would greatly reduce the size of our application.

The following file has a nice comparison of the resulting application sizes for Huo Chess console and GUI applications for various publishing and trimming configurations.

Please refer to the references below for trimming in order to read more on that topic.

Huo Chess for Visual Studio 2022 (C# NET 6.0)

As promised in the beginning, here one can download the latest Huo Chess projects for Visual Studio 2022.

Use these projects to learn how to program, to view them as you are reading the relative tutorials available in Harmonia Philosophica, or simple to experiment with your own ideas on how a chess program should look like! Again, re-using the code is free and encouraged, as long as you properly indicate where you found it!

Keep experimenting! Keep coding! And have fun!

References

  1. What is .NET?
  2. Five Things You Should Know About .NET 5
  3. Introduction to .NET
  4. Difference Between .NET and .NET Core
  5. How To Create EXE For .Net Core Console Application
  6. What is the difference between a Windows Forms App and Windows Forms App (.NET Framework)
  7. What’s the difference between .NET Core, .NET Framework, and Xamarin?
  8. MSBuild reference for .NET SDK projects
  9. Libraries Feature Switches
  10. Implicit global using directives for new C# projects only
  11. using directive
  12. An introduction to NuGet
  13. Install and manage packages in Visual Studio using the NuGet Package Manager
  14. Quickstart: Install and use a package using the dotnet CLI
  15. Package versioning
  16. Dependencies
  17. .NET 5.0 App Trimming and Potential for Future Progress
  18. App Trimming in .NET 5: Reduce Your App Sizes Dramatically
  19. How to reduce .Net Core App Size
  20. Reduce the size of your app in .NET Core 3 and above
  21. Remove Unused References
  22. App Trimming in .NET 5
  23. Trim self-contained deployments and executables
  24. Working with Assemblies and the Global Assembly Cache
  25. How to: Remove an Assembly from the Global Assembly Cache
  26. What’s new in Microsoft .NET 6
  27. c# How do I create a manifest file?
  28. winforms – Font blurry in Windows Forms C#
Exit mobile version
%%footer%%