Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. import { cols, rows } from "../09-game-of-life/gol-model";
  2. import { print } from "introcs";
  3.  
  4. /*
  5. *
  6. * Author: Lillian Dotson
  7. *
  8. * ONYEN: Janelil
  9. *
  10. * UNC Honor Pledge: I certify that no unauthorized assistance has been received
  11. * or given in the completion of this work. I certify that I understand and
  12. * could now rewrite on my own, without assistance from course staff,
  13. * the problem set code I am submitting.
  14. */
  15.  
  16.  
  17.  
  18. declare let drawBoard: () => void;
  19.  
  20. export let model: number[][] = [];
  21. export let p1Score: number = 0;
  22. export let p0Score: number = 0;
  23. export let winner: number = Number.NaN;
  24. export let player: number = 0;
  25.  
  26. export let main = async () => {
  27. initModel( 2, 6 );
  28.  
  29. drawBoard();
  30.  
  31. };
  32.  
  33. export let initModel = ( row: number, col: number ): void => {
  34. for ( row = 0; row < 2; row++) {
  35.  
  36. model[row] = [];
  37.  
  38. for ( col = 0; col < 6; col++) {
  39. model[row][col] = 4;
  40. }
  41. }
  42.  
  43.  
  44. return;
  45. };
  46.  
  47. export let onClick = (row: number, col: number): boolean => {
  48.  
  49. let stonesInHand = model[row][col];
  50. model[row][col] = 0;
  51. if ( row === 0 ) {
  52. if ( stonesInHand === 0 || player === 1) {
  53. model[row][col] = stonesInHand;
  54.  
  55. return false;
  56.  
  57. }
  58. let direction: number = -1;
  59. let goAgain: boolean = false;
  60.  
  61. while (stonesInHand > 0) {
  62. col = direction + col;
  63. if (col === -1 ) {
  64. p0Score++;
  65. model[row][col] = 1;
  66. direction = 1;
  67. goAgain = true;
  68. stonesInHand--;
  69. }
  70. if ( col === 6 ) {
  71. row = 0;
  72. direction = -1;
  73. }
  74. if (row === 1) {
  75. row = 0;
  76. }
  77. if (row === 0 ) {
  78. row = 1;
  79. }
  80. model[row][col]++;
  81. stonesInHand--;
  82. }
  83. if (goAgain === true) {
  84. player = 0;
  85. } else {
  86. player = 1;
  87. }
  88. }
  89.  
  90.  
  91.  
  92. if ( row === 1 ) {
  93. if ( stonesInHand === 1 || player === 0) {
  94. model[row][col] = stonesInHand;
  95.  
  96. return false;
  97.  
  98. }
  99. let direction = 1;
  100. let goAgain: boolean = false;
  101. while (stonesInHand > 0 ) {
  102. col = direction + col;
  103. if (col === 6 ) {
  104. p1Score++;
  105. model[row][col] = 0;
  106. direction = -1;
  107. goAgain = true;
  108. stonesInHand--;
  109. }
  110.  
  111. if ( col === -1 ) {
  112. row = 1;
  113. direction = 1;
  114. }
  115. if (row === 1) {
  116. row = 0;
  117. }
  118. if (row === 0 ) {
  119. row = 1;
  120. }
  121. model[row][col] = 0;
  122. model[row][col]++;
  123. stonesInHand--;
  124. }
  125. if (goAgain === true) {
  126. player = 1;
  127.  
  128. } else {
  129. player = 0;
  130. }
  131. }
  132.  
  133.  
  134. checkIfGameOver();
  135. return true;
  136. };
  137.  
  138.  
  139.  
  140.  
  141. export let checkIfGameOver = (): void => {
  142.  
  143. let sum0 = sumRow(0);
  144. let sum1 = sumRow(1);
  145.  
  146. p0Score = sum0 + sum1;
  147. p1Score = sum0 + sum1;
  148. if ( p0Score === 0 || p1Score === 0) {
  149. if (p0Score > p1Score) {
  150. winner = 0;
  151. }
  152. if (p1Score > p0Score ) {
  153. winner = 1;
  154.  
  155. }
  156. if (p0Score === p1Score) {
  157. winner = -1;
  158. }
  159. clearRow(0);
  160. clearRow(1);
  161. }
  162. return;
  163. };
  164.  
  165. export let clearRow = (row: number): void => {
  166. for ( let col = 0; col < 6; col++ ) {
  167. model[row][col] = 0;
  168. }
  169. return;
  170. };
  171.  
  172. export let sumRow = (row: number): number => {
  173. let i = 0;
  174. for ( let cols = 0; cols < 6; cols++ ) {
  175. i = model[row][cols] + i;
  176. }
  177. return i;
  178. };
  179.  
  180. /*
  181. This function is used only to make the
  182. autograder give better feedback. You
  183. should completely ignore it. If you have
  184. questions, you can ask us in office hours.
  185. */
  186. export let setPlayer = (p: number) => {
  187. player = p;
  188. };
  189.  
  190. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement