Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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:在tic-tac趾板上随机选择一个点_Python_Python 3.x_Random - Fatal编程技术网

Python:在tic-tac趾板上随机选择一个点

Python:在tic-tac趾板上随机选择一个点,python,python-3.x,random,Python,Python 3.x,Random,我正在写一个程序,基本上是一个井字游戏。它使用一个简单的基于文本的图版游戏绘图,使用-线和+加号。 在课程开始时,我将tic tac趾板中的9个空格定义为: valuea1 = " " valuea2 = " " valuea3 = " " valueb1 = " " 依此类推,通过valuec3。。。 空白空间被设置为允许X或O的替换。 我的板函数如下所示: def board(): """ prints the tic-tac-toe board""" print(" +-

我正在写一个程序,基本上是一个井字游戏。它使用一个简单的基于文本的图版游戏绘图,使用-线和+加号。 在课程开始时,我将tic tac趾板中的9个空格定义为:

valuea1 = " "
valuea2 = " "
valuea3 = " "
valueb1 = " "
依此类推,通过
valuec3
。。。 空白空间被设置为允许X或O的替换。 我的板函数如下所示:

def board():
    """ prints the tic-tac-toe board"""
    print(" +---A------B------C--+")
    print(" |      |      |      |")
    print("1|  " + valuea1 +"   |  " + valueb1 +"   |  " + valuec1 + "   |")
    print(" |      |      |      |")
    print(" ----------------------")
    print(" |      |      |      |")
    print("2|  " + valuea2 +"   |  " + valueb2 +"   |  " + valuec2 + "   |")
    print(" |      |      |      |")
    print(" ----------------------")
    print(" |      |      |      |")
    print("3|  " + valuea3 +"   |  " + valueb3 +"   |  " + valuec3 + "   |")
    print(" |      |      |      |")
    print(" +--------------------+")
    return
稍后在程序中,根据if语句,“计算机播放器”将使用的符号(X或O)存储在变量中:

computer_mark ## is either "X" or "O" based on player choice
现在是我主要关心的问题

randomlist = ("valuea1", "valuec1", "valuea3", "valuec3")
random_result = random.choice(randomlist)
## (REPLACE WITH CODE)
## takes the value stored in random_result, somehow treats the value as a variable,
## assigns that given variable the string value that is stored in computer_mark
我尝试随机选择板上4个点中的1个,并将该点从
字符串值更改为存储在计算机标记中的字符串值

有没有一种方法可以将Python编码为选择一个随机变量,并给它分配一个存储在不同变量中的值

编辑
我是一个python和通用编程的noob。我只编写了两周的代码,因此,如果我的编码技术有点不智能,我很抱歉

尝试按名称修改这些变量是一个非常糟糕的想法(如果您真的想这样做,请参阅下面的旧答案)。这就是如何通过使用列表而不是一堆变量来改进代码:

import random

board = [' '] * 9

def set_random(board,computer_mark):
    board[random.randint(0, 8)] = computer_mark

def print_board(board):
    lines = [' +--%s--+' % ('-'*5).join(['A','B','C'])]
    for i in range(3):
        lines.append('%d|  %s  |' % (i,'  |  '.join(board[3*i:(3*i+3)])))
        if i < 2:
            lines.append(' ' + '-'*19)
    lines.append(' +%s+' % ('-'*17))
    print('\n |     |     |     |\n'.join(lines))

print_board(board)

random.seed(0)
set_random(board,'X')

print_board(board)
为了更好地了解哪个列表索引进入哪个字段,将索引显示在电路板上非常有用:

>>> print_board(list(map(str,range(9))))
 +--A-----B-----C--+
 |     |     |     |
0|  0  |  1  |  2  |
 |     |     |     |
 -------------------
 |     |     |     |
1|  3  |  4  |  5  |
 |     |     |     |
 -------------------
 |     |     |     |
2|  6  |  7  |  8  |
 |     |     |     |
 +-----------------+

您可以使用
locals()
函数访问当前名称空间(作为dict)。因此,您可以执行以下操作:-然而,这是一个非常糟糕的主意。请参见上面的,了解解决代表和操纵董事会的一般问题的更好方法

locals()[random_result] = computer_mark
根据更新的问题编辑完整示例:

import random

valuea1 = valuea2 = valuea3 = valueb1 = valueb2 = valueb3 = valuec1 = valuec2 = valuec3 = " "

def board():
    """ prints the tic-tac-toe board"""
    print(" +---A------B------C--+")
    print(" |      |      |      |")
    print("1|  " + valuea1 +"   |  " + valueb1 +"   |  " + valuec1 + "   |")
    print(" |      |      |      |")
    print(" ----------------------")
    print(" |      |      |      |")
    print("2|  " + valuea2 +"   |  " + valueb2 +"   |  " + valuec2 + "   |")
    print(" |      |      |      |")
    print(" ----------------------")
    print(" |      |      |      |")
    print("3|  " + valuea3 +"   |  " + valueb3 +"   |  " + valuec3 + "   |")
    print(" |      |      |      |")
    print(" +--------------------+")
    return

random.seed(0) #to make this example reproducible
computer_mark = "X"

randomlist = ("valuea1", "valuec1", "valuea3", "valuec3")
random_result = random.choice(randomlist)
locals()[random_result] = computer_mark

board()
如果执行上述代码,我会得到以下结果:

 +---A------B------C--+
 |      |      |      |
1|      |      |      |
 |      |      |      |
 ----------------------
 |      |      |      |
2|      |      |      |
 |      |      |      |
 ----------------------
 |      |      |      |
3|      |      |  X   |
 |      |      |      |
 +--------------------+

您应该明确地考虑使用<代码>列表<代码>或嵌套列表:

import random

board = [[' ', ' ', ' '],
         [' ', ' ', ' '],
         [' ', ' ', ' ']]
然后可以访问随机场(主列表包含3个列表,每个列表包含3个项目:

random_field = random.randint(0, 2), random.randint(0, 2)
board[random_field[0]][random_field[1]] = 'x'
print(board)
# [[' ', ' ', ' '], [' ', ' ', 'x'], [' ', ' ', ' ']]  # just one example
或者,如果您希望从多种可能性中进行随机选择:

random_field = random.choice([(0, 0), (1, 1), (2, 2)])
board[random_field[0]][random_field[1]] = 'x'
print(board)
# [[' ', ' ', ' '], [' ', 'x', ' '], [' ', ' ', ' ']]  # sets only one of the diagonals

我建议使用,而不是每个选项都有一个变量,然后随机分配给其中一个。我会尽可能地查看列表。问题是我尝试了locals()这样,我仍然得到相同的结果。我的板仍然是空的,特别是,我的板值仍然是空的。你说的“相同的结果”是什么意思?我更新了我的OP以显示我的“板”是如何看起来像。如果我把事情弄糊涂了,很抱歉。我对这方面还不太熟悉。很抱歉,我应该在一个单独的文件中运行您的代码。显然,我的完整程序中的某些内容导致了问题。但是,您的方法工作得很好。谢谢。我的board()函数,以及我的代码的其余部分,都被嵌套在一个更大的函数中。我想我只是不知道函数是如何工作的。所以我删除了它,并用一个while True语句替换了它。我想让游戏可以重新玩,但显然将我所有的代码嵌套在一个我可以再次调用的函数中是个坏主意。事实上,尽管我考虑使用一个9维一维数组,当你去显示它时,只显示它作为一个网格。非常感谢。我只编码了大约2个星期,还没有得到列表。但是我可以开始研究和尝试,因为我在这个主题上。我将使用EUE示例和其他的复本作为样本。帮助我学习。@Irvillesh好的,在这种情况下,我不应该在示例中使用嵌套列表,我假设您知道
列表
,因为您在
中使用了
元组
(“valuea1”、“valuec1”、“valuea3”、“valuec3”)
:)@DavidZ您完全正确。我选择嵌套列表主要是因为它在视觉上(没有显示功能)比非嵌套列表更直观。@MSeifert我边走边学。当我在编写代码时,我需要知道如何做一些我还不知道的事情,我只是用谷歌搜索它,然后通过这种方式学习这个概念。这一切都很顺利。我还利用一些在线书籍作为参考,试图把我推向正确的方向。
random_field = random.choice([(0, 0), (1, 1), (2, 2)])
board[random_field[0]][random_field[1]] = 'x'
print(board)
# [[' ', ' ', ' '], [' ', 'x', ' '], [' ', ' ', ' ']]  # sets only one of the diagonals