Python 我试图在我的战舰游戏中实现一个保存游戏的功能

Python 我试图在我的战舰游戏中实现一个保存游戏的功能,python,Python,我正在努力让我的游戏在每个玩家移动后都能拯救棋盘,但我不知道如何做到这一点 def SavedGame(Filename, Board): BoardFile = open(Filename, 'w') for Row in range(11): for Column in range(11): File.write(Board[Row][Column]) print(Board[Row][Column]) BoardFile.close() 到目前

我正在努力让我的游戏在每个玩家移动后都能拯救棋盘,但我不知道如何做到这一点

def SavedGame(Filename, Board):
  BoardFile = open(Filename, 'w')
  for Row in range(11):
    for Column in range(11):
      File.write(Board[Row][Column])
      print(Board[Row][Column])
  BoardFile.close()
到目前为止,我只有这个,但它什么也不做,我在代码的底部调用了这个保存的游戏

if __name__ == "__main__":
  TRAININGGAME = "Training.txt"
  LOADEDGAME = "SavedGame.txt"
  MenuOption = 0
  while not MenuOption == 9:
    Board = SetUpBoard()
    Ships = [["Aircraft Carrier", 5], ["Battleship", 4], ["Submarine", 3],       ["Destroyer", 3], ["Patrol Boat", 2], ["Newpiece", 1]]
DisplayMenu()
MenuOption = GetMainMenuChoice()
if MenuOption == 1:
  PlaceRandomShips(Board, Ships)
  PlayGame(Board,Ships)
if MenuOption == 2:
  LoadGame(TRAININGGAME, Board)
  PlayGame(Board, Ships)
if MenuOption == 3:
  SavedGame(LOADEDGAME, Board)
  PlayGame(Board, Ships)
以下是完整的代码:

import random

def SavedGame(Filename, Board):
  BoardFile = open(Filename, 'w')
  for Row in range(11):
    for Column in range(11):
      File.write(Board[Row][Column])
      print(Board[Row][Column])
  BoardFile.close()

colandrow = ["0","1","2","3","4","5","6","7","8","9","10"]  
def GetRowColumn():
  print()
  Column = input("Please enter column: ")
  while Column not in colandrow:
    Column = input("Please enter column: ")
  Column = int(Column)
  Row = input("Please enter row: ")
  while Row not in colandrow:
    Row = input("Please enter row: ")
  Row = int(Row)
  print()
  return Row, Column

def MakePlayerMove(Board, Ships):
  Row, Column = GetRowColumn()
  if Board[Row][Column] == "m" or Board[Row][Column] == "h":
    print("Sorry, you have already shot at the square (" + str(Column) + "," + str(Row) + "). Please try again.")
  elif Board[Row][Column] == "-":
    print("Sorry, (" + str(Column) + "," + str(Row) + ") is a miss.")
    Board[Row][Column] = "m"
  else:
    ("Hit at (" + str(Column) + "," + str(Row) + ").")
    if Board[Row][Column] == 'A':
      print("""You have hit an aircraft
 ▒▒▒▒▒▒▒▒▒▒▒▒
 ▒▒▒▒▓▒▒▓▒▒▒▒
 ▒▒▒▒▓▒▒▓▒▒▒▒
 ▒▒▒▒▒▒▒▒▒▒▒▒
 ▒▓▒▒▒▒▒▒▒▒▓▒
 ▒▒▓▓▓▓▓▓▓▓▒▒
 ▒▒▒▒▒▒▒▒▒▒▒▒
""")
    elif Board[Row][Column] == 'B':
      print("""You have hit a Battleshit
 ▒▒▒▒▒▒▒▒▒▒▒▒
 ▒▒▒▒▓▒▒▓▒▒▒▒
 ▒▒▒▒▓▒▒▓▒▒▒▒
 ▒▒▒▒▒▒▒▒▒▒▒▒
 ▒▓▒▒▒▒▒▒▒▒▓▒
 ▒▒▓▓▓▓▓▓▓▓▒▒
 ▒▒▒▒▒▒▒▒▒▒▒▒
""")
    elif Board[Row][Column] ==  'S':
      print("""You have hit a Submarine
 ▒▒▒▒▒▒▒▒▒▒▒▒
 ▒▒▒▒▓▒▒▓▒▒▒▒
 ▒▒▒▒▓▒▒▓▒▒▒▒
 ▒▒▒▒▒▒▒▒▒▒▒▒
 ▒▓▒▒▒▒▒▒▒▒▓▒
 ▒▒▓▓▓▓▓▓▓▓▒▒
 ▒▒▒▒▒▒▒▒▒▒▒▒
""")
    elif Board[Row][Column] == 'D':
      print("""You have hit a destroyer
 ▒▒▒▒▒▒▒▒▒▒▒▒
 ▒▒▒▒▓▒▒▓▒▒▒▒
 ▒▒▒▒▓▒▒▓▒▒▒▒
 ▒▒▒▒▒▒▒▒▒▒▒▒
 ▒▓▒▒▒▒▒▒▒▒▓▒
 ▒▒▓▓▓▓▓▓▓▓▒▒
 ▒▒▒▒▒▒▒▒▒▒▒▒
""")
    elif Board[Row][Column] == 'P':
      print("""You have hit a patrol boat
 ▒▒▒▒▒▒▒▒▒▒▒▒
 ▒▒▒▒▓▒▒▓▒▒▒▒
 ▒▒▒▒▓▒▒▓▒▒▒▒
 ▒▒▒▒▒▒▒▒▒▒▒▒
 ▒▓▒▒▒▒▒▒▒▒▓▒
 ▒▒▓▓▓▓▓▓▓▓▒▒
 ▒▒▒▒▒▒▒▒▒▒▒▒
""")
    elif Board[Row][Column] == 'n':
      print ("""You have hit the new piece
 ▒▒▒▒▒▒▒▒▒▒▒▒
 ▒▒▒▒▓▒▒▓▒▒▒▒
 ▒▒▒▒▓▒▒▓▒▒▒▒
 ▒▒▒▒▒▒▒▒▒▒▒▒
 ▒▓▒▒▒▒▒▒▒▒▓▒
 ▒▒▓▓▓▓▓▓▓▓▒▒
 ▒▒▒▒▒▒▒▒▒▒▒▒
""")
    Board[Row][Column] = "h"

def SetUpBoard():
  Board = []
  for Row in range(11):
    BoardRow = []
    for Column in range(11):
      BoardRow.append("-")
    Board.append(BoardRow)
  return Board

number = 10 #made it into a global variable instead of a local variable.!
def LoadGame(Filename, Board):
  BoardFile = open(Filename, "r")
  for Row in range(number):
    Line = BoardFile.readline()
    for Column in range(number):
      Board[Row][Column] = Line[Column]
  BoardFile.close()

def PlaceRandomShips(Board, Ships):
  for Ship in Ships:
    Valid = False
    while not Valid:
      Row = random.randint(0, 10) 
      Column = random.randint(0, 10) 
      HorV = random.randint(0, 1)
      if HorV == 0:
        Orientation = "v" 
      else:
        Orientation = "h" 
      Valid = ValidateBoatPosition(Board, Ship, Row, Column, Orientation)
    print("Computer placing the " + Ship[0])
    PlaceShip(Board, Ship, Row, Column, Orientation)

def PlaceShip(Board, Ship, Row, Column, Orientation):
  if Orientation == "v":
    for Scan in range(Ship[1]):
      Board[Row + Scan][Column] = Ship[0][0]
  elif Orientation == "h":
    for Scan in range(Ship[1]):
      Board[Row][Column + Scan] = Ship[0][0]

def ValidateBoatPosition(Board, Ship, Row, Column, Orientation):
  if Orientation == "v" and Row + Ship[1] > 10:
    return False
  elif Orientation == "h" and Column + Ship[1] > 10:
    return False
  else:
    if Orientation == "v":
      for Scan in range(Ship[1]):
        if Board[Row + Scan][Column] != "-":
          return False
    elif Orientation == "h":
      for Scan in range(Ship[1]):
        if Board[Row][Column + Scan] != "-":
          return False
  return True

def CheckWin(Board):
  for Row in range(10):
    for Column in range(10):
      if Board[Row][Column] in ["A","B","S","D","P","N"]:
        return False
  return True

def PrintBoard(Board):
  print()
  print("The board looks like this: ")  
  print()
  print (" ", end="")
  for Column in range(11):
    print(" " + str(Column) + "  ", end="")
  print()
  for Row in range(11):
    print (str(Row) + " ", end="")
    for Column in range(11):
      if Board[Row][Column] == "-":
        print(" ", end="")
      elif Board[Row][Column] in ["A","B","S","D","P","N"]:
        print(" ", end="")                
      else:
        print(Board[Row][Column], end="")
      if Column != 10:
        print(" | ", end="")
    print()



def DisplayMenu():
  print("MAIN MENU")
  print()
  print("1. Start new game")
  print("2. Load training game")
  print("3. Load a saved game")
  print("9. Quit")
  print()

def GetMainMenuChoice():
  print("Please enter your choice: ", end="")
  Choice = input()
  while Choice not in ["1","2","3","9"]:
    Choice = input("Please enter your choice: ")
  Choice = int(Choice)
  print()
  return Choice

def PlayGame(Board, Ships):
  GameWon = False
  while not GameWon:
    PrintBoard(Board)
    MakePlayerMove(Board, Ships)
    GameWon = CheckWin(Board)
    if GameWon:
      print("All ships sunk!")
      print()

if __name__ == "__main__":
  TRAININGGAME = "Training.txt"
  LOADEDGAME = "SavedGame.txt"
  MenuOption = 0
  while not MenuOption == 9:
    Board = SetUpBoard()
    Ships = [["Aircraft Carrier", 5], ["Battleship", 4], ["Submarine", 3], ["Destroyer", 3], ["Patrol Boat", 2], ["Newpiece", 1]]
    DisplayMenu()
    MenuOption = GetMainMenuChoice()
    if MenuOption == 1:
      PlaceRandomShips(Board, Ships)
      PlayGame(Board,Ships)
    if MenuOption == 2:
      LoadGame(TRAININGGAME, Board)
      PlayGame(Board, Ships)
    if MenuOption == 3:
      SavedGame(LOADEDGAME, Board)
      PlayGame(Board, Ships)

我还想用它做一个2人游戏,这样我就可以和电脑玩了,但我不知道怎么做。

如果你想存储数据,你可以用PlayerPrefs来做

def SavedGame(Filename, Board):
  BoardFile = open(Filename, 'w')
  for Row in range(11):
    for Column in range(11):
      File.write(Board[Row][Column])
      print(Board[Row][Column])
  BoardFile.close()

并通过以下方式获取数据:

PlayerPrefs.GetInt("number"); // returns 30

这个问题很不清楚,在我看来,你希望有人为你写代码,这不是这么做的目的。我唯一的建议和Unity论坛上的建议是不要首先使用Python。这是一个非unity3d问题。@joshijos你不应该与第三方分享你的计算机科学A水平的初步材料-你有可能被AQA考试委员会取消所有资格。我建议你尽快删除这个问题。