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

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

新規登録して質問してみよう
ただいま回答率
85.48%
JupyterLab

JupyterLabは、Jupyter notebookの後継の対話型開発環境(IDE)です。データの可視化がインタラクティブで、プラグイン作成により新しいコンポーネントの追加および既存のコンポーネントも統合可能。サーバに閉じているため、データ分析に向いています。

Python

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

Q&A

解決済

1回答

625閲覧

JupyterLabのオセロのコンピューターがすべて打ってしまう

onigiri1889

総合スコア2

JupyterLab

JupyterLabは、Jupyter notebookの後継の対話型開発環境(IDE)です。データの可視化がインタラクティブで、プラグイン作成により新しいコンポーネントの追加および既存のコンポーネントも統合可能。サーバに閉じているため、データ分析に向いています。

Python

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

0グッド

0クリップ

投稿2022/12/01 00:15

前提

jupyterlabでオセロのコンピューターを作っています

実現したいこと

  • コンピューターが一手ごとに動くようにする

発生している問題

プログラムを実行するとこのように表示される

コンピューターが考えています... 1 2 3 4 5 6 7 8 1 ○ ○ ○ ○ ● ● ● ● 2 ● ○ ○ ○ ● ○ ○ ○ 3 ○ ● ○ ● ● ○ ○ ○ 4 ○ ● ● ○ ○ ○ ○ ○ 5 ○ ● ● ● ○ ○ ○ ○ 6 ○ ● ● ● ● ○ ● ● 7 ○ ○ ● ○ ○ ● ● ● 8 ○ ● ● ● ● ● ● ● 終了 1 2 3 4 5 6 7 8 1 ○ ○ ○ ○ ● ● ● ● 2 ● ○ ○ ○ ● ○ ○ ○ 3 ○ ● ○ ● ● ○ ○ ○ 4 ○ ● ● ○ ○ ○ ○ ○ 5 ○ ● ● ● ○ ○ ○ ○ 6 ○ ● ● ● ● ○ ● ● 7 ○ ○ ● ○ ○ ● ● ● 8 ○ ● ● ● ● ● ● ● あなたの番です (横縦)の形で入力してください

該当のソースコード

python

