インフラエンジニアの技術ブログ

日々学んでいることをブログでまとめていきます。

独学プログラマー_15章_知識を一つにまとめる_カードゲーム(戦争)

独学プログラマー15章掲載されていたコードを手元で、入力して無事に実行できた。

ここで掲載しておく

 

作成およそ2時間

まずは作成環境とそのコード内容

-----

[root@centos]# cat /etc/redhat-release
CentOS release 6.9 (Final)

[root@centos]# ls -l |grep war
-rw-r--r-- 1 root root 2901  2月 10 14:59 2019 war.py

[root@centos]# cat war.py
from random import shuffle

class Card:
    suits = ["spades", "hearts", "diamonds", "clubs"]
    values = [None, None, "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"]

    def __init__(self, v, s):
        """スート(マーク)も値も整数値です"""
        self.value = v
        self.suit = s

    def __lt__(self, c2):
        if self.value < c2.value:
            return True
        if self.value == c2.value:
            return True
        else:
            return False
        return False

    def __gt__(self, c2):
        if self.value > c2.value:
            return True
        if self.value == c2.value:
            if self.suit > c2.suit:
                return True
            else:
                return False
        return False

    def __repr__(self):
        v = self.values[self.value] + " of " \
            + self.suits[self.suit]
        return v

class Deck:
    def __init__(self):
        self.cards = []
        for i in range(2, 15):
            for j in range(4):
                self.cards.append(Card(i, j))
            shuffle(self.cards)

    def rm_card(self):
        if len(self.cards) == 0:
            return
        return self.cards.pop()


class Player:
    def __init__(self, name):
        self.wins = 0
        self.card = None
        self.name = name


class Game:
    def __init__(self):
        name1 = input("プレーヤー1の名前 ")
        name2 = input("プレーヤー2の名前 ")
        self.deck = Deck()
        self.p1 = Player(name1)
        self.p2 = Player(name2)

    def wins(self, winner):
        w = "このラウンドは {} が勝ちました"
        w = w.format(winner)
        print(w)

    def draw(self, p1n, p1c, p2n, p2c):
        d = "{} は {}、{} は {} を引きました"
        d = d.format(p1n, p1c, p2n, p2c)
        print(d)

    def play_game(self):
        cards = self.deck.cards
        print("戦争を始めます")
        while len(cards) >= 2:
            m = "q で終了、それ以外のキーでPlay:"
            response = input(m)
            if response == 'q':
                break
            p1c = self.deck.rm_card()
            p2c = self.deck.rm_card()
            p1n = self.p1.name
            p2n = self.p2.name
            self.draw(p1n, p1c, p2n ,p2c)
            if p1c > p2c:
                self.p1.wins += 1
                self.wins(self.p1.name)
            else:
                self.p2.wins += 1
                self.wins(self.p2.name)

        win = self.winner(self.p1, self.p2)
        print("ゲーム終了、{} の勝利です!".format(win))

    def winner(self, p1, p2):
        if p1.wins > p2.wins:
            return p1.name
        if p1.wins < p2.wins:
            return p2.name
        return "引き分け!"

game = Game()
game.play_game()
[root@centos69]#

------

 

以下実行結果

注意点

実行コマンドが

python war.py

だとpython2.7で実行されてしまいうまく動かない。

該当エラー

SyntaxError: Non-ASCII character '\xe3' in file war.py on line 8, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

 

手元のデフォルトのpythonバージョン

[root@centos]# python --version
Python 2.7.9

 

・以下が表示されたら[Enter]キーを押下している

q で終了、それ以外のキーでPlay:

・taroとjiroは手入力文字

------

