我正在尝试在我的战舰gam中实现一个保存游戏功能

2024-04-25 17:54:53 发布

您现在位置: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()

我只有这个到目前为止,但这没有任何作用,并调用保存的游戏,我在底部的代码这

^{2}$

以下是完整代码:

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人游戏,这样我就可以和电脑玩了,但我不知道怎么做。在


Tags: inboardforreturnifdefrangecolumn