1import pandas as pd 2import numpy as np 3import random 4 5BLACK = 1 6WHITE = 2 7proc = 0 8now_board = pd.DataFrame([["-","-","-","-","-","-","-","-"], 9 ["-","-","-","-","-","-","-","-"], 10 ["-","-","-","-","-","-","-","-"], 11 ["-","-","-","-","-","-","-","-"], 12 ["-","-","-","-","-","-","-","-"], 13 ["-","-","-","-","-","-","-","-"], 14 ["-","-","-","-","-","-","-","-"], 15 ["-","-","-","-","-","-","-","-"]]) 16now_board.columns = ["1","2","3","4","5","6","7","8"] 17now_board.index = ["1","2","3","4","5","6","7","8"] 18board = [] 19put_x = 0 20put_y = 0 21can_put_X = [] 22can_put_Y = [] 23turn = 0 24color = [1,2] 25who = ["コンピューター","あなた"] 26black_number = 0 27white_number = 0 28memory_can_X = [] 29memory_can_Y = [] 30put_number = 0 31memory_board = [] 32memory_white_number = 0 33memory_black_number = 0 34auto_result_X = 0 35auto_result_Y = 0 36for y in range(8): 37 board.append([0]*8) 38 39def board_init(): 40 board[3][4] = BLACK 41 board[3][3] = WHITE 42 board[4][3] = BLACK 43 board[4][4] = WHITE 44 45def board_surface(): 46 for x in range(8): 47 for y in range(8): 48 if board[y][x]==0: 49 now_board.iloc[y,x] = "-" 50 if board[y][x]==BLACK: 51 now_board.iloc[y,x] = "●" 52 if board[y][x]==WHITE: 53 now_board.iloc[y,x] = "○" 54 55def put_input(): 56 global put_x, put_y 57 put_xy = input("(横縦)の形で入力してください") 58 while len(put_xy)!=2: 59 put_xy = input("二文字で入力してください") 60 put_x = put_xy[0] 61 put_y = put_xy[1] 62 put_x = int(put_x) - 1 63 put_y = int(put_y) - 1 64 65def can_return(x,y,color): 66 if board[y][x]>0: 67 return -1 68 total = 0 69 for dx in range(-1,2): 70 for dy in range(-1,2): 71 k = 0 72 nx = x 73 ny = y 74 if total>0: 75 return total 76 while True: 77 nx+=dx 78 ny+=dy 79 if nx>7 or nx<0 or ny>7 or ny<0: 80 break 81 if board[ny][nx]==0: 82 break 83 if board[ny][nx]==3-color: 84 k+=1 85 if board[ny][nx]==color: 86 total+=k 87 break 88 return total 89 90def memory_can_return(x,y,color): 91 if memory_board[y][x]>0: 92 return -1 93 total = 0 94 for dx in range(-1,2): 95 for dy in range(-1,2): 96 k = 0 97 nx = x 98 ny = y 99 if total>0: 100 return total 101 while True: 102 nx+=dx 103 ny+=dy 104 if nx>7 or nx<0 or ny>7 or ny<0: 105 break 106 if memory_board[ny][nx]==0: 107 break 108 if memory_board[ny][nx]==3-color: 109 k+=1 110 if memory_board[ny][nx]==color: 111 total+=k 112 break 113 return total 114 115def put_stone(x,y,color): 116 board[y][x] = color 117 for dy in range(-1,2): 118 for dx in range(-1,2): 119 k = 0 120 sx = x 121 sy = y 122 while True: 123 sx +=dx 124 sy +=dy 125 if sx<0 or sx>7 or sy<0 or sy>7: 126 break 127 if board[sy][sx]==0: 128 break 129 if board[sy][sx]==3-color: 130 k += 1 131 if board[sy][sx]==color: 132 for i in range(k): 133 sx -=dx 134 sy -=dy 135 board[sy][sx] = color 136 break 137 138def memory_put_stone(x,y,color): 139 memory_board[y][x] = color 140 for dy in range(-1,2): 141 for dx in range(-1,2): 142 k = 0 143 sx = x 144 sy = y 145 while True: 146 sx +=dx 147 sy +=dy 148 if sx<0 or sx>7 or sy<0 or sy>7: 149 break 150 if memory_board[sy][sx]==0: 151 break 152 if memory_board[sy][sx]==3-color: 153 k += 1 154 if memory_board[sy][sx]==color: 155 for i in range(k): 156 sx -=dx 157 sy -=dy 158 memory_board[sy][sx] = color 159 break 160 161def dis_can_put(color): 162 can_put_X.clear() 163 can_put_Y.clear() 164 for x in range(8): 165 for y in range(8): 166 if can_return(x,y,color)>0: 167 can_put_X.append(x) 168 can_put_Y.append(y) 169 if len(can_put_X)>0: 170 return True 171 return False 172 173def memory_dis_can_put(color): 174 can_put_X.clear() 175 can_put_Y.clear() 176 for x in range(8): 177 for y in range(8): 178 if memory_can_return(x,y,color)>0: 179 can_put_X.append(x) 180 can_put_Y.append(y) 181 if len(can_put_X)>0: 182 return True 183 return False 184 185def stone_number(): 186 global black_number, white_number 187 for x in range(8): 188 for y in range(8): 189 if board[y][x]==BLACK: 190 black_number+=1 191 if board[y][x]==WHITE: 192 white_number+=1 193 194def memory_stone_number(): 195 global memory_black_number, memory_white_number 196 for x in range(8): 197 for y in range(8): 198 if memory_board[y][x]==BLACK: 199 memory_black_number+=1 200 if memory_board[y][x]==WHITE: 201 memory_white_number+=1 202 203def auto_turn(color): 204 global memory_board, auto_result_X, auto_result_Y 205 memory_board = board 206 memory_can_X.clear() 207 memory_can_Y.clear() 208 now_memory_can_X = [] 209 now_memory_can_Y = [] 210 win_lose_number = [] 211 now_color = [1,2] 212 now_turn = 0 213 win_lose_number.clear() 214 now_win_lose = 0 215 i=0 216 now_proc = 0 217 rondom_index = 0 218 for x in range(8): 219 for y in range(8): 220 if can_return(x,y,color)>0: 221 memory_can_X.append(x) 222 memory_can_Y.append(y) 223 for n in memory_can_X: 224 win_lose_number.append(0) 225 for n in memory_can_X: 226 for m in range(100): 227 memory_board = board 228 now_proc = 0 229 while not(memory_dis_can_put(BLACK)==False and memory_dis_can_put(WHITE)==False): 230 if now_proc==0: 231 now_turn = 1 232 now_proc = 1 233 now_turn = 1-now_turn 234 now_memory_can_X.clear() 235 now_memory_can_Y.clear() 236 if memory_dis_can_put(now_color[now_turn])==False: 237 now_turn = 1-now_turn 238 for x in range(8): 239 for y in range(8): 240 if memory_can_return(x,y,now_color[now_turn])>0: 241 now_memory_can_X.append(x) 242 now_memory_can_Y.append(y) 243 random_index = random.randint(0,len(now_memory_can_X)-1) 244 memory_put_stone(now_memory_can_X[random_index],now_memory_can_Y[random_index],now_color[now_turn]) 245 memory_stone_number() 246 if memory_black_number>memory_white_number and color==BLACK: 247 win_lose_number[i]+=1 248 i+=1 249 result_index = win_lose_number.index(max(win_lose_number)) 250 auto_result_X = memory_can_X[result_index] 251 auto_result_Y = memory_can_Y[result_index] 252 253def main(): 254 global proc 255 while True: 256 if proc==0: 257 board_init() 258 board_surface() 259 turn = 0 260 proc = 1 261 if proc==1: 262 if color[turn]==WHITE: 263 for x in range(8): 264 for y in range(8): 265 if can_return(x,y,WHITE)>0: 266 now_board.iloc[y,x] = "*" 267 print(now_board) 268 print("あなたの番です") 269 put_input() 270 put_stone(put_x,put_y,WHITE) 271 board_surface() 272 print(now_board) 273 proc = 2 274 if color[turn]==BLACK: 275 print("コンピューターが考えています...") 276 auto_turn(BLACK) 277 put_stone(auto_result_X, auto_result_Y, BLACK) 278 board_surface() 279 print(now_board) 280 proc = 2 281 if proc==2: 282 turn = 1-turn 283 if dis_can_put(BLACK)==False and dis_can_put(WHITE)==False: 284 print("終了") 285 proc = 3 286 elif dis_can_put(color[turn])==False: 287 print(who[turn] + "は打てないのでパスです") 288 turn = 1-turn 289 proc = 1 290 if proc==3: 291 stone_number() 292 if black_number>white_number: 293 print("コンピューターの勝ち") 294 if white_number>black_number: 295 print("あなたの勝ち") 296 if white_number==black_number: 297 print("引き分け") 298main()

試したこと

boardに書き込んでいるところがないか探しましたが僕が見た中では見つかりませんでした。

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

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

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

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

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

guest

回答1

0

ベストアンサー

board_suface関数の中でboardからnow_boardに代入しているので、boardが問題でしょう。
少しばかり調べてみましたが、auto_turn関数の中の処理がうまくいってないことが原因かと思います。

auto_turn(BLACK)を呼ぶ前 -------- -------- -------- ---●○--- ---○●--- -------- -------- -------- auto_turn(BLACK)を呼んだ後 ○●●●●●●● ○●●○○●●● ○○○○○○●● ○○○○●○●● ○○○○○●●● ○●●○●●●● ○○○○○○○● ○○○○○○○○

auto_turn関数の中の

memory_board = board

これとか結構怪しいと思います。リストをコピーしたいときは、それに対応する関数などを使ってください。(copy.deepcopyなど)リスト=リストは私たちが想定している動作になりません。

投稿2022/12/01 03:12

CreeperSaviour

総合スコア129

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

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

onigiri1889

2022/12/01 04:15 編集

copy.deepcopyを使ったらできました!ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問