I am trying to convert a chart of pokemon types into a PHP function so that it will return an attack multiplier based on the types that two pokemon have.

First, here is the chart that holds all of the different types: http://www.serebii.net/games/type.shtml

Other than making an insanely large function filled with if else conditions, is there any easier way for me to convert the chart data to PHP.

This is what I have so far:

Code:

function type($user_type1, $user_type2, $enemy_type1, $enemy_type2) {
   $types = array(1 => "normal", "fire", "water", "electric", "grass", "ice", "fighting", "poison", "ground", "flying", "psychic", "bug", "rock", "ghost", "dragon", "dark", "steel");
   
   
   
   
   
   
   return $multiplier;
}


Basically, I was wondering if anyone had any better ideas. There is most likely an easier way that I haven't discovered.
Any time you start needing a massive column of conditional statements, you probably should be using a jump table instead, or the PHP equivalent. In this case, it would indeed be a multi-dimensional array, most likely.
I just thought of a crazy idea for this problem. Is it possible for php to return the RGB value of a pixel in a certain location of an image? Since all of the cells are equally spaced, could I make the chart into an image and have PHP return the effectiveness of the attack based on the x and y axis of the chart?

The above most likely made no sense at all and I have no idea if PHP is capable of this, but think of it like a coordinate system.

Lets say that I want to see the effectiveness of a fire attack against a water attack.

Since fire is 2 over to the right on the x axis and water is 3 down on the y axis, I might be able to get the pixel of the center of the cell. I could also change the effectiveness images into a square of a solid color so that PHP could more easily differ between each multiplier.

If you need me to elucidate, I'll try my best.

EDIT: Here is my new chart that I made for the PHP image reader if such a thing exists Smile

Link: http://master-alligator.com/pokemon/types.php

Now the location that PHP reads the pixel color at will be easier.

EDIT EDIT: Holy crap! I had an epiphany! Eureka! Instead of making a large multi-dimensional array like Kerm mentioned, I have transformed the chart into a large image and have made a function that will return the multiplier attack based off of reading the pixel colors at the coords specified.

It is kind of complicated but it works! Thanks for the help Kerm.
kpa4941 wrote:
EDIT EDIT: Holy crap! I had an epiphany! Eureka! Instead of making a large multi-dimensional array like Kerm mentioned, I have transformed the chart into a large image and have made a function that will return the multiplier attack based off of reading the pixel colors at the coords specified.

It is kind of complicated but it works! Thanks for the help Kerm.


So instead of using built in multi-dimensional arrays, you created a really, really shitty multi-dimensional array using an image?

What the hell is wrong with you?
Kllrnohj wrote:

So instead of using built in multi-dimensional arrays, you created a really, really shitty multi-dimensional array using an image?

What the hell is wrong with you?


I am not quite sure what is wrong with me, the doctors still haven't figured it out.

I am assuming that the above was too convoluted for you to follow so I'll try to repeat my previous explanation in vernacular.

Instead of making an arduous multi-dimensional array out of this chart, I decided to make an AWESOME and efficient image that could use x and y coords along with PHP's imagecolorat function. By assigning values to the Pokemon types, I was able to get the color of the pixel at the specified location of my improved type chart.

Here is my function that I made that will return the damage multiplier of the pokemon's attack vs the enemy's defense.

Code:

function type($attack_type, $defense_type, $defense_type2) {
    # Checks for 2nd defense type
    if ($defense_type2 != null) {
        $defense_type .= $defense_type2;
    }
    # X Coordinate for Attack
    $attack_x = $types[0][strtolower($attack_type)]-1;
    # Y Coordinate for Defense
    $defense_y = $types[0][strtolower($defense_type)]-1;
    $im = imagecreatefrompng("Types.png");
    $rgb = imagecolorat($im, $attack_x, $defense_y);
   
    # Green
    if ($rgb == 14089693) {
        $multi = 2;
    }
    # Yellow
    if ($rgb == 16710240) {
        $multi = .5;
    }
    # Orange
    if ($rgb == 16745788) {
        $multi = 0;
    }
    # Dark Red
    if ($rgb == 10494237) {
        $multi = 4;
    }
    # Light Red
    if ($rgb == 16724779) {
        $multi = .25;
    }
    # Gray
    if ($rgb == 5395026) {
        $multi = 1;
    }
    return $multi;
}


Now that you have seen my really, really sh¡tty multi-dimensional array, it would be really, really nice if I could see what you have come up with. It would be really, really helpful to get another morale filled comment from you.
I must have to agree with Kllrnohj, why use a picture when you could use a multi-dimensional array? It'll require a lot less math, it'll be faster, and you don't need to lug that image along with the script.
Well rather than cursing me out and telling me how stupid I am, could you explain or provide code that shows the multi-dimensional array. The picture is only 4 kb big. The code itself is bigger than the picture. It works perfectly fine in my situation.

I won't jump to conclusions like dogmatic people and say that your multidimensional array is really, really sh¡tty without even seeing how it works, but it'd be nice if you would show me some of your code rather than just say that it is better and I ought to belief you.
It's Kllrnohj. He's like that, ignore that part. However, 99.99% of the time, he's right about things, but rather blunt in execution of explaining. Hence, his avatar.

I don't use PHP, but I do know it is slow enough without using images instead of arrays; think about it -- with a picture, not only are you using non-native structures (which means slower and larger code), but code for retrieving data from images is generally slow already, so multidimensional arrays would be much faster and a better choice.
Can you show me maybe the first line of how you would convert the chart data from the original image here into an array in PHP or something familiar? I am more of a visual rather than textual learner, and I don't really see how you could convert the chart all into a multi-dimensional array and what you would do after that to calculate the multipliers. Thanks.
Kllrnohj is correctly pointing out that using an image as a two-dimensional array is staggeringly inefficient; you're essentially using the time-wasteful image set and read routines to access the underlying two-dimensional array that PHP uses to store the image rather than just using a two-dimensional array of your own. Listen to the point that Kllrnohj is trying to make rather than the blunt way in which he put it, as he's generally correct if you shrug off his delivery. He's a skilled programmer, so I'm sure he understood what you were suggesting be done, and while it works, it's a bad idea speed-wise.
You can create a multidimensional array like so:


Code:
$pokemon_thing = array (
array (1,2)
array (3,4)
);


Makes a 2x2 array. Here's how to access an element:


Code:
$pokemon_thing[1][0]


This will return 3. A lot easier than using an image, and a LOT faster.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement