How to write the 7 Boom game. If a number is divided by 7 or includes the digit 7, the user writes Boom in Pascal

1 Answer

0 votes
program SevenBoomGame;

// 7 Boom game. The user enters a number he wants to start the game, 
// (-1 to end the game, or lost), then the user enters numbers, from the number he entered
// If the current number is divisible by 7, or one of its digits is 7, 
// And the user did not write the word 'Boom', the user loses. 

function ContainsSeven(num: integer): boolean;
var
  s: string;
begin
  str(num, s);
  ContainsSeven := pos('7', s) <> 0;
end;

function IsBoom(num: integer): boolean;
begin
  IsBoom := (num mod 7 = 0) or ContainsSeven(num);
end;

procedure GetUserInput(var input: string);
begin
  write('Enter a number (or Boom): ');
  readln(input);
end;

function ProcessTurn(expected: integer; input: string): boolean;
var
  value: integer;
  code: integer;
  lower: string;
  i: integer;
begin
  { Convert to lowercase }
  lower := input;
  for i := 1 to length(lower) do
    lower[i] := lowercase(lower[i]);

  if IsBoom(expected) then
  begin
    if lower <> 'boom' then
    begin
      writeln(input, ' (should be Boom)');
      writeln('YOU LOSE!');
      ProcessTurn := false;
      exit;
    end;
  end
  else
  begin
    if lower = 'boom' then
    begin
      writeln('Wrong move! It was ', expected);
      writeln('YOU LOSE!');
      ProcessTurn := false;
      exit;
    end;

    val(input, value, code);
    if (code <> 0) or (value <> expected) then
    begin
      writeln('Wrong move! It was ', expected);
      writeln('YOU LOSE!');
      ProcessTurn := false;
      exit;
    end;
  end;

  ProcessTurn := true;
end;

procedure RunGame;
var
  input: string;
  current: integer;
  code: integer;
begin
  write('Enter a number from which you want to start the game (-1 to exit): ');
  readln(input);

  val(input, current, code);
  if (code <> 0) or (current = -1) then
    exit;

  while true do
  begin
    GetUserInput(input);

    if input = '-1' then
    begin
      writeln('Game Over!');
      break;
    end;

    if not ProcessTurn(current, input) then
      break;

    current := current + 1;
  end;
end;

begin
  RunGame;
end.




(*
run:

the: the the the the 
low: lowly lowland lower 
inh: inhabitants 
wer: were 
sur: surprised 
see: see 
bra: branches 
tre: trees 

*)

 



answered Mar 15 by avibootz
edited Mar 16 by avibootz
...