【Python CheckiO 题解】Xs and Os Referee
CheckiO 是面向初學者和高級程序員的編碼游戲,使用 Python 和 JavaScript 解決棘手的挑戰和有趣的任務,從而提高你的編碼技能,本博客主要記錄自己用 Python 在闖關時的做題思路和實現代碼,同時也學習學習其他大神寫的代碼。
CheckiO 官網:https://checkio.org/
我的 CheckiO 主頁:https://py.checkio.org/user/TRHX/
CheckiO 題解系列專欄:https://itrhx.blog.csdn.net/category_9536424.html
CheckiO 所有題解源代碼:https://github.com/TRHX/Python-CheckiO-Exercise
題目描述
【Xs and Os Referee】:井字游戲,兩個玩家(X和O)輪流在 3×3 的網格上落棋,最先在任意一條直線(水平線,垂直線或對角線)上成功連接三個網格的一方獲勝。在本題中,你將是這個游戲的裁判。你必須判斷游戲是平局還是有人勝出,以及誰將會成為最后的贏家。如果 X 玩家獲勝,返回“X”。如果 O 玩家獲勝,返回“O”。如果比賽是平局,返回“D”。
【鏈接】:https://py.checkio.org/mission/x-o-referee/
【輸入】:“X”,“O”在棋盤上的位置,“.”表示空格(列表)
【輸出】:獲勝的一方,“X”,“O”或“D”(字符串)
【范例】:
checkio(["X.O","XX.","XOO"]) == "X" checkio(["OO.","XOX","XOX"]) == "O" checkio(["OOX","XXO","OXX"]) == "D"解題思路
判斷誰贏有 6 種情況,橫著 3 種,豎著 3 種,斜著 2 種,我用了最笨的辦法,直接將每顆棋子轉換成列表,依次判斷是否相等就行了,就是不斷的用 if 語句,另外也可以分橫豎和斜著兩種情況來寫代碼,這樣的話,橫豎可以設置一個變量,循環 3 次,利用類似于 game_result[i][0]、game_result[i][1] 來依次比較,需要特別注意的是,要排除三個空格,也就是三個相同 . 的情況
代碼實現
from typing import Listdef checkio(game_result: List[str]) -> str:list2 = []for i in game_result:list2.extend(list(i))if list2[0] != '.' and (list2[0] == list2[3] == list2[6] or list2[0] == list2[4] == list2[8] or list2[0] == list2[1] == list2[2]):return list2[0]elif list2[1] != '.' and (list2[1] == list2[4] == list2[7]):return list2[1]elif list2[2] != '.' and (list2[2] == list2[4] == list2[6] or list2[2] == list2[5] == list2[8]):return list2[2]elif list2[3] != '.' and (list2[3] == list2[4] == list2[5]):return list2[3]elif list2[6] != '.' and (list2[6] == list2[7] == list2[8]):return list2[6]else:return 'D'if __name__ == '__main__':print("Example:")print(checkio(["X.O","XX.","XOO"]))# These "asserts" using only for self-checking and not necessary for auto-testingassert checkio(["X.O","XX.","XOO"]) == "X", "Xs wins"assert checkio(["OO.","XOX","XOX"]) == "O", "Os wins"assert checkio(["OOX","XXO","OXX"]) == "D", "Draw"assert checkio(["O.X","XX.","XOO"]) == "X", "Xs wins again"print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")大神解答
大神解答 NO.1
def checkio(result):rows = resultcols = map(''.join, zip(*rows))diags = map(''.join, zip(*[(r[i], r[2 - i]) for i, r in enumerate(rows)]))lines = rows + list(cols) + list(diags)return 'X' if ('XXX' in lines) else 'O' if ('OOO' in lines) else 'D'大神解答 NO.2
# From Daniel Dou with love...def checkio(board):# First we put everything together into a single stringx = "".join(board)# Next we outline the 8 possible winning combinations. combos = ["012", "345", "678", "036", "147", "258", "048", "246"]# We go through all the winning combos 1 by 1 to see if there are any# all Xs or all Os in the combosfor i in combos:if x[int(i[0])] == x[int(i[1])] == x[int(i[2])] and x[int(i[0])] in "XO":return x[int(i[0])]return "D"大神解答 NO.3
# migrated from python 2.7 def checkio(game_result):sample = "".join(game_result)data = game_result + [sample[i:9:3] for i in range(3)] + [sample[0:9:4], sample[2:8:2]]if "OOO" in data:return "O"elif "XXX" in data:return "X"else:return "D"大神解答 NO.4
def checkio(game_result):patterns = [] + game_resultsize = len(game_result)for col in range(size):patterns.append(''.join([game_result[row][col] for row in range(size)]))patterns.append(''.join([game_result[x][x] for x in range(size)]))patterns.append(''.join([game_result[x][size - x - 1] for x in range(size)]))return 'X' if 'XXX' in patterns else 'O' if 'OOO' in patterns else 'D'大神解答 NO.5
def checkio(game_result):result = 'D'if game_result[0][0] == game_result[1][1] == game_result[2][2] and game_result[0][0] != '.':result = game_result[0][0]return resultif game_result[2][0] == game_result[1][1] == game_result[0][2] and game_result[2][0] != '.':result = game_result[2][0]return result for i in range(3):if game_result[i][0] == game_result[i][1] == game_result[i][2] and game_result[i][0] != '.':result = game_result[i][0]breakif game_result[0][i] == game_result[1][i] == game_result[2][i] and game_result[0][i] != '.':result = game_result[0][i]breakreturn result 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的【Python CheckiO 题解】Xs and Os Referee的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 比5G网络快100倍!专家称2030年左
- 下一篇: 《糖豆人》免费后一摊子事!大批玩家吐槽: