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

Make mover.js in tic-tac-toe #17

Open
wants to merge 2 commits 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
87 changes: 87 additions & 0 deletions __tests__/move-maker.test.js
Original file line number Diff line number Diff line change
@@ -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
});
});
65 changes: 51 additions & 14 deletions move-maker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}