2016-05-19 4 views
-1

Python connect 4ゲームでこのエラーが発生し続けます。私は、配列のサイズを大きくしようとしたが、私はまだ、このエラーが発生します。ここでは私は配列インデックスが範囲外であることを続けています

if board[x,y] == Player1 or board[x,y] == Empty: 
[ASE]: IndexError: index 8 is out of bounds for axis 0 with size 8 

は私のコードです:

def ai_player(board, turn, valid_moves): 

WIDTH = 10 
HEIGHT = 8 
Player1 = '1' 
Opponent = '2' 
Empty = '0' 

s=[8,10] 
board= np.zeros(s) 
lines = [] 
#print board 
valid_moves=[0,1,2,3,4,5,6,7,8,9] 
for turn in range(0,43,1): 
    if turn<=42: 

    #vertical win 

     for x in range(WIDTH): 
      valid_moves = [] 
      for y in range(HEIGHT): 
       if board[x,y] == Player1 or board[x,y] == Empty: 
        valid_moves.append((x, y)) 
        if len(valid_moves) >=4: 
         lines.append(valid_moves[-4:]) 

       else: 
        valid_moves = [] 

    # Horizontal win 

    for y in range(HEIGHT): 
      valid_moves = [] 
      for x in range(WIDTH): 
       if board[x,y] == Player1 or board[x,y] == Empty: 
        valid_moves.append((x,y)) 
        if len(valid_moves) >=4: 
         lines.append(valid_moves[-4:]) 

       else: 
        valid_moves = [] 


    for cx in range(WIDTH -1): 
      sx , sy = max(cx -2, 0), abs(min(cx-2, 0)) 
      winning = [] 
      for cy in range(HEIGHT): 
       x, y = sx = cy, sy + cy 
       if x < 0 or y < 0 or x > WIDTH or y >= HEIGHT: 
        continue 
       if(board[x, y] == Player1 or board[x,y] == Empty): 
        valid_moves.append((x,y)) 
        if len(valid_moves) >= 4: 
         lines.append(valid_moves[-4:]) 
       else: 
        winning = [] 

    #other Diagonal win 

    for cs in range(WIDTH -1): 
      sx, sy = WIDTH - 1 - max(cx-2,0), abs(min(cx - 2, 0)) 
      winning = [] 
      for cy in range(HEIGHT): 
       x,y = sx-cy, sy + cy 
       if x < 0 or y < 0 or x >= WIDTH or y >= HEIGHT: 
        continue 
       if board[x,y] == Player1 or board[x,y] == Empty: 
        valid_moves.append((x,y)) 
        if len(valid_moves) >=4: 
         lines.append(valid_moves[-4:]) 
       else: 
        valid_moves = [] 

    return lines 

else: 
    ind=r.randint(0,9) 
    return valid_moves[ind] 
+0

どのようにこの中に 'pycharm'タグフィットしますか? – AKS

答えて

1

あなたは間違って配列を初期化しています。試してみてください

s = [10, 8] 

またはより良い

s = [WIDTH, HEIGHT] 
関連する問題