Game

The game is simple. Test the user's response time by asking them to enter the correct value that is displayed on the screen in the shortest time possible. Each round of the game will be as follows:

Show Value ----> Get Response ----> Check Response ----> Report Response Time

Example

ROUND ONE
---------
COMPUTER : G
USER     : G
CORRECT INPUT, TIME = 0.26 SECONDS

Code

  • You may use any language or program.
  • Do not use third party libraries, except GUI libraries.
    (as suggested by marinus)
  • You may choose between CLI and GUI.

Points

  • +12 for the shortest code
  • -2 for every byte after the 1024th byte
  • +8 for GUI
  • +8 for showing questions (eg: 4 + 2 = ?)
    (rather than static values shown in the example above. There must be at least 10 questions.)
  • +2 for random intervals between each round
  • +4 for reporting average response time (in a set of 10 rounds)
  • +4 for quickly vanishing questions/values
    (questions/values stays on screen only for a short interval of time.
    The vanish time must be less than or equal to 1 seconds.)

You must, in your answer, calculate the total points you have scored. eg (+12 (-2x3) +4 = 10)

  • You should specify the exact prompt texts, because they will affect the character count significantly. Likewise you should specify what a GUI needs to contain to be sufficient. Also how long should the prompt be visible if it does vanish for the last bullet point? – Martin Ender Oct 5 '14 at 20:35
  • @MartinBüttner It is a trade-off between shortest code(+12) vs question prompts(+8). There must be minimum 10 questions. The vanish time must be less than or equal to 1 seconds. – Renae Lider Oct 5 '14 at 20:42
  • Do they have to enter in the correct answer for it to register as "time" for reaction? – Elias Benevedes Oct 5 '14 at 20:48
  • Do we have to output exactly like in your example after each correct output ? – Optimizer Oct 5 '14 at 20:50
  • 1
    @RenaeLider If the user enters incorrect answer, do we ignore that answer and start again ? Asking as it might impact the average of 10 answers timings. – Optimizer Oct 5 '14 at 21:11

JavaScript (E6) 305 bytes. Score 8+8+2+4=22

Rusty GUI, but GUI it is. Try in FireFox/FireBug console.
At the time shortest code, but I don't claim the 12 points. Waiting for some more answer.

(Q=i=>(
  i&&(t=r=x=y=0,msg=''),
  w=~new Date(),
  v=prompt(msg+'\nRound '+ ++r+': '+(a=1+t%9)+'+'+(b=1+(t>>4)%9)+'=?'),
  u=~new Date(),
  v-a-b?(++x,msg='Wrong'):(++y,msg='OK Time '+(p=w-u)/1000+' sec'),
  t+=p,
  r<10?setTimeout(Q,u%2000)
  :confirm('Wrong '+x+', OK '+y+' in Avg time '+(t/y/1000)+' sec\nRepeat?')&&Q(1)
))(1)
  • Since you cannot anyways get the last 4 point bonus, do you mind if I consider this your final submission in order to shorten my submission ? – Optimizer Oct 6 '14 at 18:29
  • @Optimizer I still could shorten it a bit - millisec instead of sec, less text ... – edc65 Oct 6 '14 at 19:54

JavaScript, ES6 352 340 335 bytes, Potential score : 2 + 8 + 12 + 4 + 4 + 8 = 38

<script>r=Math.random,s=setTimeout;T=N=0;a=_=>(S=s(_=>(C=eval(G.innerHTML=((k=~~(r()*5))+~~(r()*5))+'-'+k),t=new Date,R=s(_=>C=G.innerHTML=a(clearTimeout(S)),r()*1e3)),r()*2e3+1e3),'');document.onkeyup=E=>{clearTimeout(R);E.key==C&&++N&&(T+=(X=new Date-t))&&alert([X,N%10?'':T/10]);C=G.innerHTML=a(N%10||(T=0))};a()</script><a id='G'/>

or try this JSFiddle on latest Firefox.

Working:

  1. Shows a random question in form of X-Y after a random interval of [2,4] seconds.
  2. Waits for a keypress till a random time in an interval of [1,2] seconds, after which, clears the input and moves to step 1.
  3. If correct number is pressed, alerts the time in milliseconds.
  4. If this is the tenth (or rather every tenth) correct answer, show the average time in milliseconds for the last 10 correct answers after a comma.
  5. Empties the input area, goes to Step 1 again.

Please comment if something is incorrect or if you have any doubts.

Ruby (237)

def measure_time(v)
  t = Time.now
  puts "Enter the value: #{v}"
  result = gets.chomp
  if result == v
    puts "CORRECT INPUT, TIME = #{(Time.now - t).to_f(2)} SECONDS"
  else
    puts "Incorrect"
  end
end

WIP, Python, 366 bytes, Potential score : 8 + 4 = 12

