diff --git a/__tests__/board-printer.test.js b/__tests__/board-printer.test.js index cdf5f02..2acaa7c 100644 --- a/__tests__/board-printer.test.js +++ b/__tests__/board-printer.test.js @@ -1,6 +1,100 @@ -import { checkIfNoMovesLeft } from "../board-printer"; +import { printBoard, checkIfNoMovesLeft } from "../board-printer.js"; +import { jest } from '@jest/globals'; -test("checkIfNoMovesLeft should return true if there are no moves left", () => { - const board = []; - expect(checkIfNoMovesLeft(board)).toBe(true); -}); +describe("board-printer.js", () => { + describe("printBoard", () => { + let consoleSpy + + beforeEach(() => { + consoleSpy = jest.spyOn(console, "log").mockImplementation() + }) + + afterEach(() => { + consoleSpy.mockRestore() + }) + + test("should print an empty board", () => { + const board = [ + ["_", "_", "_"], + ["_", "_", "_"], + ["_", "_", "_"], + ] + + printBoard(board) + + expect(consoleSpy).toHaveBeenCalled() + const output = consoleSpy.mock.calls.map((call) => call[0]).join("\n") + expect(output).toContain("|") + expect(output).toContain("=================") + }) + + test("should print a board with moves", () => { + const board = [ + ["X", "_", "O"], + ["_", "X", "_"], + ["O", "_", "X"], + ] + + printBoard(board) + + expect(consoleSpy).toHaveBeenCalled() + const output = consoleSpy.mock.calls.map((call) => call[0]).join("\n") + expect(output).toContain("X") + expect(output).toContain("O") + }) + + test("should print a full board", () => { + const board = [ + ["X", "O", "X"], + ["O", "X", "O"], + ["O", "X", "X"], + ] + + printBoard(board) + + expect(consoleSpy).toHaveBeenCalled() + }) + }) + + describe("checkIfNoMovesLeft", () => { + test("should return true when board is full", () => { + const board = [ + ["X", "O", "X"], + ["O", "X", "O"], + ["O", "X", "X"], + ] + + expect(checkIfNoMovesLeft(board)).toBe(true) + }) + + test("should return false when board has empty spaces", () => { + const board = [ + ["X", "_", "_"], + ["_", "X", "_"], + ["O", "O", "X"], + ] + + expect(checkIfNoMovesLeft(board)).toBe(false) + }) + + test("should return false when board has one empty space", () => { + const board = [ + ["X", "O", "X"], + ["O", "X", "O"], + ["O", "X", "_"], + ] + + expect(checkIfNoMovesLeft(board)).toBe(false) + }) + + test("should return false when board is empty", () => { + const board = [ + ["_", "_", "_"], + ["_", "_", "_"], + ["_", "_", "_"], + ] + + expect(checkIfNoMovesLeft(board)).toBe(false) + }) + }) +}) \ No newline at end of file diff --git a/board-printer.js b/board-printer.js index e11a262..035f994 100644 --- a/board-printer.js +++ b/board-printer.js @@ -16,6 +16,21 @@ Test your function by calling it with an example tic-tac-toe board. */ export function printBoard(board) { + console.log("\n") // Add some spacing + + for (let i = 0; i < board.length; i++) { + const row = board[i] + // Replace '_' with space for better display + const displayRow = row.map((cell) => (cell === "_" ? " " : cell)) + console.log(` ${displayRow[0]} | ${displayRow[1]} | ${displayRow[2]} `) + + // Print separator line after each row except the last + if (i < board.length - 1) { + console.log("=================") + } + } + + console.log("\n") // Add some spacing } /* @@ -24,4 +39,14 @@ export function printBoard(board) { - return false if there are still moves that can be made */ export function checkIfNoMovesLeft(board) { + // Check every cell in the board + for (const row of board) { + for (const cell of row) { + if (cell === "_") { + return false // Found an empty space, so there are moves left + } + } + } + + return true // No empty spaces found, no moves left }