This is a simple Connect Four game. It only records plays on the board. It is in bash and does not tell you if some one wins.
#! /usr/bin/bash
function reset_board {
colunm_0=000000
colunm_1=000000
colunm_2=000000
colunm_3=000000
colunm_4=000000
colunm_5=000000
colunm_6=000000
}
function display_board {
row=5
clear
while test $row -gt -1
do
echo ${colunm_0:$row:1} ${colunm_1:$row:1} ${colunm_2:$row:1} ${colunm_3:$row:1} ${colunm_4:$row:1} ${colunm_5:$row:1} ${colunm_6:$row:1}
row=$(expr $row - 1)
done
}
function change_board {
if test $played == 1
then
change_colunm=$colunm_0
elif test $played == 2
then
change_colunm=$colunm_1
elif test $played == 3
then
change_colunm=$colunm_2
elif test $played == 4
then
change_colunm=$colunm_3
elif test $played == 5
then
change_colunm=$colunm_4
elif test $played == 6
then
change_colunm=$colunm_5
elif test $played == 7
then
change_colunm=$colunm_6
fi
if ! [[ $change_colunm == *'0'* ]]
then
echo 'Can not play in colunm '$played
else
good_move=1
if test $player == 1
then
change_colunm=$(echo $change_colunm | sed 's/0/1/')
else
change_colunm=$(echo $change_colunm | sed 's/0/2/')
fi
if test $played == 1
then
colunm_0=$change_colunm
elif test $played == 2
then
colunm_1=$change_colunm
elif test $played == 3
then
colunm_2=$change_colunm
elif test $played == 4
then
colunm_3=$change_colunm
elif test $played == 5
then
colunm_4=$change_colunm
elif test $played == 6
then
colunm_5=$change_colunm
elif test $played == 7
then
colunm_6=$change_colunm
fi
fi
}
function player_move {
good_move=0
while test $good_move == 0
do
read -n 1 -p "1~7: " played
if test $played == 'q'
then
echo ""
playing=0
break 1
else
change_board $played
fi
done
}
reset_board
display_board
player=1
playing=1
while test $playing == 1
do
player_move
display_board
if test $player == 1
then
player=2
else
player=1
fi
done