From d7a77b73c9e37bab85f1e8b5fa67fb327c996ab7 Mon Sep 17 00:00:00 2001 From: oloturia <5429234+oloturia@users.noreply.github.com> Date: Sat, 30 Oct 2021 03:05:01 +0200 Subject: [PATCH 1/6] alignment not good --- damastodon.py | 3 +-- draughts_engine.py | 5 ++--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/damastodon.py b/damastodon.py index 919b531..d98b9ec 100755 --- a/damastodon.py +++ b/damastodon.py @@ -7,7 +7,6 @@ import pickle import random import os import time -import re import sys api_url = sys.argv[1] @@ -62,7 +61,7 @@ def check_message(notification): pickle.dump("@"+challenger,f) pickle.dump(False,f) pickle.dump(board,f) - mastodon.status_post("◾: @"+account+" ◽: @"+challenger+" turn ◽\n"+dama.draw_checkerboard(board,space="🟥 ",white_norm="◽ ",white_knight="⚪ ",black_norm="◾ ",black_knight="⚫ ",empty="🟦 ",frstrow="0🇦 🇧 🇨 🇩 🇪 🇫 🇬 🇭 \n"),visibility="direct") + mastodon.status_post("◾: @"+account+" ◽: @"+challenger+" turn ◽\n"+dama.draw_checkerboard(board,space="🟥",white_norm="◽",white_knight="⚪",black_norm="◾",black_knight="⚫",empty="🟦",frstrow="0 🇦 🇧 🇨 🇩 🇪 🇫 🇬 🇭 \n"),visibility="direct") return elif content.split(" ")[1].lower() == "no": os.remove(save_position+content.split(" ")[0][1:]) diff --git a/draughts_engine.py b/draughts_engine.py index fcc4212..617b667 100755 --- a/draughts_engine.py +++ b/draughts_engine.py @@ -2,12 +2,11 @@ import re -def draw_checkerboard(status,space="▒",white_norm="h",white_knight="H",black_norm="b",black_knight="B",empty=" ",frstrow=" abcdefgh\n"): +def draw_checkerboard(status,space="▒",white_norm="h",white_knight="H",black_norm="b",black_knight="B",empty=" ",frstrow=" abcdefgh\n",column="12345678"): bstr = "" bstr += frstrow for row in range(0,len(status)): - bstr += str(row+1)+"" - + bstr += column[row] if row % 2 == 0: bstr += space for cell in range(0,int(len(status)/2)): From 90748cee5d29c8ce93f22955c5f6a7a6ff5f20ba Mon Sep 17 00:00:00 2001 From: oloturia <5429234+oloturia@users.noreply.github.com> Date: Sat, 30 Oct 2021 03:06:31 +0200 Subject: [PATCH 2/6] typo --- damastodon.py | 1 + 1 file changed, 1 insertion(+) diff --git a/damastodon.py b/damastodon.py index d98b9ec..27f5c5f 100755 --- a/damastodon.py +++ b/damastodon.py @@ -8,6 +8,7 @@ import random import os import time import sys +import re api_url = sys.argv[1] save_position = "/tmp/" From 8e12b8e259a459d567262c39b2357b0403c49a16 Mon Sep 17 00:00:00 2001 From: oloturia <5429234+oloturia@users.noreply.github.com> Date: Sat, 30 Oct 2021 03:10:55 +0200 Subject: [PATCH 3/6] alignment again --- damastodon.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/damastodon.py b/damastodon.py index 27f5c5f..eb9e8f5 100755 --- a/damastodon.py +++ b/damastodon.py @@ -62,7 +62,7 @@ def check_message(notification): pickle.dump("@"+challenger,f) pickle.dump(False,f) pickle.dump(board,f) - mastodon.status_post("◾: @"+account+" ◽: @"+challenger+" turn ◽\n"+dama.draw_checkerboard(board,space="🟥",white_norm="◽",white_knight="⚪",black_norm="◾",black_knight="⚫",empty="🟦",frstrow="0 🇦 🇧 🇨 🇩 🇪 🇫 🇬 🇭 \n"),visibility="direct") + mastodon.status_post("◾: @"+account+" ◽: @"+challenger+" turn ◽\n"+dama.draw_checkerboard(board,space="🟥",white_norm="◽",white_knight="⚪",black_norm="◾",black_knight="⚫",empty="🟦",frstrow="0🇦 🇧 🇨 🇩 🇪 🇫 🇬 🇭\n"),visibility="direct") return elif content.split(" ")[1].lower() == "no": os.remove(save_position+content.split(" ")[0][1:]) From 9a9c4ae9b67e0e44d2d2e0a75af6ecd4c8caeb0d Mon Sep 17 00:00:00 2001 From: oloturia <5429234+oloturia@users.noreply.github.com> Date: Sat, 30 Oct 2021 03:19:13 +0200 Subject: [PATCH 4/6] alignment again --- damastodon.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/damastodon.py b/damastodon.py index eb9e8f5..42c128d 100755 --- a/damastodon.py +++ b/damastodon.py @@ -100,7 +100,7 @@ def check_message(notification): colour = "◾" winner = dama.checkWin(board) if winner == (False,False): - mastodon.status_post("◾: "+black+" ◽: "+white+" turn "+colour+"\n"+dama.draw_checkerboard(board,space="🟥 ",white_norm="◽ ",white_knight="⚪ ",black_norm="◾ ",black_knight="⚫ ",empty="🟦 ",frstrow="0🇦 🇧 🇨 🇩 🇪 🇫 🇬 🇭 \n"),visibility="direct") + mastodon.status_post("◾: "+black+" ◽: "+white+" turn "+colour+"\n"+dama.draw_checkerboard(board,space="🟥",white_norm="◽",white_knight="⚪",black_norm="◾",black_knight="⚫",empty="🟦",frstrow="0🇦 🇧 🇨 🇩 🇪 🇫 🇬 🇭 \n"),visibility="direct") return else: if winner == (True,False): @@ -109,7 +109,7 @@ def check_message(notification): winner_t = "BLACK" os.remove(save_position+black[1:]) os.remove(save_position+white[1:]) - mastodon.status_post("◾: "+black+" ◽: "+white+"\n"+winner_t+" WINS!\n"+dama.draw_checkerboard(board,space="🟥 ",white_norm="◽ ",white_knight="⚪ ",black_norm="◾ ",black_knight="⚫ ",empty="🟦 ",frstrow="0🇦 🇧 🇨 🇩 🇪 🇫 🇬 🇭 \n"),visibility="direct") + mastodon.status_post("◾: "+black+" ◽: "+white+"\n"+winner_t+" WINS!\n"+dama.draw_checkerboard(board,space="🟥",white_norm="◽",white_knight="⚪",black_norm="◾",black_knight="⚫",empty="🟦",frstrow="0🇦 🇧 🇨 🇩 🇪 🇫 🇬 🇭 \n"),visibility="direct") return else: mastodon.status_post("@"+account+" \nIt's not your turn.",visibility="direct") From 22f2e7700ba73623b7e70d4d8aacb8ec3280b3ce Mon Sep 17 00:00:00 2001 From: oloturia <5429234+oloturia@users.noreply.github.com> Date: Sat, 30 Oct 2021 17:19:55 +0200 Subject: [PATCH 5/6] added inverted rows and columns --- damastodon.py | 9 ++++--- draughts_engine.py | 60 ++++++++++++++++++++++++++++++++++------------ 2 files changed, 51 insertions(+), 18 deletions(-) diff --git a/damastodon.py b/damastodon.py index 919b531..a313792 100755 --- a/damastodon.py +++ b/damastodon.py @@ -15,6 +15,9 @@ save_position = "/tmp/" CLEANR = re.compile('<.*?>') botname = "@damastodon " +firow = "🔠1️⃣ 2️⃣ 3️⃣ 4️⃣ 5️⃣ 6️⃣ 7️⃣ 8️⃣\n" +colsx = "🇦 🇧 🇨 🇩 🇪 🇫 🇬 🇭 " + def cleanHTML(raw): cleanText = re.sub(CLEANR, '',raw) return cleanText @@ -62,7 +65,7 @@ def check_message(notification): pickle.dump("@"+challenger,f) pickle.dump(False,f) pickle.dump(board,f) - mastodon.status_post("◾: @"+account+" ◽: @"+challenger+" turn ◽\n"+dama.draw_checkerboard(board,space="🟥 ",white_norm="◽ ",white_knight="⚪ ",black_norm="◾ ",black_knight="⚫ ",empty="🟦 ",frstrow="0🇦 🇧 🇨 🇩 🇪 🇫 🇬 🇭 \n"),visibility="direct") + mastodon.status_post("◾: @"+account+" ◽: @"+challenger+" turn ◽\n"+dama.draw_checkerboard(board,space="🟥 ",white_norm="◽ ",white_knight="⚪ ",black_norm="◾ ",black_knight="⚫ ",empty="🟦 ",frstrow=firow, column=colsx),visibility="direct") return elif content.split(" ")[1].lower() == "no": os.remove(save_position+content.split(" ")[0][1:]) @@ -83,7 +86,7 @@ def check_message(notification): mastodon.status_post(black+" "+white+" the match was cancelled.") return if (black == "@"+account and turn == 1) or (white == "@"+account and turn == 0): - board = dama.valid_move(content.lower(),turn,board) + board = dama.valid_move(content.lower(),turn,board,inversion=True) if board == -1: mastodon.status_post("@"+account+" \nInvalid move.",visibility="direct") return @@ -100,7 +103,7 @@ def check_message(notification): colour = "◾" winner = dama.checkWin(board) if winner == (False,False): - mastodon.status_post("◾: "+black+" ◽: "+white+" turn "+colour+"\n"+dama.draw_checkerboard(board,space="🟥 ",white_norm="◽ ",white_knight="⚪ ",black_norm="◾ ",black_knight="⚫ ",empty="🟦 ",frstrow="0🇦 🇧 🇨 🇩 🇪 🇫 🇬 🇭 \n"),visibility="direct") + mastodon.status_post("◾: "+black+" ◽: "+white+" turn "+colour+"\n"+dama.draw_checkerboard(board,space="🟥 ",white_norm="◽ ",white_knight="⚪ ",black_norm="◾ ",black_knight="⚫ ",empty="🟦 ",frstrow=firow, column=colsx),visibility="direct") return else: if winner == (True,False): diff --git a/draughts_engine.py b/draughts_engine.py index fcc4212..991a442 100755 --- a/draughts_engine.py +++ b/draughts_engine.py @@ -2,12 +2,14 @@ import re -def draw_checkerboard(status,space="▒",white_norm="h",white_knight="H",black_norm="b",black_knight="B",empty=" ",frstrow=" abcdefgh\n"): +def draw_checkerboard(status,space="▒",white_norm="h",white_knight="H",black_norm="b",black_knight="B",empty=" ",column="12345678",frstrow=" abcdefgh\n"): + """ Draw a checkerboard, status is the virtual representation of the board, as a bi-dimensional array, white/black norm/knight are the representation of the pieces + space is a non-walkable cell, empty is a walkable cell, column contains labels for the left column, frstrow the labels for the first row. + """ bstr = "" bstr += frstrow for row in range(0,len(status)): - bstr += str(row+1)+"" - + bstr += column[row] if row % 2 == 0: bstr += space for cell in range(0,int(len(status)/2)): @@ -23,13 +25,14 @@ def draw_checkerboard(status,space="▒",white_norm="h",white_knight="H",black_n bstr += empty if (cell < 3): bstr += space - if row % 2 !=0: + if row % 2 !=0: #odd lines ends with space and a line feed, even ones just with the line feed bstr += space+"\n" else: bstr += "\n" return bstr def position_resolver(pos,board): + """Checks if the position is valid and wether it contains a piece or not""" row,col = row_column_translation(pos[1],pos[0]) if (col == -1): return -1 @@ -37,6 +40,8 @@ def position_resolver(pos,board): return board[row][col] def row_column_translation(row,col): + """Converts a coordinate made by a letter/number couple in the index usable with the array that represents the board""" + row = int(row) - 1 if row % 2 != 0: cols = range(ord("a"),ord("a")+7,2) @@ -47,13 +52,27 @@ def row_column_translation(row,col): else: return row,cols.index(ord(col)) -def valid_move(pos_toParse,turn,board): +def traslate_coord(pos): + for en,p in enumerate(pos): + temp_col = chr( (int(pos[en][1])-1)+ord("a") ) + temp_row = ord(pos[en][0])-ord("a") + pos[en] = temp_col + pos[en] += str(temp_row +1) + return pos + +def valid_move(pos_toParse,turn,board,inversion=False): + """Checks if the move is valid, execute it and returns the updated board""" #pos = pos_toParse.split(" ") #tricky part, pos[0] is the starting cell, pos[1] to pos[n] the destination point(s), for every coord, [x][0] is the column (the letter) and [x][1] the row (the number) - pos = re.findall("\\b[abcdefgh][12345678]\\b",pos_toParse) - #for rowcol in pos: - # if (rowcol[0] not in ("a","b","c","d","e","f","g","h")) or (rowcol[1] not in ("1","2","3","4","5","6","7","8")): #check if the positions are valid - # return -1 - if len(pos) < 2: + pos = re.findall("\\b[abcdefgh][12345678]\\b",pos_toParse) #both row-col and col-row are accepted + if len(pos) == 0: + pos = re.findall("\\b[12345678][abcdefgh]\\b",pos_toParse) + for en,x in enumerate(pos): + pos[en] = x[1]+x[0] + + if inversion: + pos = traslate_coord(pos) + + if len(pos) < 2: #less than 2 coords were given, movement is invalid return -1 row_start,col_start = row_column_translation(pos[0][1],pos[0][0]) if(row_start == -1): @@ -79,7 +98,7 @@ def valid_move(pos_toParse,turn,board): start_cell = 3 board[row][col] = start_cell return board - else: + else: #destination cell is not empty return -1 else: for x in range(0,len(pos) -1): @@ -106,9 +125,9 @@ def valid_move(pos_toParse,turn,board): if ((row == 7) and start_cell == 1): start_cell = 3 pos.pop(0) #if there are other moves this check loops - else: + else: #one of the pieces moves are invalid return -1 - else: + else: #destination not empty return -1 else: return -1 @@ -136,18 +155,29 @@ def checkWin(board): return (white_won,black_won) def main(): + import sys + column_i = "12345678" + frstrow_i = " abcdefgh\n" + inverted = False + try: + if sys.argv[1] == "i": + column_i = "abcdefgh" + frstrow_i = " 12345678\n" + inverted = True + except IndexError: + pass main_board = init_board() s = "" turn = False while (s !="q"): - vis_board = draw_checkerboard(main_board) + vis_board = draw_checkerboard(main_board,column=column_i,frstrow=frstrow_i) print(vis_board) if turn: s = input("Black move:") else: s = input("White move:") if (s !="q"): - result = valid_move(s,turn,main_board) + result = valid_move(s,turn,main_board,inversion=inverted) if result != -1: main_board = result winner = checkWin(main_board) From bd8453b351eb30f80504171b83fd5fe08eb3f442 Mon Sep 17 00:00:00 2001 From: oloturia <5429234+oloturia@users.noreply.github.com> Date: Sat, 30 Oct 2021 17:29:46 +0200 Subject: [PATCH 6/6] fixed merge --- damastodon.py | 8 -------- draughts_engine.py | 4 ---- 2 files changed, 12 deletions(-) diff --git a/damastodon.py b/damastodon.py index 5367e0e..fb69ccd 100755 --- a/damastodon.py +++ b/damastodon.py @@ -65,11 +65,7 @@ def check_message(notification): pickle.dump("@"+challenger,f) pickle.dump(False,f) pickle.dump(board,f) -<<<<<<< HEAD - mastodon.status_post("◾: @"+account+" ◽: @"+challenger+" turn ◽\n"+dama.draw_checkerboard(board,space="🟥",white_norm="◽",white_knight="⚪",black_norm="◾",black_knight="⚫",empty="🟦",frstrow="0🇦 🇧 🇨 🇩 🇪 🇫 🇬 🇭\n"),visibility="direct") -======= mastodon.status_post("◾: @"+account+" ◽: @"+challenger+" turn ◽\n"+dama.draw_checkerboard(board,space="🟥 ",white_norm="◽ ",white_knight="⚪ ",black_norm="◾ ",black_knight="⚫ ",empty="🟦 ",frstrow=firow, column=colsx),visibility="direct") ->>>>>>> reversecheckboard return elif content.split(" ")[1].lower() == "no": os.remove(save_position+content.split(" ")[0][1:]) @@ -107,11 +103,7 @@ def check_message(notification): colour = "◾" winner = dama.checkWin(board) if winner == (False,False): -<<<<<<< HEAD - mastodon.status_post("◾: "+black+" ◽: "+white+" turn "+colour+"\n"+dama.draw_checkerboard(board,space="🟥",white_norm="◽",white_knight="⚪",black_norm="◾",black_knight="⚫",empty="🟦",frstrow="0🇦 🇧 🇨 🇩 🇪 🇫 🇬 🇭 \n"),visibility="direct") -======= mastodon.status_post("◾: "+black+" ◽: "+white+" turn "+colour+"\n"+dama.draw_checkerboard(board,space="🟥 ",white_norm="◽ ",white_knight="⚪ ",black_norm="◾ ",black_knight="⚫ ",empty="🟦 ",frstrow=firow, column=colsx),visibility="direct") ->>>>>>> reversecheckboard return else: if winner == (True,False): diff --git a/draughts_engine.py b/draughts_engine.py index 84017c7..991a442 100755 --- a/draughts_engine.py +++ b/draughts_engine.py @@ -2,14 +2,10 @@ import re -<<<<<<< HEAD -def draw_checkerboard(status,space="▒",white_norm="h",white_knight="H",black_norm="b",black_knight="B",empty=" ",frstrow=" abcdefgh\n",column="12345678"): -======= def draw_checkerboard(status,space="▒",white_norm="h",white_knight="H",black_norm="b",black_knight="B",empty=" ",column="12345678",frstrow=" abcdefgh\n"): """ Draw a checkerboard, status is the virtual representation of the board, as a bi-dimensional array, white/black norm/knight are the representation of the pieces space is a non-walkable cell, empty is a walkable cell, column contains labels for the left column, frstrow the labels for the first row. """ ->>>>>>> reversecheckboard bstr = "" bstr += frstrow for row in range(0,len(status)):