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