SHARE
TWEET

Untitled

a guest Feb 26th, 2020 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     while ( row === 0 ) {
  52.         if ( model[row][col] === 0 || player === 1) {
  53.             model[row][col] = stonesInHand;
  54.            
  55.             return false;
  56.        
  57.         }
  58.     }
  59.    
  60.     let direction: number = -1;
  61.     let goAgain: boolean = false;
  62.    
  63.     for (stonesInHand > 0; row === 0; ) {
  64.         col = direction + col;
  65.         if (col === -1 ) {
  66.           p0Score++;
  67.           model[row][col] = 1;
  68.           direction = 1;
  69.           goAgain = true;
  70.           stonesInHand--;
  71.     }
  72.         if ( col === 6 ) {
  73.             row = 0;
  74.             direction = -1;
  75.         }    
  76.         if (row === 1) {
  77.             row = 0;
  78.         }
  79.         if (row === 0 ) {
  80.             row = 1;
  81.         }
  82.         model[row][col]++;
  83.         stonesInHand--;
  84.     }
  85.     if (goAgain === true) {
  86.         player = 0;
  87.  
  88.     } else {
  89.     player = 1;
  90.     }
  91.  
  92.     while ( row === 1 ) {
  93.         if ( model[row][col] === 1 || player === 0) {
  94.             model[row][col] = stonesInHand;
  95.            
  96.             return false;
  97.        
  98.         }
  99.     }
  100.     direction = 1;
  101.     for (stonesInHand > 0; row === 1; ) {
  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.     checkIfGameOver();
  132.     return true;
  133.    };
  134.  
  135.  
  136.  
  137.  
  138. export let checkIfGameOver = (): void => {
  139.    
  140. let sum0 = sumRow(0);
  141. let sum1 = sumRow(1);
  142.  
  143. p0Score = sum0 + sum1;
  144. p1Score = sum0 + sum1;
  145. if ( p0Score === 0 ||  p1Score  === 0) {
  146.     if (p0Score > p1Score) {
  147.     winner = 0;
  148. }
  149.     if (p1Score > p0Score ) {
  150.     winner = 1;
  151.  
  152. }
  153.     if (p0Score === p1Score) {
  154.     winner = -1;
  155. }
  156.     clearRow(0);
  157.     clearRow(1);
  158. }
  159. return;
  160. };
  161.  
  162. export let clearRow = (row: number): void => {
  163.     for ( let col = 0; col < 6; col++ ) {
  164.         model[row][col] = 0;
  165.     }
  166.     return;
  167. };
  168.  
  169. export let sumRow = (row: number): number => {
  170.     let i = 0;
  171.     for ( let cols = 0; cols < 6; cols++ ) {
  172.          i = model[row][cols] + i;
  173.     }
  174.     return i;
  175. };
  176.  
  177. /*
  178. This function is used only to make the
  179. autograder give better feedback. You
  180. should completely ignore it. If you have
  181. questions, you can ask us in office hours.
  182. */
  183. export let setPlayer = (p: number) => {
  184.     player = p;
  185. };
  186.  
  187. main();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top