generated from AustinCodingAcademy/JS211_PigLatinProject
-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathmain.js
More file actions
107 lines (92 loc) · 2.91 KB
/
main.js
File metadata and controls
107 lines (92 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'use strict';
// brings in the assert module for unit testing
const assert = require('assert');
// brings in the readline module to access the command line
const readline = require('readline');
// use the readline module to print out to the command line
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// creates and empty "board" for the user to see where marks can be placed.
// using let because the variable is expected to change with more 'X's and 'O's to add
let board = [
[' ', ' ', ' '],
[' ', ' ', ' '],
[' ', ' ', ' ']
];
// assigns the first mark as 'X'
// using let because the variable is expected to change from 'X' to 'O' and back
let playerTurn = 'X';
// is a function that print the current status of the board using the variable - board
const 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(' | '));
}
const horizontalWin = () => {
// Your code here to check for horizontal wins
}
const verticalWin = () => {
// Your code here to check for vertical wins
}
const diagonalWin = () => {
// Your code here to check for diagonal wins
}
const checkForWin = () => {
// Your code here call each of the check for types of wins
}
const ticTacToe = (row, column) => {
// Your code here to place a marker on the board
// then check for a win
}
const getPrompt = () => {
printBoard();
console.log("It's Player " + playerTurn + "'s turn.");
rl.question('row: ', (row) => {
rl.question('column: ', (column) => {
ticTacToe(row, column);
getPrompt();
});
});
}
// Unit Tests
// You use them run the command: npm test main.js
// to close them ctrl + C
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', () => {
ticTacToe(0, 0)
ticTacToe(0, 1)
ticTacToe(1, 1)
ticTacToe(0, 2)
ticTacToe(2, 2)
assert.equal(checkForWin(), true);
});
});
} else {
getPrompt();
}