Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { cols, rows } from "../09-game-of-life/gol-model";
- import { print } from "introcs";
- /*
- *
- * Author: Lillian Dotson
- *
- * ONYEN: Janelil
- *
- * UNC Honor Pledge: I certify that no unauthorized assistance has been received
- * or given in the completion of this work. I certify that I understand and
- * could now rewrite on my own, without assistance from course staff,
- * the problem set code I am submitting.
- */
- declare let drawBoard: () => void;
- export let model: number[][] = [];
- export let p1Score: number = 0;
- export let p0Score: number = 0;
- export let winner: number = Number.NaN;
- export let player: number = 0;
- export let main = async () => {
- initModel( 2, 6 );
- drawBoard();
- };
- export let initModel = ( row: number, col: number ): void => {
- for ( row = 0; row < 2; row++) {
- model[row] = [];
- for ( col = 0; col < 6; col++) {
- model[row][col] = 4;
- }
- }
- return;
- };
- export let onClick = (row: number, col: number): boolean => {
- let stonesInHand = model[row][col];
- model[row][col] = 0;
- while ( row === 0 ) {
- if ( model[row][col] === 0 || player === 1) {
- model[row][col] = stonesInHand;
- return false;
- }
- }
- let direction: number = -1;
- let goAgain: boolean = false;
- for (stonesInHand > 0; row === 0; ) {
- col = direction + col;
- if (col === -1 ) {
- p0Score++;
- model[row][col] = 1;
- direction = 1;
- goAgain = true;
- stonesInHand--;
- }
- if ( col === 6 ) {
- row = 0;
- direction = -1;
- }
- if (row === 1) {
- row = 0;
- }
- if (row === 0 ) {
- row = 1;
- }
- model[row][col]++;
- stonesInHand--;
- }
- if (goAgain === true) {
- player = 0;
- } else {
- player = 1;
- }
- while ( row === 1 ) {
- if ( model[row][col] === 1 || player === 0) {
- model[row][col] = stonesInHand;
- return false;
- }
- }
- direction = 1;
- for (stonesInHand > 0; row === 1; ) {
- col = direction + col;
- if (col === 6 ) {
- p1Score++;
- model[row][col] = 0;
- direction = -1;
- goAgain = true;
- stonesInHand--;
- }
- if ( col === -1 ) {
- row = 1;
- direction = 1;
- }
- if (row === 1) {
- row = 0;
- }
- if (row === 0 ) {
- row = 1;
- }
- model[row][col] = 0;
- model[row][col]++;
- stonesInHand--;
- }
- if (goAgain === true) {
- player = 1;
- } else {
- player = 0;
- }
- checkIfGameOver();
- return true;
- };
- export let checkIfGameOver = (): void => {
- let sum0 = sumRow(0);
- let sum1 = sumRow(1);
- p0Score = sum0 + sum1;
- p1Score = sum0 + sum1;
- if ( p0Score === 0 || p1Score === 0) {
- if (p0Score > p1Score) {
- winner = 0;
- }
- if (p1Score > p0Score ) {
- winner = 1;
- }
- if (p0Score === p1Score) {
- winner = -1;
- }
- clearRow(0);
- clearRow(1);
- }
- return;
- };
- export let clearRow = (row: number): void => {
- for ( let col = 0; col < 6; col++ ) {
- model[row][col] = 0;
- }
- return;
- };
- export let sumRow = (row: number): number => {
- let i = 0;
- for ( let cols = 0; cols < 6; cols++ ) {
- i = model[row][cols] + i;
- }
- return i;
- };
- /*
- This function is used only to make the
- autograder give better feedback. You
- should completely ignore it. If you have
- questions, you can ask us in office hours.
- */
- export let setPlayer = (p: number) => {
- player = p;
- };
- main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement