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?

【PHP】ポケモンタイプ相性表

0
Last updated at Posted at 2025-10-17

参考
https://www.pokemon.co.jp/ex/sun_moon/fight/161215_01.html

ポケモンの防御側から見たタイプ相性表です

例:ギャラドス 水・飛行タイプ

["×4"]=>電気
["×0.5"]=>炎・水・格闘・鋼
["×0"]=>地面

コード


<?php

function getTypeDefenseTable(...$typeIds) {
    // 引数が空またはnullの場合はnullを返す
    if (empty($typeIds) || (count($typeIds) === 1 && (is_null($typeIds[0]) || $typeIds[0] === ''))) {
        return null;
    }
    
    // 引数が1つで文字列(カンマ区切り)の場合は配列に変換
    if (count($typeIds) === 1 && is_string($typeIds[0]) && strpos($typeIds[0], ',') !== false) {
        $typeIds = array_map('intval', explode(',', $typeIds[0]));
    }
    
    // 有効なタイプID(1〜18)のみを抽出
    $validTypeIds = array_filter((array)$typeIds, function($id) {
        return is_numeric($id) && $id >= 1 && $id <= 18;
    });
    
    if (empty($validTypeIds)) {
        return null;
    }
    
    $typeIds = array_map('intval', $validTypeIds);
    
    /**
     * 防御側から見た耐性チャート(最新世代準拠)
     * 各IDが飛んできた時、そのタイプ(INDEX)が受ける倍率の定義
     */
    $defenseChart = [
        1  => ['x2' => [7],          'x0.5' => [],           'x0' => [14]], // ノーマル
        2  => ['x2' => [3,9,13],     'x0.5' => [2,5,6,12,17,18], 'x0' => []], // ほのお
        3  => ['x2' => [4,5],        'x0.5' => [2,3,6,17],   'x0' => []], // みず
        4  => ['x2' => [9],          'x0.5' => [4,10,17],    'x0' => []], // でんき
        5  => ['x2' => [2,6,8,10,12], 'x0.5' => [3,4,5,9],    'x0' => []], // くさ
        6  => ['x2' => [2,7,9,13,17], 'x0.5' => [6],           'x0' => []], // こおり
        7  => ['x2' => [10,11,18],   'x0.5' => [12,13,16],   'x0' => []], // かくとう
        8  => ['x2' => [9,11],       'x0.5' => [5,7,8,12,18], 'x0' => []], // どく
        9  => ['x2' => [3,5,6],      'x0.5' => [8,13],       'x0' => [4]], // じめん
        10 => ['x2' => [4,6,13],     'x0.5' => [5,7,12],      'x0' => [9]], // ひこう
        11 => ['x2' => [12,14,16],   'x0.5' => [7,11],       'x0' => []], // エスパー
        12 => ['x2' => [2,10,13],    'x0.5' => [5,7,9],       'x0' => []], // むし
        13 => ['x2' => [3,5,7,9,17], 'x0.5' => [1,2,8,10],    'x0' => []], // いわ
        14 => ['x2' => [14,16],      'x0.5' => [8,12],       'x0' => [1,7]], // ゴースト
        15 => ['x2' => [6,15,18],    'x0.5' => [2,3,4,5],    'x0' => []], // ドラゴン
        16 => ['x2' => [7,12,18],    'x0.5' => [14,16],      'x0' => [11]], // あく
        17 => ['x2' => [2,7,9],      'x0.5' => [1,5,6,10,11,12,13,15,17,18], 'x0' => [8]], // はがね
        18 => ['x2' => [8,17],       'x0.5' => [7,12,16],    'x0' => [15]] // フェアリー
    ];

    $typeNames = [
        1 => 'ノーマル', 2 => 'ほのお', 3 => 'みず', 4 => 'でんき', 5 => 'くさ',
        6 => 'こおり', 7 => 'かくとう', 8 => 'どく', 9 => 'じめん', 10 => 'ひこう',
        11 => 'エスパー', 12 => 'むし', 13 => 'いわ', 14 => 'ゴースト', 15 => 'ドラゴン',
        16 => 'あく', 17 => 'はがね', 18 => 'フェアリー'
    ];

    // 攻撃タイプ(atk)ごとに最終倍率を計算
    $finalMultipliers = [];
    for ($atk = 1; $atk <= 18; $atk++) {
        $mult = 1.0;
        foreach ($typeIds as $defType) {
            if (!isset($defenseChart[$defType])) continue;
            
            $effect = $defenseChart[$defType];
            
            if (in_array($atk, $effect['x0'])) {
                $mult = 0;
                break; // 1つでも無効タイプがあれば最終倍率は0確定
            }
            if (in_array($atk, $effect['x2'])) $mult *= 2;
            if (in_array($atk, $effect['x0.5'])) $mult *= 0.5;
        }
        $finalMultipliers[$atk] = $mult;
    }

    // 倍率ごとに分類
    $groups = [
        '×4' => [],
        '×2' => [],
        '×0.5' => [],
        '×0.25' => [],
        '×0' => []
    ];

    foreach ($finalMultipliers as $atk => $mult) {
        if ($mult == 4.0) $groups['×4'][] = $typeNames[$atk];
        elseif ($mult == 2.0) $groups['×2'][] = $typeNames[$atk];
        elseif ($mult == 0.5) $groups['×0.5'][] = $typeNames[$atk];
        elseif ($mult == 0.25) $groups['×0.25'][] = $typeNames[$atk];
        elseif ($mult == 0.0) $groups['×0'][] = $typeNames[$atk];
    }

    return array_filter($groups); // 中身がある倍率のみ返す
}

// 例:くさ(5)・どく(8) の相性を表示
echo('<pre>');var_dump(getTypeDefenseTable(5,8));echo('</pre>');

結果

array(5) {
  ["×4"]=>
  array(1) {
    [0]=>
    string(9) "でんき"
  }
  ["×2"]=>
  array(0) {
  }
  ["×0.5"]=>
  array(4) {
    [0]=>
    string(9) "ほのお"
    [1]=>
    string(6) "みず"
    [2]=>
    string(12) "かくとう"
    [3]=>
    string(9) "はがね"
  }
  ["×0.25"]=>
  array(0) {
  }
  ["×0"]=>
  array(1) {
    [0]=>
    string(9) "じめん"
  }
}

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