Question

In program #4, you will further test your abilities to use pointers and pointer arithmetic, in this

case to traverse and manipulate a string (array of chars). As with program #3, you are only allowed

to access the array (string) using pointers and move the pointers via pointer arithmetic.

Specifically, you will implement a version of the game Chutes and Ladders. In the game, players

take turns rolling a 6-sided die, moving their piece on the game board and possibly moving again

if they land on a chute (they slide "down" to the end of the chute, moving backward) or a ladder

(they climb up the ladder moving forward)./nThis version of the game has some changes.

1. There will only be two players

2. After a player lands on a chute or ladder, the chute/ladder is removed so that it will not be

used again

3. Some squares are denoted "havens" and some squares are denoted as "Back" (B) or

"Forward" (F) in which case the player moves back or forward to the next available haven;

upon moving forward or backward to a haven, the haven is removed; if a player lands on a

haven without the B/F then nothing happens (the player does not move again and the haven

is not removed); if, in moving backward there are no remaining havens then the player

moves back to the beginning and if in moving forward there are no remaining havens the

player does not move;

4. If a player lands on a square currently occupied by the other player than this player (the

one currently moving) must move back 1 square; in moving back 1 square, if the player

lands on a chute, ladder or F/B, then that additional move is ignored; on the other hand, if

a player moves because of a chute/ladder/F/B and the move causes the player to land on

the other player than this player is moved back 1 additional location; this requires that you

test for collision only after the entire move takes place (move the player based on the die

roll, test for chute/ladder/F/B and move if needed, and then test for collision)

5. The collision rule is not in effect if both players are on square 0 either at the beginning of

the game or if both wind up moving back to 0 because of landing on a B and there being

no more havens

The game board is set up as an array of characters, as shown below. Do not copy and paste from

this document, instead a copy of this code is uploaded in Canvas for you to copy from. Copy it

straight into your program (copying from the pdf will cause extra blank spaces to be ignored).

char board [100] =" mHk tH 1 B He Flq p H hByHlho H B jr HFB ir

jH FF ku gd H pjB mH x BF i H Bm oB H1HFBhoH BB ";

The characters of the board have the following meaning:

*: a normal square (a player who lands here does not move again unless landing on the

other player)

'B': move backward to the nearest preceding haven, stop at the beginning of the board if

no more havens exist

*F': move forward to the next haven, stay here if no more havens exist

*H': a haven which a player might move to when landing on a 'B' or 'F'; if a player moves

here because of a 'B' or 'F' then remove the haven by changing the 'H' to a ***

(do not change the 'H' if the player lands on square without moving from 'B'/'F')

'a' -'m': a chute, move backward (see below), change this to after the player moves

'o' - 'z': a ladder, move forward (see below), change this to *-' after the player moves

'n': not used

The distance to move from a chute or ladder ('a' - 'm', 'o' - 'z') is computed as the int value that

the letter is from the letter 'n' (ASCII 110). For instance, 'm' - 'n' = -1 (109-110) so the player

would move back 1 square when landing on the 'm'. Landing on 'p' would move the player

forward 2 squares (112 - 110). 'n' does not appear on the board because it would result in no

movement.

Now for the tricky part of this program. You cannot use array accessing at all to access into the

board array. You must use pointers to indicate where each player is currently on the board and to/naccess the character at that position to determine what to do (e.g., climb up, slide down, move

forward/backward to a haven). You will do this through two char pointers, say *p1 and *p2 which

will be initialized to the location of board[0] (you can initialize a pointer using p1 = board; or

pl = &board [0];). Moving a player is a matter of pointer arithmetic such as pl-pl+move

where move is the amount to move. You test the char at the position of the player using *p1/*p2

as in if (*pl=='B')... However, if a move takes a player beyond the end of the board,

dereferencing that pointer may lead to a runtime error (pointer is out of bounds). You will want to

make sure that the pointer is always within bounds by comparing it to both the beginning of and

the end of the board by using if (pl >= board && pl < board + SIZE) where SIZE

is 100 (make SIZE a constant to easily modify if you change the board).

Break the program into the following functions. You may have additional functions if you find

them needed or useful.

• main-declare and initialize the players (char pointers), the board and a FILE pointer. Seed

the random number generator. Open the FILE as an output file. Loop until a player wins:

in the loop, call the function move (see below) to move p1 and then to move p2;

move will output the result of each player's move and return the new location of

the player; call output to output the current board to disk file (see below).

o

o after the loop (after the game ends), determine and output who won the game; the

winner is not necessarily be the player who reaches board+100 first as both players

may reach the end of the board during the same turn; the winner is the player who

has gone the farthest (for instance, if p1 is at 98 and p2 is at 97, p1 rolls a 4 to reach

101, that doesn't mean p1 has won, if p2 then rolls a 6 and moves to 103 then p2

has won); do not stop a player right at board+SIZE.

output-output the current game board to disk file on a single line; use a loop to iterate

down the string, printing each current character (e.g., spaces and letters) but output '1' and

*2* for the location of the two players; for this to work, pass the board, both player pointers,

and the file pointer and use a loop that has a pointer as its loop variable; to determine if the

current position, say current, is where pl is, you would test if (current==pl); the

output is generated character-by-character using putc (or if you prefer, fprintf for a single

char); do not use fprintf(fp,"%s", board); to output the full board); this loop

can continue while current <= board + SIZE or until *current== '\0'; don't

forget to output a \n at the end.

• move - this function receives both player pointers, the board, the player number (1 or 2)

and the size of the board and provides all output (to the console window) of what happens

with the player; output the player number; randomly generate a move from 1-6; move the

player by that amount; if the player is still within the board determine if the player has

landed on a chute/ladder/B/F and if so call the appropriate function (see below) to further

move the player; see if the player lands on the other player (p1==p2) and if so, move the

player back 1 square; all output from this function goes to the console window using printf.

main will call move twice, once for each player, using notation like this:

pl = move (p1, p2, 1, board, SIZE); //player 1's turn

p2 = move (p2, pl, 2, board, SIZE); // player 2's turn

SIZE is a constant, passed as a parameter which you will use to make sure after the initial

move that the pointer is still within the bounds of the board; as this function returns the

new location of the pointer, it is a char * function (it returns a char pointer)./n• findHaven - call this function if the player lands on a 'B' or 'F', passing it the player's

location, board and SIZE; search forward for the next 'H' or backward for the previous

'H'; if when moving backward you reach the beginning of the board

(pointer==board), stop searching and move the player here; if you reach

board+SIZE when moving forward, stop searching and do not move the player; if the

player has been moved to an 'H' change it on the board to ***; this is another char *

function as it returns the new location of the player; this function does not output anything

as all printf output is handled in move.

• chuteLadder - call this function if the player lands on a chute or ladder; this function

receives the player's current location and computes and returns the player's new location,

which is determined by player+ (int) (*player-'n'); but you will need both the

player pointer and a temp pointer to remember the current location so that after moving

player you can reset the letter as stored in the temp pointer) to '-'; this function does not

output anything (again, all output is handled in move).

Although this program requires multiple functions, write it in one file (you may have a separate

header file if desired).

Run your program and look over both your console and disk output to make sure it looks correct.

When ready, run the program as many times as needed until you have a run which includes each

of these: players landing on chutes/ladders/Bs/Fs, a move to a B or F that causes the player to move

back to the beginning of the board or not move, and a collision. From this run, copy the console

output and paste it at the bottom of your .c file and copy the text file output and paste it at the

bottom of your .c file. Submit the one file (or two files if you have a separate .h file).

Below and on the next page is an example of output from one of my runs (both console and then

file). Looking at the outputs, here are some things you might notice. Player 1 immediately lands

on a chute ('k') and moves back to 1 and the 'k' is change to '-'. In the next turn, player 1 moves

4 and lands on player 2 so moves back to square 4. The 'F' that player 2 lands on causes player 2

to move up to 22 and that 'H' is changed to ***. Toward the end, having removed both havens

early on the board, player 1 lands on a 'B' and there are no remaining havens so player 1 moves

to the beginning of the board. The game ends when player 2 moves from 98 to 101.

Player 1 rolled 4 and is moved to 4 and landed on a chute and slid down to 1

Player 2 rolled 5 and is moved to 5

Player 1 rolled 4 and is moved to 5 and landed on the other player and moved back to 4

Player 2 rolled 5 and is moved to 10

Player 1 rolled 2 and is moved to 6 and landed on a ladder and climbed up to 12

Player 2 rolled 6 and is moved to 16 and landed on a 'F', and moved to 22

chute and slid down to 5

Player 1 rolled 2 and is moved to 14 and landed on a

Player 2 rolled 2 and is moved to 24

Player 1 rolled 2 and is moved to 7

ladder and climbed up to 38

Player 2 rolled 3 and is moved to 27 and landed on a

Player 1 rolled 4 and is moved to 11 and landed on a 'B', moved backward to 7

moved to 40

Player 2 rolled 2 and is

Player 1 rolled 3 and is moved to 10

Player 2 rolled 2 and is moved to 42 and landed on a 'F', and moved to 50

Player 1 rolled 2

and is

moved to 12

Player 2 rolled 1

Player 1 rolled 1

Player 2 rolled 1 and is moved to 52 and landed on a

Player 1 rolled 6 and is moved to 19

and is

and is

moved to 51

moved to 13

'F', and moved to 62

Player 2 rolled 2 and is moved to 64 and landed on a

ladder and climbed up to 66

Player 1 rolled 1 and is moved to 20 and landed on a ladder and climbed up to 22/nPlayer 2 rolled 3 and is moved to 69

Player 1 rolled 3 and is moved to 25 and landed on a chute and slid down to 19

Player 2 rolled 3 and is moved to 72

Player 1 rolled 1

Player 2 rolled 3

Player 1 rolled 6

Player 2 rolled 6

Player 1 rolled 2

Player 2 rolled 4

Player 1 rolled 5

Player 2 rolled 6

Player 1 rolled 1

and is

and is

and is

moved to 26 and landed on a 'B', moved backward to 13

and is moved to 85 and landed on a 'B', moved backward to 69

and is moved to 15

and is moved to 73

and is moved to 20

and is moved to 79

and is moved to 21

Player 2 rolled 1 and is moved to 80

Player 1 rolled 5 and is moved to 26 and landed on a 'B', moved backward to 3

moved to 20

moved to 75 and landed on a 'F', and moved to 79

Player 2 rolled 6 and is

moved to 86

Player 1 rolled 3 and is moved to 6

Player 2 rolled 3 and is moved to 89

Player 1 rolled 2 and is moved to 8

Player 2 rolled 4 and is moved to 93 and landed on a ladder and climbed up to 94

Player 1 rolled 3 and is moved to 11 and landed on a 'B', moved backward to the beginning

of the board

Player 2 rolled 4 and is moved to 98

Player 1 rolled 6 and is moved to 6

Player 2 rolled 3 and is moved to 101

Player 2 wins!

1m8-2tH 1 B Be Flq pH

m1 t 128 Be Flq pH

- - 1 818 Flq p 2

8-1-818 - Flq p

H--118 - Flq p

H--1 1 B H- Flq p

m-118 H- Flq p

m-1818- Flq p

H-1B1- Flq p

H-18H- Flqlp

H-18H- Plq

H-1 BH- Fiql-

B-1 BB- Flq 1

H-1B 1- Fiq

m-18-1Flq

1

H-1B Flq 1

mH-1B - Flq-1*

ml-1 B - Fiq

m 1 1 B - Flq -

m*--*11 B-Flq -

1B Flq

m*- 1 1 8 - Flq -

hByHlho H B

hByHlho H B

hByHlho H B

2hByHlho BB

hB-Hlho HB

hB-Hlho H B

hB-Hlho H B

hB-Hlho H B

hB-Hiho H B

hB-Hlho HB

hB-Hlho H B

-B-Hlho H B

-B-Hlho H B

-B-Hlho H B

-B-Hlho H B

-B-Hlho H B

-B-Hlho H B

-B-Hlho H B

-B-Hlho H B

-B-Hlho H B

-B-Hlho H B

-B-Hlho H B

jr HFB ir j H FF ku gd

jr HFB ir 5 H FF ku gd

jr HFB ir j H FF ku gd

jr HFB ir 5 H FF ku gd

2r HFB ir H FF ku gd

5x2HFB ir HFF ku gd

jr HFB ir 5 2 FF ku gd

jr HFB ir j *2FF ku gd

jHFB ir 3

jr HFB ir 5

jr HFB ir 5

jr HFB ir j

3 HFB ir

jHFB ir 5

r HFB ir

je HFB ir 5

jr HFB ir 5

jr HFB irj

jr HFB ir j

jr HFB ir j

je HFB i j

je HPB iz 5.

PF ku gd

FF ku gd

FF ku gd

FF ku gd

FF ku gd

FF ku gd

• FF ku gd

PP ku gd

FF ku gd

FF ku gd

• FF ku gd

FF ku gd

ku gd

FF ku gd

BF

8 pjß mH x

H B

8 pjBH x BF HB

8 pjBH x BF H B

8 pjBH x BF H B

pjBH x BFH Bm

8 pj8 mH x BF H B

8 pj8 mH x BF H B

8pja x BF H B

2 pj8 mH x BF i H B

* -j2 mH x

-j8 m2 x

-58 H x2

-j8 mH x

-18 m2 x

-58 x

-18 m x

-jB a* x

oB HIHFBhoH BB

oB HIHFBhoH BB

oB HIHFBhoH BB

oB HIHFBhoH BB

oB HIHFBhoH BB

oB HIHFBhoH BB

oB HIHFBhoH BB

oB H1HFBhoH BB

oB HIHFBhoH BB

BF H B oB H1HFBhoH BB

BFH Bm oB HIHFBhoH BB

BF i H Bm oB HIHFBhoH BB

BF i 2 Bm oB H1HFBhoH BB

BFB oB HIHFBhoH BB

28F i Bm oB H1HFBhoH BB

BF 2 Bm oB HIHFBhoH BB

BF2Bm oB H1HFBhoH BB

Bm oB2H1HFBhoH BB

Bm oB H12FBhoH BB

BF

-58 x BF i

-58 x BF i

-58 mx BF i

-58 * x BF

Bm OB HIHFB-2 BB

B

oB H1HFBB-8 BB2

Bm oB HIHFBh-H BB

Question image 1Question image 2Question image 3Question image 4Question image 5