Reputation: 73
I am trying to do a tictactoe game using browser console, step by step, and at the end improve my functions. However, i am stuck at the player 2 turn(ttt_player2_turn()), I have to check if the case is empty or not. It's seems i have a problem with arguments in this exemple. Thanks for your advices(there is lodash functions). Edit : Now the problem in the function is that it's doesn't verify the is_case_free statement.
// to have a printed board
function ttt_print_board(array){
return ('\n' +
' ' + array[0] + ' | ' + array[1] + ' | ' + array[2] + '\n' +
' ---------\n' +
' ' + array[3] + ' | ' + array[4] + ' | ' + array[5] + '\n' +
' ---------\n' +
' ' + array[6] + ' | ' + array[7] + ' | ' + array[8] + '\n')
}
//display things
function ttt_display(stuff){
console.log(stuff)
}
//ask something
function ttt_ask(question) {
return prompt(question)
}
// to verify if the answers is between 1-9
function ttt_verify_response(response) {
var integer_response = _.toInteger(response)
if(integer_response < 10 && integer_response > 0 && _.isInteger(integer_response)){
return true
} else {
return false
}
}
// this is my function to verify if case empty or not, at the moment just for 'x'
function ttt_verify_case (board, response) {
if(board[response] === 'x' ){
return false
} else{
return true
}
}
// return the player 1 choice
function ttt_player1_turn() {
var response = 0;
var is_correct_response = false
while(! is_correct_response){
response = ttt_ask("player 1 it's your turn (play 1 to 9)")
is_correct_response = ttt_verify_response(response)
}
return response
}
//return the player 2 choice and have to check if the case is empty from 'x', it seems like it doesn't go through the 2nd if condition
function ttt_player2_turn(board) {
var is_correct_response = false
var is_case_free = false
while(!is_correct_response || !is_case_free){
var response_player2 = ttt_ask("player 2 it's your turn, play 1 to 9");
is_correct_response = ttt_verify_response(response_player2)
if (!is_correct_response) {
console.log("incorrect response")
continue
}
is_case_free = ttt_verify_case(board, response_player2);
if(!is_case_free) {
console.log("player 1 already put a piece on this case")
}
}
return response_player2
}
//main function
function ttt_new_game() {
var board = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
var printed_board = ttt_print_board(board)
ttt_display(printed_board)
board[_.toInteger(ttt_player1_turn()) - 1] = 'x'
ttt_display(ttt_print_board(board))
board[_.toInteger(ttt_player2_turn()) - 1] = 'o'
ttt_display(ttt_print_board(board))
}
Upvotes: 0
Views: 54
Reputation: 2342
There are two things that I have found in relation to your problem:
First, in ttt_verify_case
, you use assignment (=
) instead of equality check (===
) in an if
statement. Second, you do not subtract 1 from the response (response
is in [1;9]
, board
has domain [0;8]
). It should be:
// this is my function to verify if case empty or not, at the moment just for 'x'
function ttt_verify_case (board, response) {
if (board[response - 1] === 'x') {
return false;
} else {
return true;
}
}
In the future you will want to change this into a switch
statement to handle both the x
and the o
cases.
The second problem is that you never actually check whether the case is empty for player 2. You set var is_case_free = false
and never call ttt_verify_case
. In fact you are not passing board
to ttt_player2_turn
, so it cannot actually verify that the case is free anyway.
And finally, you never actually pass board
to ttt_player2_turn
in ttt_new_game
.
An additional comment on your code: terminate all of your statements with semicolons.
There are a few changes you might want to make, but I will show you the changes I would make to ttt_player2_turn
:
// Get player 2's move
function ttt_player2_turn(board) {
var is_correct_response = false;
var is_case_free = false;
// Keep asking the player for a response until it is valid
while (!is_correct_response || !is_case_free) {
var response_player2 = ttt_ask("player 2 it's your turn, play 1 to 9");
// Make sure that the response is valid
is_correct_response = ttt_verify_response(response_player2);
if (!is_correct_response) {
console.log("incorrect response");
continue; // loop again
}
// Make sure that the case is free
is_case_free = ttt_verify_case(board, response_player2);
if(!is_case_free) {
console.log("player 1 already put a piece on this case");
}
}
return response_player2;
}
Upvotes: 1
Reputation: 73
I found my mistake, since it is an array i forget in my ttt_verify_case to -1 the response.
// this is my function to verify if case empty or not, at the moment just for 'x'
function ttt_verify_case (board, response) {
if(board[response - 1] === 'x' ){
return false
} else{
return true
}
}
Upvotes: 1