import msvcrt as x
import random as r
import time,os
t=0
while t<11:
    time.sleep(1)
    n=r.randint(1,4)
    m=r.randint(1,4)
    print(str(n)+"+"+str(m)+"?")
    time.sleep(.1)
    t1=time.clock()
    os.system('cls')
    c=x.getch()
    if int(c)==n+m:
        print("Rct time:"+str(time.clock()-t1))
    else:
        print("Wrong char")
    time.sleep(1)
    t+=1

Kivy Application 983 bytes, 26 points (8+8+2+4+4)

I used Python's application framework, Kivy, to create a GUI, just under the 1024 byte penalty.

The game starts with a textbox. Once you click it, a question of the form A+B or A*B, where A,B are between 1 and 10, is shown. To have quick input the user's answer is read as soon as the length of text in the textbox is the same length as the length of the correct answer. For example, if 7*4 is shown, the user's input will be read as an answer once they have entered at least two characters. If the user enters the correct value their response time in milliseconds is shown, otherwise "W" is displayed. The user's input and the question will immediately be hidden, followed by the next question displayed after a random interval of time between 0 and 1 seconds. Every time the user inputs 10 correct responses their average response time is shown for those ten responses.

To run this code you will need a Python interpreter with the Kivy library, and you will want to place both the python file and the kv file in the same directory and name the kv file g.kv. Then you can run the python file as usual.

Python File

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.clock import Clock as K
from random import randint as R, random as Q
import time

class GApp(App):
    def build(s):
        g=G()
        K.schedule_interval(g.P, 0.01)
        return g

class G(Widget):
    def __init__(s):
        super(G, s).__init__()
        s.c=0
        s.t=10*[0]
        s.o=['+','*']

    def on_touch_up(s,t):
        s.U(0)

    def U(s,J):
        s.q.text=str(R(1,10))+s.o[R(0,1)]+str(R(1,10))
        s.i=int(round(time.time()*1000))

    def P(s,J):
        A=s.r
        B=s.q
        C=s.u
        if s.c==10:
            A.text+=";"+str(sum(s.t)/10)
            s.c=0
        try:
            S=str(eval(B.text))
        except:
            return

        if C.text!=S and len(C.text)>=len(S):
            A.text="W"
            B.text=""
            C.text=""
            K.schedule_once(s.U, Q())

        if C.text==str(S):
            s.t[s.c]=int(round(time.time()*1000))-s.i
            A.text=str(s.t[s.c])
            s.c+=1
            C.text=""
            B.text=""
            K.schedule_once(s.U,Q())
GApp().run()

Kv File

#:kivy 1.0.9
<G>:
    u:u
    r:r
    q:q
    Label:
        center:100,400
        text: "A"
    Label:
        center:100,300
        text: "Q"
    Label:
        id: q
        center:200,300
    TextInput:
        id: u
        center:200,400
    Label:
        id: r
        center:100,200

JAVA: (8+8+2+4+4) = 26

Not going for shortest, just grabbing all bonus points first:

Code: (999 Bytes)

import java.awt.*;
import java.util.*;
import java.util.concurrent.*;
import javax.swing.*;

class Q
{
    public static void main( String[] z ) throws Exception
    {
        Random r = new Random();

        int v = 0;
        String x = "FIRST ROUND";

        ScheduledExecutorService e = Executors.newScheduledThreadPool( 1 );
        for( int i = 0; i < 10; ++i )
        {
            int a = r.nextInt( 10 );
            int b = r.nextInt( 10 );
            long s = System.currentTimeMillis();
            ScheduledFuture< ? > f = e.schedule( ( ) -> Window.getWindows()[ 0 ].dispose(), 3, TimeUnit.SECONDS );
            String c = JOptionPane.showInputDialog( x + "\n" + a + "+" + b + "=" );
            if( ( a + b + "" ).equals( c ) )
            {
                long t = System.currentTimeMillis() - s;
                x = "CORRECT :" + t + "ms";
                v += t / 10;
            }
            else
            {
                x = "WRONG";
                v += 300;
            }
            f.cancel( true );
            Thread.sleep( r.nextInt( 1000 ) );
        }
        e.shutdown();
        JOptionPane.showMessageDialog( null, "Average: " + v + "ms" );
    }
}

APL(72), Score: 12+2+8=22

{a←?9⋄c←?9⋄b←⎕TS⋄⎕←a,'+',c⋄d←(⎕TS-b)×⎕=a+c⋄⎕←(5⌷d×60)+d[6]+7⌷d÷999⋄⎕DL(?⌊*9)÷*8}¨⍳⌊*9

Gets two random numbers between 1 and 9. Adds them together and asks the user the answer. If the answer is correct, it prints the reaction time in seconds. Else it prints 0. Repeats e^9=8103.08... times. Breaks when given a vector, a command or a variable name. But you could feed it ⌊*1 (floor(e^1))and it would happily interpret it as 2. Gives a negative score if the answer is not given within the same hour because of the dumb system of APL timestamps.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.