Skip to content

Instantly share code, notes, and snippets.

@ounziw
Last active April 18, 2016 04:12
Show Gist options
  • Save ounziw/ed70cd258158ebc5bfa875a8746d9ea2 to your computer and use it in GitHub Desktop.
Save ounziw/ed70cd258158ebc5bfa875a8746d9ea2 to your computer and use it in GitHub Desktop.
<?php
$woman_1 = new Woman(0,1);
$woman_1->bear(2);
$woman_1->bear(3);
$woman_1->bear(4);
$woman_2->bear(5);
$woman_2->bear(6);
$woman_2->bear(7);
$woman_3->bear(8);
$woman_3->bear(9);
$woman_3->bear(10);
$woman_4->bear(11);
$woman_4->bear(12);
$woman_4->bear(13);
$woman_5->bear(14);
$woman_5->bear(15);
$woman_5->bear(16);
$woman_6->bear(17);
$woman_6->bear(18);
$woman_6->bear(19);
$woman_7->bear(20);
$woman_7->bear(21);
$woman_7->bear(22);
$woman_8->bear(23);
$woman_8->bear(24);
$woman_8->bear(25);
$woman_9->bear(26);
$woman_9->bear(27);
$woman_9->bear(28);
$woman_10->bear(29);
$woman_10->bear(30);
$woman_10->bear(31);
$woman_11->bear(32);
$woman_11->bear(33);
$woman_11->bear(34);
$woman_12->bear(35);
$woman_12->bear(36);
$woman_12->bear(37);
$woman_13->bear(38);
$woman_13->bear(39);
$woman_13->bear(40);
<?php
require 'woman.php';
require 'bear.php';
function getStart($inputarray) {
return 'woman_'. $inputarray[0];
}
function getEnd($inputarray) {
return intval($inputarray[1]);
}
$input = '12->7';
$inputarray = explode('->',$input);
$start = getStart($inputarray);
$end = getEnd($inputarray);
if ($end === $$start->getMe() ){
echo 'me';
} elseif ($end === $$start->getParent() ){
echo 'mo';
} elseif (in_array($end,$$start->getDaughter()) ){
echo 'da';
} elseif (in_array($end,$$start->getSister()) ){
echo 'si';
} elseif (in_array($end,$$start->getAunt()) ){
echo 'au';
} elseif (in_array($end,$$start->getNiece()) ){
echo 'ni';
} elseif (in_array($end,$$start->getCousin()) ){
echo 'co';
} else {
echo '-';
}
<?php
class Woman {
protected $me;
protected $parent;
protected $daughter;
/**
* 親と自分の番号を決めてクラスを作る
* この時点では子は無し
* new Woman(親の番号, 自分の番号)
* @param $parent
* @param $number
*/
function __construct($parent,$number) {
$this->me = intval($number);
$this->parent = intval($parent);
$this->daughter = array();
}
/**
* 子を産む
* 自分の$daughterに子の番号入れる
* 子の$parentに自分の番号を入れる
* @param $number
*/
function bear($number) {
$this->daughter[] = intval($number);
$daughter = 'woman_' . $number;
global $$daughter;
$$daughter = new Woman($this->me,$number);
}
function getMe() {
return $this->me;
}
function getParent() {
return $this->parent;
}
function getDaughter() {
return $this->daughter;
}
/**
* 姉妹を取得
* ● 親の子を取得
* ● 自分を除く
* @return array
*/
function getSister() {
$mother = 'woman_' . $this->parent;
global $$mother;
if (!is_object($$mother)) {
return array();
}
$me = array($this->me);
$sister = array_diff($$mother->getDaughter(),$me);
return $sister;
}
/**
* おばを取得
* ● 母を取得
* ● 母の姉妹を取得
* @return array
*/
function getAunt() {
$mother = 'woman_' . $this->parent;
global $$mother;
if (!is_object($$mother)) {
return array();
}
$aunt = $$mother->getSister();
return $aunt;
}
/**
* めいを取得
* ● 姉妹を取得
* ● 姉妹の子を取得
* @return array
*/
function getNiece() {
$niece = array();
$sister = $this->getSister();
foreach ($sister as $val) {
$obj = 'woman_' . $val;
global $$obj;
if (is_object($$obj)) {
$niece = array_merge($niece,$$obj->getDaughter());
}
}
return $niece;
}
/**
* いとこを取得
* ● おばを取得
* ● おばの子を取得
* @return array
*/
function getCousin() {
$cousin = array();
$aunt = $this->getAunt();
foreach ($aunt as $val) {
$obj = 'woman_' . $val;
global $$obj;
if (is_object($$obj)) {
$cousin = array_merge($cousin,$$obj->getDaughter());
}
}
return $cousin;
}
}
@ounziw
Copy link
Author

ounziw commented Apr 18, 2016

おばの取得方法を変更した。

変更前
祖母を取得=>その子を取得
自分の親が含まれる

変更後
母を取得=>その姉妹を取得
自分の親が含まれない(姉妹を取得するメソッドで「自分でない」をチェックしているため)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment