python3
1from pprint import pprint 2import random 3board = [[0 for _ in range(8)]for _ in range(8)] 4 5board[3][3] = 1 6board[4][4] = 1 7board[3][4] = -1 8board[4][3] = -1 9 10#黒=1 , 白=-1, 無し=0 11 12def can_put(black_or_white,board): 13 if black_or_white == 1: 14 put = 1 15 else: 16 put = -1 17 18 # 19 x = [-1,0,1,0,-1,1,-1,1] 20 y = [0,1,0,-1,1,1,-1,-1] 21 return_list =[] 22 for i in range(8): 23 for j in range(8): 24 for k in range(4): 25 times = 1 26 temp_list = [[i,j]] 27 for _ in range(7): 28 X = i+x[k]*times 29 Y = i+y[k]*times 30 if 0>X or X>7: 31 break 32 if 0>Y or Y>7: 33 break 34 if times >1: 35 if board[X][Y] == put: 36 return_list = temp_list 37 break 38 #print(X,Y) 39 if board[X][Y] == put*(-1): 40 temp_list.append([X,Y]) 41 times += 1 42 if len(return_list) > 1: 43 return True 44 return False 45 46 47 48def return_output(black_or_white,i,j,board): 49 # 50 if black_or_white == 1: 51 put = 1 52 else: 53 put = -1 54 55 x = [-1,0,1,0,-1,1,-1,1] 56 y = [0,1,0,-1,1,1,-1,-1] 57 return_list = [] 58 for k in range(len(x)): 59 times = 1 60 temp_list = [[i,j]] 61 62 for _ in range(7): 63 X = i+x[k]*times 64 Y = i+y[k]*times 65 print(X,Y) 66 if 0>X or X>8: 67 break 68 if 0>Y or Y>8: 69 break 70 if times >1: 71 if board[X][Y] == put: 72 return_list = temp_list 73 return return_list 74 75 if board[X][Y] == put*(-1): 76 temp_list.append([X,Y]) 77 times += 1 78 79 80 81def update(black_or_white,return_list,board): 82 print(return_list) 83 for i in range(len(return_list)): 84 x = return_list[i][0] 85 y = return_list[i][1] 86 board[x][y] = black_or_white 87 return board 88 89def black_strategy(board): 90 91 return 2,4 92 93def white_strategy(board): 94 return 2,3 95 96def judge(board): 97 b = 0 98 w = 0 99 for i in range(8): 100 for j in range(8): 101 if board[i][j] == 1: 102 b += 1 103 elif board[i][j] == -1: 104 w += 1 105 return b,w 106 107def main(board): 108 black = 1 109 white = -1 110 while True: 111 #can_put(black,board) or can_put(white,board): 112 while True: 113 #can_put(black): 114 output = black_strategy(board) 115 board = update( 116 black, 117 return_output(black,output[0],output[1],board), 118 board 119 ) 120 121 while True: 122 #can_put(white): 123 output = white_strategy(board) 124 board = update( 125 white, 126 return_output(white,output[0],output[1],board),#リスト 127 board 128 ) 129 130 black_score,white_score = judge(board) 131 132 if black_score > white_score: 133 print("win:black"+str(black_score)+"-"+str(white_score)) 134 elif black_score < white_score: 135 print("win:white"+str(black_score)+"-"+str(white_score)) 136 else: 137 print("even:"+str(black_score)+"-"+str(white_score)) 138 pprint(board) 139 140main(board) 141> None 142Traceback (most recent call last): 143 File "othello1.py", line 140, in <module> 144 main(board) 145 File "othello1.py", line 118, in main 146 board 147 File "othello1.py", line 83, in update 148 for i in range(len(return_list)): 149TypeError: object of type 'NoneType' has no len()
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/03/13 04:45