Skip to content
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
104 changes: 99 additions & 5 deletions __tests__/board-printer.test.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
})
25 changes: 25 additions & 0 deletions board-printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/*
Expand All @@ -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
}