The goal was to fix a previous tic tac toe game in node and then add new features. I have a working tic tac toe game but the last feature to add is to allow the user to choose any symbol to use on the game board. I tried to implement it but it broke the other games(I have comp vs comp human vs comp and human vs human).
Here's the working code before trying to add the use of any symbol
let patterns_1 = [
[(/ OO....../),0],
[(/O..O.. ../),6],
[(/......OO /),8],
[(/.. ..O..O/),2],
[(/ ..O..O../),0],
[(/...... OO/),6],
[(/..O..O.. /),8],
[(/OO ....../),2],
[(/ ...O...O/),0],
[(/..O.O. ../),6],
[(/O...O... /),8],
[(/.. .O.O../),2],
[(/O O....../),1],
[(/O.. ..O../),3],
[(/......O O/),7],
[(/..O.. ..O/),5],
[(/. ..O..O./),1],
[(/... OO.../),3],
[(/.O..O.. ./),7],
[(/...OO .../),5]
]
let patterns_2 = [
[(/ X . X /),1],
[(/ XX....../),0],
[(/X..X.. ../),6],
[(/......XX /),8],
[(/.. ..X..X/),2],
[(/ ..X..X../),0],
[(/...... XX/),6],
[(/..X..X.. /),8],
[(/XX ....../),2],
[(/ ...X...X/),0],
[(/..X.X. ../),6],
[(/X...X... /),8],
[(/.. .X.X../),2],
[(/X X....../),1],
[(/X.. ..X../),3],
[(/......X X/),7],
[(/..X.. ..X/),5],
[(/. ..X..X./),1],
[(/... XX.../),3],
[(/.X..X.. ./),7],
[(/...XX .../),5],
[(/ X X.. ../),0],
[(/ ..X.. X /),6],
[(/.. ..X X /),8],
[(/ X ..X.. /),2],
[(/ XX.. ../),0],
[(/X.. .. X /),6],
[(/.. .XX /),8],
[(/X ..X.. /),2],
[(/ X ..X../),0],
[(/ ..X.. X/),6],
[(/..X.. X /),8],
[(/X ..X.. /),2]
]
let patterns_3 = [
[(/OOO....../),'O'],
[(/...OOO.../),'O'],
[(/......OOO/),'O'],
[(/O..O..O../),'O'],
[(/.O..O..O./),'O'],
[(/..O..O..O/),'O'],
[(/O...O...O/),'O'],
[(/..O.O.O../),'O'],
[(/XXX....../),'X'],
[(/...XXX.../),'X'],
[(/......XXX/),'X'],
[(/X..X..X../),'X'],
[(/.X..X..X./),'X'],
[(/..X..X..X/),'X'],
[(/X...X...X/),'X'],
[(/..X.X.X../),'X']
]
let board = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '];
let X = 'X';
let O = 'O';
let players = [X, O];
let curr_turn = X;
let player_letter = X
let game_running = false;
let game_type_selected = false;
let game_type = 0; // 0 = human vs computer, 1 = comp vs comp, 2 = human vs human
const comp = function () {
x = get_pattern_1_move ()
if (x == -1) {
x = get_pattern_2_move ()
if (x == -1) {
x = get_move ()
}
}
// if player_letter is X's turn, comp is O else X
move(x,player_letter === X ? O:X)
if (game_type === 1) {
// flip players
player_letter = player_letter === X ? O:X
}
}
const move = function (pos,x) {
// if (x != curr_turn) {
// return false
// }
if (+pos >= 0 && +pos <= 8 && !isNaN(+pos) && board[+pos]==' ') {
board.splice(+pos,1,x)
curr_turn= (x == X)? O: X
let player = 'Computer'
if (x === player_letter) {
player = 'You'
}
console.log(player + ' played a ' + x + ' at ' + pos)
// console.log("Enter [0-8]:")
return true
}
return false
}
const board_display = function () {
return ' '+board[0]+' |'+' '+board[1]+' |'+' '+board[2]+'\n===+===+===\n'+' '+board[3]+' |'+' '+board[4]+' |'+' '+board[5]+'\n===+===+===\n'+' '+board[6]+' |'+' '+board[7]+' |'+' '+board[8]
}
const show = function () {
console.log(board_display())
}
const board_filled = function () {
x = get_move()
if (x==-1) {
// show()
console.log('DRAW!')
exit()
return true
}
return false
}
const winner = function () {
board_string= board.join('')
the_winner= null
for (i=0; i < patterns_3.length; i++) {
array = board_string.match(patterns_3[i][0])
if (array) {
the_winner = patterns_3[i][1]
}
}
if (the_winner) {
show()
console.log('Game over! ' + the_winner + ' wins!')
return true
}
return false
}
const get_pattern_1_move = function () {
board_string = board.join('')
for (i=0; i < patterns_1.length;i++) {
array = board_string.match(patterns_1[i][0])
if (array) {
return patterns_1[i][1]
}
}
return -1
}
const get_pattern_2_move = function () {
board_string= board.join('')
for (i=0;i<patterns_2.length;i++) {
array= board_string.match(patterns_2[i][0])
if (array) {
return patterns_2[i][1]
}
}
return -1
}
const get_move = function () {
if (board[4] == ' ') {
return 4
}
return board.indexOf(' ')
}
const exit = function () {
process.exit()
}
const play = function () {
console.log('select a game type:')
console.log('0 = computer vs human')
console.log('1 = computer vs computer')
console.log('2 = human vs human')
// console.log("Pick a player: X or O")
// show()
// console.log("Enter [0-8]:")
process.openStdin().on('data',function(res) {
if (game_running) {
if (move(res, player_letter)) {
if (winner()|| board_filled()) {
exit()
} else {
if (game_type === 0) {
comp()
} else {
player_letter = player_letter === X ? O:X
}
if (winner()||board_filled()) {
exit()
} else {
show()
}
if (game_type === 2) {
console.log('Player ' + player_letter + '\'s turn, Make a Move')
console.log("Enter [0-8]:")
}
}
} else {
console.log("Invalid Move, Try Again")
console.log("Enter [0-8]:")
show()
}
} else if (game_type_selected === true && game_type === 0) {
// changing character number to character
selected_character = String.fromCharCode(res.readInt8(0)).toUpperCase()
// console.log(selected_character)
if (selected_character === 'X' || selected_character === 'O') {
// converts from number to character
player_letter = selected_character;
game_running = true;
if (selected_character === 'O' ) {
console.log("Computer goes first as X")
comp()
show()
}
// show()
if (selected_character === 'X') {
console.log("You go first as X")
// show()
}
console.log("Enter [0-8]:")
} else {
console.log("Invalid Player, Try Again")
}
} else if (game_type_selected === true && game_type === 1) {
selected_character = 'O'
while (!(winner()||board_filled())) {
comp()
show()
}
} else if (game_type_selected === true && game_type === 2) {
console.log('Player X\'s turn, Make a Move')
console.log("Enter [0-8]:")
player_letter = X
game_running = true;
} else {
let choice = String.fromCharCode(res.readInt8(0))
if (choice === '0' || choice === '1' || choice === '2') {
game_type_selected = true;
game_type = parseInt(choice);
if (game_type === 1 || game_type === 2) {
console.log('You selected (' + game_type + ') Hit Enter to Play')
} else {
console.log('You selected (' + game_type + ')')
console.log("Pick a player: X or O")
}
} else {
console.log('Invalid Choice, Try Again')
}
}
})
}
play()
And here's what I tried,
let board = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '];
let X = 'X';
let O = 'O';
let players = [X, O];
let curr_turn = X;
let player_letter = X;
let game_running = false;
let selected_character = null;
let letters_picked = false;
let x_symbol = null;
let o_symbol = null;
let game_type_selected = false;
let game_type = -1; // 0 = human vs computer, 1 = comp vs comp, 2 = human vs human
const comp = function () {
x = get_pattern_1_move ()
if (x == -1) {
x = get_pattern_2_move ()
if (x == -1) {
x = get_move ()
}
}
// if player_letter is X's turn, comp is O else X
move(x,player_letter === X ? O:X)
if (game_type === 1) {
// flip players
player_letter = player_letter === X ? O:X
}
}
const move = function (pos,x) {
// if (x != curr_turn) {
// return false
// }
if (+pos >= 0 && +pos <= 8 && !isNaN(+pos) && board[+pos]==' ') {
x = x === X ? x_symbol : o_symbol
board.splice(+pos,1,x)
curr_turn= (x == X)? O: X
let player = 'Computer'
if (x === player_letter) {
player = 'You'
}
if (game_type === 0) {
console.log(player + ' played at ' + x + ' at ' + pos)
} else if (game_type === 1 || game_type === 2) {
console.log(x + ' played at ' + pos)
}
return true
}
return false
}
const board_display = function () {
return ' '+board[0]+' |'+' '+board[1]+' |'+' '+board[2]+'\n===+===+===\n'+' '+board[3]+' |'+' '+board[4]+' |'+' '+board[5]+'\n===+===+===\n'+' '+board[6]+' |'+' '+board[7]+' |'+' '+board[8]
}
const show = function () {
console.log(board_display())
}
const board_filled = function () {
x = get_move()
if (x==-1) {
show()
console.log('DRAW!')
return true
}
return false
}
const winner = function () {
board_string= board.join('')
the_winner= null
for (i=0; i < patterns_3.length; i++) {
array = board_string.match(patterns_3[i][0])
if (array) {
the_winner = patterns_3[i][1]
}
}
if (the_winner) {
show()
console.log('Game over! ' + the_winner + ' wins!')
return true
}
return false
}
const get_pattern_1_move = function () {
board_string = board.join('')
for (i=0; i < patterns_1.length;i++) {
array = board_string.match(patterns_1[i][0])
if (array) {
return patterns_1[i][1]
}
}
return -1
}
const get_pattern_2_move = function () {
board_string= board.join('')
for (i=0;i<patterns_2.length;i++) {
array= board_string.match(patterns_2[i][0])
if (array) {
return patterns_2[i][1]
}
}
return -1
}
const get_move = function () {
if (board[4] == ' ') {
return 4
}
return board.indexOf(' ')
}
const exit = function () {
process.exit()
}
const play = function () {
console.log('select a game type:')
console.log('0 = computer vs human')
console.log('1 = computer vs computer')
console.log('2 = human vs human')
// console.log("Pick a player: X or O")
// show()
// console.log("Enter [0-8]:")
process.openStdin().on('data',function(res) {
if (!letters_picked && ((game_type === 0 && selected_character != null) || game_type === 2)) {
if (x_symbol === null) {
if (game_type === 0 && player_letter === O) {
x_symbol = X;
} else {
let input = res.readInt8(0)
if (input >= 33 && input <= 126) {
x_symbol = String.fromCharCode(input)
} else {
console.log('Invalid Input, Defaulting to X')
x_symbol = X
}
if ((game_type === 0 && player_letter === 0) || game_type === 2) {
console.log('Player O, Enter Custom Letter')
} else {
if (selected_character === 'O' ) {
console.log("Computer goes first")
comp()
}
show()
if (selected_character === 'X') {
console.log("You go first")
}
console.log("Enter [0-8]:")
letters_picked = true;
game_running = true;
o_symbol = O;
}
}
if (game_type === 0 && player_letter === X) {
o_symbol = O;
}
} else if (o_symbol === null) {
let input = res.readInt8(0)
if (input >= 33 && input <= 126 && x_symbol.charCodeAt(0) != input) {
o_symbol = String.fromCharCode(input)
} else {
console.log('Invalid Input, Defaulting to O')
o_symbol = O
}
letters_picked = true;
game_running = true;
if (selected_character === 'O' ) {
console.log("Computer goes first")
comp()
}
show()
if (selected_character === 'X') {
console.log("You go first")
}
console.log("Enter [0-8]:")
}
} else if (game_running) {
if (move(res, player_letter)) {
if (winner()||board_filled()) {
exit()
} else {
if (game_type === 0) {
comp()
} else {
player_letter = player_letter === X ? O:X
}
if (winner()||board_filled()) {
exit()
} else {
show()
}
if (game_type === 2) {
let player_name = player_letter === X ? x_symbol : o_symbol
console.log(player_name + ' turn, Make a Move')
console.log("Enter [0-8]:")
}
}
} else {
console.log("Invalid Move, Try Again")
console.log("Enter [0-8]:")
show()
}
} else if (game_type_selected === true && game_type === 0) {
// changing character number to character
selected_character = String.fromCharCode(res.readInt8(0)).toUpperCase()
// console.log(selected_character)
if (selected_character === 'X' || selected_character === 'O') {
// converts from number to character
player_letter = selected_character;
game_running = true;
if (selected_character === 'O' ) {
console.log("Computer goes first")
comp()
}
show()
if (selected_character === 'X') {
console.log("You go first")
}
console.log("Enter [0-8]:")
} else {
console.log("Invalid Player")
}
} else if (game_type_selected === true && game_type === 1) {
selected_character = 'O'
while (!(winner()||board_filled())) {
comp()
show()
}
} else if (game_type_selected === true && game_type === 2) {
console.log('Player ' + x_symbol + '\'s turn, Make a Move')
console.log("Enter [0-8]:")
player_letter = X
game_running = true;
} else {
let choice = String.fromCharCode(res.readInt8(0))
if (choice === '0' || choice === '1' || choice === '2') {
game_type_selected = true;
game_type = parseInt(choice);
if (game_type === 0) {
console.log("Pick a player: X or O")
} else if (game_type === 2) {
console.log('Player X, pick a custom letter')
} else {
x_symbol = X;
o_symbol = O;
letters_picked = true;
}
// if (game_type === 1 || game_type === 2) {
// console.log('You selected (' + game_type + ') Hit Enter to Play')
// } else {
// console.log("Pick a player: X or O")
// }
} else {
console.log('Invalid Choice')
}
}
})
}
play()
I tried to add variables for x_symbol and o_symbol in hopes that I could just set the new characters equal to them. The symbols work in the game board but then the win function stops working and the other games have new issues too. I know the play function is messy with all of the if statements and for loops but the game worked before. Is there a cleaner way of implementing this feature? Or am I missing another if statement?
via gab
No comments:
Post a Comment