2

こんにちは、Ada でシングル プレイヤーのサイコロ ゲームを作成する最初のプログラムを試しています。しかし、プレーヤーのスコアを維持する上で問題に直面しています。目標: 各プレイヤーは 10 ターンを持ち、2 回のロールの合計が 7 の場合、10 ポイントを獲得します。 問題: 合計スコアがリセットされるたびに、現在のスコアに 10 が追加されません。Total_Score は、表示される最終スコアです。助けてください!!!助けていただければ幸いです!!!

ありがとう :)

私のコードは次のとおりです。

with Ada.Numerics.Discrete_Random,Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;

   procedure Game is

      subtype Die is Integer range 1 .. 6;
      subtype Dice is Integer range 2*Die'First .. 2*Die'Last;
      package Random_Die is new Ada.Numerics.Discrete_Random (Die);
      use Random_Die;

  type MY_TYPE is range 1..10;
  package My_Int_IO is new Ada.Text_IO.Integer_IO(MY_TYPE);
  use My_Int_IO;

  My_Range : MY_TYPE;
      G : Generator;
      Roll : Dice;  -- Total Rolled
  Roll_One : INTEGER;   -- Roll 1
  Roll_Two : INTEGER;   -- Roll 2
  Total_Score : INTEGER;    -- Current Score
  Choice : INTEGER;     -- Game Choice
  Total_Roll : INTEGER;     -- Total Rolled Returned
  Score : INTEGER;  -- Static Score count

  function Roll_Dice return INTEGER is
   begin
    -- Start the generator in a unique state in each run
        Reset (G); 
    Total_Score := 0;
            -- Roll a pair of dice
        Roll_One := Random(G);
        Roll_Two := Random(G);
    Put(Roll_One,3);
        Put(Roll_Two,3);
            Roll := Roll_One + Roll_Two;
        return Roll;
  end Roll_Dice;

   begin
   Total_Score := 0;
   for Index in MY_TYPE loop
    Put("Roll Dice: Press 1 To Exit: Press 2 ");
    New_Line;
    Get(Item => Choice);
    if Choice = 1 then
       Total_Roll := Roll_Dice;
       if Total_Roll = 7 then
        Put("Current Score : ");
        Put(Total_Score , 3);
        Total_Score := Total_Score + 10;
            New_Line;
        Put("Your Score :  ");
        Put(Total_Score, 3);
            else
        New_Line;
        Put("Sorry! you do not score");
            end if;
    elsif Choice = 2 then
       Put("Score ");
       Put(Total_Score, 3);
       exit when Choice = 2;
    else
       Put("Wrong Choice! You lost one chance! Try Again");
    end if;
   end loop;
    New_Line;
    Put("Total Score for this game: ");
    Put(Total_Score, 3);
   end Game;
4

1 に答える 1

3

合計スコアがリセットされ、10が現在のスコアに追加されないたび。

これは、関数Total_Scoreでゼロに設定したためです。Roll_Dice

Total_Score := 0;

10追加されTotal_Scoreます:

Total_Score := Total_Score + 10;

しかし、次のロールでは、合計はゼロにリセットされます。

于 2010-10-17T17:40:10.363 に答える