SlideShare a Scribd company logo
1 of 33
K3Python講座2018 第6回
オブジェクト指向入門コース 第2回
前回の復習
(*>△<)ナーンナーン!
再帰関数とは
関数の中で自分自身を呼び出すもの
再帰の例
def plusplus(x):
if x == 1:
return 1
else:
return plusplus(x - 1) + x
print(plusplus(10))
plusplus(10)
plusplus(9)の結果がほしいよ!!
plusplus(9)
plusplus(8)の結果がほしいよ!!
plusplus(2)
plusplus(1)の結果がほしいよ!!
plusplus(1)
1やで!!
1
plusplus(10)
plusplus(9)の結果がほしいよ!!
plusplus(9)
plusplus(8)の結果がほしいよ!!
plusplus(2)
1 + 2 = 3
やで!!
3
plusplus(10)
plusplus(9)の結果がほしいよ!!
plusplus(9)
36 + 9 = 45やで!!
45
plusplus(8) = 36
plusplus(10)
plusplus(9)の結果が
ほしいよ!!
45
plusplus(10) = 45 + 10 = 55
plusplus(10)で行われている計算
plusplus(10)
= plusplus(9) + 10
= plusplus(8) + 9 + 10
= plusplus(7) + 8 + 9 + 10
= plusplus(6) + 7 + 8 + 9 + 10
= plusplus(5) + 6 + 7 + 8 + 9 + 10
= plusplus(4) + 5 + 6 + 7 + 8 + 9 + 10
= plusplus(3) + 4 + 5 + 6 + 7 + 8 + 9 + 10
= plusplus(2) + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10
= plusplus(1) + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10
= 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10
フラクタル
Git
バージョン管理を簡単にしてくれるツールです
詳しい説明は省きますが、簡単に保存した好きな時期
のファイルに戻したり、並列して同じファイルに違う
変更を加えていったり出来ます
興味がある方は各自で調べてみてくださいませ
GitHub
Gitで管理しているファイルをアップロードして公開できるサイト
(クラウドの一種)
イメージとしてはコードにとってのミニSNSのようなもの
企業の方が採用に使うこともある
非公開のところでは企業が実際のソフトウェアを共同開発してい
たりするようです
クラスを使おうというご提案
クラスとは、変数を集め、それを扱うため
の関数を機能として付属させた設計図
のようなもの
クラスを使おうというご提案
プレイヤークラスを基に生成
player1インスタンス
hp = 100, atk = 30, defe = 20
spd = 10, url = “pl1.jpg”
player1.hpやplayer1.atkのように
[インスタンス名].[変数名]で扱う
Playerクラス
変数: hp, atk, defe, spd, url
関数: damage(atk): 相手の攻撃力を引数
として取り、ダメージを受ける
player3インスタンス
player2インスタンス
クラスを使おう
player1インスタンス
player1.hp =
100
player1.atk =
30
player1.defe =
100
player1.spd =
10
player1.url =
“pl1.jpg”
__init__関数
インスタンスを作るときに
呼び出される
damage関数
hpをへらすための関数
コードにしてみる
class Player:
def __init__(self, _hp, _atk, _def, _spd, _url): # 変数の初期設定
self.hp = _hp
self.atk = _atk
self.defe = _def
self.spd = _spd
self.url = _url
def damage(self, enemyAtk):
dmg = enemyAtk - self.defe
if dmg < 1:
dmg = 1
self.hp -= dmg
各変数の値を設定する
selfが頭についているものはクラス全体の変数
selfが頭についていない変数はその関数が終わったら死ぬ変数
最初の引数はクラス自体を表す
コードにしてみる
player1 = Player(100, 30, 20, 10, "pl1.jpg")
print(player1.hp) # HPの100を出力
player1.damage(50) # Atk=50で攻撃され30ダメージ
print(player1.hp) # HPの70を出力
player2 = Player(100, 80, 10, 5, "pl2.jpg")
player1.damage(player2.atk) # pl2からpl1に攻撃
# Atk=80で攻撃され60ダメージ
print(player1.hp) # HPの10を出力
復習問題(再帰)
• 最初に2値の値が順に与えられる。これを順に𝑎𝑎1, 𝑎𝑎2とする。
• これ以降については𝑎𝑎𝑛𝑛を以下の漸化式で定義する。
𝑎𝑎𝑛𝑛+1 = 2 × (𝑎𝑎𝑛𝑛 + 𝑎𝑎𝑛𝑛−1)
このとき、𝑎𝑎1, 𝑎𝑎2と𝑛𝑛の値がスペース区切りで標準入力(input)
から与えられたときに、𝑎𝑎𝑛𝑛を出力するプログラムを作成せよ。
10 50 20
↑これがスペース区切りの例
復習問題(再帰) ― 標準入力の扱い
a = input() # これが標準入力
st = a.split()
# スペース区切りで入力されたものをsplit()をつけると、リストにして
くれる
# st[0] = "10", st[1] = "50", st[2] = "20" みたいになる
a1 = int(st[0]) # これで最初の文字列10を数値の10に変えられる
復習問題(再帰) ― 大まかな流れ
def nisebonacchi(a1,a2,n):
# 再帰プログラムを書く
print ----
s = input() # 単に入力を待つならこう
# 文字を処理してa1,a2,nを数値として取り出す
print(nisebonacchi(a1,a2,n))
チャレンジ問題(再帰)
• 正方形のタイルが敷き詰められた長方形の部屋がある.
• タイルの色は赤か黒 である.
• 最初に一人の人が部屋の黒いタイルの上に立っている.
• あるタイルからは隣接する四つのタイルに移動することができる.
• ただし,赤いタイルの上に移動することはできない.
• 移動できるのは黒いタイルの上だけである.
上記の移動を繰り返すことによって到達できるタイルの数を答えるプロ
グラムを書きなさい.→より詳しい入力等はScrapboxを参照
カプセル化
隠す機能
端的にいうと変数や関数を隠す機能
変数や関数の名前の最初2文字を_(アンダースコア)にすると隠せる
__hp
__check(self, x, y): など
__init__もこれにより隠された機能の一つ
なぜ隠すのか
プログラムで変数などを隠す必要って??
保守性(serviceability)に関連する
保守性とは
プログラムの維持管理(保守)がどのくらいしやすいかを表す
メンテが多い/長いソシャゲは保守性が悪いと考えられる
どう関係あるねん??
保守性とカプセル化
class Person:
def __init__(self, h, w):
self.__height = 100
self.__weight = 0
self.set_height(h)
self.set_weight(w)
def get_height(self):
return __height
def get_weight(self):
return __weight
def set_height(self, h):
if 100 <= h < 300:
self.__height = h
else:
print("Error: …")
def set_weight(self, w):
if 0 <= w < 300:
self.__weight = w
else:
print("Error: …")
つまり
身長が10,000cmなんて数値はありえない
体重がマイナスとかもありえない
こういう値を排除することができる!
複数人でやるときは
クラスを他の人に使わせるという機会も多い
このとき、その人はどの関数で何が出来る、程度しかわからない
理解されていない変数を適当に扱われると意図しない動きをするかも
そんなときはカプセル化して触れなくすればいい!!!
練習問題(クラス)
複素数クラスを作る
持つ変数(2つ)
実数部・虚数部をそれぞれ表す変数
持つ関数(__init__+4つ)
引数に別の複素数を取って、自らとその引数で足す引く掛けるを
行った複素数を返す関数
その複素数をstringとして(“-5+i”のような形で)返す関数
練習問題のイメージ
class ComplexNumber:
def __init__(self, r, i):
# 初期値設定
def addition(self, comp):
# 足し合わせる
return ComplexNumber(r, i)
def subtraction(self, comp):
# 引く
return ComplexNumber(r, i)
def multiplication(self, comp):
# 掛ける
return ComplexNumber(r,i)
def to_string(self):
# __str__の仕様が分かる人はそれでもOK
return ----
c1 = ComplexNumber(5,2)
c2 = ComplexNumber(-4,2)
print(c1.to_string())
print(c2.to_string())
pl = c1.addition(c2)
mi = c1.subtraction(c2)
mul = c1.multiplication(c2)
print(pl.to_string())
print(mi.to_string())
print(mul.to_string())
カードゲームを作ろうプロジェクト②
トランプ~~(o・∇・o) ウノとかでもいいよ(天゚∀゚)
演習問題(クラス・前回の続き)
前回作ったCardクラス、Deckクラスの他に、Playerクラスを作り、
手札を作る
Playerクラスはプレイヤーのステータスを表す。各々が作るゲー
ムに依って以下のような変数などを持つ。
Hands:手札
Point:現在の点数
Coin:持っている金
プログラムを組む前にどんな構造にするか考えてみると良いかも。
例:ブラックジャックの場合
Playerクラス
変数
Hands:手札
Coins:持っているコイン
Bet:掛けたお金
などなど・・・
関数
draw:カードを追加で一枚引く
betting:掛け金を決める
などなど・・・

