Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write in Javascript //Define the order in which to examine/expand possible moves

ID: 3751242 • Letter: W

Question

Write in Javascript

//Define the order in which to examine/expand possible moves
//(This affects alpha-beta pruning performance)
let move_expand_order=[0,1,2,3,4,5,6,7,8]; //Naive (linear) ordering
//let move_expand_order=[4,0,1,2,3,5,6,7,8]; //Better ordering?

/////////////////////////////////////////////////////////////////////////////

function tictactoe_minimax(board,cpu_player,cur_player) {
/***********************************************************
* board: game state, an array representing a tic-tac-toe board
* The positions correspond as follows
* 0|1|2
* -+-+-
* 3|4|5 -> [ 0,1,2,3,4,5,6,7,8 ]
* -+-+-
* 6|7|8
* For each board location, use the following:
* -1 if this space is blank
* 0 if it is X
* 1 if it is O
*
* cpu_player: Which piece is the computer designated to play
* cur_player: Which piece is currently playing
* 0 if it is X
* 1 if it is O
* So, to check if we are currently looking at the computer's
* moves do: if(cur_player===cpu_player)
*
* Returns: Javascript object with 2 members:
* score: The best score that can be gotten from the provided game state
* move: The move (location on board) to get that score
***********************************************************/

//BASE CASE
if(is_terminal(board)) //Stop if game is over
return {
move:null,
score:utility(board,cpu_player) //How good was this result for us?
}

++helper_expand_state_count; //DO NOT REMOVE
//GENERATE SUCCESSORS
for(let move of move_expand_order) { //For each possible move (i.e., action)
if(board[move]!=-1) continue; //Already taken, can't move here (i.e., successor not valid)
  
let new_board=board.slice(0); //Copy
new_board[move]=cur_player; //Apply move
//Successor state: new_board

//RECURSION
// What will my opponent do if I make this move?
let results=tictactoe_minimax(new_board,cpu_player,1-cur_player);

//MINIMAX
/***********************
* TASK: Implement minimax here. (What do you do with results.move & results.score ?)
*
* Hint: You will need a little code outside the loop as well, but the main work goes here.
*
* Hint: Should you find yourself in need of a very large number, try Infinity or -Infinity
***********************/
}

//Return results gathered from all sucessors (moves).
//Which was the "best" move?  
return {
move: /* What do you return here? */,
score: /* And here? */
};
}

function is_terminal(board) {
++helper_eval_state_count; //DO NOT REMOVE
  
/*************************
* TASK: Implement the terminal test
* Return true if the game is finished (i.e, a draw or someone has won)
* Return false if the game is incomplete
*************************/
return false;
}

function utility(board,player) {
/***********************
* TASK: Implement the utility function
*
* Return the utility score for a given board, with respect to the indicated player
*
* Give score of 0 if the board is a draw
* Give a large, positive score if the player had a fast win (i.e., 5 if it only took 5 moves to win)
* Give a small, positive score if the player had a slow win (i.e., 1 if it took all 9 moves to win)
* Give a small, negative score if the player had a slow loss (i.e., -1 if it took all 9 moves to lose)
* Give a large, negative score if the player had a fast loss (i.e., -5 if it only took 5 moves to lose)
* (You may return either 0 or null if the game isn't finished, but this function should never be called in that case anyways.)
*
* Hint: You can find the number of turns by counting the number of non-blank spaces
* (Or the number of turns remaining by counting blank spaces.)
***********************/
}

function tictactoe_minimax_alphabeta(board,cpu_player,cur_player,alpha,beta) {
/***********************
* TASK: Implement Alpha-Beta Pruning
*
* Once you are confident in your minimax implementation, copy it here
* and add alpha-beta pruning. (What do you do with the new alpha and beta parameters/variables?)
*
* Hint: Make sure you update the recursive function call to call this function!
***********************/
}

function debug(board,human_player) {
/***********************
* This function is run whenever you click the "Run debug function" button.
*
* You may use this function to run any code you need for debugging.
* The current "initial board" and "human player" settings are passed as arguments.
*
* (For the purposes of grading, this function will be ignored.)
***********************/
helper_log_write("Testing board:");
helper_log_board(board);
  
var tm=is_terminal(board);
helper_log_write("is_terminal() returns "+(tm?"true":"false"));

var u=utility(board,human_player);
helper_log_write("utility() returns "+u+" (w.r.t. human player selection)");
}

Explanation / Answer

// JavaScript Document

