I've written some code below and I can't get it to compile. I think I have my While and If in the wrong place.

Any help is greatly appreciated!

Here is the structured english:

PlayerOneScore <- 0
PlayerTwoScore <- 0
OUTPUT ‘How many balls do you wish to face?’
INPUT BallsNo
FOR EachPlayer <- 1 TO 2 DO
	CurrentScore <- 0
	OUTPUT ‘Player’ + EachPlayer + ‘to go’
OUTPUT ‘Please roll the bowling die’
	OUTPUT ‘Enter 1 if result is a 1’
OUTPUT ‘Enter 2 if result is a 2’
OUTPUT ‘Enter 3 if result is a 4’
OUTPUT ‘Enter 4 if result is a 6’
OUTPUT ‘Enter 5 if result is a 0’
	FOR EachBall <- 1 TO BallsNo DO
		OUTPUT ‘Ball number: ‘ + EachBall
INPUT BowlResult
IF BowlResult = 1 THEN
	CurrentScore <- CurrentScore + 1
ELSE IF BowlResult = 2 THEN
	CurrentScore <- CurrentScore + 2
ELSE IF BowlResult = 3 THEN
	CurrentScore <- CurrentScore + 4
ELSE IF BowlReuslt = 4 THEN
	CurrentScore <- CurrentScore + 6
END IF
END FOR
IF EachPlayer = 1 THEN
	PlayerOneScore <- CurrentScore
ELSE
	PlayerTwoScore <- CurrentScore
END FOR
IF PlayerOneScore > PlayerTwoScore THEN
	OUTPUT ‘Player One Wins’
ELSE IF PlayerTwoScore > PlayerOneScore THEN
	OUTPUT ‘Player Two Wins’
ELSE
	OUTPUT ‘Draw’
Program Game;

var
PlayerOneScore: Integer;
PlayerTwoScore: Integer;
BallsNo: Integer;
CurrentScore: Integer;
Ptr: Integer;
Result:Integer;

Begin
PlayerOneScore:= 0;
PlayerTwoScore:= 0;
Writeln('How many balls do you wish to face?');
Readln(BallsNo);
Ptr:=1;
While Ptr < 1
Do Ptr:=Ptr+1;
CurrentScore:=0;
Writeln ('Player turn');
Writeln ('Please roll the bowling die');
Writeln ('Enter 1 if result is a 1');
Writeln ('Enter 2 if result is a 2');
Writeln ('Enter 3 if result is a 4');
Writeln ('Enter 4 if result is a 6');
Writeln ('Enter 5 if result is a 0');
        While BallsNo >0 Do
        BallsNo:=BallsNo-1;
        Writeln('This is',BallsNo);
        Readln(Result);
        If Result = 1 Then
           CurrentScore:= CurrentScore+1
           Else If Result = 2 THEN
           CurrentScore:= CurrentScore+2
           Else If Result  = 3 THEN
           CurrentScore:= CurrentScore+4
           Else If Result=4 THEN
           CurrentScore := CurrentScore+6;
        Until BallsNo = 0;
If Ptr = 1 THEN
PlayerOneScore := CurrentScore
Else PlayerTwoScore := CurrentScore;
Until Ptr=2;
If PlayerOneScore > PlayerTwoScore Then
Writeln('Player One Wins');
If PlayerTwoScore > PlayerOneScore Then
Writeln('Player Two Wins');
Else
Writeln('Draw');
End.

end.

Recommended Answers

All 5 Replies

I managed to get it to compile but it crashes when I enter a number.

Program Game;

var
PlayerOneScore: Integer;
PlayerTwoScore: Integer;
BallsNo: Integer;
CurrentScore: Integer;
Ptr: Integer;
Result:Integer;

Begin
PlayerOneScore:= 0;
PlayerTwoScore:= 0;
Writeln('How many balls do you wish to face?');
Readln(BallsNo);
                Ptr:=1;
                While Ptr < 1 Do
                      Begin
                      Repeat
                      Ptr:=Ptr+1;
                      CurrentScore:=0;
                      Writeln ('Player turn');
                      Writeln ('Please roll the bowling die');
                      Writeln ('Enter 1 if result is a 1');
                      Writeln ('Enter 2 if result is a 2');
                      Writeln ('Enter 3 if result is a 4');
                      Writeln ('Enter 4 if result is a 6');
                      Writeln ('Enter 5 if result is a 0');
                              While BallsNo >0 Do
                              Begin
                              Repeat
                              BallsNo:=BallsNo-1;
                              Writeln('This is',BallsNo);
                              Readln(Result);
                              Until BallsNo = 0;
                                             If Result = 1 Then
                                              CurrentScore:= CurrentScore+1
                                              Else If Result = 2 THEN
                                              CurrentScore:= CurrentScore+2
                                              Else If Result  = 3 THEN
                                              CurrentScore:= CurrentScore+4
                                              Else If Result=4 THEN
                                              CurrentScore := CurrentScore+6
                                              End;
                Until Ptr=2;
                    End;
If Ptr = 1 THEN
PlayerOneScore := CurrentScore
Else PlayerTwoScore := CurrentScore;
If PlayerOneScore > PlayerTwoScore Then
Writeln('Player One Wins');
If PlayerTwoScore > PlayerOneScore Then
Writeln('Player Two Wins');
End.

Why if not Case in lines 36..44? Your code has no reasonable function, for example continuously overwriting the Result variable.

...
...
ReadLn(BallsNo);
ptr:= 1;
While ptr <= 1 Do Begin   {you wrote < sign instead of <=, got it?}
...
...
commented: thanks man! +2
...
...
ReadLn(BallsNo);
ptr:= 1;
While ptr <= 1 Do Begin   {you wrote < sign instead of <=, got it?}
...
...

Thanks it doesn't crash now.

The code I wrote is obviously wrong. What I want it to do is that there are 2 players who take turns to roll the dice, they can decide how many times they can roll the dice (BallsNo) and every time they roll the enter the number they got, and the scores are added each round. After player 1 round player 2 goes and the same happens for the player 2. At the end scores are compared and outputs who has won out of the 2.

Any help on where I went wrong please.

I will post dice throwing code snippet in Python forum. It is said that Python code is like pseudo code. This is very trivial program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.