파이썬 기초 공부
from itertools import cycledef win(current_game):def check_winner(check, type_win):if check.count(check[0]) == len(check) and check[0] != 0:print(f"Player {check[0]} is the winner : {type_win}")return Truereturn False#Diagnol Back-slashdiags = []test_reverse = reversed(range(len(current_game)))for col, row in enumerate(test_reverse):diags.append(current_game[row][col])if check_winner(diags, "Diagnol(\\)"):return True#Diagnol Slashdiags = []for idx in range(len(current_game)):diags.append(current_game[idx][idx])if check_winner(diags, "Diagnol(/)"):return True#Rowrows = []for idx, enums in enumerate(current_game):rows = list(enums)if check_winner(rows, "Diagnol(-)"):return True#Colfor col in range(len(current_game)):cols = []for row in range(len(current_game)):cols.append(current_game[row][col])if check_winner(cols, "Diagnol(|)"):return Truereturn Falsedef game_board(game_map, player=0, row=0, col=0, just_display=False):try:if game_map[row][col] != 0:print(f"[{row}][{col}] is already occupied. Find another spot!")return False, game_maps = " " + " ".join([str(i) for i in range(game_size)])print(s)if not just_display:game_map[row][col] = playerfor count, row in enumerate(game):print(count, row)return True, game_mapexcept IndexError as e:print("Error : make sure you input row/clumn as 0 1 and 2? ", e)return False, game_mapexcept Exception as e:print("Something went very wrong", e)return False, game_mapplay = Truewhile play:players = cycle([1,2])game_size = int(input("size of rectangular board size. ex)3x3, 4x4, etc.."))print(f"gamesize: {game_size}")game = [[0 for i in range(game_size)] for i in range(game_size)]game_won = False_, game = game_board(game, just_display=True)while not game_won:current_player = next(players)print(f"Current player : {current_player}")played = Falsewhile not played:row_choice = int(input("What row do you want to play? (0, 1, 2): "))colum_choice = int(input("What column do you want to play? (0, 1, 2): "))played, game = game_board(game, current_player, row_choice, colum_choice)if win(game):game_won = Trueagain = input("The game is over, would you like to play again? (y/n) : ")if again.lower() == 'y':print("restarting")else:print("Byeeee")play = False
반응형
'공부하며놀자 > 프로그래밍' 카테고리의 다른 글
Visual Studio Post-build 적용하기 (0) | 2019.07.24 |
---|---|
[LeetCode] 2.Add Two Numbers (Medium) (0) | 2019.04.11 |
[Python][PyVisa] 파이썬으로 VISA 통신 방법 (0) | 2018.09.19 |
[Python][PySerial] ModuleNotFoundError : No module name 'serial' issue solve 문제 해결 (0) | 2018.08.30 |
[프로그래밍][c/c++] Copy CString to clipboard. CString 클립보드에 붙여넣기 (0) | 2016.09.13 |
댓글