More Related Content

Recently uploaded

Recently uploaded (8)

MPAなWebフレームワーク、Astroの紹介 (その1) 2024/05/17の勉強会で発表されたものです。
MPAなWebフレームワーク、Astroの紹介 (その1) 2024/05/17の勉強会で発表されたものです。MPAなWebフレームワーク、Astroの紹介 (その1) 2024/05/17の勉強会で発表されたものです。
MPAなWebフレームワーク、Astroの紹介 (その1) 2024/05/17の勉強会で発表されたものです。
 
情報を表現するときのポイント
情報を表現するときのポイント情報を表現するときのポイント
情報を表現するときのポイント
 
LoRaWAN無位置ロープ型水漏れセンサー WL03A-LB/LSカタログ ファイル
LoRaWAN無位置ロープ型水漏れセンサー WL03A-LB/LSカタログ ファイルLoRaWAN無位置ロープ型水漏れセンサー WL03A-LB/LSカタログ ファイル
LoRaWAN無位置ロープ型水漏れセンサー WL03A-LB/LSカタログ ファイル
 
2024年5月17日 先駆的科学計算フォーラム2024 機械学習を用いた新たなゲーム体験の創出の応用
2024年5月17日 先駆的科学計算フォーラム2024 機械学習を用いた新たなゲーム体験の創出の応用2024年5月17日 先駆的科学計算フォーラム2024 機械学習を用いた新たなゲーム体験の創出の応用
2024年5月17日 先駆的科学計算フォーラム2024 機械学習を用いた新たなゲーム体験の創出の応用
 