[root@centos]# /usr/bin/python3.6 war.py
プレーヤー1の名前 taro
プレーヤー2の名前 jiro
戦争を始めます
q で終了、それ以外のキーでPlay:
taro は King of hearts、jiro は 5 of clubs を引きました
このラウンドは taro が勝ちました
q で終了、それ以外のキーでPlay:
taro は 5 of diamonds、jiro は 9 of clubs を引きました
このラウンドは jiro が勝ちました
q で終了、それ以外のキーでPlay:
taro は Ace of hearts、jiro は 6 of spades を引きました
このラウンドは taro が勝ちました
q で終了、それ以外のキーでPlay:
taro は 10 of diamonds、jiro は King of clubs を引きました
このラウンドは jiro が勝ちました
q で終了、それ以外のキーでPlay:
taro は 7 of spades、jiro は 9 of spades を引きました
このラウンドは jiro が勝ちました
q で終了、それ以外のキーでPlay:
taro は 4 of hearts、jiro は Ace of diamonds を引きました
このラウンドは jiro が勝ちました
q で終了、それ以外のキーでPlay:
taro は 6 of diamonds、jiro は Queen of diamonds を引きました
このラウンドは jiro が勝ちました
q で終了、それ以外のキーでPlay:
taro は 10 of clubs、jiro は 9 of hearts を引きました
このラウンドは taro が勝ちました
q で終了、それ以外のキーでPlay:
taro は 2 of hearts、jiro は Jack of diamonds を引きました
このラウンドは jiro が勝ちました
q で終了、それ以外のキーでPlay:
taro は 3 of clubs、jiro は 4 of diamonds を引きました
このラウンドは jiro が勝ちました
q で終了、それ以外のキーでPlay:
taro は 6 of hearts、jiro は Jack of hearts を引きました
このラウンドは jiro が勝ちました
q で終了、それ以外のキーでPlay:
taro は 8 of diamonds、jiro は 3 of diamonds を引きました
このラウンドは taro が勝ちました
q で終了、それ以外のキーでPlay:
taro は 7 of hearts、jiro は 3 of spades を引きました
このラウンドは taro が勝ちました
q で終了、それ以外のキーでPlay:
taro は 4 of spades、jiro は Queen of clubs を引きました
このラウンドは jiro が勝ちました
q で終了、それ以外のキーでPlay:
taro は 6 of clubs、jiro は Queen of spades を引きました
このラウンドは jiro が勝ちました
q で終了、それ以外のキーでPlay:
taro は 2 of spades、jiro は 8 of hearts を引きました
このラウンドは jiro が勝ちました
q で終了、それ以外のキーでPlay:
taro は King of diamonds、jiro は 8 of clubs を引きました
このラウンドは taro が勝ちました
q で終了、それ以外のキーでPlay:
taro は 8 of spades、jiro は 2 of clubs を引きました
このラウンドは taro が勝ちました
q で終了、それ以外のキーでPlay:
taro は Ace of spades、jiro は Jack of clubs を引きました
このラウンドは taro が勝ちました
q で終了、それ以外のキーでPlay:
taro は 9 of diamonds、jiro は Queen of hearts を引きました
このラウンドは jiro が勝ちました
q で終了、それ以外のキーでPlay:
taro は 5 of spades、jiro は 7 of diamonds を引きました
このラウンドは jiro が勝ちました
q で終了、それ以外のキーでPlay:
taro は Ace of clubs、jiro は Jack of spades を引きました
このラウンドは taro が勝ちました
q で終了、それ以外のキーでPlay:
taro は 3 of hearts、jiro は 4 of clubs を引きました
このラウンドは jiro が勝ちました
q で終了、それ以外のキーでPlay:
taro は 5 of hearts、jiro は 10 of spades を引きました
このラウンドは jiro が勝ちました
q で終了、それ以外のキーでPlay:
taro は King of spades、jiro は 7 of clubs を引きました
このラウンドは taro が勝ちました
q で終了、それ以外のキーでPlay:
taro は 10 of hearts、jiro は 2 of diamonds を引きました
このラウンドは taro が勝ちました
ゲーム終了、jiro の勝利です!
[root@centos69 dokugaku]#