Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Scaffolding code for Assignment #1: Tic-tac-toe
- YOU MUST BUILD YOUR IMPLEMENTION BY COMPLETING THE CODE IN THIS TEMPLATE.
- YOU ARE NOT ALLOWED TO DELETE ANY LINE OR IGNORE ANY LINE IN THIS CODE.
- */
- /* Your implementation must represent the game's board using a data structure consisting of an array of arrays.
- Each nested array represents a row in the board. */
- let BOARD = [
- [1, 2, 3],
- [4, 5, 6],
- [7, 8, 9]
- ];
- // While the game runs, the board gets updated with marks placed by the human and computer players using the following values.
- const HUMAN_MARK = -1;
- const COMPUTER_MARK = 0;
- /* TASK 5: Complete the play() function.
- This function should implement an iteration statement that let both players keep playing until a player wins the game or all positions are filled.
- PARAMETERS: None
- RETURN: No value
- */
- function playGame() {
- debugger;
- printBoard();
- // Check if the board has enough positions left.
- if (checkBoard()) {
- console.log("GAME OVER!");
- } else {
- // Ask the human player a position on the board, i.e., a number between 1 and 9.
- // Assume the user always enters a valid number.
- let humanNumber = parseInt(prompt("Enter a position on the board: "));
- // Place the mark of the human player on the board
- placeMark(humanNumber, HUMAN_MARK);
- // Check if the board has enough positions left.
- if (!checkBoard()) {
- // Generate a mark for the computer player and place it on the board.
- generateComputerMove();
- }
- }
- }
- while(checkBoard()==false){
- playGame();
- }
- /************************************************************************************************************************
- TASK 1: Complete the printBoard() function.
- This function should print to the console (using console.log) the board with the marks for each player. "If a player has placed three marks in a row, either horizontally, vertically, or diagonally, or if
- the board is full, then this function should return false.
- Marks for the human player should be represented with an "X".
- Marks for the computer player should be represented with an "O" (capital letter "o").
- Empty positions on the board should be represented by their corresponding number (i.e., a number between 1 and 9).
- PARAMETERS: None
- RETURN: No value
- *************************************************************************************************************************/
- function printBoard() {
- console.clear();
- console.log(BOARD[0][0] + " | " + BOARD[0][1] + " | "+BOARD[0][2]);
- console.log("---+---+---\n");
- console.log(BOARD[1][0] + " | " + BOARD[1][1] + " | "+BOARD[1][2]);
- console.log("---+---+---\n");
- console.log(BOARD[2][0] + " | " + BOARD[2][1] + " | "+BOARD[2][2]);
- }
- /************************************************************************************************************************"If a player has placed three marks in a row, either horizontally, vertically, or diagonally, or if
- the board is full, then this function should return false.
- TASK 2: Complete the placeMark() function.
- This function should place a mark on the board for either player.
- PARAMETERS: number: An integer value indicating the position on the board to place the mark (i.e., a number between 1 and 9).
- mark: An integer value indicating the mark to be placed (either -1 or 0).
- RETURN: A boolean value indicating whether the mark has been placed.
- If the mark was succesfully placed in the given position, then this function should return true.
- It should return false otherwise.
- *************************************************************************************************************************/
- function placeMark(number, mark) {
- for (var i=0;i<BOARD.length;i++){
- var innerArrayLength=BOARD[i].length;
- for(var j=0; j<innerArrayLength; j++){ //iterate multi-array
- if(BOARD[i][j] == parseInt(number, 10)){ //check values for match against selected position
- if(mark == -1){ //Check human
- BOARD[i][j] = "X"; //replace value
- }else if(mark == 0){ //Check computer
- BOARD[i][j] = "O" //replace value
- }
- return true; //successful insertion
- }else{
- return false; //falied insertion
- }
- }
- }
- }
- /************************************************************************************************************************
- TASK 3: Complete the generateComputerMove() function.
- This function should generate a mark at random for the computer player and place it on the board.
- This function should call the placeMark() function.
- PARAMETERS: None
- RETURN: No value
- *************************************************************************************************************************/
- function generateComputerMove() {
- var computerPosition = Math.floor((Math.random() * 9) + 1);
- if(placeMark(computerPosition, COMPUTER_MARK)==false){
- generateComputerMove(computerPosition,COMPUTER_MARK);
- }
- }
- /*************************************************"If a player has placed three marks in a row, either horizontally, vertically, or diagonally, or if
- the board is full, then this function should return false.
- ***********************************************************************
- TASK 4: Complete the checkBoard() function.
- This function should inform whether the board has positions left to let both players keep placing
- their marks on the board.
- PARAMETERS: None
- RETURN: A boolean value.
- If the board has at least one position empty, then this function should return true."If a player has placed three marks in a row, either horizontally, vertically, or diagonally, or if the board is full, then this function should return false.
- *************************************************************************************************************************/
- function checkBoard() {
- debugger;
- if((BOARD[0][0]=="X" && BOARD[0][1]=="X" && BOARD[0][2]=="X") || (BOARD[0][0]=="O" && BOARD[0][1]=="O" && BOARD[0][2]=="O")){//top row
- return true;
- }else if((BOARD[1][0]=="X" && BOARD[1][1]=="X" && BOARD[1][2]=="X" ) || (BOARD[1][0]=="O" && BOARD[1][1]=="O" && BOARD[1][2]=="O" )){//middle row
- return true;
- }else if((BOARD[2][0]=="X" && BOARD[2][1]=="X" && BOARD[2][2]=="X" ) || (BOARD[2][0]=="O" && BOARD[2][1]=="O" && BOARD[2][2]=="O" )){//bottom row
- return true;
- }else if((BOARD[0][0]=="X" && BOARD[1][0]=="X" && BOARD[2][0]=="X" ) || (BOARD[0][0]=="O" && BOARD[1][0]=="O" && BOARD[2][0]=="O" )){//left column
- return true;
- }else if((BOARD[0][1]=="X" && BOARD[1][1]=="X" && BOARD[2][1]=="X" ) || (BOARD[0][1]=="O" && BOARD[1][1]=="O" && BOARD[2][1]=="O" )){//middle column
- return true;
- }else if((BOARD[0][2]=="X" && BOARD[1][2]=="X" && BOARD[2][2]=="X" ) || (BOARD[0][2]=="O" && BOARD[1][2]=="O" && BOARD[2][2]=="O" )){//right column
- return true;
- }else if((BOARD[0][0]=="X" && BOARD[1][1]=="X" && BOARD[2][2]=="X" ) || (BOARD[0][0]=="O" && BOARD[1][1]=="O" && BOARD[2][2]=="O" )){//top left diag
- return true;
- }else if((BOARD[0][2]=="X" && BOARD[1][1]=="X" && BOARD[2][0]=="X" ) || (BOARD[0][2]=="O" && BOARD[1][1]=="O" && BOARD[2][0]=="O" )){//top right diag
- return true;
- }
- for (var i=0, len=BOARD.length; i<len; i++) {
- for (var j=0, len2=BOARD[i].length; j<len2; j++) {
- if (typeof BOARD[i][j] == 'number'){
- return false;//stop as soon as a number is found
- }
- }
- }
- return true;//if ive managed to iterate over all values without turning up true then one all board must be a "X" or "O"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement