Simple Chess Clock


 
Thread Tools Search this Thread
Top Forums Programming Simple Chess Clock
# 1  
Old 12-08-2014
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.
# 2  
Old 12-08-2014
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...
# 3  
Old 12-08-2014
Looks like start sets timer to zero.
# 4  
Old 12-08-2014
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.
This User Gave Thanks to wbport For This Post:
# 5  
Old 12-08-2014
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> _

# 6  
Old 12-09-2014
Code:
#!/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.
# 7  
Old 12-09-2014
Yet another possiblity:

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

Hth Smilie

Last edited by sea; 12-09-2014 at 06:44 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. What is on Your Mind?

How to Play Chess in Facebook Messenger

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)
Discussion started by: Neo
0 Replies

2. What is on Your Mind?

Chess Players: Which Online Resources (and Software) Do You Use and Why?

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)
Discussion started by: Neo
8 Replies

3. UNIX for Dummies Questions & Answers

A Simple Clock, Well Maybe Not That Simple...

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

Chess at the UNIX Forums.

Just to let you know, we have installed Chess at the UNIX forums: Chess Club - The UNIX Forums (0 Replies)
Discussion started by: Neo
0 Replies

5. Shell Programming and Scripting

chess perl program questions

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)
Discussion started by: hankooknara
1 Replies
Login or Register to Ask a Question