Download Reversi game source and code in C# C#
Today, in this post, for you, dear users, we have prepared a source and code for the Reversi game in C# C# ready for download.
نمونه تصویر در زمان اجرا
این یک پیاده سازی از بازی Reversi است که با زبان سی شارپ نوشته شده است.
I originally wrote this program as an exercise to learn C# programming and .NET in general. Reversi - or Othello as it is known - is a popular game that is fun, requires only a few basic ingredients and has simple rules. This made it a good choice for learning a new programming environment.
The result was a fairly playable game, but it lacked some of the more common features of computer board games, such as the ability to cancel moves. So, as I gained more experience with .NET, it was time to upgrade. The result adds some new features and improves the original graphics and artificial intelligence.
Using the code
فایل های منبع را کامپایل کرده و فایل اجرایی Reversi.exeRun to run the game. You can play with different options and settings using the menu or toolbar. Try resizing the window, changing colors, or switching sides with the computer mid-game.
You may notice that when the program exits, a file calledReversi.xmlCreates. This file is used to store various settings such as game options, window size, position and player statistics, which are reloaded when the program is restarted.
Points of interest
The resource is well explained, but some overviews are in order.
بازی هوش مصنوعی
A good portion of the code deals with calculating moves for the computer player, so it's worth discussing. It uses a standard min-max look-ahead algorithm to determine the best move for the computer. Alpha-beta pruning is used to improve the performance of the forward search. If you are not familiar with the min-max algorithm and/or alpha-beta pruning, you will find lots of information and examples with a Google search.
Naturally, there are too many possible sequences of moves in the game to perform an exhaustive search. The exception is at the end of the game, where there are relatively few empty squares left - about ten or twelve. At this point, a thorough search can be performed and a move with the best possible outcome for a given player can be found.
But in most cases, the look-ahead depth should be limited to a certain number of turns (based on the game's difficulty settings). Therefore, for each set of possible moves and countermoves searched, the resulting game board must be evaluated to determine which player has the highest chance of winning the game. This evaluation is done by calculating the rank with the following criteria:
- فسخ- Leaving your opponent without any legal moves forces them to forfeit their turn, giving you the huge advantage of being able to move two (or more) times in a row.
- تحرک- This is a measure of how many legal moves you can make versus how many legal moves your opponent has left. Similar toفسخ, the idea is to reduce your opponent's options while maximizing your own.
- مرز- The boundary disk is a disk that is adjacent to an empty square. Having a lot of border discs, in general, gives your opponent more mobility in later turns. Conversely, having fewer boundary discs means your opponent will have limited mobility later on. This score shows the number of your boundary discs against your opponent's discs.
- پایداری- Corner discs are stable, can never be removed. Other discs become stable as the game progresses. This score shows the number of stable disks you have against your opponent's disks.
- امتیاز- This is simply the difference between the number of discs you have on the board versus your opponent's.
Each of these points is assigned different weights (again based on the game's current difficulty setting). By multiplying the score of each criterion by its corresponding weight and adding these values together, a rating is assigned to a panel. A large negative rating indicates a favorable board for Black, while a large positive rating indicates a favorable board for White. So for a given set of possible moves, the computer chooses the one that has the best ranking for the suit being played.
یک ثابت به نام
maxRankUsed to indicate the end of the game. Its value is set
System.Int32.MaxValue - 64. This ensures that any move that ends the game will always have a higher rank (negative or positive) than other moves.
Subtracting 64 from the system's maximum correct value allows us to add the final score to the rank so that the 10-disc range is higher than the 2-disc range. This causes the computer player to maximize their score when winning (or minimize the opponent's score when losing).
The current implementation is no match for the better AI players, but it plays very hard against a weak human opponent (at least, this weak human opponent). Again, a Google search will turn up many resources describing game AI strategies and approaches.
Dear user, you are offered a download.