ネットワーク可視化 振る舞い検知(NDR)ご紹介_キンドリル202405.pdf
ネットワーク可視化 振る舞い検知(NDR)ご紹介_キンドリル202405.pdfネットワーク可視化 振る舞い検知(NDR)ご紹介_キンドリル202405.pdf
ネットワーク可視化 振る舞い検知(NDR)ご紹介_キンドリル202405.pdf
 
LoRaWAN無位置ロープ式水漏れセンサーWL03A 日本語マニュアル
LoRaWAN無位置ロープ式水漏れセンサーWL03A 日本語マニュアルLoRaWAN無位置ロープ式水漏れセンサーWL03A 日本語マニュアル
LoRaWAN無位置ロープ式水漏れセンサーWL03A 日本語マニュアル
 
Hyperledger Fabricコミュニティ活動体験& Hyperledger Fabric最新状況ご紹介
Hyperledger Fabricコミュニティ活動体験& Hyperledger Fabric最新状況ご紹介Hyperledger Fabricコミュニティ活動体験& Hyperledger Fabric最新状況ご紹介
Hyperledger Fabricコミュニティ活動体験& Hyperledger Fabric最新状況ご紹介
 
Keywordmap overview material/CINC.co.ltd
Keywordmap overview material/CINC.co.ltdKeywordmap overview material/CINC.co.ltd
Keywordmap overview material/CINC.co.ltd
 

Featured

Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Saba Software
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
Simplilearn
 

Featured (20)

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
 

K3Python2018 OOP-02