感谢您阅读这篇文章。我想用AI和其他东西制作一个高级的TicTacToe游戏。我需要在不同的函数之间传递spot(s1-s9)变量。我已经进行了相当多的研究,希望得到一个答案。这是我需要执行的部分代码:
def set_spots(s1, s2, s3, s4, s5, s6, s7, s8, s9):
return s1,s2,s3,s4,s5,s6,s7,s8,s9
def print_spots():
print('\n')
print(str(s1) + ' | ' + str(s2) + ' | ' + str(s3))
print('--+---+--')
print(str(s4) + ' | ' + str(s5) + ' | ' + str(s6))
print('--+---+--')
print(str(s7) + ' | ' + str(s8) + ' | ' + str(s9))
def game_loop():
set_spots(1,2,3,4,5,6,7,8,9)
print_spots()
game_loop()
我希望能够在任何功能中设置位置,例如我具有turnX功能。就像我有一样:
def turnx(): #This isnt in this code though
#if stuff == other stuff (just example):
set_spots('X','O',3,4,5,6,7,8,9)
但是输出是:
NameError: name 's1' is not defined
因此,基本上,我需要程序询问用户将x或o放置在板上的位置(您不必担心),然后存储该值以进行打印。就像我在游戏中将1更改为X一样,它需要存储才能打印出来。
答案 0 :(得分:0)
我相信您应该尝试在打印位置使用设置位置的变量:
def print_spots(s1, s2, s3, s4, s5, s6, s7, s8, s9):
return s1,s2,s3,s4,s5,s6,s7,s8,s9):
print('\n')
print(str(s1) + ' | ' + str(s2) + ' | ' + str(s3))
print('--+---+--')
print(str(s4) + ' | ' + str(s5) + ' | ' + str(s6))
print('--+---+--')
print(str(s7) + ' | ' + str(s8) + ' | ' + str(s9))
def game_loop():
print_spots(1,2,3,4,5,6,7,8,9)
print_spots()
game_loop()
如果那不起作用,那么我不确定
答案 1 :(得分:0)
您可以执行此操作,而无需不必要的set_spots()
函数。
您只需要一个数组:
def turnx(s):
if 1 == 1: # (a condition):
s = ['X','O',3,4,5,6,7,8,9]
return s
def print_spots(s):
print('\n')
print(str(s[0]) + ' | ' + str(s[1]) + ' | ' + str(s[2]))
print('--+---+--')
print(str(s[3]) + ' | ' + str(s[4]) + ' | ' + str(s[5]))
print('--+---+--')
print(str(s[6]) + ' | ' + str(s[7]) + ' | ' + str(s[8]))
def game_loop():
spots = [1,2,3,4,5,6,7,8,9] # Use an array
print_spots(spots)
spots = turnx(spots)
print_spots(spots)
game_loop()
这将输出:
1 | 2 | 3
--+---+--
4 | 5 | 6
--+---+--
7 | 8 | 9
X | O | 3
--+---+--
4 | 5 | 6
--+---+--
7 | 8 | 9
答案 2 :(得分:-1)
帮自己一个忙,并以列表的形式表示景点。然后,当您调用设置点并将其传递到打印时,将该列表存储在变量中:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let line = line {
ImageService.downloadImageFrom(url: self.line?.author.photoURL, completion: { (image) in
self.imageView.image = image
}) // The error is in this line.
self.username.text = line.author.username
self.park.text = line.park
self.attraction.text = line.attraction
self.timestamp.text = line.createdAt.calendarTimeNow()
}
}