Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用2d numpy数组和字典制作tic-tac-toe_Python_Numpy_Dictionary - Fatal编程技术网

Python 使用2d numpy数组和字典制作tic-tac-toe

Python 使用2d numpy数组和字典制作tic-tac-toe,python,numpy,dictionary,Python,Numpy,Dictionary,我正在用一个二维Numpy数组做一个Tic Tac Toe游戏,我希望它能用字典将用户的输入转换成正确的矩阵位置,但我得到了这个错误: movesDict = {1:[[0][0]], 2:[[0][1]], 3:[[0][2]], 4:[[1][0]], 5:[[1][1]], 6:[[1][2]], 7:[[2][0]], 8:[[2][1]], 9:[[2][2]]} IndexError: list index out of range 示例用户给出了数字1,它被转换为矩阵位置[0][

我正在用一个二维Numpy数组做一个Tic Tac Toe游戏,我希望它能用字典将用户的输入转换成正确的矩阵位置,但我得到了这个错误:

movesDict = {1:[[0][0]], 2:[[0][1]], 3:[[0][2]], 4:[[1][0]], 5:[[1][1]], 6:[[1][2]], 7:[[2][0]], 8:[[2][1]], 9:[[2][2]]}
IndexError: list index out of range
示例用户给出了数字1,它被转换为矩阵位置[0][0],他们的回合被放置在那里

move = input("Move to which place {}?".format(turn))
realMove = movesDict[move]

字典中的键是整数,用户输入的是字符串

您需要将用户输入转换为整数,并且可能需要检查无效输入

move = input("Move to which place {}?".format(turn))
realMove = movesDict.get(int(move), 'Invalid Move')

好吧,我意识到问题是我忘了在字典中的元素之间加逗号。正确的代码是:

movesDict = {1:[[0],[0]], 2:[[0],[1]], 3:[[0],[2]], 4:[[1],[0]], 5:[[1],[1]], 6:[[1],[2]], 7:[[2],[0]], 8:[[2],[1]], 9:[[2],[2]]}

你不需要字典
divmod(move-1,3)
将为您提供坐标。但是使用矩阵进行tic-tac-toe过于复杂。有关更简单的方法,请参见此处: