Simple Chess Clock

I am trying to implement a simple chess clock. It should have the following options: start, stop, reset, read.

Reset will set the time to zero
Start will start the clock
Stop will stop the clock

My problem is that I want that start continues counting
the time from the time it had when it was stopped.

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...

#!/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:-

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> _

Looks like start sets timer to zero.

Some other features to consider:

  1. Enter an initial time for both players.
  2. Count backwards.
  3. Stop both clocks (tournament director resolving problem).
  4. Resume.
1 Like

I must be mis-interpreting your requirement...
How about this?

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:-

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

menu () {
    printf "\n %s\n\n" "Duration (total): $durtot seconds, duration (last run): $durlr seconds"
    printf "    1 $startorcont     2 RESET    3 QUIT\n\n"
    printf " => "; read choice

    case "$choice" in
        1) time1=$(date '+%s')
            printf " Counting... Press ENTER to stop counting "; read blah
            time2=$(date '+%s'); durlr=$((time2-time1)); durtot=$((durtot + durlr))
            startorcont="CONTINUE" ;;
        2) durtot=0; durlr=0; startorcont="START   " ;;
        3) exit ;;
        *) echo "Invalid choice!" ;;
    esac
}

durtot=0
durlr=0
startorcont="START   "

while :; do
    menu
done

Above is my humble bash / GNU date approach. Hope I didn't miss the point.

Yet another possiblity:

Code:

#!/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:

:) ~ $ 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

Hth :slight_smile: