ETH官方钱包

前往
大廳
主題

[小工具練習] 囲碁紀錄小工具 Ver. 0.1 (緩慢更新中)

テリ君(桃夫模式) | 2023-04-01 16:06:49 | 巴幣 8 | 人氣 226

安安這裡是泰瑞
其實這個東西有九成都是寒假的時候做的
先附上GitHub

主要是寒假心血來潮想要玩圍棋
但想要記錄自己有多爛
所以就做了這個紀錄程式
囲碁紀錄!!

程式本身是pygame構成的
目前就是有主選單
可以選黑白先後手的功能(雖然一定是執黑先,所以這個功能沒屁用= =)
然後程式本身就是可以點擊下子
顯示棋子和手數
不過目前還沒有做出若是棋子被吃會不見的部分
預計0.2版會新增上去
然後簡單講一下這個版面怎麼做的
基本上第一個是怎麼把棋盤畫出來
首先我們先來看這張圖
有點醜請見諒
這個就是棋盤上所有可能性的編號
然後我們再照這個可能性寫成一個List
再import到主程式

# 0 = normal 1 = point 2 = top 3 = bottom 4 = left 5 = right 6 = top left 7 = top right 8 = bot left 9 = top right
boards = [
    [6, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 7], # a
    [4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5], # b
    [4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5], # c
    [4, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 5], # d
    [4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5], # e
    [4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5], # f
    [4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5], # g
    [4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5], # h
    [4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5], # i
    [4, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 5], # j
    [4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5], # k
    [4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5], # l
    [4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5], # m
    [4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5], # n
    [4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5], # o
    [4, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 5], # p
    [4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5], # q
    [4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5], # r
    [8, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 9]  # s
]
然後還有該位置是否有子
預設為N

bc = [
    ['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
    ['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
    ['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
    ['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
    ['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
    ['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
    ['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
    ['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
    ['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
    ['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
    ['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
    ['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
    ['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
    ['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
    ['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
    ['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
    ['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
    ['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
    ['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N']
]

之後就是把大小規劃好
就可以DrawBoard了

# draw board
def draw_board(tabs):
    K = 40
    for i in range(len(boards)):
        for j in range(len(boards[i])):
            if boards[i][j] == 0: # normal point
                pg.draw.line(screen, B, (K * j + K / 2 , K * i), (K * j + K / 2, K * (i +1)), 2)
                pg.draw.line(screen, B, (K * j, K * i + K / 2), (K * (j + 1), K * i + K / 2), 2)
            if boards[i][j] == 1: # star point
                pg.draw.line(screen, B, (K * j + K / 2 , K * i), (K * j + K / 2, K * (i +1)), 2)
                pg.draw.line(screen, B, (K * j, K * i + K / 2), (K * (j + 1), K * i + K / 2), 2)
                pg.draw.circle(screen, B, (K * j + K / 2 + 1, K * i + K / 2 + 1), 6, 0) # +1 to make slight fix
            if boards[i][j] == 2: # top
                pg.draw.line(screen, B, (K * j, K * i + K / 2), (K * (j + 1), K * i + K / 2), 2)
                pg.draw.line(screen, B, ((K * j + K / 2, K * i + K / 2)), ((K * j + K / 2, K * i + K)), 2)
            if boards[i][j] == 3: # bottom
                pg.draw.line(screen, B, (K * j, K * i + K / 2), (K * (j + 1), K * i + K / 2), 2)
                pg.draw.line(screen, B, ((K * j + K / 2, K * i + K / 2)), ((K * j + K / 2, K * i)), 2)
            if boards[i][j] == 4: # left
                pg.draw.line(screen, B, (K * j + K / 2, K * i + K / 2), (K * (j + 1), K * i + K / 2), 2)
                pg.draw.line(screen, B, ((K * j + K / 2, K * i)), ((K * j + K / 2, K * (i + 1))), 2)
            if boards[i][j] == 5: # right
                pg.draw.line(screen, B, (K * j + K / 2, K * i + K / 2), (K * j, K * i + K / 2), 2)
                pg.draw.line(screen, B, ((K * j + K / 2, K * i)), ((K * j + K / 2, K * (i + 1))), 2)
            if boards[i][j] == 6: # top left
                pg.draw.line(screen, B, (K * j + K / 2, K * i + K / 2), (K * j + K, K * i + K / 2), 2)
                pg.draw.line(screen, B, (K * j + K / 2, K * i + K / 2), (K * j + K / 2, K * i + K), 2)
            if boards[i][j] == 7: # top right
                pg.draw.line(screen, B, (K * j + K / 2, K * i + K / 2), (K * j, K * i + K / 2), 2)
                pg.draw.line(screen, B, (K * j + K / 2, K * i + K / 2), (K * j + K / 2, K * i + K), 2)
            if boards[i][j] == 8: # down left
                pg.draw.line(screen, B, (K * j + K / 2, K * i + K / 2), (K * j + K, K * i + K / 2), 2)
                pg.draw.line(screen, B, (K * j + K / 2, K * i + K / 2), (K * j + K / 2, K * i), 2)
            if boards[i][j] == 9: # down right
                pg.draw.line(screen, B, (K * j + K / 2, K * i + K / 2), (K * j, K * i + K / 2), 2)
                pg.draw.line(screen, B, (K * j + K / 2, K * i + K / 2), (K * j + K / 2, K * i), 2)

    for i in range(len(tabs)):
            for j in range(len(tabs)):
                if tabs[i][j] == 'B':
                    screen.blit(b_piece, (K * j + 4 , K * i + 4))
                if tabs[i][j] == 'W':
                    screen.blit(w_piece, (K * j + 4 , K * i + 4))
概念上是這樣

之後就是用clickEvent去偵測是否要下子,再去判斷座標下子
基本上整個程式就是這樣
只是我卡在我不知道原來remove跟pop的差別是這樣
= =
好好笑
大概就這樣ㄅ
Back space可以一直回上一步
下面是我之前跟AI打的一場
我真的不會玩= =
更新後再說
ㄅㄅ

創作回應

貓狗喵
格子直接畫一整條線比較快,draw 的結果是可以交疊的,後續下的棋也是哪邊有棋哪邊再畫上去就好
2023-04-01 16:18:51
テリ君(桃夫模式)
那時候是看pacMan教學臨時起意做的,現在想想這樣也是很複雜www
2023-04-01 16:33:10
柳夜魚
看的整個一頭霧水(好複雜喔…)
2023-04-04 20:53:53
テリ君(桃夫模式)
班長有興趣學嗎?哈哈
2023-04-06 09:46:00

更多創作