![](/img/trans.png)
[英]What's the best way to generate random strings of a specific length in Python?
[英]What is the best way to generate random ships in an array in Python?
最近,我一直在為我的 CS2 class 開發一款戰艦游戲。 這個項目的重點是使用arrays創建一個棋盤游戲,我決定創建一艘戰艦。 我有大部分代碼,但我不知道如何在沒有船只的情況下讓船只在 10x10 陣列上隨機生成......
A. 長度錯誤 B. 循環數組
下面是我目前擁有的function。
def createShips(board):
shipLen = [5,4,3,3,2]
shipAvalible = 5
directionposibilities = ["vertical", "horizontal"]
j = 0
for i in range(shipAvalible):
boatMade = False
#REGULAR VAR STATMENTS
direction = random.choice(directionposibilities)
col = randint(0,9)
row = randint(0,9)
while boatMade == False:
if direction == "vertical":
buildCount = 0
if col + int(shipLen[i]) <= 11:
colission = False
for i in range(0, int(shipLen[i])):
buildCount += 1
if board[int(row-i)][int(col)-1] == "X":
if colission:
pass
else:
colission = True
if colission:
col = randint(0,9)
row = randint(0,9)
else:
for j in range(buildCount):
board[int(row-j)][int(col)-1] = "X"
boatMade = True
else:
col = randint(0,9)
row = randint(0,9)
if direction == "horizontal":
if col + int(shipLen[i]) <= 10:
colission = False
buildCount = 0
for i in range(0, int(shipLen[i])):
buildCount += 1
if board[int(row)][int(col)+i-1] == "X":
if colission:
pass
else:
colission = True
if colission:
col = randint(0,9)
row = randint(0,9)
else:
for j in range(buildCount):
board[int(row)][int(col)+j-1] = "X"
boatMade = True
else:
col = randint(0,9)
row = randint(0,9)
shipAvalible = shipAvalible - 1
return(board)
board = [["■"] * 10 for x in range(10)]
print(createShips(board))
如果您知道為什么這可能不起作用,請告訴我!
PS 我正在使用另一個 function 可以很好地打印數組,如果您為了方便起見想要這樣做,如下所示:
def showBoard(board):
print(" A B C D E F G H I J")
print(" ------------------------------")
rownumb = 1
for r in board:
if rownumb == 10:
space = ""
else:
space = " "
print("%d|%s|" % (rownumb, space + "|".join(r)))
rownumb += 1
好的,讓 go 解決這個問題:
長度檢查
if col + int(shipLen[i]) <= 9:
由於 arrays 在 python 中從 0 開始,10x10 板的最大索引為 9,因此您需要檢查 9 而不是 11
循環變量
for k in range(0, int(shipLen[i])):
對外部和內部循環使用相同的變量名(在這種特定情況下工作時)是非常糟糕的形式
碰撞檢查
if board[row+k][col] == "X":
你想建造船“前鋒”所以你使用row+k
,當你只使用整數開頭時,你也可以放棄 int 轉換
碰撞檢查 2
if colission: pass else: colission = True
這整個塊可以縮短為collision = True break
因為你不需要繼續檢查多個碰撞
構建計數
for j in range(buildCount):
除非發生碰撞(否則您不會在此分支中) buildCount 始終與shipLen[i]
相同,因此可以從您的代碼中完全刪除
造船
board[row+j][col] = "X":
和我們構建前鋒之前一樣
榮譽獎
shipAvailable = len(shipLen)
你也可以完全刪除這部分並直接在你的 out for 循環中迭代你的船
您可以在 while 循環開始時將其隨機化一次,而不是在每個中斷條件上對方向行和 col 進行輻射
j = 0
與 C 不同,您不必在循環使用變量之前定義變量,此語句什么也不做
編輯:@Eumel 有點快,在這里說得更好
首先,您有一個帶有變量i
的內部和外部循環,這實際上可能不會干擾,但它會使代碼更難閱讀。 考慮重命名變量之一。
創建vertical
船時,檢查col
而不是row
是否有索引錯誤,並根據值 11 而不是板尺寸 10 進行檢查。
在vertical
情況下,您從選定的 position “向后”建造船,即使您檢查了您的板可以在正方向上適合船(並且您將其放置在左側的列中)。 這些負值使您的船可以環繞。
在horizontal
情況下,您再次索引有點奇怪,其中board[int(row)][int(col)+i-1]
這使得當i=0
時,您再次將您的船的索引放置在比預期更靠左的位置當col=0
時導致船包裹。
此外,您可以通過移動來刪除一些多余的if
語句和重復的代碼行:
direction = choice(directionposibilities)
col = randint(0, 9)
row = randint(0, 9)
在while
循環內。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.