1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

電気椅子ゲームをPythonで書いてみた

Posted at

はじめに

Pythonで書いたら面白そうと教えてもらったので、やってみた。

書いてみた

import random

class ElectricChairGame:
    def __init__(self):
        self.round = 1
        self.turn = ""
        self.scores = {"先攻": 0, "後攻": 0}
        self.shock_points = {"先攻": 0, "後攻": 0}
        self.chairs = list(range(1, 13))
        self.active_chair = None
        self.current_player = "先攻"

    def switch_turn(self):
        self.current_player = "後攻" if self.current_player == "先攻" else "先攻"
        self.turn = "" if self.turn == "" else ""
        if self.turn == "":
            self.round += 1

    def set_active_chair(self):
        self.active_chair = random.choice(self.chairs)

    def player_choice(self, choice):
        if choice == self.active_chair:
            self.shock_points[self.current_player] += 1
            print(f"{self.current_player}感電!得点が取り消されました。感電ポイント: {self.shock_points[self.current_player]}")
            self.scores[self.current_player] = 0
        else:
            self.scores[self.current_player] += choice
            self.chairs.remove(choice)
            print(f"{self.current_player}{choice}番のイスに座りました。現在の得点: {self.scores[self.current_player]}")

    def check_end_conditions(self):
        if self.round > 8:
            return True
        if any(score >= 40 for score in self.scores.values()):
            return True
        if any(points >= 3 for points in self.shock_points.values()):
            return True
        if len(self.chairs) == 1:
            return True
        return False

    def get_winner(self):
        if self.scores["先攻"] > self.scores["後攻"]:
            return "先攻"
        elif self.scores["後攻"] > self.scores["先攻"]:
            return "後攻"
        else:
            return "引き分け"

    def play(self):
        while not self.check_end_conditions():
            self.set_active_chair()
            print(f"\n--- {self.round}回の{self.turn} ---")
            print(f"現在のプレイヤー: {self.current_player}")
            print(f"残りのイス: {self.chairs}")

            choice = int(input(f"{self.current_player}の選択(1-12): "))
            self.player_choice(choice)

            self.switch_turn()

        winner = self.get_winner()
        print(f"\nゲーム終了!勝者は: {winner}")

if __name__ == "__main__":
    game = ElectricChairGame()
    game.play()

実行結果

 1回の表 
現在のプレイヤー: 先攻
残りのイス: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
先攻の選択: 2
先攻が2番のイスに座りました現在の得点: 2

 1回の裏 
現在のプレイヤー: 後攻
残りのイス: [1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
後攻の選択: 9
後攻が9番のイスに座りました現在の得点: 9

 2回の表 
現在のプレイヤー: 先攻
残りのイス: [1, 3, 4, 5, 6, 7, 8, 10, 11, 12]
先攻の選択: 1
先攻が1番のイスに座りました現在の得点: 3

 2回の裏 
現在のプレイヤー: 後攻
残りのイス: [3, 4, 5, 6, 7, 8, 10, 11, 12]
後攻の選択: 11
後攻が11番のイスに座りました現在の得点: 20

 3回の表 
現在のプレイヤー: 先攻
残りのイス: [3, 4, 5, 6, 7, 8, 10, 12]
先攻の選択: 7
先攻が7番のイスに座りました現在の得点: 10

 3回の裏 
現在のプレイヤー: 後攻
残りのイス: [3, 4, 5, 6, 8, 10, 12]
後攻の選択: 6
後攻が6番のイスに座りました現在の得点: 26

 4回の表 
現在のプレイヤー: 先攻
残りのイス: [3, 4, 5, 8, 10, 12]
先攻の選択: 4
先攻が4番のイスに座りました現在の得点: 14

 4回の裏 
現在のプレイヤー: 後攻
残りのイス: [3, 5, 8, 10, 12]
後攻の選択: 8
後攻感電得点が取り消されました感電ポイント: 1

 5回の表 
現在のプレイヤー: 先攻
残りのイス: [3, 5, 8, 10, 12]
先攻の選択: 8
先攻が8番のイスに座りました現在の得点: 22

 5回の裏 
現在のプレイヤー: 後攻
残りのイス: [3, 5, 10, 12]
後攻の選択: 12
後攻が12番のイスに座りました現在の得点: 12

 6回の表 
現在のプレイヤー: 先攻
残りのイス: [3, 5, 10]
先攻の選択: 3
先攻感電得点が取り消されました感電ポイント: 1

 6回の裏 
現在のプレイヤー: 後攻
残りのイス: [3, 5, 10]
後攻の選択: 3
後攻感電得点が取り消されました感電ポイント: 2

 7回の表 
現在のプレイヤー: 先攻
残りのイス: [3, 5, 10]
先攻の選択: 10
先攻が10番のイスに座りました現在の得点: 10

 7回の裏 
現在のプレイヤー: 後攻
残りのイス: [3, 5]
後攻の選択: 3
後攻が3番のイスに座りました現在の得点: 3

ゲーム終了勝者は: 先攻

感想

randomモジュールを使えて嬉しかった。
関数をたくさん作るのが難しかった。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?