Skip to content

Instantly share code, notes, and snippets.

@bag-man

bag-man/C - Language Tests

Last active Apr 19, 2016
Embed
What would you like to do?
Language tests
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
void end() {
printf("Game over.\n");
exit(0);
}
void *timer() {
int time = 5;
for (int i = 5; i > 0; i--) {
sleep(1);
printf("%d\n", i);
}
end();
}
int main(void) {
pthread_t pth;
pthread_create(&pth, NULL, timer, NULL);
while(1) {
if(getchar()) {
pthread_cancel(pth);
pthread_create(&pth, NULL, timer, NULL);
}
}
return 0;
}
// gcc prototype.c -o prototype --std=c99 -lpthread
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class prototype {
private static Thread timer;
private static volatile boolean isRunning = true;
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
createThread();
while(true) {
try {
String input = br.readLine();
if(input != "asdf") {
killThread();
createThread();
}
} catch(Exception e) {
}
}
}
private static void end() {
System.out.println("Game over.");
System.exit(0);
}
public static void killThread() {
isRunning = false;
}
private static void createThread() {
try {
Thread.sleep(1000);
} catch(Exception e) {
}
isRunning = true;
timer = new Thread() {
public void run() {
int time = 5;
for(int i = 5; i > 0; i--) {
if(!prototype.isRunning) {
Thread.currentThread().stop();
break;
}
try {
Thread.sleep(1000);
} catch(Exception e) {
}
System.out.println(i);
}
end();
}
};
timer.start();
}
}
var count = 5;
var loop;
var stdin = process.stdin;
stdin.setRawMode( true );
stdin.resume();
stdin.setEncoding('utf8');
stdin.on('data', function(key){
if ( key === '\u0003' ) {
process.exit();
}
//process.stdout.write(key);
clearInterval(loop);
count = 5;
startTimer();
});
function timer() {
if (count <= 0) {
clearInterval(loop);
end();
return;
}
console.log(count);
count=count-1;
}
function end() {
console.log("Game ended.");
clearInterval(loop);
process.exit();
}
function startTimer() {
loop = setInterval(timer,1000);
}
import thread
import time
import sys
count = 5
def input_thread(L):
raw_input()
L.append(None)
def loop():
global count
L = []
thread.start_new_thread(input_thread, (L,))
while 1:
time.sleep(1)
if L:
count = 5
loop()
thread.exit()
timer()
def timer():
global count
if count <= 0:
end()
return
print count
count = count - 1
def end():
print "Game Over"
sys.exit(0)
loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.