Not sure if this is what you are after.
A very basic keyboard input version using a pseudo-INKEY$ in seconds only.
OSX 10.7.5, default bash terminal. Keys S=Start, F=Finish, Q=Quit...
Code:
#!/bin/bash
timer=0
count=0
# 1 second delay with keyboard override.
delay()
{
read -n1 -s -t1 keyboard
}
read -p "Press any key to start:- " -n1
while true
do
keyboard=""
delay
timer=$((timer+1))
count=$((count+1))
if [ "$keyboard" == "q" ] || [ "$keyboard" == "Q" ]
then
echo "Quit..."
exit 0
fi
if [ "$keyboard" == "r" ] || [ "$keyboard" == "R" ]
then
read -p "Reset! Press any key to start:- " -n1
timer=0
count=0
fi
if [ "$keyboard" == "s" ] || [ "$keyboard" == "S" ]
then
echo "Start timer..."
timer=0
fi
if [ "$keyboard" == "f" ] || [ "$keyboard" == "F" ]
then
echo "Finish timer..."
echo "Number of seconds = $timer..."
echo "Timer count = $count..."
timer=0
fi
done
Results:-
Code:
AMIGA:barrywalker~/Desktop/Code/Shell> ./timer.sh
Press any key to start:-
Reset! Press any key to start:-
Start timer...
Finish timer...
Number of seconds = 4...
Timer count = 6...
Start timer...
Finish timer...
Number of seconds = 7...
Timer count = 17...
Start timer...
Finish timer...
Number of seconds = 2...
Timer count = 29...
Reset! Press any key to start:-
Quit...
AMIGA:barrywalker~/Desktop/Code/Shell> _
Last edited by wisecracker; 12-08-2014 at 06:29 PM..
Reason: Misread OPs post. Forgot the RESET...
I must be mis-interpreting your requirement...
How about this?
Code:
timer=0
count=0
totaltime=0
# 1 second delay with keyboard override.
delay()
{
read -n1 -s -t1 keyboard
}
read -p "Press any key to start:- " -n1
while true
do
keyboard=""
delay
timer=$((timer+1))
count=$((count+1))
totaltime=$((totaltime+1))
echo "Total elapsed time = $totaltime."
if [ "$keyboard" == "q" ] || [ "$keyboard" == "Q" ]
then
echo "Quit..."
exit 0
fi
if [ "$keyboard" == "r" ] || [ "$keyboard" == "R" ]
then
read -p "Reset! Press any key to start:- " -n1
timer=0
count=0
totaltime=0
fi
if [ "$keyboard" == "s" ] || [ "$keyboard" == "S" ]
then
echo "Start timer..."
count=$timer
timer=0
fi
if [ "$keyboard" == "f" ] || [ "$keyboard" == "F" ]
then
echo "Finish timer..."
echo "Number of seconds = $timer..."
echo "Timer count = $count..."
timer=0
fi
done
Results:-
Code:
AMIGA:barrywalker~/Desktop/Code/Shell> ./timer.sh
Press any key to start:-
Total elapsed time = 1.
Total elapsed time = 2.
Total elapsed time = 3.
Start timer...
Total elapsed time = 4.
Total elapsed time = 5.
Finish timer...
Number of seconds = 2...
Timer count = 5...
Total elapsed time = 6.
Total elapsed time = 7.
Total elapsed time = 8.
Start timer...
Total elapsed time = 9.
Total elapsed time = 10.
Total elapsed time = 11.
Total elapsed time = 12.
Total elapsed time = 13.
Finish timer...
Number of seconds = 5...
Timer count = 8...
Total elapsed time = 14.
Total elapsed time = 15.
Quit...
AMIGA:barrywalker~/Desktop/Code/Shell> _
#!/bin/bash
# Chessclock
#
#
# Variables
#
white=0 # Time of white player
black=0 # Time of black player
cur=white # White begings always
# intervall=1 # Default intervall
isPaused=true # If the game is paused (or just not started yet) and neither one makes a move
#
# Action & Display
#
while true
do # Do math while players are playing....
if $isPaused
then # Noone plays at the moment
echo -e "\rGame is paused, press 'b' to beginn/resume"
read -n1 input
[[ $input = b ]] && isPaused=false
else # Get current players time:
case $cur in
black) black=$(($black+1))
time=$black ;;
white) white=$(($white+1))
time=$white ;;
esac
fi
echo -e "\rActive Player ($cur): ${time} secs"
#sleep $intervall
read -n1 -t1 input
case $input in
b|B) # Beginn
isPaused=false
;;
e|E) # Exit
break
;;
p|P) # Pause
isPaused=true
;;
n|N) # Next player
[[ $cur = black ]] && cur=white || cur=black
;;
esac
done
#
# Final output
#
echo -e "\rPlayer 'black' had used: $black seconds"
echo -e "\rPlayer 'white' had used: $white seconds"
Outputs:
Code:
:) ~ $ chessclock
Game is paused, press 'b' to beginn/resume
Active Player (white): secs
Active Player (white): 1 secs
Active Player (white): 2 secs
Active Player (white): 3 secs
Active Player (white): 4 secs
Active Player (black): 1 secs
Active Player (black): 2 secs
Active Player (white): 5 secs
Active Player (white): 6 secs
Active Player (black): 3 secs
Active Player (black): 4 secs
Active Player (black): 5 secs
Active Player (black): 6 secs
Active Player (black): 7 secs
Game is paused, press 'b' to beginn/resume
Active Player (black): 7 secs
Active Player (black): 8 secs
Active Player (black): 9 secs
Active Player (black): 10 secs
Active Player (white): 7 secs
Active Player (white): 8 secs
Player 'black' had used: 10 seconds
Player 'white' had used: 8 seconds
Have you ever wondered to play chess while you chat with your friends?
Facebook has made this possible. FB keeps coming up with more and more new ideas for its users but this time it is a more intellectual one. Facebook has built a build-in-functionality in Facebook messenger, in which you just... (0 Replies)
Hi Chess Players,
Which Online Resources (and Software) Do You Use and Why?
As for me, I use chessgames.com and chessbase.com; but currently I'm using chessgames.com the most; I like exploring openings in the chessgames.com opening explorer.
For analysis on the desktop (Mac) or iPhone I... (8 Replies)
The attachment says it all really...
It is a DEMO at a glance digital readout using the "date" command to make it useful...
For a Mocbook Pro 13", OSX 10.7.5, but may well work on Linux variants too.
Enjoy...
#!/bin/bash
#
# Clock.sh
# A bash DEMO to create a 6 x 7 character set... (4 Replies)
Discussion started by: wisecracker
4 Replies
4. Forum Support Area for Unregistered Users & Account Problems
Hello guys,
While going over the book, I ran into this chess program and I have few questions
1) on line 40), why is not $chessboard-> ) ???
40 unless (defined $chessboard->) {
2)
20 foreach my $i (reverse (0..7)) { #Row
1 #!/usr/bin/perl -w
2 #
3 #
4
5
... (1 Reply)