成员值的静态引用 - Ada

发布于 2024-09-28 01:10:04 字数 2150 浏览 6 评论 0原文

你好 我正在尝试用 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;

Hi
I am trying out my first program in Ada of creating a single player dice game.
But facing problem in maintaining score of the player.
Goal: Each player has 10 turns and scores 10 points if total of 2 rolls is 7
Problem: Every time total score gets reset and 10 does not get added to current score.
Total_Score is the final score to be displayed.
Please help!!! Any help appreciated!!!

Thanks :)

My code is as follows:

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转身以后 2024-10-05 01:10:04

每次总得分都会重置,并且 10 不会添加到当前得分中。

这是因为您在 Roll_Dice 函数中将 Total_Score 设置为零:

Total_Score := 0;

10 确实会添加到 Total_Score 中:

Total_Score := Total_Score + 10;

但是在后续滚动,总数重置为零。

Every time total score gets reset and 10 does not get added to current score.

That's because you set Total_Score to zero in the Roll_Dice function:

Total_Score := 0;

10 does get added to Total_Score:

Total_Score := Total_Score + 10;

but on the subsequent roll, the total is reset to zero.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文