1.

Solve : ArrayList in PHP Question - Find line in file and populate with list data?

Answer»

Making a Turn Based RPG game to strengthen skills in PHP and looking for advice on handling population of stats per usable items.

To me it seems as though a list would be usefull such as an ArrayList to where say a piece of gear is looted from the seeded random generator, that points at say element 88 on the loot table ( also ArrayList for now until I implement mySQL Database) and 0-99 srand spits out 88 in which at the ArrayList the data contained within the location of ArrayList[88] is item 0179. Now Item # 0179 has a list of information associated with it such as the following data components, where the last 4 stats are for better items that have benefit abilities that the integer for each one either disables any benefit at all with a 0 or say 1 = +10 strength, and 2 = +20 health on top of the base level stats that each player is given based on their player class etc.

Item# - Description - Swings per turn - minimum damage - maximum damage - stat0 - stat1 - stat2 - stat3

0179 - Broad Axe - 1 - 10 - 20 - 0 - 0 - 2 - 1

So the ArrayList is populated with ("0179, Broad Axe, 1, 10, 20, 0, 0, 2, 1")

But the problem is that I need to call 0179 and then confirm a match to 0179 of the first element of the list, and then populate the mixture of string and integer data into variables which then are used to populate the player stat build, and I am curious as to how I can populate this info into the ArrayList from a file, when I would need to have either a file for each and every item which would be foolish, or even better, a way to locate the line in this file that holds the starting value of 0179 and then populates the ArrayList with the 9 elements from the file with error checking of the 0179 to confirm that it is in fact being populated from the correct line in the file. Maybe this error checking is overboard and not needed, but I am use to placing error checking into programs to maintain that they execute within their intended operating limits. Maybe this is code execution paranoia, but it was soemthing that was stated over and over by college professor for C++ to maintain error checking and never assume that its going to always run as intended.

I made a similar turn based peer to peer game using C++ many years ago that was able to be played over network between 2 computers as a final project for intermediate C++, but to avoid cheating I decided to place all the items hard CODED into the source so it was a very lengthy program in size by not using an ArrayList and pointing to a file for reference to populate the ArrayList with the data and keeping the program small. The instructor in SCORING your projects, in addition to looking at techniques used would take away points if for example he could get the game or program to crash due to poor technique when it asks for you to enter your age for example and then he would type in a non integer input etc as one student made the mistake of having this input in his program and not testing the input to be an integer within the limits of say 1 and 120, and the screen would scroll and he would instantly take away 10 points from the 100 you could have gotten etc so now your working with potential for a 90 and hopefully he doesnt find any additional flaws in the design. I got a 100 on my project because he actually looked to see if he could manipulate a data file to his benefit and the data that was shared between the 2 computers was a bunch of numbers of which I masked the numbers intentions by given them all offsets so that he couldnt look at his health on the game and then loot at the file that was ACCESSED between both systems over the network and easily make sense of it to give himself 1 million health or make the next attack an instant fatality by editing this file to have say 1 million damage when it was like 10 or 20 points of damage per hit etc with randoms for dodge, block, hit etc so that david can beat goliath if the numbers are in his favor even with weaker stats making it fun. Being that this data file needed to have the ability to be read/write from these 2 systems, the data on this file was unprotected really in its current state so if given enough time he could have figured out the offsets to decipher it and edit the file to his benefit etc.

BUT the good thing with PHP is that the data that can allow for cheating etc is all server side with the only input being form data submissions, so I just need to make sure that I dont code it with any injection attack vulnerabilities and then it should be all set.

So looking for any input people may have on this for better suggestions on doing this or if anyone has a code example that shows how to search and pick a line of data out of say a text file which populates the ArrayList that would be an awesomely big help to the roadblock I am at with handling this. Below is an example of the data file and finding say 0181 and populating the ArrayList with the data of the 9 elements is where I am stumped if this is the best method?

The data structure would be comma delimited: ( * since space delimited would be a major problem with space in description titles )

0179, Broad Axe, 1, 10, 20, 0, 0, 2, 1
0180, Heavy Sword, 1, 11, 21, 0, 0, 1, 2
0181, Bone Dagger, 2, 6, 12, 0, 0, 3, 1
0182, MYSTERY Staff, 1, 10, 20, 2, 1, 1, 0

*Note: Current plan is to get the game working with local data file etc and then later implement calls to mySQL and do away with the file containing the contents which is best for a actual transactional database to manage.Sorry I can't help. It is much to hard for me. But if it was me, I would run PHP at home, which I do for testing WordPress themes. You don't need a second computer.

Here is a article that claims you can test PHP, doing it on Apple, Linux and Windows with one PC. I( have not tried this method.)
Quote

You can easily install the Apache web server (A), MySQL (M) and PHP (P) on your laptop or home PC. The three come bundled together, referred to as either the WAMP, MAMP or LAMP according to whether you are installing them on Windows (W), a Mac (M), or Linux (L)
How to run PHP at home: WAMP, MAMP and LAMP

Hope that MIGHT help a little.
There is no arraylist in PHP. PHP arrays however are associative arrays/hashmaps, so you can use any key you want.
This loads in a file with a similar format to what you gave and creates a associative array with the key being the Item# (the first item on each line).
Code: [Select]<?php
functionreadData(datafile){
$lines=explode("\n",file_get_contents(datafile));
return$lines;
}
functionparseData(input){
hash=array();
foreach(inputas$line){
$splitline=explode(",",$line);
hash[$splitline[0]]=array_slice($splitline,1);
}
returnhash;
}
echo"begin";
$weapondata=parseData(readData("weapon.txt"))
//$weapondatacannowbelookedupwithIDnumbers.
//eg$weapondata["0179"]willreturnanarray{"BroadAxe","1","10","20","0","0","2","1"}
?>
Ideally, you would be using a separate class to store the information for each item; then you could use something like $weapondata["0179"].getDamage() or something to that effect.Quote
There is no arraylist in PHP. PHP arrays however are associative arrays/hashmaps, so you can use any key you want.

hmmm .... I found this example when looking for an arraylist for PHP at the link below using the list() function the other day and since it is termed an arraylist even though list() function I thought it was the same.

http://www.w3schools.com/php/func_array_list.asp

Thanks for providing me with code below to work with showing the explode and parse of the data. Thats a Big Help! Now beyond the roadblock I had with your help.


Discussion

No Comment Found