我的PHP分数没有增加

时间:2018-03-27 11:07:21

标签: php

我有一个简单的程序卡片游戏我一直在制作,除了我的乐谱以外,一切似乎都很好。

我可以让分数自我调整并增加1但是当玩家连续获得2个正确答案时,分数仍为1。

<body>
    <form action=Cardgame.php method="get">
        <button name="Higher" type="submit" value="submit">Higher</button>
        <button name="Lower" type="submit" value="submit">Lower</button>
        <button name="NewDeal" type="submit" value="submit">NewDeal?</button>
    </form>
</body>
<?php
    $Cards = array(
        1 => '2 of Clubs',
        2 => '2 of Diamonds',
        3 => '2 of Hearts',
        4=>'2 of spades',
        5=>'3 of Clubs',
        6=>'3 of Diamonds',
        7=>'3 of Hearts',
        8=>'3 of spades',
        9=>'4 of Clubs',
        10=>'4 of Diamonds',
        11=>'4 of Hearts',
        12=>'4 of spades',
        13=>'5 of Clubs',
        14=>'5 of Diamonds',
        15=>'5 of Hearts',
        16=>'5 of spades',
        17=>'6 of Clubs',
        18=>'6 of Diamonds',
        19=>'6 of Hearts',
        20=>'6 of spades',
        21=>'7 of Clubs',
        22=>'7 of Diamonds',
        23=>'7 of Hearts',
        24=>'7 of spades',
        25=>'8 of Clubs',
        26=>'8 of Diamonds',
        27=>'8 of Hearts',
        28=>'6 of spades',
        29=>'9 of Clubs',
        30=>'9 of Diamonds',
        31=>'9 of Hearts',
        32=>'6 of spades',
        33=>'10 of Clubs',
        34=>'10 of Diamonds',
        35=>'10 of Hearts',
        36=>'10 of spades',
        37=>'J of Clubs',
        38=>'J of Diamonds',
        39=>'J of Hearts',
        40=>'J of spades',
        41=>'Q of Clubs',
        42=>'Q of Diamonds',
        43=>'Q of Hearts',
        44=>'Q of spades',
        45=>'K of Clubs',
        46=>'K of Diamonds',
        47=>'K of Hearts',
        48=>'K of spades',
        49=>'A of Clubs',
        50=>'A of Diamonds',
        51=>'A of Hearts',
        52=>'A of spades'
    );
    $Score= 0;
    $PlayerCard = rand (1,52);
    $Cardrand = rand (1,52);
    if ($PlayerCard==$Cardrand) {
        $Cardrand = rand (1,52);
    }
    echo $Cards[$PlayerCard] . $Cards[$Cardrand];
    if (isset($_GET['Higher'])) {
        if ($PlayerCard>$Cardrand) {
            ++$Score ;
        }
        else {
            $Score = 0;
        }
    }
    if (isset($_GET['Lower'])) {
        if ($PlayerCard<$Cardrand){
            ++$Score ;
        }
        else {
            $Score = 0;
        }
    }
    if (isset($_GET['NewDeal'])) {
        $Score=0;
        $PlayerCard = rand (0,51);
        $Cardrand = rand (0,51);
    }
    echo $Score;
?>

我已经尝试了$ variable ++ ++ $ Variable,我一直在尝试不同的方法,但仍然无法让计数器增加1以上。

请帮忙......

2 个答案:

答案 0 :(得分:0)

每次刷新页面时,您都会将$ score重置为0。

这就是为什么$ score ++只加1。

代码从上到下阅读。最先发生的事情之一是什么? - 你将$ score设置为0.然后你在$ score ++中加入1得分;

实现此目的的唯一方法是在会话(推荐)或cookie或文件中设置得分变量,或者即使刷新页面也要保留变量值。

这是一个简单的例子:

session_start();

if(!isset($_SESSION['score'])){
    $_SESSION['score'] = 0;
}

if (isset($_GET['Higher'])) {
    if ($PlayerCard > $Cardrand) {
        $_SESSION['score']++;
    }
}

答案 1 :(得分:0)

如果我认为你的游戏正确应该是这样的事情

    <form action="/testphp" method="get">
    <button name="Higher" type="submit" value="submit">Higher</button>
     <button name="Lower" type="submit" value="submit">Lower</button>
    <button name="NewDeal" type="submit" value="submit">NewDeal?</button>
    </form>
    </body>


    <?php



    $Cards = array(
     1=>'2 of Clubs' ,2=>'2 of Diamonds',3=>'2 of Hearts',4=>'2 of spades',
    5=>'3 of Clubs',6=>'3 of Diamonds',7=>'3 of Hearts',8=>'3 of spades',
    9=>'4 of Clubs',10=>'4 of Diamonds',11=>'4 of Hearts',12=>'4 of spades',
    13=>'5 of Clubs',14=>'5 of Diamonds',15=>'5 of Hearts',16=>'5 of spades',
    17=>'6 of Clubs',18=>'6 of Diamonds',19=>'6 of Hearts',20=>'6 of spades',
    21=>'7 of Clubs',22=>'7 of Diamonds',23=>'7 of Hearts',24=>'7 of spades',
    25=>'8 of Clubs',26=>'8 of Diamonds',27=>'8 of Hearts',28=>'6 of spades',
    29=>'9 of Clubs',30=>'9 of Diamonds',31=>'9 of Hearts',32=>'6 of spades',
    33=>'10 of Clubs',34=>'10 of Diamonds',35=>'10 of Hearts',36=>'10 of spades',
    37=>'J of Clubs',38=>'J of Diamonds',39=>'J of Hearts',40=>'J of spades',
    41=>'Q of Clubs',42=>'Q of Diamonds',43=>'Q of Hearts',44=>'Q of spades',
    45=>'K of Clubs',46=>'K of Diamonds',47=>'K of Hearts',48=>'K of spades',
    49=>'A of Clubs',50=>'A of Diamonds',51=>'A of Hearts',52=>'A of spades',);


    session_start();
    if (!empty($_SESSION['Score'])) {
    $Score =   $_SESSION['Score']  ;
    } else {
      $_SESSION['Score'] = 0 ;
      $Score= 0;
    }

  $PlayerCard = rand (1,52);
  $Cardrand = rand (1,52);

  if ($PlayerCard==$Cardrand) {
    $Cardrand = rand (1,52);
    }
   echo $Cards[$PlayerCard] . $Cards[$Cardrand];




  if (isset($_GET['Higher'])) {
  if ($PlayerCard>$Cardrand) {

   ++$Score ;

  }
    else {
     $Score--;

  }
  $_SESSION['Score'] = $Score;
  }



   if (isset($_GET['Lower'])) {
   if ($PlayerCard<$Cardrand){

    ++$Score ;

    }
    else {
    $Score--;

     }
     $_SESSION['Score'] = $Score;
   }


  if (isset($_GET['NewDeal'])) {

  $Score=0;
  $PlayerCard = rand (0,51);
  $Cardrand = rand (0,51);
  $_SESSION['Score'] = $Score;
   }



   echo "<br><strong>" .$_SESSION['Score']."</strong>";

  ?>