Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 94 additions & 78 deletions 03week/ticTacToe.js
Original file line number Diff line number Diff line change
@@ -1,93 +1,109 @@
'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
}

function verticalWin() {
// Your code here
}

function diagonalWin() {
// 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() {
// Your code here
}

function ticTacToe(row, column) {
// Your code here
// will check for horizontal win first
function horizontalWin() {
if (horizontalWin()) {
console.log("Game over|" + playerTurn + " wins!");
board = [[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]];
}
}

function verticalWin() {
// Your code here
}

function diagonalWin() {
// 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(row, column);
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();
}