{"id":297,"date":"2020-01-29T11:01:26","date_gmt":"2020-01-29T02:01:26","guid":{"rendered":"http:\/\/bunktiruu.wpx.jp\/tomosuke-blog\/?p=297"},"modified":"2023-03-16T18:35:12","modified_gmt":"2023-03-16T09:35:12","slug":"python%e3%81%a7%e3%82%aa%e3%82%bb%e3%83%ad%e3%82%92%e3%81%a4%e3%81%8f%e3%81%a3%e3%81%a6%e3%81%bf%e3%81%9f","status":"publish","type":"post","link":"https:\/\/bunktiruu.com\/?p=297","title":{"rendered":"\u3010Python\u3011\u30aa\u30bb\u30ed\u3092\u3064\u304f\u3063\u3066\u307f\u305f"},"content":{"rendered":"\n<p>\u3053\u3093\u306b\u3061\u306f\u3001\u30b3\u30f3\u30c6\u30f3\u30c4\u30af\u30ea\u30a8\u30a4\u30bf\u30fc\u306e\u3068\u3082\u3059\u3051\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u306e\u4ed5\u69d8\u3067\u5b9f\u88c5\u3057\u305f\u3001\u306a\u3093\u3061\u3083\u3063\u3066\u30aa\u30bb\u30ed\u306e\u30b3\u30fc\u30c9\u3092\u516c\u958b\u3057\u307e\u3059\u3002<span class=\"ystdb-inline--1\">\u30b3\u30fc\u30c9\u306f\u6700\u9069\u5316\u3055\u308c\u3066\u3044\u307e\u305b\u3093<\/span>\u306e\u3067\u3001\u3054\u4e86\u627f\u306e\u4e0a\u3054\u89a7\u304f\u3060\u3055\u3044\u3002\u8cbc\u308a\u4ed8\u3051\u305f\u30b3\u30fc\u30c9\u306f\u3001<span class=\"ystdb-inline--1\">\u5ff5\u306e\u305f\u3081\u3053\u3053\u304b\u3089\u30b3\u30d4\u30da\u3057\u3066\u3082\u52d5\u4f5c\u3059\u308b\u3053\u3068\u3092\u78ba\u8a8d\u6e08\u307f<\/span>\u3067\u3059\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5fc5\u305a\u767d\u304b\u3089\u59cb\u307e\u308a\u307e\u3059\u3002<\/li>\n\n\n\n<li>\u76f8\u624b\u304c\u7f6e\u3051\u306a\u304b\u3063\u305f\u3068\u304d\u3001\u518d\u5ea6\u81ea\u5206\u306e\u756a\u3068\u306a\u308b\u3088\u3046\u306b\u3057\u305f\u3064\u3082\u308a\u3067\u3059\u3002<\/li>\n\n\n\n<li>\u4e21\u65b9\u624b\u8a70\u307e\u308a\u3068\u306a\u3063\u305f\u3068\u304d\u3092\u8003\u616e\u3057\u305f\u3064\u3082\u308a\u3067\u3059\u3002<\/li>\n\n\n\n<li>AI\u306f\u7d44\u307f\u8fbc\u3093\u3067\u3044\u307e\u305b\u3093\u3002<\/li>\n\n\n\n<li>GUI\u3092\u4f7f\u3046\u306e\u3067\u3001tkinter\u3092\u4f7f\u7528\u3057\u3066\u3044\u307e\u3059\u3002<\/li>\n\n\n\n<li>\u52d5\u4f5c\u78ba\u8a8d\u306f\u3001 windows\u7248python3.7\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3001cygwin\u3067\u52d5\u304b\u3057\u3066\u3044\u307e\u3059\u3002\n<ul class=\"wp-block-list\">\n<li>cygwin\u306epython\u4f7f\u3048\u3070\u3044\u3044\u306e\u3067\u306f\uff1f\u3068\u601d\u3063\u305f\u306e\u3067\u3059\u304c\u3001tkinter\u3092\u3046\u307e\u304f\u5165\u308c\u3089\u308c\u307e\u305b\u3093\u3067\u3057\u305f\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python line-numbers\">import tkinter as tk\nimport time        # \u77f3\u3092\u3081\u304f\u308b\u901f\u5ea6\u3092\u5909\u3048\u3088\u3046\u3068\u601d\u3063\u3066\u5165\u308c\u307e\u3057\u305f\u304c\u3001\u3046\u307e\u304f\u3044\u304b\u305a\n# -*- coding: utf-8 -*-\n\ncolor = 1\n\n# \u5ea7\u6a19\u3068\u30d9\u30af\u30c8\u30eb\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306e\u30af\u30e9\u30b9\nclass Position:\n    def __init__(self,x,y,dx,dy):\n        self.x  = x+dx\n        self.y  = y+dy\n        self.dx = dx\n        self.dy = dy\n\ndef checkZero(a,b):\n    if a==0:\n        return 0\n    else:\n        return b\n\ndef clearMap(target):\n    global mark_map, reserve_map\n    for x in range(8):\n        for y in range(8):\n            if target==0:\n                mark_map[x][y] = 0\n            else:\n                reserve_map[x][y] = 0\n\ndef reverse():\n    if color==1:\n        return 2\n    else:\n        return 1\n\ndef getColor(color):\n    if color==1:\n        return \"white\"\n    elif color==2:\n        return \"black\"\n    else:\n        return \"no stone\"\n\ndef same(a,b):\n    if a==b:\n        return True\n    else:\n        return False\n\n#\u3053\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u30e1\u30a4\u30f3\u3067\u3059\u3002\u30af\u30ea\u30c3\u30af\u3057\u305f\u3068\u3053\u308d\u306b\u7f6e\u3051\u308b\u304b\u5224\u5b9a\u3057\u307e\u3059\ndef okeruka(mine, pos, flag, indent):\n    global stone_db\n    global reserve_map\n    global mark_map\n    ret = -10\n    dx_list = [-1,0,1]\n    dy_list = [-1,0,1]\n    print(\"{}x={}, y={}, stone={}\".format(indent, pos.x, pos.y, getColor(stone_db[pos.x][pos.y])))\n    indent += \"  \"\n    if stone_db[pos.x][pos.y]==0 and flag==0:\n        mark_map[pos.x-1][pos.y-1] = 1\n        for dx in dx_list:\n            for dy in dy_list:\n                if dx==0 and dy==0:\n                    continue\n                _pos = Position(pos.x, pos.y, dx, dy)\n                ret = checkZero(ret, okeruka(mine, _pos, 1, indent))\n    else:\n        if stone_db[pos.x][pos.y]==0 or stone_db[pos.x][pos.y]==-1: # \u77f3\u304c\u306a\u3044\n            clearMap(1)\n            return -1\n        else:   # \u77f3\u304c\u3042\u308b\n            if flag==1: # \u540c\u8272\u306f\u30c0\u30e1\u3001\u7570\u8272\u306fOK\n                if stone_db[pos.x][pos.y]==mine:    # \u540c\u8272\n                    return -2\n                else:\n                    _pos = Position(pos.x, pos.y, pos.dx, pos.dy)\n                    reserve_map[pos.x-1][pos.y-1] = 1\n                    ret = okeruka(mine, _pos, 2, indent)\n            elif flag==2:   # \u540c\u8272\u3001\u7570\u8272\u3069\u3061\u3089\u3082OK\n                if stone_db[pos.x][pos.y]==mine:\n                    for i in range(8):\n                        for j in range(8):\n                            if reserve_map[i][j]==1:\n                                #mark_map[i][j] = reserve_map[i][j]\n                                mark_map[i][j] = 1\n                    print(\"reserve_map\")\n                    for y in range(8):\n                        for x in range(8):\n                            print(\"{} \".format(reserve_map[x][y]), end=\"\")\n                        print(\"\")\n                            \n                    clearMap(1) # clear reserve_map\n                    return 0\n                else:\n                    _pos = Position(pos.x, pos.y, pos.dx, pos.dy)\n                    reserve_map[pos.x-1][pos.y-1] = 1\n                    ret = okeruka(mine, _pos, 2, indent)\n    return ret\n\n# \u77f3\u3092\u7f6e\u304f\u305f\u3081\u306e\u30e1\u30bd\u30c3\u30c9\u3067\u3059\ndef placeStone(x,y,color):\n    canvas.create_oval(offset_x+pitch*x+margin,     offset_y+pitch*y+margin,\n                       offset_x+pitch*(x+1)-margin, offset_y+pitch*(y+1)-margin,\n                       fill = color)\n    print(\"write ({},{})\".format(x,y))\n \ndef drawStone(mode):\n    global stone_db, mark_map\n    global stone_status\n    global finish, finish2\n    black = 0\n    white = 0\n    if mode==0:\n        for x in range(8):\n            for y in range(8):\n                if stone_db[x+1][y+1]==1:\n                    placeStone(x, y, \"white\")\n                elif stone_db[x+1][y+1]==2:\n                    placeStone(x, y, \"black\")\n    else:\n        for x in range(8):\n            for y in range(8):\n                if mark_map[x][y]==1:\n                    if stone_db[x+1][y+1]==1:\n                        placeStone(x, y, \"white\")\n                    elif stone_db[x+1][y+1]==2:\n                        placeStone(x, y, \"black\")\n                    #time.sleep(1)\n    for x in range(8):\n        for y in range(8):\n            if stone_db[x+1][y+1]==1:\n                white += 1\n            elif stone_db[x+1][y+1]==2:\n                black += 1\n    stone_status.set(\"\u767d\uff1a{}, \u9ed2\uff1a{}\".format(white,black))\n    if (white+black)==64 or finish2==1:\n        finish = 1\n        if white &gt; black:\n            judge_text.set(\"\u767d\u306e\u52dd\u3061\u3067\u3059\")\n        elif black &gt; white:\n            judge_text.set(\"\u9ed2\u306e\u52dd\u3061\u3067\u3059\")\n        else:\n            judge_text.set(\"\u5f15\u304d\u5206\u3051\u3067\u3059\")\n\n \ndef on_click(event):\n    global finish, finish2\n    global color\n    global mark_map, stone_db\n    global next_user\n\n    if finish==1:\n        return 0\n\n    x = event.x\n    y = event.y\n \n    click_x  = -1\n    click_y  = -1\n \n    for i in range(8):\n        for j in range(8):\n            if x &gt;= (offset_x + i*pitch + margin) and x &lt;= (offset_x + (i+1)*pitch - margin) and \\\n               y &gt;= (offset_y + j*pitch + margin) and y &lt;= (offset_y + (j+1)*pitch - margin):\n                   click_x = i+1\n                   click_y = j+1\n                   break\n \n    pos = Position(click_x, click_y, 0, 0)\n    ret = okeruka(color, pos, 0, \"\")\n    if ret==0:\n        print(\"x={}, y={}, can place\".format(click_x, click_y))\n        print(\"--- mark_map ---\")\n        for y in range(8):\n            for x in range(8):\n                print(\"{} \".format(mark_map[x][y]), end=\"\")\n                if mark_map[x][y]==1:\n                    stone_db[x+1][y+1] = color\n                    #print(\"x={}, y={}, placed {}\".format(x+1,y+1,getColor(color)))\n            print(\"\")\n\n        drawStone(1)\n        color = reverse()\n        if color==1:\n            next_user.set(\"\u767d\u306e\u756a\u3067\u3059\")\n        else:\n            next_user.set(\"\u9ed2\u306e\u756a\u3067\u3059\")\n\n        print(\"next color is {}\".format(getColor(color)))\n    else:\n        print(\"x={}, y={}, ret={}, cannot place\".format(click_x, click_y, ret))\n\n    clearMap(0)\n    clearMap(1)\n\n    ret = -1\n    for x in range(8):\n        for y in range(8):\n            pos = Position(x+1, y+1, 0, 0)\n            ret = checkZero(ret, okeruka(color, pos, 0, \"\"))\n    clearMap(0)\n    clearMap(1)\n    \n    if ret==0:  # \u76f8\u624b\u306e\u77f3\u306f\u7f6e\u3051\u308b\n        pass\n    else:       # \u76f8\u624b\u306e\u77f3\u306f\u7f6e\u3051\u306a\u3044\u306e\u3067\u3001\u81ea\u5206\u306e\u77f3\u304c\u7f6e\u3051\u308b\u304b\u78ba\u8a8d\n        ret = -1\n        color = reverse()\n        for x in range(8):\n            for y in range(8):\n                pos = Position(x+1, y+1, 0, 0)\n                ret = checkZero(ret, okeruka(color, pos, 0, \"\"))\n        clearMap(0)\n        clearMap(1)\n        if ret==0:\n            if color==1:\n                next_user.set(\"\u9ed2\u304c\u7f6e\u3051\u306a\u3044\u306e\u3067\u3001\u767d\u306e\u756a\u3067\u3059\")\n            else:\n                next_user.set(\"\u767d\u304c\u7f6e\u3051\u306a\u3044\u306e\u3067\u3001\u9ed2\u306e\u756a\u3067\u3059\")\n        else:\n            finish2 = 1\n            drawStone(1)    # \u77f3\u306e\u72b6\u614b\u3092\u30c1\u30a7\u30c3\u30af\u3055\u305b\u308b\uff08\u9014\u4e2d\u7d42\u4e86\uff09\n\n \n### start\noffset_x = 10\noffset_y = 10\nmargin   = 10\npitch    = 10\n \nroot = tk.Tk()\nroot.title(\"\u30aa\u30bb\u30ed\")\ncanvas = tk.Canvas(root, width = 700, height = 700)\n\ncanvas.pack()\ncanvas.bind(\"&lt;Button-1&gt;\",on_click)\n\n### label\nnext_user = tk.StringVar()\nlabel = tk.Label(root, textvariable=next_user)\nlabel.place(x=10, y=660)\nnext_user.set(\"\u767d\u306e\u756a\u3067\u3059\")\n\nstone_status = tk.StringVar()\nlabel2 = tk.Label(root, textvariable=stone_status)\nlabel2.place(x=200, y=660)\n\njudge_text = tk.StringVar()\nlabel3 = tk.Label(root, textvariable=judge_text)\nlabel3.place(x=400, y=660)\n\n### status\nfinish  = 0     # \u901a\u5e38\u306e\u7d42\u4e86\u30d5\u30e9\u30b0\nfinish2 = 0     # \u9014\u4e2d\u306e\u7d42\u4e86\u30d5\u30e9\u30b0\n\n# \u76e4\u9762\u63cf\u753b\noffset_x = 10\noffset_y = 10\npitch    = 80\n_width   = 0.0\nfor i in range(9):\n    if i==0 or i==8:\n        _width = 3.0\n    else:\n        _width= 1.0\n         \n    canvas.create_line(offset_x,           offset_y + i*pitch,\n                  offset_x + pitch*8, offset_y + i*pitch,\n                  width = _width)\n    canvas.create_line(offset_x + i*pitch, offset_y,\n                  offset_x + i*pitch, offset_y + pitch*8,\n                  width = _width)\n \n### \u77f3\u306e\u914d\u7f6e\n#   -1 \u30a8\u30ea\u30a2\u5916\n#    0 \u4f55\u3082\u7f6e\u3044\u3066\u306a\u3044\n#    1 \u767d\n#    2 \u9ed2\nstone_db    = [[0] * 10 for i in range(10)]    # \u3068\u308a\u3042\u3048\u305a\u5168\u9762\u300c\u4f55\u3082\u7f6e\u3044\u3066\u306a\u3044\u300d\nmark_map    = [[0] * 8  for i in range(8)]     # \u77f3\u3092\u7f6e\u304d\u63db\u3048\u308b\u30c7\u30fc\u30bf\u30de\u30c3\u30d7\nreserve_map = [[0] * 8  for i in range(8)]     # \u4eee\u306e\u3001\u77f3\u3092\u7f6e\u304d\u63db\u3048\u308b\u30c7\u30fc\u30bf\u30de\u30c3\u30d7\n#print(\"DBG(0) : mark_map.id={}\".format(id(mark_map)))\nfor x in range(10):\n    for y in range(10):\n        if x==0 or x==9 or y==0 or y==9:\n            stone_db[x][y] = -1\n        if (x==4 and y==4) or (x==5 and y==5):\n            stone_db[x][y] = 1\n        if (x==4 and y==5) or (x==5 and y==4):\n            stone_db[x][y] = 2\n \n### \u6700\u521d\u306e\u77f3\u306e\u914d\u7f6e\n#placeStone(3,3,\"black\")\n#placeStone(4,4,\"black\")\n#placeStone(4,3,\"white\")\n#placeStone(3,4,\"white\")\ndrawStone(0)\n \nroot.mainloop()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u30b3\u30f3\u30c6\u30f3\u30c4\u30af\u30ea\u30a8\u30a4\u30bf\u30fc\u306e\u3068\u3082\u3059\u3051\u3067\u3059\u3002 \u4ee5\u4e0b\u306e\u4ed5\u69d8\u3067\u5b9f\u88c5\u3057\u305f\u3001\u306a\u3093\u3061\u3083\u3063\u3066\u30aa\u30bb\u30ed\u306e\u30b3\u30fc\u30c9\u3092\u516c\u958b\u3057\u307e\u3059\u3002\u30b3\u30fc\u30c9\u306f\u6700\u9069\u5316\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u306e\u3067\u3001\u3054\u4e86\u627f\u306e \u2026<\/p>\n","protected":false},"author":1,"featured_media":2822,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[51,56,75],"class_list":{"0":"post-297","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-develop","8":"tag-othello","9":"tag-python","10":"tag-75"},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/bunktiruu.com\/index.php?rest_route=\/wp\/v2\/posts\/297","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bunktiruu.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bunktiruu.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bunktiruu.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bunktiruu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=297"}],"version-history":[{"count":5,"href":"https:\/\/bunktiruu.com\/index.php?rest_route=\/wp\/v2\/posts\/297\/revisions"}],"predecessor-version":[{"id":3428,"href":"https:\/\/bunktiruu.com\/index.php?rest_route=\/wp\/v2\/posts\/297\/revisions\/3428"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/bunktiruu.com\/index.php?rest_route=\/wp\/v2\/media\/2822"}],"wp:attachment":[{"href":"https:\/\/bunktiruu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bunktiruu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bunktiruu.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}