Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP]Dziwny problem z pętlą...
crackcomm
post 21.07.2009, 02:56:51
Post #1





Grupa: Zarejestrowani
Postów: 184
Pomógł: 6
Dołączył: 23.02.2008
Skąd: Katowice

Ostrzeżenie: (0%)
-----


Witam mam dziwny problem z pętlami
  1. <?
  2.  
  3. Class Fight {
  4.  
  5.      function Oblicz( $arrParams ) {
  6.  
  7.            if( $arrParams[1]['wpn'] == 1 ) {
  8.                  $arrParams[1]['wpn'] = 11;
  9.                  $arrParams[1]['deff'] = 8;
  10.            } else {
  11.                  $arrParams[1]['wpn'] = 15;
  12.                  $arrParams[1]['deff'] = 4;
  13.            }
  14.  
  15.            if( $arrParams[2]['wpn'] == 1 ) {
  16.                  $arrParams[2]['wpn'] = 11;
  17.                  $arrParams[2]['deff'] = 8;
  18.            } else {
  19.                  $arrParams[2]['wpn'] = 15;
  20.                  $arrParams[2]['deff'] = 4;
  21.            }
  22.  
  23.            $arrParams[1]['hp'] = $arrParams[1]['pw'] * 15 + $arrParams[1]['deff'] * 4;
  24.            $arrParams[2]['hp'] = $arrParams[2]['pw'] * 15 + $arrParams[2]['deff'] * 4;
  25.  
  26.  
  27.  
  28.            return $arrParams;
  29.      }
  30.  
  31.      function Hit( $arrParams, $intKiller, $intDie, $logFight ) {
  32.            #Obiczamy moc hita
  33.            $hit = rand( 1, $arrParams[$intKiller]['str'] ) + $arrParams[$intKiller]['wpn'];
  34.  
  35.            #Test uniku
  36.            $rand = rand( 1, 100 );
  37.            $unik = $arrParams[$intDie]['zr'];
  38.            if( $rand <= $unik ) {
  39.                  $return = 'unik';
  40.                  #Test odpowiedzi
  41.                  if( $arrParams[$intDie]['sz'] > $arrParams[$intKiller]['zr'] ) {
  42.                        #Udalo się odparcie ataku
  43.                        $return = 'odp';
  44.                        #Obliczamy moc hita
  45.                        $hit = rand( 1, $arrParams[$intDie]['str'] ) + $arrParams[$intDie]['wpn'];
  46.                  }
  47.            }
  48.            switch ($return) {
  49.                  case 'unik':
  50.                       $logFight[] = $arrParams[$intKiller]['name'].' próbuje zadać ciosu';
  51.                       $logFight[] = $arrParams[$intDie]['name'].' robi unik';
  52.                       break;
  53.                  case 'odp':
  54.                       $logFight[] = $arrParams[$intKiller]['name'].' wymachuje niezdarnie mieczem, nie trafia';
  55.                       $logFight[] = $arrParams[$intDie]['name'].' wykorzustując okazje zadaje '.$hit.' obrażeń';
  56.                       $arrParams[$intKiller]['hp'] = $arrParams[$intKiller]['hp'] - $hit;
  57.                       break;
  58.                  default:
  59.                       $logFight[] = $arrParams[$intKiller]['name'].' trafia w przeciwnika, '.$arrParams[$intDie]['name'].' traci '.$hit.' życia';
  60.                       $arrParams[$intDie]['hp'] = $arrParams[$intDie]['hp'] - $hit;
  61.                       break;
  62.            }
  63.            return array('log' => $logFight, 'arrParams' => $arrParams);
  64.      }
  65.      function Walka( $arrParams ) {
  66.            $arrParams = $this->Oblicz( $arrParams );
  67.            $logFight = array();
  68.            //while( $arrParams[1]['hp'] != 0 AND $arrParams[1]['hp'] != 0 ) { // tutaj jest ta pętla, próbowałem też dawać  np.for($x=0;$x<5;$x++) {
  69.            for($i=1;$i<=2;$i++) {
  70.                  if($i==1) { $x = 2; } else { $x = 1; }
  71.                  if( $arrParams[1]['hp'] > 0 AND $arrParams[1]['hp'] > 0 ) {
  72.                        $walka = $this->Hit( $arrParams, $i, $x, $logFight );
  73.                        $logFight = $walka['log'];
  74.                        $arrParams = $walka['arrParams'];
  75.                        if( $arrParams[1]['hp'] <= 0 ) {
  76.                              $logFight[] = $arrParams[1]['name'].' Ginie !';
  77.                              $logFight[] = $arrParams[2]['name'].' Wygrał pojedynek !';
  78.                        } elseif ( $arrParams[2]['hp'] <= 0 ) {
  79.                              $logFight[] = $arrParams[2]['name'].' Ginie !';
  80.                              $logFight[] = $arrParams[1]['name'].' Wygrał pojedynek !';
  81.                        }
  82.                  }
  83.            }
  84.            //}
  85.           return $logFight;
  86.      }
  87.  
  88. }
  89. ?>

a tutaj plik test


  1. <?
  2. if($_POST['btn']=="GO!") {
  3. require_once 'la.php';
  4. $fight = new Fight;
  5.  
  6. $arr = array(
  7. '1' => array(
  8.     'pw' => $_POST['pw1'],
  9.     'str' => $_POST['str1'],
  10.     'wpn' => $_POST['wpn1'],
  11.     'zr' => $_POST['zr1'],
  12.     'sz' => $_POST['sz1'],
  13.     'name' => $_POST['name1']
  14.     ),
  15. '2' => array(
  16.     'pw' => $_POST['pw2'],
  17.     'str' => $_POST['str2'],
  18.     'wpn' => $_POST['wpn2'],
  19.     'zr' => $_POST['zr2'],
  20.     'sz' => $_POST['sz2'],
  21.     'name' => $_POST['name2']
  22.     )
  23. );
  24. print_r($fight->Walka( $arr ));
  25. }
  26. ?>


I problem jest taki: jak odkomentuje pętlę w klasie Fight (ta przy której jest komentarz), nie ważne czy to pętla for czy while, ładuje się aż wyskoczy max_execution_time limit reached. Nie wiecie co tu może być nie tak ?

Ten post edytował crackcomm 21.07.2009, 02:57:51


--------------------
Sygnaturka comes here.
Go to the top of the page
+Quote Post
Pawel_W
post 21.07.2009, 05:39:54
Post #2





Grupa: Zarejestrowani
Postów: 1 675
Pomógł: 286
Dołączył: 15.06.2009
Skąd: Wieliczka

Ostrzeżenie: (0%)
-----


  1. <?php
  2. $arrParams[1]['hp'] != 0 AND $arrParams[1]['hp'] != 0
  3. ?>

po co ty to wszędzie piszesz po 2 razy?
Go to the top of the page
+Quote Post
crackcomm
post 25.07.2009, 17:23:53
Post #3





Grupa: Zarejestrowani
Postów: 184
Pomógł: 6
Dołączył: 23.02.2008
Skąd: Katowice

Ostrzeżenie: (0%)
-----


k...

Ten post edytował crackcomm 25.07.2009, 17:40:14


--------------------
Sygnaturka comes here.
Go to the top of the page
+Quote Post
ayeo
post 25.07.2009, 17:32:34
Post #4





Grupa: Przyjaciele php.pl
Postów: 1 202
Pomógł: 117
Dołączył: 13.04.2007
Skąd: 127.0.0.1

Ostrzeżenie: (0%)
-----


Pętla działa poprawnie! Przecież warunek w pętli cały czas jest spełniony!


--------------------
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 19.07.2025 - 06:58