|
| 1 | +import random |
| 2 | +import operator |
| 3 | + |
| 4 | +game_point = {"human":0, "computer1":0, "computer2":0} |
| 5 | + |
| 6 | +while True: |
| 7 | + print("*"*20) |
| 8 | + print(""" 31 숫자게임""") |
| 9 | + print("*"*20) |
| 10 | + |
| 11 | + game_log = [] |
| 12 | + |
| 13 | + current_number = 1 |
| 14 | + |
| 15 | + randnum = random.randint(0, 2) |
| 16 | + if randnum == 0: |
| 17 | + current_player = "human" |
| 18 | + elif randnum == 1: |
| 19 | + current_player = "computer1" |
| 20 | + else: |
| 21 | + current_player = "computer2" |
| 22 | + |
| 23 | + while current_number <= 31: |
| 24 | + |
| 25 | + print("현재 숫자는 " + str(current_number) + " 입니다.") |
| 26 | + |
| 27 | + if current_player == "human": |
| 28 | + |
| 29 | + print("1, 2, 또는 3을 입력하세요. 더해서 31 이상이 되면 게임에서 지는 것입니다.") |
| 30 | + |
| 31 | + player_choice = "" |
| 32 | + while player_choice not in ["1", "2", "3"]: |
| 33 | + player_choice = input("어떤 값을 더할까요? ") |
| 34 | + |
| 35 | + player_choice = int(player_choice) |
| 36 | + current_number = current_number + player_choice |
| 37 | + |
| 38 | + if current_number >= 31: |
| 39 | + print("현재 숫자는 " + str(current_number) + " 입니다.") |
| 40 | + print() |
| 41 | + print("human 이 졌습니다.") |
| 42 | + break |
| 43 | + |
| 44 | + game_log.append("human") |
| 45 | + current_player = "computer1" |
| 46 | + |
| 47 | + |
| 48 | + elif current_player == "computer1": |
| 49 | + |
| 50 | + computer_choice = random.randint(1, 3) |
| 51 | + current_number = current_number + computer_choice |
| 52 | + print("Computer1 순서입니다. computer1 은(는) " + str(computer_choice) + " 을 선택했습니다.") |
| 53 | + |
| 54 | + if current_number >= 31: |
| 55 | + print("현재 숫자는 " + str(current_number) + " 입니다.") |
| 56 | + print() |
| 57 | + print("computer1 이 졌습니다.") |
| 58 | + break |
| 59 | + |
| 60 | + game_log.append("computer1") |
| 61 | + current_player = "computer2" |
| 62 | + |
| 63 | + else: |
| 64 | + computer_choice = random.randint(1, 3) |
| 65 | + current_number = current_number + computer_choice |
| 66 | + print("Computer2 순서입니다. computer2 은(는) " + str(computer_choice) + " 을 선택했습니다.") |
| 67 | + |
| 68 | + if current_number >= 31: |
| 69 | + print("현재 숫자는 " + str(current_number) + " 입니다.") |
| 70 | + print() |
| 71 | + print("computer2 이 졌습니다.") |
| 72 | + break |
| 73 | + |
| 74 | + game_log.append("computer2") |
| 75 | + current_player = "human" |
| 76 | + |
| 77 | + # 게임포인트 지급 |
| 78 | + player = game_log.pop() |
| 79 | + game_point[player] += 2 |
| 80 | + |
| 81 | + player = game_log.pop() |
| 82 | + game_point[player] += 1 |
| 83 | + |
| 84 | + print("게임점수") |
| 85 | + print(game_point) |
| 86 | + |
| 87 | + play_again = input("게임을 다시 진행하겠습니까? ") |
| 88 | + if play_again.lower().startswith("y"): |
| 89 | + continue |
| 90 | + else: |
| 91 | + print("게임종료") |
| 92 | + break |
| 93 | + |
| 94 | +print("") |
| 95 | +print("[게임점수 순위]") |
| 96 | +sort_list = sorted(game_point.items(), key=operator.itemgetter(1), reverse=True) |
| 97 | +for i in range(len(sort_list)): |
| 98 | + print("{}위 : {}".format(i+1, sort_list[i][0])) |
| 99 | + |
0 commit comments