I'm working through the tutorial at http://ryanstutorials.net/bash-scrip...bash-loops.php. As an exercise, I'm supposed to write a tic-tac-toe game in Bash. My game has the standard nine squares and looks like this:
Code:
me@mycomputer:~/bash/20160501/Ryans Tutorials/6$ ./tictactoe.sh
X X -
- - -
- - -
Choose a space [1-9] or q to quit:
Each space's value (-,X, or O) is stored in an array, a. I'm working on this routine to tell when the human player has won. So far, it can only evaluate a win across the top row, that is, spaces 1, 2, and 3:
Code:
#Check if player has won
checkforplayerwin () {
threeinarow=0
for i in {1..3};do
if [ "${a[$i]}" = "X" ]
then
((threeinarow++))
fi
done
if [ $threeinarow -eq 3 ]; then
win=1
fi
}
Code:
for i in ((1 2 3, 4 5 6, 7 8 9, 1 4 7, 2 5 8, 3 6 9, 1 5 9, 3 5 7));do
برچسب:
نویسنده: استخدام کار