0
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?

More than 5 years have passed since last update.

頭チキンのプログラム vol.2

Last updated at Posted at 2018-11-17

pythonの『%』を算術の『÷』と勘違いした間抜けがいるらしい・・・・・・私です

本当に恥ずかしくて泣きたくなりますねぇ!

と、そんなpythonなんですが、ふと思ったので久々にCheckiOにログインしてみました

chekio.png

こんな感じで解ける所だけ解いていたんですが、Homeの所に一問新しく追加されていました

(画像は解いた後なのでオレンジになっています)

今回はそこを解こうと試行錯誤したことでも書こうかな、と思います

The Warriors

the_warriors.py
技の威力が固定で命中率が100のポケモンを再現せよ! という問題
_______________________________________________________________
class Warrior:
   health = 50
   is_alive = True
   attack = 5

class Knight(Warrior):
    attack = 7

def fight(unit_1, unit_2):
    while unit_1.health > 0:
        unit_2.health -= unit_1.attack
        unit_1.health -= unit_2.attack
    
    if unit_2.health>unit_1.health:
        unit_1.is_alive = False
        return False
    else:
        unit_2.is_alive = False
        return True

私のpython習熟度は、構文△→関数☓→クラス☓☓

なので、個人的に解けたことが奇跡感あります

classってここまで気楽に使えるんですね・・・インスタンス?でしたっけ?

あれ定義しなくても何とかなるんですねぇ

Fortranで言う所の副プログラムのような使い勝手です

なら、Fortranで書き換えられるのでは?・・・次の課題としましょう

と、一発でこれが書けていればよかったんですがね!

miss.py
class Warrior:
    def __init__(self):
        self.health = 0
        self.attack = 0
        self.is_alive = 1
        
    def character(self):
        self.health += 50
        self.attack += 5

class Knight(Warrior):
    def __init__(self):
        super().__init__()
        self.health += 50
        self.attack += 7
    
def fight(unit_1, unit_2):
    while unit_1.health == 0:
        unit_2.health -= unit_1.attack
        unit_1.health -= unit_2.attack
    
    if unit_2.health>unit_1.health:
        unit_1.is_alive -= 1
        return False
    else:
        unit_2.is_alive -= 1
        return True

ろくに学びもせずに使うもんじゃないですね!

CheckiOは基本的にある程度の形だけは与えてくれるんですよね

この問題だと、

ChekiO.py
class Warrior:
    pass

class Knight(Warrior):
    pass

def fight(unit_1, unit_2):
    return 0

こう書いてくれているわけです

これを使わない手はないので、使おうとしたのですが、クラスについて私は何も分かりません

そこで何となく形が似てそうな構文を探し始めるわけですよ

で、どうも継承とやらを使うと、こんな感じの形式になると分かりました

そうして、この二つ目を書いたのですが、大きな落とし穴があったのです

"self.is_alive"の処理が一切できない

色々なパターンを見た感じ、"def init(self):"に問題がありそう・・・多分ですが

おいおいclassや関数はゆっくり学んでいけたらなーと考えています

と、こんな感じですかね

結局の所、今回を通じて言いたいことは、これ位ガバガバでもなんとかなるよ!という話

分からないから使わないより、使ってみたけどダメでしたーの方がいいんじゃないですかね?

チャレンジに無駄はないのです

以上!

0
0
1

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
0
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?