Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

London 9 - Oleh Pysmenko - Terminal-tic-tac-toe #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
30 changes: 30 additions & 0 deletions board-printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,42 @@
Test your function by calling it with an example tic-tac-toe board.
*/
export function printBoard(board) {
for (let rows of board) {
console.log(` ${rows[0] !== "_" ? rows[0] : " "} | ${rows[1] !== "_" ? rows[1] : " "} | ${rows[2] !== "_" ? rows[2] : " "}\n=================`)
}
}

// TESTS FOR printBoard
// let board = [
// ['X', '_', '_'],
// ['_', 'X', '_'],
// ['O', 'O', 'X']
// ];
// printBoard(board);
/*
Given a tic-tac-toe board (an array of arrays),
- return true if there are no moves left to make (there are no more '_' values)
- return false if there are still moves that can be made
*/
export function checkIfNoMovesLeft(board) {
for (let i of board){
for (let j of i){
if (j==="_"){
return false
}
}
}
return true
}

// TESTS FOR checkIfNoMovesLeft
// let board1 = [['X', '_', '_'],
// ['_', 'X', '_'],
// ['O', 'O', 'X']];
// let board2 = [['X', 'X', 'X'],
// ['X', 'X', 'X'],
// ['O', 'O', 'X']];
// console.log(checkIfNoMovesLeft(board1));
// // false
// console.log(checkIfNoMovesLeft(board2));
// // true
17 changes: 16 additions & 1 deletion move-maker.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,17 @@
];
*/
function validateMove(move, board) {
// Implement this at the end if you have time, otherwise you can help your teammates!
if(
move.length != 3 ||
move[0] < '1' || move[0] > '3' ||
move[2] < '1' || move[2] > '3' ||
move[1] !== ',' ||
board[move[0]-1][move[2]-1] !== '_'
) {
console.log('Try again...');
return false;
}

return true;
}

Expand All @@ -32,5 +42,10 @@ function validateMove(move, board) {
- Return true
*/
export function makeMove(board, move, player) {
if(validateMove(move, board)) {
board[move[0]-1][move[2]-1] = player;
return true;
}

return false;
}
130 changes: 1 addition & 129 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 61 additions & 2 deletions status-checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,18 @@ import { checkIfNoMovesLeft } from './board-printer.js';
Otherwise, return false
*/
function checkRow(board, player, rowNumber) {
return board[rowNumber].every(element => element===player)
}
// TESTS FOR checkRow
// let boardTest1 = [
// ['_', '_', '_'],
// ['_', '_', '_'],
// ['O', 'O', 'O']
// ];
// console.log(checkRow(boardTest1, "O", 2));
// true
// console.log(checkRow(boardTest1, "O", 1));
// false

/*
Given 3 parameters:
Expand All @@ -29,8 +40,23 @@ function checkRow(board, player, rowNumber) {
Otherwise, return false
*/
function checkColumn(board, player, columnNumber) {
for (let row of board){
if(row[columnNumber]!==player){
return false
}
}
return true
}

// TESTS FOR checkColumn
// let boardTest2 = [
// ['_', '_', 'O'],
// ['_', '_', 'O'],
// ['_', '_', 'O']
// ];
// console.log(checkColumn(boardTest2, "O", 2));
// true
// console.log(checkColumn(boardTest2, "O", 1));
// false
/*
Given 2 parameters:
- a tic-tac-toe board (array of arrays)
Expand All @@ -40,8 +66,41 @@ function checkColumn(board, player, columnNumber) {
*/
function checkDiagonal(board, player) {
// It may be easier to use an if statement than a loop here
if (
board[0][0] === player &&
board[1][1] === player &&
board[2][2] === player ||
board[0][2] === player &&
board[1][1] === player &&
board[2][0] === player
) {
return true;
} else {
return false;
}
}

// TESTS FOR checkColumn
// let boardTest3 = [
// ['O', '_', '_'],
// ['_', 'O', '_'],
// ['_', '_', 'O']
// ];
// let boardTest4 = [
// ['_', '_', 'O'],
// ['_', 'O', '_'],
// ['O', '_', '_']
// ];
// let boardTest5 = [
// ['O', '_', 'O'],
// ['_', '_', '_'],
// ['O', '_', 'O']
// ];
// console.log(checkDiagonal(boardTest3, "O"));
// true
// console.log(checkDiagonal(boardTest4, "O"));
// true
// console.log(checkDiagonal(boardTest5, "O"));
// false

/*
There is no need to change any code below this line.
Expand Down