The rules are simple:
- It's turn-based logic game: Player vs. AI;
- There's 8x8 matrix of random numbers between 1-99;
- The player moves in columns;
- AI moves in rows;
- The goal is to gather more points than the opponent;
Since I remember only the idea, I wrote this game from scratch.
But my AI seems to behave in 90% as original...just sometimes makes stupid move.
The game is written in Bash and it's ugly as hell (see screenshot).
This is not a top of programming skills also, but I'm beginner in Bash.
If someone is willing to colorize/gtkdialog'ize/improve/expand/anything - feel free! For now I give up...
I just finished the work about two hours ago (but have played several rounds), so cannot assure you that it's 100% bug free.
There's at least one issue - don't press any key right after your move, or else few unwanted characters will be displayed.
Ok, code in Bash included, but I also made a .pet with menu entry (for lazy ones )
PS. I made the script 'clickable' by adding those 2 lines right after '#!/bin/sh'.
If you'd like to run it in other terminal emulator than "rxvt" (I recommend LXTerminal), just remove them and run the script in traditional way.
Written on Lupu-528.004
EDIT: Added little improvement to eliminate that rare stupid moves. Code & .pet updated.
Have fun & Greetings!
Code: Select all
#!/bin/bash
tail -n +4 $0 > /tmp/Add-Ictive_Temp.txt
exec rxvt -e sh /tmp/Add-Ictive_Temp.txt
# ======================
# Add-Ictive by SFR'2012
# ======================
# Functions
function setcursor () {
tput cup $(($starty+$y)) $(($startx+($x*4)))
}
function printinvert () {
setcursor
if [ ${cell[($x+1)+(10*$y)]} -eq 0 ]; then
echo -en "\033[7m --"
else
echo -en "\033[7m "${cell[($x+1)+(10*$y)]}""
fi
}
function printnormal () {
setcursor
if [ ${cell[($x+1)+(10*$y)]} -eq 0 ]; then
echo -en "\033[0m --"
else
echo -en "\033[0m "${cell[($x+1)+(10*$y)]}""
fi
}
function clearscores () {
player=0
puppy=0
left=0
end=0
}
function createarray () {
for y in `seq 0 7`; do
for x in `seq 1 8`; do
cell[$x+(10*$y)]=$((RANDOM%99+1))
left=$[$left+cell[$x+(10*$y)]]
if [ ${#cell[$x+(10*$y)]} -eq 1 ]; then
temp="0"${cell[$x+(10*$y)]}
cell[$x+(10*$y)]=$temp
fi
done
done
}
function setupscreen () {
echo -en "\033[0m "
clear
echo "=========="
echo "Add-Ictive"
echo "=========="
echo
echo "[Keys: CRSR Up/Down = Movement; Enter = Choose; n = New Game; x = Exit]"
echo
echo "Player: $player"
echo "Puppy: $puppy"
echo "Left: $left"
echo
for y in `seq 0 7`; do
echo -n " "
for x in `seq 1 8`; do
echo -n ${cell[$x+(10*$y)]}" "
done
echo
done
startx=2
starty=10
x=$(($RANDOM%8))
y=$(($RANDOM%8))
printinvert
}
function addplayerscore () {
temp=${cell[($x+1)+(10*$y)]}
player=$(($player+10#$temp))
left=$(($left-10#$temp))
tput cup 6 9
echo -e "\033[0m"$player
tput cup 8 9
echo $left" "
cell[($x+1)+(10*$y)]=0
printinvert
}
function addpuppyscore () {
temp=${cell[($x+1)+(10*$y)]}
puppy=$(($puppy+10#$temp))
left=$(($left-10#$temp))
tput cup 7 9
echo -e "\033[0m"$puppy
tput cup 8 9
echo $left" "
cell[($x+1)+(10*$y)]=0
printinvert
}
function checkrow () {
sum=0
for xx in `seq 0 7`; do
sum=$(($sum+10#${cell[($xx+1)+(10*$y)]}))
done
if [ $sum -eq 0 ]; then
end=1
fi
}
function checkcolumn () {
sum=0
for yy in `seq 0 7`; do
sum=$(($sum+10#${cell[($x)+(10*$yy+1)]}))
done
if [ $sum -eq 0 ]; then
end=1
fi
}
function checkwinner () {
if [ $player -gt $puppy ]; then winner="Player"; fi
if [ $puppy -gt $player ]; then winner="Puppy"; fi
if [ $puppy -eq $player ]; then winner="No winners - draw!"; fi
}
# Puppy AI
function puppymove () {
for xx in `seq 1 8`; do
myscores[xx]=${cell[($xx)+(10*$y)]}
for yy in `seq 0 7`; do
z[yy]=${cell[($xx)+(10*$yy)]}
done
z[y]=0
plscores[xx]=`echo -e "${z[0]}\n${z[1]}\n${z[2]}\n${z[3]}\n${z[4]}\n${z[5]}\n${z[6]}\n${z[7]}" | sort -rn | head -1`
if [ ${plscores[xx]} -eq 0 ]; then plscores[xx]=99; fi
done
for q in `seq 1 8`; do
z[q-1]=$(( 10#${myscores[q]} - 10#${plscores[q]} ))
if [ ${cell[($q)+(10*$y)]} -eq 0 ]; then z[q-1]=-999; fi
done
result=`echo -e "${z[0]}\n${z[1]}\n${z[2]}\n${z[3]}\n${z[4]}\n${z[5]}\n${z[6]}\n${z[7]}" | sort -rn | head -1`
for q in `seq 0 7`; do
if [ $result -eq ${z[q]} ]; then
pos=$q
fi
done
}
# Main Loop
while true
do
clearscores
createarray
setupscreen
while true
do
read -sn1 key
case "$key" in
"A")
if [ $y != 0 ]; then
printnormal
y=$(($y-1))
printinvert
fi
;;
"B")
if [ $y != 7 ]; then
printnormal
y=$(($y+1))
printinvert
fi
;;
"n") break;;
"x") exit;;
"")
if [ ${cell[($x+1)+(10*$y)]} -ne 0 ]; then
addplayerscore
checkrow
if [ $end -eq 1 ]; then
checkwinner
tput cup 20 0
echo -e"\033[0mThe winner is: "$winner
echo "Press any key for new game..."
read -sn 1
break;
fi
puppymove
printnormal
sleep 0.2
x=$pos
printinvert
sleep 0.2
addpuppyscore
checkcolumn
if [ $end -eq 1 ]; then
checkwinner
tput cup 20 0
echo -e "\033[0mThe winner is: "$winner
echo "Press any key for new game..."
read -sn 1
break;
fi
fi
;;
esac
done
sleep 0.1
done
#END