diff --git a/__tests__/move-maker.test.js b/__tests__/move-maker.test.js new file mode 100644 index 0000000..077bf0a --- /dev/null +++ b/__tests__/move-maker.test.js @@ -0,0 +1,87 @@ +import { validateMove, makeMove } from '../move-maker'; // Adjust the import path if necessary + +// Mock board for testing +let board; + +beforeEach(() => { + // Reset the board before each test + board = [ + ['X', '_', '_'], + ['_', 'X', '_'], + ['O', 'O', 'X'], + ]; +}); + +// Test suite for validateMove +describe('validateMove function', () => { + it('should return true for valid moves in an empty space', () => { + const move = '1,2'; // Row 1, Column 2 + const result = validateMove(move, board); + expect(result).toBe(true); // Move is valid + }); + + it('should return false for moves out of bounds (row too low)', () => { + const move = '0,2'; // Invalid row + const result = validateMove(move, board); + expect(result).toBe(false); // Move is invalid + }); + + it('should return false for moves out of bounds (column too high)', () => { + const move = '2,4'; // Invalid column + const result = validateMove(move, board); + expect(result).toBe(false); // Move is invalid + }); + + it('should return false for moves to a non-empty space', () => { + const move = '1,1'; // Row 1, Column 1 is occupied by 'X' + const result = validateMove(move, board); + expect(result).toBe(false); // Space is not empty + }); +}); + +// Test suite for makeMove +describe('makeMove function', () => { + it('should return true and update the board for valid moves', () => { + const move = '2,3'; // Row 2, Column 3 + const player = 'X'; + const result = makeMove(board, move, player); + + // Check if the move was valid + expect(result).toBe(true); + + // Check if the board was updated + expect(board[1][2]).toBe('X'); // Row 2 (index 1), Column 3 (index 2) + }); + + it('should return false for invalid moves (out of bounds)', () => { + const move = '4,3'; // Invalid row + const player = 'O'; + const result = makeMove(board, move, player); + + // Check if the move was invalid + expect(result).toBe(false); + + // Check if the board was not updated + expect(board).toEqual([ + ['X', '_', '_'], + ['_', 'X', '_'], + ['O', 'O', 'X'], + ]); // Board remains the same + }); + + it('should return false for invalid moves (space occupied)', () => { + const move = '3,1'; // Row 3, Column 1 is occupied by 'O' + const player = 'X'; + const result = makeMove(board, move, player); + + // Check if the move was invalid + expect(result).toBe(false); + + // Check if the board was not updated + expect(board).toEqual([ + ['X', '_', '_'], + ['_', 'X', '_'], + ['O', 'O', 'X'], + ]); // Board remains the same + }); +}); diff --git a/move-maker.js b/move-maker.js index b462350..87aa849 100644 --- a/move-maker.js +++ b/move-maker.js @@ -15,22 +15,59 @@ ['O', 'O', 'X'] ]; */ -function validateMove(move, board) { - // Implement this at the end if you have time, otherwise you can help your teammates! +// function validateMove(move, board) { +// // Implement this at the end if you have time, otherwise you can help your teammates! +// return true; +// } + +// /* +// Given 3 parameters: +// - a board (an array of arrays) +// - a move (2 numbers separated by a comma) +// - a player ('X' or 'O'): +// Check that the move is valid using the validateMove function. +// If the move is not valid, the function should just return false. +// If the move is valid, the function should: +// - Update the board with the player's value ('X' or 'O') in the correct position +// - Return true +// */ +// export function makeMove(board, move, player) { +// return false; +// } +export function validateMove(move, board) { + // Your validateMove implementation + let [row, col] = move.split(',').map(Number); + + // Check if row and col are valid + if (row < 1 || row > 3 || col < 1 || col > 3) { + console.log('Try again...'); + return false; + } + + // Adjust for 0-based indexing + row -= 1; + col -= 1; + + // Check if the space is empty + if (board[row][col] !== '_') { + console.log('Try again...'); + return false; + } + return true; } -/* - Given 3 parameters: - - a board (an array of arrays) - - a move (2 numbers separated by a comma) - - a player ('X' or 'O'): - Check that the move is valid using the validateMove function. - If the move is not valid, the function should just return false. - If the move is valid, the function should: - - Update the board with the player's value ('X' or 'O') in the correct position - - Return true -*/ export function makeMove(board, move, player) { - return false; + if (!validateMove(move, board)) { + return false; + } + + let [row, col] = move.split(',').map(Number); + + // Adjust for 0-based indexing + row -= 1; + col -= 1; + + board[row][col] = player; + return true; }