我是python的新手,作为一项任务的一部分,我必须创建一个连接4游戏。我有这个功能的valid_moves部分,但我不知道如何检查对手盘的位置是什么以及我应该如何将我的盘放在避免对手获胜的位置,但是为我提供了获胜的优势。这是代码:
def ai_player(board, turn, valid_moves):
"""
Inputs:
board: numpy array of the disks for each player, e.g.
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 2, 0, 0, 0, 0, 0],
[0, 0, 1, 2, 1, 0, 0, 0, 0, 0]]
- 0 empty locations
- 1 your disks
- 2 your opponents disks
turn: integer turn counter (starts from 1)
valid_moves: numpy array of valid col index numbers where a disk can be
placed, e.g. [0, 1, 2, 3, 4, 5, 6]
Return:
col index number --> an integer number of the col where you want to
place a disk e.g. 0 (NB: this return value must
appear in the valid_moves array)
"""
colIndex = range(10)
for i in range(len(valid_moves) - 1):
if turn == 1:
ind = colIndex[4]
elif turn > 1 and (len(valid_moves) > 0):
if valid_moves[colIndex[i] + 1] == 0 and colIndex[i] < 10:
ind += 1
elif valid_moves[colIndex[i] - 1] == 0:
ind -= 1
else:
# choose a random move to make from the valid_moves list
ind = random.randint(0, len(valid_moves)-1)
#This is my code to add my coin (1) at an empty position and after the opponents coin (2)
for j in range(len(valid_moves) - 1):
if(colIndex[i] == 2):
colIndex[i + 1] = 1
elif colIndex[i] == 0 and colIndex[i] == 1 and colIndex[i] != 2:
colIndex[i + 1] = 1
return valid_moves[ind]
非常感谢任何帮助。
答案 0 :(得分:0)
This is more of an AI problem rather than a Python one. I'm unsure whether you are familiar with the term heuristics applied to the field of computer science but imagine this situation: you start the game and you want to make a move. To automatically plan your move (and to make a meaningful move that could make you win the game) you would need to consider the oponent's possibilities after you make your move.
To do this, you could try listing all possible moves and assign a "score" (a numerical value) to those moves depending on how good they are. Example: a move that makes you win the game has to have the maximum score.
From a over-simplified point of view, the concept of heuristics is to assign a "score" to the possible actions to decide which move is the one you, or the machine, want to make. To code a good strategy to winning is to code several good and meaningful strategies and putting them all together.
How to decide on the strategies? You could think of several game situations and to determine which action is better. You could ask yourself the following questions:
If you consider several heuristics, you could automatically determine which is the best possible move given the board and a simulation of the X following moves (to avoid making the problem computationally unsolvable). I'm sure you can find some other examples on several academic webpages, and you could find examples of heuristics even googling "connect 4 heuristics".
Edit: actually, I just found a webpage explaining possible applicable heuristics.