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

made changes to check-row function #12

Open
wants to merge 6 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
37 changes: 34 additions & 3 deletions board-printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,48 @@
['O', 'O', 'X']
];
We should output something like this (feel free to be creative):
X | |
| X | |
=================
| X |
| | X |
=================
O | O | X
| O | O | X
=================


_X_
00X
Test your function by calling it with an example tic-tac-toe board.
*/
let board = [
["X", "_", "_"],
["_", "X", "_"],
["O", "O", "X"],
];

export function printBoard(board) {
let greeting = " ";
for (let array of board){
for (let value of array) {
// console.log(value)
if( value ==="X"){
greeting=greeting +"| x "

}
if (value==="_"){
greeting = greeting + "| ";
}
if (value==="0"){
greeting = greeting + "| 0 ";
}

}
greeting = greeting + "\n"+ "============="+"\n"
}
return greeting
}

console.log(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)
Expand Down
11 changes: 11 additions & 0 deletions move-maker.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,25 @@
- you can output 'Try again...'
- and then return false
Testing your function by calling it with some values. An example board is:

let board = [
['X', '_', '_'],
['_', 'X', '_'],
['O', 'O', 'X']
];
*/
let board1 = [
["X", "_", "_"],
["_", "X", "_"],
["O", "O", "X"],
];

function validateMove(move, board) {



// Implement this at the end if you have time, otherwise you can help your teammates!

return true;
}

Expand Down
68 changes: 42 additions & 26 deletions status-checker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
import { checkIfNoMovesLeft } from './board-printer.js';
import { checkIfNoMovesLeft } from "./board-printer.js";

//Example board:
let boardNoWinner = [
["X", "_", "_"],
["_", "_", "_"],
["O", "O", "X"],
];

//Example board:
let boardXWon = [
["X", "X", "X"],
["_", "_", "_"],
["O", "O", "X"],
];

/*
Example board:
Expand All @@ -18,7 +32,11 @@ import { checkIfNoMovesLeft } from './board-printer.js';
Otherwise, return false
*/
function checkRow(board, player, rowNumber) {
let newArr = board[rowNumber];
return newArr.every((item) => item === player);
}
console.log(checkRow(boardXWon, "X", 0));
console.log(checkRow(boardNoWinner, "X", 0));

/*
Given 3 parameters:
Expand All @@ -28,8 +46,7 @@ function checkRow(board, player, rowNumber) {
Return true if the player has made a move in all 3 squares in the column
Otherwise, return false
*/
function checkColumn(board, player, columnNumber) {
}
function checkColumn(board, player, columnNumber) {}

/*
Given 2 parameters:
Expand All @@ -39,43 +56,42 @@ function checkColumn(board, player, columnNumber) {
Otherwise, return false
*/
function checkDiagonal(board, player) {
// It may be easier to use an if statement than a loop here
// It may be easier to use an if statement than a loop here
}


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

function checkIfPlayerWon(board, player) {
for(let i = 0; i <= 2; i++) {
if(checkRow(board, player, i) || checkColumn(board, player, i)) {
return true;
}
for (let i = 0; i <= 2; i++) {
if (checkRow(board, player, i) || checkColumn(board, player, i)) {
return true;
}
}

if(checkDiagonal(board, player)) {
return true;
}
if (checkDiagonal(board, player)) {
return true;
}

return false;
return false;
}

export function isGameOver(board) {
if(checkIfPlayerWon(board, 'X')) {
console.log('X has won the game!\n');
return true;
}
if (checkIfPlayerWon(board, "X")) {
console.log("X has won the game!\n");
return true;
}

if(checkIfPlayerWon(board, 'O')) {
console.log('O has won the game!\n');
return true;
}
if (checkIfPlayerWon(board, "O")) {
console.log("O has won the game!\n");
return true;
}

if(checkIfNoMovesLeft(board)) {
console.log('Game Over - It\s a tie!\n');
return true;
}
if (checkIfNoMovesLeft(board)) {
console.log("Game Over - Its a tie!\n");
return true;
}

return false;
return false;
}