0 AND $personB == 0) { // personA is sick and personB not $random=rand(1,100); if ( $random <= $GLOBALS['changeToGetSick'] ) { // infection! // echo "Someone get sick"; $personB=$GLOBALS['daysSick']; } } if ($personB > 0 AND $personA == 0) { // personB is sick and personA not $random=rand(1,100); if ( $random <= $GLOBALS['changeToGetSick'] ) { // infection! // echo "Someone get sick"; $personA=$GLOBALS['daysSick']; } } return array($personA, $personB); // return personA and personB new status } function checkContacts(&$arrayPopulation, &$arrayContact) { // have everyone have contact for($i=0; $i"; $arrayPopulation[$i] = $GLOBALS['dead']; } else { // if not dead then become immune $arrayPopulation[$i]=$GLOBALS['immune']; // Immune } } if ( $arrayPopulation[$i] > 1 ) { $arrayPopulation[$i]--; // Healing slowly... } } // end for } function printPopulation($arrayPopulation) { echo '
';
    for($i=0; $i 0 ) {
        echo 's';
      }
      if ($arrayPopulation[$i] == 0 ) {
        echo '-';
      }
      if ($arrayPopulation[$i] == -9 ) {
        echo 'x';
      }
      if ($arrayPopulation[$i] == -1 ) {
        echo 'o';
      }
      if ( ($i+1) % $GLOBALS['popWidth'] == 0) {
        echo "
"; } } echo '
'; } function init(&$arrayPopulation, &$arrayContact, &$numberSick) { $arrayPopulation=[]; // status of population -1 immun, 0 healthy, 1 sick $arrayContacts=[]; // contacts per person. $numberSick=[]; $arrayPopulation = array_fill(0, $GLOBALS['population'], $GLOBALS['healthy']); $arrayPopulation[rand(0,$GLOBALS['population']-1)]=$GLOBALS['daysSick']; // one random person sick // $arrayPopulation[rand(0,$GLOBALS['population']-1)]=$GLOBALS['daysSick']; // one random person sick // set contacts per person for($j=0; $j<$GLOBALS['population']; $j++) { $theseContacts=[]; for($i=0; $i<$GLOBALS['contactPerPerson']; $i++) { $random=rand(1,9); switch ($random) { case 1: $offSet = $j - $GLOBALS['popWidth']-1; break; case 2: $offSet = $j - $GLOBALS['popWidth']; break; case 3: $offSet = $j - $GLOBALS['popWidth']+1; break; case 4: $offSet = $j - 1; break; case 5: $offSet = $j + 1; break; case 6: $offSet = $j + $GLOBALS['popWidth']+1; break; case 7: $offSet = $j +$GLOBALS['popWidth']-1; break; case 8: $offSet = $j + $GLOBALS['popWidth']; break; case 9: $offSet = rand(0, $GLOBALS['population']-1); break; } if ( $offSet > 0 && $offSet < $GLOBALS['population'] ) { $theseContacts[]=$offSet; // contact is neighbour } else { $theseContacts[]=rand(0,$GLOBALS['population']-1); // when fell of grid, person has travelled } // $theseContacts[]=rand(0,$GLOBALS['population']-1); } $arrayContact[$j]=$theseContacts; } } // *** main *** // read data from previous run from file or init if no data file exists if ( file_exists('corona.json') ) { $arrayPopulation = json_decode(file_get_contents('corona.json'), true)['population']; $arrayContact = json_decode(file_get_contents('corona.json'), true)['contacts']; $numberSick = json_decode(file_get_contents('corona.json'), true)['sick']; } else { init($arrayPopulation, $arrayContact, $numberSick); } // did wwe get a parameter? if ( isset($_GET['step']) ) { $step=(int)$_GET['step']; } else { $step=1; } // Calculate steps for($i=0; $i<$step; $i++) { checkContacts($arrayPopulation, $arrayContact); passTime($arrayPopulation); $counts = array_count_values($arrayPopulation); $h = (array_key_exists($GLOBALS['healthy'], $counts)) ? $counts[$GLOBALS['healthy']] : '0'; $d = (array_key_exists($GLOBALS['dead'], $counts)) ? $counts[$GLOBALS['dead']] : '0'; $m = (array_key_exists($GLOBALS['immune'], $counts)) ? $counts[$GLOBALS['immune']] : '0'; $p = $GLOBALS['population']; $s = $p - $h - $d - $m; $numberSick[]=$s; } // write result file_put_contents("corona.json",json_encode(['population' => $arrayPopulation, 'contacts' => $arrayContact, 'sick' => $numberSick])); // *** output section - of MVC, the view part *** ?> '; echo ''; echo ''; echo ''; echo ''; echo '
'; echo '
Population:'.$p.' 100%
Heatlhy:'.$h.''.number_format(100*$h/$p,1).'%
Sick:'.$s.''.number_format(100*$s/$p,1).'%
Dead:'.$d.''.number_format(100*$d/$p,1).'%
Immune:'.$m.''.number_format(100*$m/$p,1).'%
'; //print_r($numberSick); ?>