From eacb34daee8678cb5199e4129f43c0ecf2264996 Mon Sep 17 00:00:00 2001 From: nae deshazer Date: Tue, 2 Jul 2019 00:26:03 -0500 Subject: [PATCH 1/2] tictac --- 03week/ticTacToe.js | 135 +++++++++++++++++++++++--------------------- 1 file changed, 71 insertions(+), 64 deletions(-) diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index 1abf5b900..cc718a0ce 100644 --- a/03week/ticTacToe.js +++ b/03week/ticTacToe.js @@ -1,93 +1,100 @@ -'use strict'; +"use strict"; -const assert = require('assert'); -const readline = require('readline'); +const assert = require("assert"); +const readline = require("readline"); const rl = readline.createInterface({ - input: process.stdin, - output: process.stdout + input: process.stdin, + output: process.stdout }); -let board = [ - [' ', ' ', ' '], - [' ', ' ', ' '], - [' ', ' ', ' '] -]; +let board = [[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]]; -let playerTurn = 'X'; +// if horizontal win then ... +// let playerTurn = "X"; +// playerO = "O"; +// isValidn(place "x" or "o") +// switchPLayer +// check for win +// switch player +//repeat cycle node function printBoard() { - console.log(' 0 1 2'); - console.log('0 ' + board[0].join(' | ')); - console.log(' ---------'); - console.log('1 ' + board[1].join(' | ')); - console.log(' ---------'); - console.log('2 ' + board[2].join(' | ')); + console.log(" 0 1 2"); + console.log("0 " + board[0].join(" | ")); + console.log(" ---------"); + console.log("1 " + board[1].join(" | ")); + console.log(" ---------"); + console.log("2 " + board[2].join(" | ")); } function horizontalWin() { - // Your code here + if (board[(0, 0)] && board[(0, 1)] && board[(0, 2)]); + else if (board[(1, 0)] && board[(1, 1)] && board[(1, 2)]); + else if (board[(2, 0)] && board[(2, 1)] && board[(2, 2)]); } function verticalWin() { - // Your code here + // Your code here } function diagonalWin() { - // Your code here + // Your code here } function checkForWin() { - // Your code here + // Your code here } -function ticTacToe(row, column) { - // Your code here +function ticTacToe() { + // Your code here } function getPrompt() { - printBoard(); - console.log("It's Player " + playerTurn + "'s turn."); - rl.question('row: ', (row) => { - rl.question('column: ', (column) => { - ticTacToe(row, column); - getPrompt(); - }); - }); - + printBoard(); + console.log("It's Player " + playerTurn + "'s turn."); + rl.question("row: ", row => { + rl.question("column: ", column => { + ticTacToe(); + getPrompt(); + }); + }); } - - // Tests -if (typeof describe === 'function') { - - describe('#ticTacToe()', () => { - it('should place mark on the board', () => { - ticTacToe(1, 1); - assert.deepEqual(board, [ [' ', ' ', ' '], [' ', 'X', ' '], [' ', ' ', ' '] ]); - }); - it('should alternate between players', () => { - ticTacToe(0, 0); - assert.deepEqual(board, [ ['O', ' ', ' '], [' ', 'X', ' '], [' ', ' ', ' '] ]); - }); - it('should check for vertical wins', () => { - board = [ [' ', 'X', ' '], [' ', 'X', ' '], [' ', 'X', ' '] ]; - assert.equal(verticalWin(), true); - }); - it('should check for horizontal wins', () => { - board = [ ['X', 'X', 'X'], [' ', ' ', ' '], [' ', ' ', ' '] ]; - assert.equal(horizontalWin(), true); - }); - it('should check for diagonal wins', () => { - board = [ ['X', ' ', ' '], [' ', 'X', ' '], [' ', ' ', 'X'] ]; - assert.equal(diagonalWin(), true); - }); - it('should detect a win', () => { - assert.equal(checkForWin(), true); - }); - }); +if (typeof describe === "function") { + describe("#ticTacToe()", () => { + it("should place mark on the board", () => { + ticTacToe(); + assert.deepEqual(board, [ + [" ", " ", " "], + [" ", "X", " "], + [" ", " ", " "] + ]); + }); + it("should alternate between players", () => { + ticTacToe(); + assert.deepEqual(board, [ + ["O", " ", " "], + [" ", "X", " "], + [" ", " ", " "] + ]); + }); + it("should check for vertical wins", () => { + board = [[" ", "X", " "], [" ", "X", " "], [" ", "X", " "]]; + assert.equal(verticalWin(), true); + }); + it("should check for horizontal wins", () => { + board = [["X", "X", "X"], [" ", " ", " "], [" ", " ", " "]]; + assert.equal(horizontalWin(), true); + }); + it("should check for diagonal wins", () => { + board = [["X", " ", " "], [" ", "X", " "], [" ", " ", "X"]]; + assert.equal(diagonalWin(), true); + }); + it("should detect a win", () => { + assert.equal(checkForWin(), true); + }); + }); } else { - - getPrompt(); - + getPrompt(); } From 7f3486786efbdabed686049cb0b0b3f834e720dd Mon Sep 17 00:00:00 2001 From: nae deshazer Date: Tue, 2 Jul 2019 21:39:02 -0500 Subject: [PATCH 2/2] tictac --- 03week/ticTacToe.js | 61 ++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index cc718a0ce..020c44bab 100644 --- a/03week/ticTacToe.js +++ b/03week/ticTacToe.js @@ -25,38 +25,47 @@ function printBoard() { console.log(" ---------"); console.log("2 " + board[2].join(" | ")); } - -function horizontalWin() { - if (board[(0, 0)] && board[(0, 1)] && board[(0, 2)]); - else if (board[(1, 0)] && board[(1, 1)] && board[(1, 2)]); - else if (board[(2, 0)] && board[(2, 1)] && board[(2, 2)]); -} - -function verticalWin() { - // Your code here +const switchPlayer = () => { + if (playerTurn == "X") { + playerTurn = "O"; + } else { + playerTurn = "X"; + } +}; +for (i = 0; i <= 3; i++) { + board = [i][i] && [i][1] && [i][2] && [1][1] && [1]; } +function checkForWin() { + // will check for horizontal win first + function horizontalWin() { + if (horizontalWin()) { + console.log("Game over|" + playerTurn + " wins!"); + board = [[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]]; + } + } -function diagonalWin() { - // Your code here -} + function verticalWin() { + // Your code here + } -function checkForWin() { - // Your code here -} + function diagonalWin() { + // Your code here + } -function ticTacToe() { - // Your code here -} + function ticTacToe() { + // Your code here + } -function getPrompt() { - printBoard(); - console.log("It's Player " + playerTurn + "'s turn."); - rl.question("row: ", row => { - rl.question("column: ", column => { - ticTacToe(); - getPrompt(); + function getPrompt() { + printBoard(); + console.log("It's Player " + playerTurn + "'s turn."); + rl.question("row: ", row => { + rl.question("column: ", column => { + ticTacToe(); + getPrompt(); + }); }); - }); + } } // Tests