C Builder 6 中按钮的动态分配?
我想制作一款井字棋游戏。当我调整窗口大小时,我想在界面上显示更多按钮。从 3x3 到 4x4 等到 9x9 的矩阵,具体取决于我调整窗口大小的程度。我该怎么做?
我将为任何向我提供有效答案的人制作一个免费的网站设计(以及玩井字游戏的完整程序的额外内容)。
谢谢你!
I want to make a game of TicTacToe. WhenI resize the window I want to appear more buttons on the interface. From a matrix of 3x3 to 4x4 etc up to 9x9, depending on how much I resize the window. How do I do this?
I will make a free web site design to whoever provides me with a working answer (and something extra for the full program of playing tictactoe).
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以做的是设置按钮应具有的最大大小,然后只需除以容器大小即可找到您想要的按钮数量。这部分很简单,添加按钮并不是什么问题。您可以使用
Tag
属性来跟踪 tic tac toe 数组中的按钮位置。我在想,既然您想要的按钮数量达到了上限,您可以从头开始创建 9x9 按钮,只需将其
Visible
属性设置为false
显示当您需要它们时。然后,调整大小例程只需调整按钮的大小。我提供了一个如何在表单构造函数中创建按钮的简单示例,请注意,虽然这非常简单,但只是为了说明我的上述观点。请注意,我使用的是一维数组,而不是二维数组,这是为了以后更容易找到获胜者。
创建按钮后,您可以在表单
OnResize
事件中控制显示数量和大小:现在我们需要代码来处理用户输入,即每当用户按下其中一个按钮时,我们将 Sender 参数转换为 TButton,这样我们就可以在单个函数中进行点击处理。
最后,我们添加一个简单的函数来检查当前玩家是否是获胜者,这显然可以变得更加智能和优化,但这只是一个简单的示例,作为灵感来源:
显然还有很多工作要做,谁赢了的整个处理(更新只是添加了一个简单的检查),创建一个开始新游戏的界面等。除了游戏并不完全防错之外,在当前版本中游戏添加了即使在游戏开始后调整大小时也会出现额外的按钮,这很可能不是您想要的,但解决此问题是一个简单的问题,只需在调整大小功能中添加检查即可。不管怎样,我不打算为你创建整个游戏,我很确定你想自己做。
如果您发现它有用,您可以使用它,如果没有,那么也许(并且很可能)您会受到我的方法的启发(这实际上是一个 2 分钟完成的解决方案)。希望它对你有用。免费的网页设计不是必需的,我想您可以将其保存到以后的问题中;)
What you could do is set a maximum size a button should have, and then simply divide the container size to find the amount of buttons you'd want. This part is all easy, adding buttons is not really any problem. You could use the
Tag
property to keep track of the buttons position in the tic tac toe array.I'm thinking though since you have a maximum amount of buttons you'd want, you could create the 9x9 buttons from the beginning, simply setting their
Visible
property tofalse
showing them as you need them. The resize routine then only has to resize the buttons.I have provided a simple example of how to create the buttons in the form constructor, note though that this is extremely simple, but simply serves to illustrate my above point. Note that I'm using a single dimensional array, instead of a 2 dimensional one, this is to make it easier finding a winner later on.
After creating the buttons you can control how many to display and size these in the forms
OnResize
event:Now we need the code to process the user input, that is whenever the user presses one of our buttons, we cast the Sender parameter to a TButton, this way we can do the click processing in a single function.
Finally we add a simple function to check if the current player is the winner, this can obviously be made more smart and much more optimized, but this is just a simple example serving as source of inspiration:
Obviously there is a lot left to be done, the whole processing of who has won (update just added a simple check), creating an interface to start a new game etc. Besides the game is not entirely error proof, in the current version the game adds extra buttons when you resize even after the game has started, this is most likely not what you want, but fixing this is a simple question of adding a check to the resize function. Anyway I'm not about to create the entire game for you, I'm quite certain you'd wanna do that yourself.
If you find it useful you can use it, if not then perhaps (and most likely) you can be inspired by my approach (which is really a 2 minute made solution). Hope it works for you. The free webdesign won't be neccesary, I guess you can save that for some later question ;)