質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

1178閲覧

オセロのエラーが取れない python3

ab_bcde

総合スコア24

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

1グッド

1クリップ

投稿2020/03/12 05:57

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()
s.k👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

Y = i+y[k]*timesY = j+y[k]*timesの間違いでは。

老婆心ながら、バグを出しにくい命名をした方がいいと思います。たとえば、i,jではなくてhx,hyであったら、Y = hx + y[k] * timesというコードになり、「なぜyにxを足してるんだ?」と、間違いに気付きやすいはず。

投稿2020/03/13 03:42

編集2020/03/13 03:49
Lhankor_Mhy

総合スコア36126

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ab_bcde

2020/03/13 04:45

ありがとうございます。あなたのおかげでミスを治せました。この他にもミスがありましたが、それは自力で直せました! オセロの手を反映することに成功しました、ありがとう! 命名について考え直します!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問