LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash scipting (https://www.linuxquestions.org/questions/linux-newbie-8/bash-scipting-766880/)

khilly 11-04-2009 02:00 PM

bash scipting
 
hey am trying to write a basc script for tick tack toe... i have done most of it ...but it does break when it needs and also i can seem to figure how to do the winner bit... so if anyone can help it would be great

pixellany 11-04-2009 02:47 PM

The first problem I found is that it does not do something rational if you enter something besides a number. In some cases, it shows error messages, and then repeats the prompt. But it then does not correctly handle a correct entry. My approach would be to fix this first before proceeding.

khilly 11-04-2009 02:51 PM

so how would i do that

TB0ne 11-04-2009 02:58 PM

Quote:

Originally Posted by khilly (Post 3744766)
so how would i do that

You would check the input that you get from the user, and see if it's a number. If it isn't, burp out an error message.

w1k0 11-04-2009 06:50 PM

I improved your game but spoiled your play. A lot of debugging but it was funny. Thank you and my apologies.

Code:

#!/bin/bash

# sets the players to x and o
player1="x"
player2="o"

# total results
if [ "$player1total" = "" ] ; then
player1total=0
fi
if [ "$player2total" = "" ] ; then
player2total=0
fi

# sets the global variable to empty
globalSpaces=""
#function to create the process initalise, so sets the array again to normal
function init () {

# array to enter names within the grid fot the game
for ((i=0; i<=9; i++));do
spaces[$i]=$i
done

# coped spaces to global spaces, so can be access outside the function
globalSpaces=(${spaces[*]})
}

# a function to calcuate the winner by seeing the different patterns of which a player can win
function winner() {
result[1]="${spaces[1]}${spaces[2]}${spaces[3]}"
result[2]="${spaces[4]}${spaces[5]}${spaces[6]}"
result[3]="${spaces[7]}${spaces[8]}${spaces[9]}"
result[4]="${spaces[1]}${spaces[4]}${spaces[7]}"
result[5]="${spaces[2]}${spaces[5]}${spaces[8]}"
result[6]="${spaces[3]}${spaces[6]}${spaces[9]}"
result[7]="${spaces[1]}${spaces[5]}${spaces[9]}"
result[8]="${spaces[3]}${spaces[5]}${spaces[7]}"

if [ "$move" -lt "10" ] ; then
for i in `seq 1 8`
do
if [ "${result[$i]}" == "xxx" ] ; then
export player1total=`expr $player1total + 1`
displayResult="player x won (x:o = $player1total:$player2total)"
move=10
fi
if [ "${result[$i]}" == "ooo" ] ; then
export player2total=`expr $player2total + 1`
displayResult="player o won (x:o = $player1total:$player2total)"
move=10
fi
done
fi

if [ "$move" -eq "9" ] ; then
displayResult="draw (x:o = $player1total:$player2total)"
fi
}

function drawGrid(){

# global variables into spaces so that it can be accessed within the function
spaces=(${globalSpaces[*]})

# below it prints out the grid
echo " ${spaces[1]} | ${spaces[2]} | ${spaces[3]} "
echo " ---+---+--- "
echo " ${spaces[4]} | ${spaces[5]} | ${spaces[6]} "
echo " ---+---+--- "
echo " ${spaces[7]} | ${spaces[8]} | ${spaces[9]} "

}
# calls the process init

init
declare -i move=0

while true; do

clear;
drawGrid

# winner=x|o
winner

# in this the player enters the number and the loop checks whether the number is between 1 and 9 and then
# and checks if the space is free and then enters an x for player1 and o for player2, but also
# checks to see if the grid is full then it was exit and then the player it was a draw and ask them to play again
if [ "$move" -lt "9" ] ; then
read -p "player x enter a number: " num1
if [ ${spaces[$[$num1]]} -ge 1 -a ${spaces[$[$num1]]} -le 9 ] ; then
globalSpaces[$[$num1]]="x"
else
read -p "space taken re-enter a number: " num1
fi
fi

move=$move+1

clear;
drawGrid

# winner=x|o
winner

if [ "$move" -lt "9" ] ; then
read -p "player o enter a number: " num2
if [ ${spaces[$[$num2]]} -ge 1 -a ${spaces[$[$num2]]} -le 9 ] ; then
globalSpaces[$[$num2]]="o"
else
read -p "space taken re-enter a number: " num2
fi
fi

move=$move+1

if [ "$move" -gt "9" ] ; then
break
fi

done

echo $displayResult
echo
read -p "would you like to play again? " playAgain

case $playAgain in
n|N) exit;;
*) $0;;
esac


pixellany 11-04-2009 08:10 PM

Quote:

Originally Posted by w1k0 (Post 3745027)
I improved your game but spoiled your play. A lot of debugging but it was funny. Thank you and my apologies.

Huh????

pixellany 11-04-2009 08:24 PM

Quote:

Originally Posted by khilly (Post 3744766)
so how would i do that

Ummm....if you wrote the initial script, one would assume you would know how to add some features.....? What book or reference are you using?

I found a clever way:

use this in an "if" construct:

[ $var -eq $var 2>/dev/null ]

if $var is a number then the exit value is 0 (true), otherwise it is 2


All times are GMT -5. The time now is 09:24 PM.