Tic-tac-toe: Difference between revisions

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

edits