战舰 - 从文本文件加载 [英] Battleship - loading from text file

查看:66
本文介绍了战舰 - 从文本文件加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我想问一下如何将战舰游戏板从文本文件上传到控制台应用程序。



例如:

000000

00XX00

XXX000

000XXX



0 - 是水



X - 是船只





我是初学者。

感谢您的回答。



我的尝试:



我会将它从文本文件加载到二维数组中,但我不知道该怎么做。

Hello,
I would like to ask how should I upload a battleship playing board from a text file to the Console application.

For example:
000000
00XX00
XXX000
000XXX

0 - are water

X - are ships


I am beginner.
Thank you for answers.

What I have tried:

I would load it from a text file into a two-dimensional array, but I have no idea how to do it.

推荐答案

这将取决于你在你的应用程序中如何使用它 - 如果你使用的板被组织为2D char数组,那么它很简单,你不需要转换任何东西。



请阅读您的文件:

It's going to depend on how you are using it in your app - if the board you use is organised as a 2D char array, then it's simple, you don't need to convert anything.

So read your file:
string[] lines = File.ReadAllLines(pathToFile);

并使用一对嵌套循环来传递char让你读到你的电路板。

数组包含每个索引的文件中的每一行文字。



够简单?

and use a pair of nested loops to transfer the charcaters you read into your board.
The lines array contains each line of text from your file per index.

Simple enough?


Quote:

为什么我的控制台应用程序为空在这之后?

Why is my console application empty after this?



我们怎么知道?

S2中的代码不会尝试显示任何内容。



您的代码行为不符合您的预期,或者您不明白为什么!



有一个几乎通用的解决方案:运行代码调试器一步一步,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

有在调试器中没有魔法,它不知道你的代码应该做什么,它没有找到错误,它只是通过向你展示正在发生的事情来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]


掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



在Visual Studio中调试C#代码 - YouTube [ ^ ]



这里的调试器只显示你的代码正在做什么,你的任务是与它应该做什么进行比较。


How can we know ?
Your code in S2 do not try to display anything.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于战舰 - 从文本文件加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