#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#define MAP_X 20
#define MAP_Y 20
#define SNAKE_MAX MAP_X*MAP_Y
int snake_x[SNAKE_MAX];
int snake_y[SNAKE_MAX];
int snake_head=0;
int snake_tail=0;
int snake_dir=0;
char map[MAP_Y][MAP_X];
void init(){
int i,j;
for(i=0;i<MAP_Y;i++)for(j=0;j<MAP_X;j++)map[i][j]='-';
for(i=0;i<MAP_Y;i++)map[i][0]=map[i][MAP_X-1]='@';
for(i=0;i<MAP_X;i++)map[0][i]=map[MAP_Y-1][i]='@';
snake_head=0;
snake_tail=0;
snake_x[0]=MAP_X/2;
snake_y[0]=MAP_Y/2;
snake_dir=0;
map[MAP_Y/2][MAP_X/2]='@';
}
void print_map(){
int i,j;
for(i=0;i<MAP_Y;i++){
for(j
=0;j
<MAP_X
;j
++)printf("%c",map
[i
][j
]); }
}
int step_snake(int grow){
static const int dir_d[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int pre_head,next_x,next_y;
pre_head = snake_head;
snake_head++;
snake_head%=SNAKE_MAX;
snake_x[snake_head]=next_x=(snake_x[pre_head]+dir_d[snake_dir][1])%MAP_X;
snake_y[snake_head]=next_y=(snake_y[pre_head]+dir_d[snake_dir][0])%MAP_Y;
if(map[next_y][next_x]=='@'){
return -1;
}
map[next_y][next_x]='@';
if(!grow){
map[snake_y[snake_tail]][snake_x[snake_tail]]=' ';
snake_tail++;
snake_tail%=SNAKE_MAX;
}
return 0;
}
void change_dir(int dir){
if( 0<=dir && dir<=3 && (dir+2)%4!=snake_dir ){
snake_dir=dir;
}
}
int wait_input(){
fd_set rfds;
struct timeval tv;
int retval;
char buf[10];
int ret = -1;
FD_ZERO(&rfds);
FD_SET(0, &rfds);
/* 1 秒間監視する。*/
tv.tv_sec = 1;
tv.tv_usec = 0;
retval = select(1, &rfds, NULL, NULL, &tv);
if (retval == -1){
}else if (retval){
read(0,buf,10);
switch(buf[0]){
case 'a':
ret=2;
break;
case 's':
ret=1;
break;
case 'd':
ret=0;
break;
case 'w':
ret=3;
break;
}
}
return ret;
}
int main(){
int input;
int game = 0;
int turn = 0;
init();
while(game>=0){
print_map();
input = wait_input();
change_dir(input);
game = step_snake(turn%4==0);
turn++;
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN5cy90aW1lLmg+CiNpbmNsdWRlIDxzeXMvdHlwZXMuaD4KI2luY2x1ZGUgPHVuaXN0ZC5oPgoKI2RlZmluZSBNQVBfWCAyMAojZGVmaW5lIE1BUF9ZIDIwCiNkZWZpbmUgU05BS0VfTUFYIE1BUF9YKk1BUF9ZCgppbnQgc25ha2VfeFtTTkFLRV9NQVhdOwppbnQgc25ha2VfeVtTTkFLRV9NQVhdOwppbnQgc25ha2VfaGVhZD0wOwppbnQgc25ha2VfdGFpbD0wOwppbnQgc25ha2VfZGlyPTA7CgpjaGFyIG1hcFtNQVBfWV1bTUFQX1hdOwoKdm9pZCBpbml0KCl7CiAgICBpbnQgaSxqOwogICAgZm9yKGk9MDtpPE1BUF9ZO2krKylmb3Ioaj0wO2o8TUFQX1g7aisrKW1hcFtpXVtqXT0nLSc7CiAgICBmb3IoaT0wO2k8TUFQX1k7aSsrKW1hcFtpXVswXT1tYXBbaV1bTUFQX1gtMV09J0AnOwogICAgZm9yKGk9MDtpPE1BUF9YO2krKyltYXBbMF1baV09bWFwW01BUF9ZLTFdW2ldPSdAJzsKICAgIHNuYWtlX2hlYWQ9MDsKICAgIHNuYWtlX3RhaWw9MDsKICAgIHNuYWtlX3hbMF09TUFQX1gvMjsKICAgIHNuYWtlX3lbMF09TUFQX1kvMjsKICAgIHNuYWtlX2Rpcj0wOwogICAgbWFwW01BUF9ZLzJdW01BUF9YLzJdPSdAJzsKfQoKdm9pZCBwcmludF9tYXAoKXsKICAgIGludCBpLGo7CiAgICBmb3IoaT0wO2k8TUFQX1k7aSsrKXsKIC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dGltZXZhbCB0djsKICAgIGludCByZXR2YWw7CiAgICBjaGFyIGJ1ZlsxMF07CiAgICBpbnQgcmV0ID0gLTE7CgogICAgRkRfWkVSTygmcmZkcyk7CiAgICBGRF9TRVQoMCwgJnJmZHMpOwoKICAgIC8qIDEg56eS6ZaT55uj6KaW44GZ44KL44CCKi8KICAgIHR2LnR2X3NlYyA9IDE7CiAgICB0di50dl91c2VjID0gMDsKCiAgICByZXR2YWwgPSBzZWxlY3QoMSwgJnJmZHMsIE5VTEwsIE5VTEwsICZ0dik7CgogICAgaWYgKHJldHZhbCA9PSAtMSl7CiAgICAgICAgcGVycm9yKCJzZWxlY3QoKSIpOwogICAgfWVsc2UgaWYgKHJldHZhbCl7CiAgICAgICAgcmVhZCgwLGJ1ZiwxMCk7CiAgICAgICAgc3dpdGNoKGJ1ZlswXSl7CiAgICAgICAgICAgIGNhc2UgJ2EnOgogICAgICAgICAgICByZXQ9MjsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIGNhc2UgJ3MnOgogICAgICAgICAgICByZXQ9MTsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIGNhc2UgJ2QnOgogICAgICAgICAgICByZXQ9MDsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIGNhc2UgJ3cnOgogICAgICAgICAgICByZXQ9MzsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIHJldDsKfQoKaW50IG1haW4oKXsKICAgIGludCBpbnB1dDsKICAgIGludCBnYW1lID0gMDsKICAgIGludCB0dXJuID0gMDsKICAgIGluaXQoKTsKICAgIHdoaWxlKGdhbWU+PTApewogICAgICAgIHByaW50ZigidHVybiAlZFxuIix0dXJuKTsKICAgICAgICBwcmludF9tYXAoKTsKICAgICAgICBpbnB1dCA9IHdhaXRfaW5wdXQoKTsKICAgICAgICBjaGFuZ2VfZGlyKGlucHV0KTsKICAgICAgICBnYW1lID0gc3RlcF9zbmFrZSh0dXJuJTQ9PTApOwogICAgICAgIHR1cm4rKzsKICAgIH0KICAgIHJldHVybiAwOwp9