LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-04-2009, 03:00 PM   #1
khilly
LQ Newbie
 
Registered: Nov 2009
Posts: 2

Rep: Reputation: 0
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

Last edited by khilly; 11-05-2009 at 04:29 AM.
 
Old 11-04-2009, 03:47 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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.
 
Old 11-04-2009, 03:51 PM   #3
khilly
LQ Newbie
 
Registered: Nov 2009
Posts: 2

Original Poster
Rep: Reputation: 0
so how would i do that
 
Old 11-04-2009, 03:58 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,343

Rep: Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905
Quote:
Originally Posted by khilly View Post
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.
 
Old 11-04-2009, 07:50 PM   #5
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware, Mint
Posts: 1,252

Rep: Reputation: 205Reputation: 205Reputation: 205
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
 
Old 11-04-2009, 09:10 PM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quote:
Originally Posted by w1k0 View Post
I improved your game but spoiled your play. A lot of debugging but it was funny. Thank you and my apologies.
Huh????
 
Old 11-04-2009, 09:24 PM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quote:
Originally Posted by khilly View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell scipting namit Linux - Software 12 02-07-2006 05:57 AM
shell scipting: append output of a command to a variable jonhewer Linux - Newbie 10 08-24-2005 06:42 AM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 07:42 PM
Bash Scipting ping question arkamir Linux - General 2 02-03-2004 08:18 PM


All times are GMT -5. The time now is 03:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration