且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C中创建菜单以选择游戏难度

更新时间:2022-11-16 19:56:18

这几乎完全取决于你在编码游戏和运行游戏的环境中:在他们正确的思想中没有人会尝试玩扫雷 - 控制台游戏你可能会使用某种形式的GUI。



我们不知道是什么你可以使用的GUI设备,以及C ......好吧,理论上它可以是任何东西!



回到你的开发环境,看看如何你正在展示你的游戏:这将是你需要从菜单代码开始的方式!
That's going to depend pretty much entirely on the environment in which you are both coding the game, and running the game: since nobody in their right mind is going to try and play "minesweeper - the console game" you are presumably using a GUI of some form.

And we have no idea what kind of GUI facilities you have available, and for C ... well, it could be anything in theory!

Go back to your development environment, and look at how you are displaying your game: that will be how you need to start with your menu code!


为什么必须有一个开关/案例陈述?如果输入数字,则它们确定游戏中所有内容的大小。电路板区域要么必须动态分配,要么定义最大尺寸并使一切都很大。例如,假设您将最大尺寸标注为32。定义一个32x32的板变量类型int。您只需将所有循环扩展到当前电路板尺寸所需的范围。此外 - 格里夫先生关于未使用的电路板边界的想法非常好,我肯定会使用它。如前所述,如果您将电路板固定为32x32,那么您将有一个可用的电路板尺寸为30x30,这将是您允许用户进入的最大尺寸。



我刚刚意识到我假设用户输入的是实际尺寸而不是难度。如果你愿意,你可以提供两者。您可以使用简单,中等,硬和用户定义的可能性。至于开关/案例陈述 - 有四种情况,每种情况下只分配两个变量(电路板尺寸和炸弹数量),我认为这很容易。



如果这是一个控制台应用程序,fgets(stdin,...)可用于读取用户的输入行,atoi可以将输入转换为整数。这两个函数都有tchar和wchar等价。
Why does there have to be a switch/case statement? If numbers are entered then they determine the size for everything in the game. The board area would either have to be allocated dynamically or you define a maximum size and make everything that big. For example, say you make 32 the maximum dimension size. Define a board variable type of int that is 32x32. You just make all your loops extended only as far as they need to for the current board size. Also - Mr. Griff's idea of the unused board boundary is a very good one and I would definitely use it. As previously mentioned, if you fix the board to 32x32 then you will have a usable board size of 30x30 so that would be the maximum size you allow the user to enter.

I just realized that I was assuming the user entered the actual size and not a level of difficulty. You could provide both if you wanted to. You could have easy, medium, hard, and user-defined as your possibilities. As for a switch/case statement - there are four cases and only two variables to assign in each case (board size and bomb count) and I think that's pretty easy.

If this is a console application, fgets( stdin, ... ) can be used to read lines of input from the user and atoi can convert the input into an integer. Both functions have tchar and wchar equivalents.


引用:

我知道我可以使用switch case但是代码太大了。无论如何在另一个项目的代码块中运行某个项目?因为那样可以完全解决我的问题。

i know i can use switch case but the code is too large. is there anyway to run a certain project in code blocks from an another project? because that would totally solve my problem.



即使你的项目大100倍,它也不会太大。

每个难度级别之间的差异是雷区的大小和地雷数量,你不可能需要第二个项目。


Even if your project was 100 times larger, it wouldn't be too large.
The difference between each level of difficulty is the size of minefield and number of mines, there is no way you need a second project for this.