C#中的TicTacToe(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/tictactoe-in-c-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 5 分钟阅读 - 2023 个词 阅读量 0C#中的TicTacToe(译文)
原文地址:https://www.codeproject.com/Articles/13325/TicTacToe-in-C
原文作者:TheGrandInquisitor
译文由本站 robot-v1.0 翻译
前言
A C# implementation of Tic-Tac-Toe using Minimax look-ahead.
使用Minimax预读的井字游戏的C#实现.
介绍(Introduction)
这是用C#编写的井字游戏的实现.(This is an implementation of the game Tic-Tac-Toe, written in C#.)
背景(Background)
我编写此程序是为了提高我的编码技能的基本练习.诚然,它很简单,但是由于我是新手程序员,因此具有足够的挑战性.本文适用于对棋盘游戏和/或Windows Forms感兴趣的其他新手,但他们正在寻找一个简单的示例来入门.(I wrote this program as a basic exercise to improve my coding skills. It is admittedly quite simple, but since I am a novice programmer, it was adequately challenging. This article is intended for other novices who are interested in board games and/or Windows Forms, but looking for a simple example to get them started.) 如果您不是新手,但您想查看我的代码并提供建设性的批评,请这样做!我没有收到任何正式指示,因此,您提供的任何反馈将不胜感激.(If you are not a novice, but you’d like to look at my code and provide constructive criticism, please do! I am not receiving any formal instruction, so any feedback you provide will be greatly appreciated.)
使用代码(Using the code)
只需编译源文件并运行生成的可执行文件即可.尝试调整窗口大小.(Simply compile the source files and run the resulting executable. Try resizing the window.)
兴趣点(Points of Interest)
程序结构(Program Structure)
该程序分为三类:(The program is divided into three classes:) Board
,(,) SquareControl
和(, and) TicTacToe
.(.)
董事会班(The Board class)
游戏板由二维整数数组表示.公共领域(The game board is represented by a two-dimensional integer array. The public field) BoardState
表示游戏是否正在进行中,如果没有,则表示先前的结果.的可能值(denotes whether the game is in progress, and if not, what the previous outcome was. The possible values for) BoardState
由(are defined by the) GameState
枚举.公共领域(enumeration. The public field) iEmptySquares
保持板上空方块的计数,以方便检测抽奖.(keeps count of the empty squares on the board to facilitate the detection of a draw.)
的(The) Board
类提供了两个公共构造函数.一个创建一个新的空板,而另一个通过复制现有的板来创建一个新板. AI在游戏期间使用第二个构造函数.(class provides two public constructors. One creates a new, empty board, while the other creates a new board by copying an existing one. The second constructor is used during the game by the AI.)
的(The) CheckBoard()
方法是用于在每次致电后检查董事会是否有中奖者(method is used to check the board for a winner after every call to) MakeMove()
.(.)
SquareControl类(The SquareControl class)
该类继承自(This class inherits from) UserControl
,用于表示用户界面中的每个正方形.的(, and is used to represent each square in the UI. The) OnPaint()
方法被覆盖.(method is overridden.)
最初,我有一个闪烁的问题(Initially, I had a flickering problem with) SquareControl
对象.为了解决这个问题,我在(objects. To resolve this, I set up double-buffering in the) OnPaint()
方法. Windows窗体中最常用的双缓冲规定方法涉及多次调用(method. The most commonly prescribed method for double-buffering in Windows Forms involves several calls to the) SetStyles()
设置自动双缓冲的方法.但是,这对我不起作用,所以我这样做是这样的:(method to set up automatic double-buffering. However, this didn’t work for me, so I did it this way:)
//Use a back-buffer to reduce flicker
Graphics buffer = Graphics.FromImage(bmpBackBuffer);
buffer.SmoothingMode = SmoothingMode.AntiAlias;
//....
//....draw onto buffer as needed
//....
//draw the contents of buffer to the screen
Graphics viewable = pea.Graphics;
viewable.DrawImageUnscaled(bmpBackBuffer, 0, 0);
TicTacToe类(The TicTacToe class)
此类表示程序的主要(且唯一)形式.(This class represents the main (and only) form of the program.) TicTacToe
包含几种控制游戏玩法的方法.例如,(contains several methods for controlling the game play. For example, the) MakeMove()
方法负责将给定的移动应用于(method is responsible for applying a given move to the) Board
对象和适当的(object and the appropriate) SquareControl
.的(. The) Reset()
方法用于重置(method is used to reset the) Board
和(and) SquareControl
准备新游戏的对象.(objects in preparation for a new game.) TicTacToe
还包含AI代码.(also contains the AI code.)
游戏AI(Game AI)
的(The) GetBestMove()
该方法采用Minimax超前算法来确定计算机播放器的最佳移动方式. Alpha-beta修剪用于提高性能.一种(method employs the Minimax look-ahead algorithm to determine the best possible move for the computer player. Alpha-beta pruning is used to increase performance. A) 谷歌(Google) 搜索将为该主题提供大量信息.(search will turn up plenty of information on this topic.)
在工作线程中执行前瞻以避免在搜索过程中挂起UI.搜索完成后,辅助线程将对主线程进行回调以应用新的动作.检查(The look-ahead is executed in a worker thread in order to avoid hanging the UI during the search. When the search is completed, the worker thread makes a callback to the main thread to apply the new move. Check the) CalculateComputerMove()
和(and) MakeComputerMove()
查看如何完成的方法.(methods to see how this is done.)
因为3x3井字游戏的搜索空间相对较小,所以我决定不限制搜索的深度.这简化了在前瞻期间使用的评分系统.为了使算法适用于更大的板,将需要实现深度限制和新的评估功能.您可能会注意到,计算机玩家偶尔会选择放弃立即获胜,而只是为了让您分叉,因此无论如何您都会输掉计算机的下一步.我曾考虑过要尽快赢得胜利,但是我认为这种行为很可爱.(Because the search space for a 3x3 Tic-Tac-Toe game is relatively small, I decided not to limit the depth of the search. This simplified the scoring system used during the look-ahead. In order to adapt the algorithm to boards any larger, a depth limit and a new evaluation function would need to be implemented. You may notice that the computer player occasionally chooses to forgo an immediate win, but only to fork you so that you lose on the computer’s next move anyway. I thought about adding a preference to win as soon as possible, but I decided that this behavior was cute.)
历史(History)
到目前为止没有更新.(No updates so far.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# WinXP Windows .NET .NET2.0 Visual-Studio VS2005 Dev 新闻 翻译