Forum Moderators: coopster

Message Too Old, No Replies

Complex array system

         

messageboy

2:57 am on Aug 4, 2009 (gmt 0)

10+ Year Member



Hello,

For the past couple of months I have been coding a website that involves a complex combat system for a text based game. I have hit a wall that seems to refuse to break.

Currently I have set it up so that there a function that receives ID for the players involved in the fight. From those ID it retrieves information from the database to decide attack order and other results.

The problem is that I want to make the combat system flexible instead of a preset of values and variables. So have a combat system that will work no matter how many players go into the combat.

I have found that arrays with variables in variables is the best method to do this. Which I was able to code the attack order to work but then I couldn't figure out how to process from there.

After the attack order code fragment the players are then suppose to start attacking eachother in the attack order in a turn based system where first turn is over when all the players have attacked once then second turn starts and each player attacks again in the attack order.

This might not have been explained clearly so sorry, here is the coding.

[php]

/// Combat System ///

function CombatSystem( $Array1, $Array2){

switch (rand(1,5))
{

case 1:

$Ambush = true1;

break;

case 5:

$Ambush = true2;

break;

default:

$Ambush = false;

}

If ($Ambush == true1) {

}

If ($Ambush == true2) {

}

$Team1Num = count($Array1);
$Team2Num = count($Array2);

$attackorder = array();

foreach( $Array1 as $Team1){
echo "Team 1: " . $Team1 . "<br>";

$result1 = mysql_query("SELECT * FROM Player WHERE ID2 = '$Team1'");

while($row1 = mysql_fetch_array($result1))
{
echo $row1['FirstName'] . " " . $row1['LastName'] . "<br>" . "Rank: " . $row1['Rank'];
echo "<br><br>";

$randao = rand(0,20);

switch ($row1['Rank'])
{

case S:

$defaultao = "15";

break;

case A:

$defaultao = "10";

break;

case B:

$defaultao = "7";

break;

case C:

$defaultao = "5";

break;

case D:

$defaultao = "3";

break;

default:

$defaultao = "1";

}

$endao = $defaultao + $randao;

$attackorder[$Team1] = $endao;

}

}

foreach( $Array2 as $Team2){
echo "Team 2: " . $Team2 . "<br>";

$result2 = mysql_query("SELECT * FROM Ninja WHERE ID = '$Team2'");

while($row2 = mysql_fetch_array($result2))
{

$randao = rand(0,20);

switch ($row1['Rank'])
{

case S:

$defaultao = "15";

break;

case A:

$defaultao = "10";

break;

case B:

$defaultao = "7";

break;

case C:

$defaultao = "5";

break;

case D:

$defaultao = "3";

break;

default:

$defaultao = "1";

}

$endao = $defaultao + $randao;

$attackorder[$Team2] = $endao;

}

}

arsort($attackorder);

echo "Attacking Order: <br>";
print_r($attackorder);
echo "<br><br>";
$TotalNum = $Team1Num + $Team2Num;
//echo $TotalNum;

foreach( array_keys($attackorder) AS $Key => $Val )
{

$var = $Key;
$$var = $Val;
//echo $$var;

$ordera[] = $$var;

}

//print_r($ordera);

foreach( $ordera as $ninja){

//echo $ninja;

}

[/php]

result on site:

[result]
Testing Grounds

Team 1: 3
Maria Lockheart
Rank: C

Team 2: 14
Team 2: 15
Attacking Order:
Array ( [3] => 18 [15] => 15 [14] => 1 )
[/result]

I can't figure out how to proceed from attack order to the attacking phrase.

Any kind of advice is welcome even a different method as long as the system is flexible and not preset.

Thanks in advance.

jatar_k

2:24 pm on Aug 6, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



i don't understand the true block

getting the attack order right would seem the hard part, after that you execute a set of actions, starting with 1st user, get their input, make changes to health, get next, make changes etc

I didn't read the code since it makes little sense without explanation or an understanding of the system

I understand your opening post but you didn't really describe what the wall you have encountered is or what you would like to achieve. For good code to be reusable and dynamic is hopefully an understood set of principles as opposed to a goal.