Python 当I';我在改变电路板的尺寸
看一下ChangeBoardSize() 这将是while循环的一部分,我必须更改,但我不确定要更改什么或更改为什么。 无论如何,我都希望能得到任何帮助。 忽略这一点,但现在,因为我只是想得到的字符计数,以便我可以张贴这一点Python 当I';我在改变电路板的尺寸,python,Python,看一下ChangeBoardSize() 这将是while循环的一部分,我必须更改,但我不确定要更改什么或更改为什么。 无论如何,我都希望能得到任何帮助。 忽略这一点,但现在,因为我只是想得到的字符计数,以便我可以张贴这一点 import random def SetUpGameBoard(Board, Boardsize, PlayerInitials, ComputerInitials): for Row in range(1, BoardSize + 1): for
import random
def SetUpGameBoard(Board, Boardsize, PlayerInitials, ComputerInitials):
for Row in range(1, BoardSize + 1):
for Column in range(1, BoardSize + 1):
if (Row == (BoardSize + 1) // 2 and Column == (BoardSize + 1) // 2 + 1) or (Column == (BoardSize + 1) // 2 and Row == (BoardSize + 1) // 2 + 1):
Board[Row][Column] = ComputerInitials
elif (Row == (BoardSize + 1) // 2 + 1 and Column == (BoardSize + 1) // 2 + 1) or (Column == (BoardSize + 1) // 2 and Row == (BoardSize + 1) // 2):
Board[Row][Column] = PlayerInitials
else:
Board[Row][Column] = " "
def ChangeInitials(PlayerName):
print("Enter the initials for", PlayerName, "'s piece")
PlayerInitials = input()
PlayerInitials = PlayerInitials.upper()
print("Enter the initials for the computer's piece")
ComputerInitials = input()
ComputerInitials = ComputerInitials.upper()
return ComputerInitials, PlayerInitials
def ChangeBoardSize():
BoardSize = int(input("Enter a board size (between 4 and 9): "))
while not(BoardSize >= 4 and BoardSize <= 9):
BoardSize = int(input("Enter a board size (between 4 and 9): "))
if BoardSize != [4, 5, 6, 7, 8, 9]:
ChangeBoardSize()
elif BoardSize == " ":
ChangeBoardSize()
return BoardSize
def GetHumanPlayerMove(PlayerName):
print(PlayerName, "enter the coodinates of the square where you want to place your piece: ", end="")
Coordinates = input()
if Coordinates.isdigit():
MoveValid = True
return int(Coordinates)
def GetComputerPlayerMove(BoardSize):
return random.randint(1, BoardSize) * 10 + random.randint(1, BoardSize)
def GameOver(Board, BoardSize):
for Row in range(1 , BoardSize + 1):
for Column in range(1, BoardSize + 1):
if Board[Row][Column] == " ":
return False
return True
def GetPlayersName():
PlayerName = input("What is your name? ")
return PlayerName
def CheckIfMoveIsValid(Board, Move):
Row = Move % 10
Column = Move // 10
MoveIsValid = False
if Board[Row][Column] == " ":
MoveIsValid = True
return MoveIsValid
def GetPlayerScore(Board, BoardSize, Piece):
Score = 0
for Row in range(1, BoardSize + 1):
for Column in range(1, BoardSize + 1):
if Board[Row][Column] == Piece:
Score = Score + 1
return Score
def CheckIfThereArePiecesToFlip(Board, BoardSize, StartRow, StartColumn, RowDirection, ColumnDirection):
RowCount = StartRow + RowDirection
ColumnCount = StartColumn + ColumnDirection
FlipStillPossible = True
FlipFound = False
OpponentPieceFound = False
while RowCount <= BoardSize and RowCount >= 1 and ColumnCount >= 1 and ColumnCount <= BoardSize and FlipStillPossible and not FlipFound:
if Board[RowCount][ColumnCount] == " ":
FlipStillPossible = False
elif Board[RowCount][ColumnCount] != Board[StartRow][StartColumn]:
OpponentPieceFound = True
elif Board[RowCount][ColumnCount] == Board[StartRow][StartColumn] and not OpponentPieceFound:
FlipStillPossible = False
else:
FlipFound = True
RowCount = RowCount + RowDirection
ColumnCount = ColumnCount + ColumnDirection
return FlipFound
def FlipOpponentPiecesInOneDirection(Board, BoardSize, StartRow, StartColumn, RowDirection, ColumnDirection):
FlipFound = CheckIfThereArePiecesToFlip(Board, BoardSize, StartRow, StartColumn, RowDirection, ColumnDirection)
if FlipFound:
RowCount = StartRow + RowDirection
ColumnCount = StartColumn + ColumnDirection
while Board[RowCount][ColumnCount] != " " and Board[RowCount][ColumnCount] != Board[StartRow][StartColumn]:
if Board[RowCount][ColumnCount] == "H":
Board[RowCount][ColumnCount] = "C"
else:
Board[RowCount][ColumnCount] = "H"
RowCount = RowCount + RowDirection
ColumnCount = ColumnCount + ColumnDirection
def MakeMove(Board, BoardSize, Move, HumanPlayersTurn):
Row = Move % 10
Column = Move // 10
if HumanPlayersTurn:
Board[Row][Column] = "H"
else:
Board[Row][Column] = "C"
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, 1, 0)
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, -1, 0)
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, 0, 1)
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, 0, -1)
def PrintLine(BoardSize):
print(" ", end="")
for Count in range(1, BoardSize * 2):
print("_", end="")
print()
def DisplayGameBoard(Board, BoardSize):
print()
print(" ", end="")
for Column in range(1, BoardSize + 1):
print(" ", end="")
print(Column, end="")
print()
PrintLine(BoardSize)
for Row in range(1, BoardSize + 1):
print(Row, end="")
print(" ", end="")
for Column in range(1, BoardSize + 1):
print("|", end="")
print(Board[Row][Column], end="")
print("|")
PrintLine(BoardSize)
print()
def DisplayMenu():
print("(p)lay game")
print("(e)nter name")
print("(c)hange board size")
print("(i)initials change")
print("(a)lter piece name")
print("(q)uit")
print()
def GetMenuChoice(PlayerName):
print(PlayerName, "enter the letter of your chosen option: ", end="")
Choice = input()
return Choice
def CreateBoard():
Board = []
for Count in range(BoardSize + 1):
Board.append([])
for Count2 in range(BoardSize + 1):
Board[Count].append("")
return Board
def PlayGame(PlayerName, BoardSize, PlayerInitials, ComputerInitials):
Board = CreateBoard()
SetUpGameBoard(Board, BoardSize, PlayerInitials, ComputerInitials)
HumanPlayersTurn = False
while not GameOver(Board, BoardSize):
HumanPlayersTurn = not HumanPlayersTurn
DisplayGameBoard(Board, BoardSize)
MoveIsValid = False
while not MoveIsValid:
if HumanPlayersTurn:
Move = GetHumanPlayerMove(PlayerName)
else:
Move = GetComputerPlayerMove(BoardSize)
MoveIsValid = CheckIfMoveIsValid(Board, Move)
if not HumanPlayersTurn:
print("Press the Enter key and the computer will make its move")
input()
MakeMove(Board, BoardSize, Move, HumanPlayersTurn)
DisplayGameBoard(Board, BoardSize)
HumanPlayerScore = GetPlayerScore(Board, BoardSize, "H")
ComputerPlayerScore = GetPlayerScore(Board, BoardSize, "C")
if HumanPlayerScore > ComputerPlayerScore:
print("Well done", PlayerName, ", you have won the game!")
elif HumanPlayerScore == ComputerPlayerScore:
print("That was a draw!")
else:
print("The computer has won the game!")
print()
random.seed()
BoardSize = 6
PlayerName = ""
Choice = ""
PlayerInitials = 'H'
ComputerInitials = 'C'
while Choice.lower() != "q": #.lower() makes any allows uppercase input
DisplayMenu()
Choice = GetMenuChoice(PlayerName)
if Choice.lower() == "p":
PlayGame(PlayerName, BoardSize, ComputerInitials, PlayerInitials)
elif Choice.lower() == "e":
PlayerName = GetPlayersName()
elif Choice.lower() == 'a':
ChangePieceName()
elif Choice.lower() == 'i':
ChangeInitials(PlayerName)
elif Choice.lower() == "c":
BoardSize = ChangeBoardSize()
随机导入
def SetUpGameBoard(板、板大小、播放器初始值、计算机首字母):
对于范围内的行(1,BoardSize+1):
对于范围(1,BoardSize+1)中的列:
如果(行==(BoardSize+1)//2和列==(BoardSize+1)//2+1)或(列==(BoardSize+1)//2和行==(BoardSize+1)//2+1):
Board[行][列]=计算机缩写
elif(行==(BoardSize+1)//2+1和列==(BoardSize+1)//2+1)或(列==(BoardSize+1)//2和行==(BoardSize+1)//2):
线路板[行][列]=播放器初始值
其他:
线路板[行][列]=“”
def更改首字母缩写(播放名称):
打印(“输入”PlayerName“的首字母缩写”)
PlayerInitials=输入()
PlayerInitials=PlayerInitials.upper()
打印(“输入计算机部件的首字母缩写”)
ComputerInitials=输入()
ComputerInitials=ComputerInitials.upper()
返回计算机首字母缩写、PlayerInitials
def ChangeBoardSize():
BoardSize=int(输入(“输入电路板大小(介于4和9之间):”)
而不是(BoardSize>=4,BoardSize=1,ColumnCount ComputerPlayerScore:
打印(“做得好”,玩家名称,“你赢得了比赛!”)
elif HumanPlayerScore==计算机播放器核心:
打印(“那是平局!”)
其他:
打印(“计算机赢得了比赛!”)
打印()
random.seed()
BoardSize=6
PlayerName=“”
Choice=“”
PlayerInitials='H'
计算机首字母='C'
while Choice.lower()!=“q”:#.lower()允许任何大写输入
显示菜单()
Choice=GetMenuChoice(PlayerName)
如果选择.lower()=“p”:
PlayGame(PlayerName、BoardSize、ComputerInitials、PlayerInitials)
elif Choice.lower()=“e”:
PlayerName=GetPlayerName()
elif Choice.lower()==“a”:
ChangePieceName()
elif Choice.lower()=“i”:
更改首字母缩写(PlayerName)
elif Choice.lower()=“c”:
BoardSize=ChangeBoardSize()
有一个问题,因为您不能处理输入数字以外的字符,所以在第39行失败
return int(Coordinates) # if Coordinates is not str convertable to int it fails
所以你需要做一些类似的事情
def GetHumanPlayerMove(PlayerName):
print(PlayerName, "enter the coodinates of the square where you want to place your piece: ", end="")
Coordinates = input()
if Coordinates.isdigit():
MoveValid = True
try:
return int(Coordinates)
except ValueError:
print("Please enter number")
print(PlayerName, "enter the coodinates of the square where you want to place your piece: ", end="")
return GetHumanPlayerMove(PlayerName)
或者像下面那样创建一个while
循环
while not isinstance(Coordinates, int): ...
函数将如下所示
def GetHumanPlayerMove(PlayerName):
MoveValid = False
while not MoveValid:
print(PlayerName, "enter the coodinates of the square where you want to place your piece: ", end="")
Coordinates = input()
if Coordinates.isdigit():
MoveValid = True
return int(Coordinates)