Tic-tac-toe: Difference between revisions
Content added Content deleted
(→{{header|UNIX Shell}}: Add implementation.) |
m (→{{header|UNIX Shell}}: Added player colors using ANSi escapes.) |
||
Line 14,176: | Line 14,176: | ||
local board=(1 2 3 4 5 6 7 8 9) tokens=(X O) |
local board=(1 2 3 4 5 6 7 8 9) tokens=(X O) |
||
show_board "${board[@]}" |
show_board "${board[@]}" |
||
# player colors: computer is red, human is green |
|||
local computer=31 human=32 |
|||
if (( RANDOM % 2 )); then |
if (( RANDOM % 2 )); then |
||
players=(computer human) |
players=(computer human) |
||
colors=($computer $human) |
|||
printf 'I go first!\n' |
printf 'I go first!\n' |
||
else |
else |
||
players=(human computer) |
players=(human computer) |
||
colors=($human $computer) |
|||
printf 'You go first!\n' |
printf 'You go first!\n' |
||
fi |
fi |
||
Line 14,193: | Line 14,196: | ||
*) printf 'Unknown player "%s"\n' "${players[turn%2]}"; exit 1;; |
*) printf 'Unknown player "%s"\n' "${players[turn%2]}"; exit 1;; |
||
esac |
esac |
||
show_board "${board[@]}" |
show_board "${board[@]}" | sed -e "s/${tokens[0]}/"$'\e['"${colors[0]}"$'m&\e[0m/g' \ |
||
-e "s/${tokens[1]}/"$'\e['"${colors[1]}"$'m&\e[0m/g' |
|||
(( turn=turn+1 )) |
(( turn=turn+1 )) |
||
done |
done |
||
Line 14,255: | Line 14,260: | ||
done |
done |
||
choice=${blanks[RANDOM % ${#blanks[@]}]} |
choice=${blanks[RANDOM % ${#blanks[@]}]} |
||
printf 'I go in space %d.\n' "$(( choice + 1 ))" >/dev/tty |
|||
board[choice]=$token |
board[choice]=$token |
||
printf '%s\n' "${board[@]}" |
printf '%s\n' "${board[@]}" |
||
Line 14,269: | Line 14,273: | ||
} |
} |
||
main "$@" |
main "$@"</lang> |
||
</lang> |
|||
{{Out}} |
{{Out}} |
||
Line 14,318: | Line 14,321: | ||
Play again? n |
Play again? n |
||
</pre> |
</pre> |
||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
Human play first with the "X". You must choose the row and the column you want to play... |
Human play first with the "X". You must choose the row and the column you want to play... |