战舰游戏 - 是否有更好的方式来访问此文本文件

时间:2015-12-01 15:49:23

标签: python list replace compare coordinates

我正在制作一个战舰游戏,从文本文件中绘制船舶坐标。

基本上

0000000
0000100
0000100
0000100
0000100
0000000
0000000

我有ges坐标读取正确读取文件的方法,我可以将镜头的坐标与文本文件中的坐标进行比较。

这是我到目前为止所拥有的

for turn in range(30):
  print ("Turn", turn + 1)
  guess_row = int(input("Guess Row:"))
  while guess_row < 1 or guess_row > 10: #or (guess_row.isdigit()) == False:
    guess_row = int(input("You have entered an incorrect coordinate. Please reenter: "))  #guess_col = int(raw_input("Guess Col:"))
  guess_col = int(input("Guess Column:"))
    while guess_col < 1 or guess_col > 10: #or (guess_row.isdigit()) == False:
      guess_col = int(input("You have entered an incorrect coordinate. Please reenter: "))  #guess_col = int(raw_input("Guess Col:"))
  myFile = None
  fileName = "map.txt"
  accessMode = "r"

  myFile = open(fileName, accessMode)
  fileContent = myFile.readlines()
  contentList = []
  for c in fileContent[guess_row - 1]:
    contentList = contentList.append(c)
    print(contentList[guess_col - 1])

  print(contentList[guess_col - 1])
  contentList = contentList.replace(',', '')
  print(contentList)

任何想法?我正在学习代码学院的一些新东西,我正在尝试不同的方法来完成战舰任务。我已经完成了,但要修改它。

1 个答案:

答案 0 :(得分:0)

你有正确的想法,但你可能想要用不同的方式写

1)读入文本文件

2)在阅读文件时,请确定

中的船舶坐标

3)将这些点存储在列表中。 您应该只读取一次文件

4)输入玩家想要拍摄的位置

5)将玩家坐标与可行的船舶坐标进行比较

6)告知用户是否有人

7)检查船是否已沉没

到目前为止看起来很好,只需记住验证您的输入并编写代码时请注意效率。祝你好运!

相关问题