Thecenterofthepieceisaboveatbelowthecenterofthegraph

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 100 100], ... 'curvature', [0 0]); x = rectangle('position',[p 10 10]); for t = 1:100 p = p + round(rand(1,2)) * 20 - 10; if p(1) < 0 p(1) = p(1) + 100; end if p(1) > 100 p(1) = p(1) - 100; end if p(2) < 0 p(2) = -p(2) end if p(2) > 100 p(2) = 200 - p(2) end pause(0.05); set(x, 'position', [p 10 10]); end This code draws a small square / circle shaped moving piece within a larger fixed square. The center of the piece is above / at / below the center of the graph, and the center of the piece is to the left of / at / to the right of the center of the graph. If the piece is in position A, write an A in all the possible squares where the piece might be in the next turn. Do the same for B, C and D. (be careful, this is a bit tricky) B C A (0,0) D (100,100) 15. (15 points) The program below implements a two person board game in which two players take turns rolling a die and moving on squares numbered from 0 to 100. The first person to get to 100 or more wins. Read it so that you can understand it. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 position = [0 0]; done = 0; turn = 0; while ~done turn = turn + 1; for player = 1:2 roll = 1 + floor(6*rand); position(player) = position(player) + roll; if position(player) >= 100 disp(['player ' num2str(player) ... ' wins in ' num2str(turn) ' turns.']); done = 1; end end end This same program has been modified below to add some features to the game. Note that all the inserted lines are shaded, and that the old line 9 has been deleted. On the next page you are asked about the features that have been added due to these lines 1 2 3 4 5 6 7 7.1 8 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 9 9.1 10 11 12 12.1 12.2 12.3 13 14 15 position = [0 0]; done = 0; turn = 0; while ~done turn = turn + 1; for player = 1:2 roll = 1 + floor(6*rand); if position(player)>0 || roll == 6 position(player) = position(player) + roll; if position(1) == position(2) if player==1 position(2)=0 else position(1)=0 end end end % if position(player) >= 100 if position(player) == 100 disp(['player ' num2str(player) ... ' wins in ' num2str(turn) ' turns.']); done = 1; break; elseif position(player) > 100 position(player) = position(player) – roll; end end end In front of each feature mentioned below, you may either write that this is not a feature of the program or mention which shaded lines of the program add this feature? Answer Box a. There is a bug in this program so that if player 1 gets to 100, player 2 still gets to roll, and if player 2 also gets to 100, the program will display “player 1 wins”, and then will also display “player 2 wins”. This bug corrected in the following code. b. In the original game, the first person to get to 100 or more won. In the new version, you have to get to exactly 100 in order to win. c. If you get a roll that takes you beyond 100, you return to the start. d. If you get a roll that takes you beyond 100, you miss your turn. e. When two people land on the same square (i.e. have the same position), the person who got to the square first goes back to the start. f. When two people land on the same square (i.e. have the same position), the person who got to the square last goes back to the start. g. Whenever one player gets a 6 the same player immediately gets another turn. h. Whenever you roll a 6 you miss a turn. i. If you are at the start and you don’t roll a 6, you miss your turn....
View Full Document

Ask a homework question - tutors are online