$(document).ready(function() {

var x = "x"

var o = "o"

var count = 0;

var o_win = 0;

var x_win = 0;

$('#game li').click(function(){

if ($("#one").hasClass('o') && $("#two").hasClass('o') && $("#three").hasClass('o') || $("#four").hasClass('o') && $("#five").hasClass('o') && $("#six").hasClass('o') || $("#seven").hasClass('o') && $("#eight").hasClass('o') && $("#nine").hasClass('o') || $("#one").hasClass('o') && $("#four").hasClass('o') && $("#seven").hasClass('o') || $("#two").hasClass('o') && $("#five").hasClass('o') && $("#eight").hasClass('o') || $("#three").hasClass('o') && $("#six").hasClass('o') && $("#nine").hasClass('o') || $("#one").hasClass('o') && $("#five").hasClass('o') && $("#nine").hasClass('o') || $("#three").hasClass('o') && $("#five").hasClass('o') && $("#seven").hasClass('o'))

   {

                alert('O has won the game. Start a new game')

                $("#game li").text("+");

                $("#game li").removeClass('disable')

                $("#game li").removeClass('o')

                $("#game li").removeClass('x')

                $("#game li").removeClass('btn-primary')

                $("#game li").removeClass('btn-info')

   }

else if ($("#one").hasClass('x') && $("#two").hasClass('x') && $("#three").hasClass('x') || $("#four").hasClass('x') && $("#five").hasClass('x') && $("#six").hasClass('x') || $("#seven").hasClass('x') && $("#eight").hasClass('x') && $("#nine").hasClass('x') || $("#one").hasClass('x') && $("#four").hasClass('x') && $("#seven").hasClass('x') || $("#two").hasClass('x') && $("#five").hasClass('x') && $("#eight").hasClass('x') || $("#three").hasClass('x') && $("#six").hasClass('x') && $("#nine").hasClass('x') || $("#one").hasClass('x') && $("#five").hasClass('x') && $("#nine").hasClass('x') || $("#three").hasClass('x') && $("#five").hasClass('x') && $("#seven").hasClass('x'))

{

   alert('X wins has won the game. Start a new game')

   $("#game li").text("+");

                $("#game li").removeClass('disable')

                $("#game li").removeClass('o')

                $("#game li").removeClass('x')

                $("#game li").removeClass('btn-primary')

                $("#game li").removeClass('btn-info')   

}

else if (count == 9)

{

                alert('Its a tie. It will restart.')

                $("#game li").text("+");

                $("#game li").removeClass('disable')

                $("#game li").removeClass('o')

                $("#game li").removeClass('x')

                $("#game li").removeClass('btn-primary')

                $("#game li").removeClass('btn-info')

                count = 0

}

else if ($(this).hasClass('disable'))

{

    alert('Already selected')

}

else if (count%2 == 0)

{

                count++

                $(this).text(o)

      $(this).addClass('disable o btn-primary')

                    if ($("#one").hasClass('o') && $("#two").hasClass('o') && $("#three").hasClass('o') || $("#four").hasClass('o') && $("#five").hasClass('o') && $("#six").hasClass('o') || $("#seven").hasClass('o') && $("#eight").hasClass('o') && $("#nine").hasClass('o') || $("#one").hasClass('o') && $("#four").hasClass('o') && $("#seven").hasClass('o') || $("#two").hasClass('o') && $("#five").hasClass('o') && $("#eight").hasClass('o') || $("#three").hasClass('o') && $("#six").hasClass('o') && $("#nine").hasClass('o') || $("#one").hasClass('o') && $("#five").hasClass('o') && $("#nine").hasClass('o') || $("#three").hasClass('o') && $("#five").hasClass('o') && $("#seven").hasClass('o'))

                    {

                   alert('O wins')

                   count = 0

                   o_win++

$('#o_win').text(o_win)

        }

}

   else

{

                count++

    $(this).text(x)

    $(this).addClass('disable x btn-info')

                   if ($("#one").hasClass('x') && $("#two").hasClass('x') && $("#three").hasClass('x') || $("#four").hasClass('x') && $("#five").hasClass('x') && $("#six").hasClass('x') || $("#seven").hasClass('x') && $("#eight").hasClass('x') && $("#nine").hasClass('x') || $("#one").hasClass('x') && $("#four").hasClass('x') && $("#seven").hasClass('x') || $("#two").hasClass('x') && $("#five").hasClass('x') && $("#eight").hasClass('x') || $("#three").hasClass('x') && $("#six").hasClass('x') && $("#nine").hasClass('x') || $("#one").hasClass('x') && $("#five").hasClass('x') && $("#nine").hasClass('x') || $("#three").hasClass('x') && $("#five").hasClass('x') && $("#seven").hasClass('x'))

        {

                alert('X wins')

                count = 0

                x_win++

                $('#x_win').text(x_win)

        }

}

   });

    $("#reset").click(function () {

    $("#game li").text("+");

                $("#game li").removeClass('disable')

                $("#game li").removeClass('o')

                $("#game li").removeClass('x')

                $("#game li").removeClass('btn-primary')

                $("#game li").removeClass('btn-info')

                count = 0

});

});

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote