Ejemplo n.º 1
0
Archivo: snake.c Proyecto: Seppho/snake
int main(int argc,char **argv){
	snake cobra;
	int i;
	struct winsize w;

	stat.i=0;
	if(argc>1)
		stat.velocity=atoi(argv[1]);

	else stat.velocity=1;
	ioctl(0, TIOCGWINSZ, &w);
	rows=w.ws_row-5;
	cols=w.ws_col-1;

	init_snake(&cobra);

	stat.i=0;
	while(1){
		print_matrix(cobra);
		sleep(stat.velocity);
		if(stat.i<2)
			go_left(cobra);
		else if(stat.i<4)
			go_right(cobra);
		else if(stat.i<10)
			go_up(cobra);
		else if(stat.i<18)
			go_right(cobra);
		else go_bottom(cobra);
		stat.i++;
	}

return 0;
}
Ejemplo n.º 2
0
void MainWindow::move()
{
    auto s_head = getHead();
    auto s_food = QPoint(food_pos.x(), food_pos.y());
    auto s_foot = getFoot();
    state_now = find_way(s_head, s_food);
    if(state_now == -1){
        state_now = find_way(s_head, s_foot);
    }
    if(state_now == to_left)
    {
        go_left();
    }else if(state_now == to_right){
        go_right();
    }else if(state_now == to_up){
        go_up();
    }else if(state_now == to_down){
        go_down();
    }else{
        state_now = getRandomStep(s_head);
        if(state_now == -1){
            QMessageBox::information(0, "", "end");
            gameover();
        }else if(state_now == to_left){
            go_left();
        }else if(state_now == to_right){
            go_right();
        }else if(state_now == to_up){
            go_up();
        }else if(state_now == to_down){
            go_down();
        }
    }
    update();
}
Ejemplo n.º 3
0
void		go_rightup(t_struct *st)
{
  if (up_once(st) == 0)
    {
      go_right(st);
      go_right(st);
    }
}
Ejemplo n.º 4
0
void		key_handler(unsigned int key, t_entlist *l, int *running)
{
	if (key == K_ENT)
	{
		clean_screen(l);
		*running = 0;
	}
	else
	{
		if (key == K_ESC)
			quit(l);
		else if (key == K_DOWN)
			go_down(l);
		else if (key == K_UP)
			go_up(l);
		else if (key == K_RIGHT)
			go_right(l);
		else if (key == K_LEFT)
			go_left(l);
		else if (key == K_DEL || key == K_BKSPC)
			delete_key(l);
		else
			special_keys(key, l);
	}
}
Ejemplo n.º 5
0
Archivo: delete.c Proyecto: kokaz/42sh
char	*my_back_space(int buf, char *str, t_line *line)
{
  char	*cpy;

  if (!line->rk)
    return (str);
  if (!str[line->rk])
    {
      str[line->rk - 1] = 0;
      my_left(buf, str, line);
      write(line->fd, " ", 1);
      go_left();
      if (line->x == line->win_x - 1)
	go_right();
      return (str);
    }
  my_left(buf, str, line);
  if ((cpy = strdup(&str[line->rk + 1])) == NULL)
    return (NULL);
  write(line->fd, &str[line->rk + 1], strlen(&str[line->rk]));
  str[line->rk] = 0;
  if ((cpy = my_strcat(str, cpy)) == NULL)
    return (NULL);
  recup_position(cpy, line, str, 0);
  return (cpy);
}
Ejemplo n.º 6
0
void		Monster::right_left(float a, std::deque<AObject*> &map)
{
  if (a > 0) // RIGHT
    go_right(map, 1);
  else if (a < 0) // LEFT
    go_left(map, 1);
}
Ejemplo n.º 7
0
void				go_back_up_right(t_usent *us)
{
	tputs(tgetstr("up", NULL), 1, tputchar);
	us->curs_line -= 1;
	while (us->curs_col < us->winsize->ws_col - 1)
		go_right(us);
}
Ejemplo n.º 8
0
bool AI::try_go_right()
{
  if (is_right_safe_later())
    return go_right();
  else
    return false;
}
Ejemplo n.º 9
0
void		sh_pk_end(t_usent *us)
{
	while (INDEX < us->c_size)
	{
		go_right(us);
		INDEX++;
	}
}
Ejemplo n.º 10
0
void			press_right_key(void)
{
	if (g_cursor.position_line < g_shell.length_line)
	{
		if (g_shell.backslash_index != -1)
			g_shell.backslash_index++;
		go_right(g_cursor, g_shell);
		move_cursor_right(g_cursor, g_shell);
	}
}
Ejemplo n.º 11
0
void				go_right_to(t_usent *us, int start, int end)
{
	int		i;

	i = start;
	while (i < end)
	{
		go_right(us);
		i++;
	}
}
Ejemplo n.º 12
0
void	set_select(t_dlist *dlist)
{
	t_dnode	*start;

	start = dlist->head;
	while(start != NULL && start->cursor_on == 0)
		start = start->next;
	if(start->selected == 0)
		start->selected = 1;
	else
		start->selected = 0;
	go_right(dlist);
}
Ejemplo n.º 13
0
void			press_end_key(void)
{
	int			h;

	h = g_cursor.position_line;
	while (h < g_shell.length_line)
	{
		if (g_shell.backslash_index != -1)
			g_shell.backslash_index++;
		go_right(g_cursor, g_shell);
		move_cursor_right(g_cursor, g_shell);
		h++;
	}
}
Ejemplo n.º 14
0
int	move(char *key, t_data *data)
{
	if (key[0] == 27)
	{
		if (key[2] == 'D')
		{
			go_left(data->dlist);
			return (1);
		}
		else if(key[2] == 'A')
		{
			go_up(data->dlist, data->nb_word_line);
			return (1);
		}
		else if (key[2] == 'C')
		{
			go_right(data->dlist);
			return (1);
		}
		else if (key[2] == 'B')
		{
			go_down(data->dlist, data->nb_word_line);
			return (1);
		}
		else if (key[1] == 0)
			return(2);
	}
	else if(key[0] == 127)
	{
		data->dlist = del_word(data->dlist);
		tputs(tgetstr("up", NULL), 0, ft_putchar2);
		return(1);
	}
	else if (key[0] == ' ')
	{
		set_select(data->dlist);
		return (1);
	}
	else if (key[0] == '\n')
	{
		put_dlist(data->dlist, data->old_term);
		return (1);
	}
	return (0);
}
Ejemplo n.º 15
0
void		move_me(t_win *win)
{
  const bool	*key_pressed;

  key_pressed = bunny_get_keyboard();
  if (key_pressed[BKS_W])
    move(win, FORWARD);
  if (key_pressed[BKS_S])
    move(win, BACKWARD);
  if (key_pressed[BKS_A])
    move(win, LEFT);
  if (key_pressed[BKS_D])
    move(win, RIGHT);
  if (key_pressed[BKS_LEFT])
    go_left(win);
  if (key_pressed[BKS_RIGHT])
    go_right(win);
}
Ejemplo n.º 16
0
void		Monster::go_safe_place(std::deque<AObject*> &map)
{
  unsigned int	i = 0;
  bool		is_safe = false;
  int		left = 1, right = 1, up = 1, down = 1;

  while (is_safe == false && i < map.size())
    {
      if ((map[i]->getType() == DANGER || map[i]->getType() == BOMB) &&
	  map[i]->getPosition().x + 160 >= position_.x && map[i]->getPosition().x - 160 <= position_.x &&
	  map[i]->getPosition().z + 160 >= position_.z && map[i]->getPosition().z - 160 <= position_.z)
	{
	  if ((safe(position_.x + (300 * i), position_.z, map, &left) == true) || (safe(position_.x - (300 * i), position_.z, map, &right) == true))
	    {
	      if (left == 1)
		{
		  go_left(map, 0);
		  is_safe = true;
		}
	      else if (right == 1)
		{
		  go_right(map, 0);
		  is_safe = true;
		}
	    }
	  else
	    {
	      if (up == 1)
		{
		  go_up(map, 0);
		  is_safe = true;
		}
	      else if (down == 1)
		{
		  go_down(map, 0);
		  is_safe = true;
		}
	    }
	  i++;
	}
      i++;
    }
}
Ejemplo n.º 17
0
int			is_a_valid_key(int c)
{
	if (c == 27)
	{
		endwin();
		exit(0);
	}
	else if (c == KEY_DOWN)
		go_down();
	else if (c == KEY_UP)
		go_up();
	else if (c == KEY_RIGHT)
		go_right();
	else if (c == KEY_LEFT)
		go_left();
	else
		return (0);
	return (1);
}
Ejemplo n.º 18
0
Archivo: parcour.c Proyecto: NSSX/ffff
int go_right(char *chaine, char *piece, int chainei, int piecei, int count, int length)
{
  if(count < 4)
    {
  piecei = piecei + 1;
  chainei = chainei + 1;
  count++;  
  piece[piecei] = 'x'; 
  chaine[chainei] = 'x';
 if(piece[piecei + 1] == '#' && chaine[chainei + 1] == '.')
   count = go_right(chaine, piece, chainei, piecei, count, length);
 if(piece[piecei + 5] == '#' && chaine[chainei + length] == '.')
   count = go_down(chaine, piece, chainei, piecei, count, length);
 if(piece[piecei - 5] == '#' && chaine[chainei - length] == '.')
   count = go_up(chaine, piece, chainei, piecei, count, length);
 if(piece[piecei - 1] == '#' && chaine[chainei - 1] == '.')
   count = go_left(chaine, piece, chainei, piecei, count, length);
    }
  return (count);
}
Ejemplo n.º 19
0
void		sh_pk_ctrl_l(t_usent *us)
{
	while (INDEX < us->c_size)
	{
		go_right(us);
		INDEX++;
	}
	while (us->curs_line != 0 || us->curs_col != us->prompt_size)
	{
		go_left(us);
		tputs(tgetstr("dc", NULL), 0, tputchar);
		INDEX--;
	}
	free(us->edit);
	if (!(us->edit = (char*)malloc(sizeof(char) * 255)))
		sh_error_quit("malloc us->edit fail");
	ft_memset(us->edit, '\0', 255);
	tputs(tgetstr("cl", NULL), 0, tputchar);
	printprompt(NULL);
}
Ejemplo n.º 20
0
int main() {
  FILE *fp;
  fp = fopen("euler11.txt", "rb");	
  int array[ARRAY_SIZE][ARRAY_SIZE];
  int line[64];
  int product = 1;
  int number;
  int i, j, k, x, y;
  int greatest = 0;

  for(i = 0; i < ARRAY_SIZE; i++) {
    for(j = 0; j < ARRAY_SIZE; j++) {
      if(fscanf(fp, "%d", &number) == -1) {}		
      array[i][j] = number;
      if((k = fgetc(fp)) == '\n') {
          break;
      }
    }
  }
  for(j = 0; j < ARRAY_SIZE; j++) {
    for(i = 0; i < ARRAY_SIZE; i++) {
      product = go_right(array, i, j);
      if(product > greatest) {
        greatest = product;
      }
      product = go_down(array, i, j);
      if(product > greatest) {
        greatest = product;
      }
      product = go_downRight(array, i, j);
      if(product > greatest) {
        greatest = product;
      }
      product = go_downLeft(array, i, j);
      if(product > greatest) {
        greatest = product;
      }
    }
  }
  printf("greatest product is %d\n", greatest);
}
Ejemplo n.º 21
0
// moves the tank
void move_tank ( Tank *tank, int direction){

    //if this tank moved TANKSPEED earlier, just ignore. else, set move_state to 0
    if (tank->move_state < tank->move_speed ) return;

    tank->move_state = 0;

    switch(direction){
    case LEFT:
        go_left(tank);
        break;
    case RIGHT:
        go_right(tank);
        break;
    case UP:
        go_up(tank);
        break;
    case DOWN:
        go_down(tank);
        break;
    }
}
Ejemplo n.º 22
0
int		main(int argc, char **argv)
{
	int key;
	t_map *g_map;
	t_game *snake;

	srand(time(NULL));
	if (argc < 3)
		return (-1);
	snake = malloc(sizeof(t_game));
	g_map = malloc(sizeof(t_map));

	g_map->x_max = ft_atoi(argv[2]);
	g_map->y_max = ft_atoi(argv[1]);
	if (g_map->x_max < 3 || g_map->y_max < 3)
		return (-1);
	g_map->x_map = rand_a_b(3, g_map->x_max + 1);
	g_map->y_map = rand_a_b(3, g_map->y_max + 1);
	// ft_putnbr(g_map->x_map);
	// ft_putchar('\n');
	// ft_putnbr(g_map->y_map);
	// ft_putchar('\n');
	snake->x_s = 1;
	snake->y_s = 1;
	// ft_putnbr(snake->x_s);
	// ft_putchar('\n');
	// ft_putnbr(snake->y_s);
	// ft_putchar('\n');
	snake->pv_s = 13;

	g_map->map = map_create(g_map->x_map, g_map->y_map);
	ft_putendl("placement du heros");
	g_map->map[snake->x_s][snake->y_s] = 's';
	ft_putendl("affichage");
	
	//free_all(snake, g_map);
	while (1)
	{
		key = 2;
		key = getarrowkey();
		aff_map(g_map);
		aff_pv(snake);
		ft_putstr("POSITION:");
		ft_putnbr(snake->x_s);
		ft_putchar(' ');
		ft_putnbr(snake->y_s);
		ft_putchar('\n');
		ft_putstr("MAP");
		ft_putnbr(g_map->x_map);
		ft_putchar(' ');
		ft_putnbr(g_map->x_map);
		if (key == 1)
		{
			if (go_left(snake, g_map))
			{
				add_room(g_map);
				g_map = g_map->next;
			}
		}
		else if (key == 2)
		{
			if (go_up(snake, g_map))
			{
				add_room(g_map);
				g_map = g_map->next;
			}
		}
		else if (key == 3)
		{
			if (go_down(snake, g_map))
			{
				add_room(g_map);
				g_map = g_map->next;
			}
		}	
		else if (key == 4)
		{
			if (go_right(snake, g_map))
			{
				add_room(g_map);
				g_map = g_map->next;
			}
		}
		else if (key == 5)
		{
			free_all(snake, g_map);
			return (0);
		}
		usleep(100000);
		system("CLEAR");
	}
	return (0);
}
Ejemplo n.º 23
0
/******************************************************************
 * 			Main-function
 */
int main(void) {

	/**************************************
	 * 	Set the clock to run at 80 MHz
	 */
	SysCtlClockSet(SYSCTL_SYSDIV_2_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);

	/**************************************
	 * 	Enable the floating-point-unit
	 */
	FPUEnable();
	// We also want Lazystacking, so enable that too
	FPULazyStackingEnable();
	// We also want to put numbers close to zero to zero
	FPUFlushToZeroModeSet(FPU_FLUSH_TO_ZERO_EN);

	/**************************************
	 * 	Init variables
	 */
	uint16_t mapData[MAP_DATA_SIZE];
	uint8_t stepDir = 0;

	/**************************************
	 * 	Init peripherals used
	 */
	bluetooth_init();
	init_stepper();							// Init the GPIOs used for the stepper and loading LED
	InitI2C1();								// Init the communication with the lidar-unit through I2C
	InitPWM();
	setupTimers();

	/**************************************
	 * 	State 2
	 */
	// Disable the timers that are used
	disableTimer(TIMER1_BASE);
	disableTimer(TIMER2_BASE);

	// Enable all interrupts
	IntMasterEnable();
	/**************************************
	 * 	State 3
	 */
	// Indicate we should start with a scan regardless of what other things we have already got
	// from UART-interrupt
	// This means setting the appropriate bit in the status vector
	stat_vec |= TAKE_MEAS;

	/**************************************
	 * 	State 4
	 */
	// Contains main-loop where decisions should be made
	for ( ; ; ) {
		/**********************************
		 * 	Decision tree
		 */
		// Highest priority case first


		// Check both interrupts at each iteration in the loop
		if ( int_vec & UART_INT ) {
			// Reset the indication
			int_vec &= ~UART_INT;

			// Remove drive-stop flag to enable movement
			stat_vec &= ~DRIVE_STOP;

			// Init data array
			uint8_t dataArr[MAX_UART_MSG_SIZE];

			// Collect the message
			if ( readUARTMessage(dataArr, MAX_UART_MSG_SIZE) < SUCCESS ) {
				// If we have recieved more data than fits in the vector we should simply
				// go in here again and grab data
				int_vec |= UART_INT;
			}
			// We have gathered a message
			// and now need to determine what the message is
			parseMsg(dataArr, MAX_UART_MSG_SIZE);
		}
		// Checking drive (movement) interrupt
		if ( int_vec & TIMER2_INT ) {
			int_vec &= ~TIMER2_INT;
			// Disable TIMER2
			disableTimer(TIMER2_BASE);
			// Set drive-stop in status vector
			stat_vec |= DRIVE_STOP;
		}
		// Checking measure interrupt
		if ( int_vec & TIMER1_INT ) {
			int_vec &= ~TIMER1_INT;
			// Disable TIMER1
			disableTimer(TIMER1_BASE);

			// Take reading from LIDAR
			mapData[stepCount++] = readLidar();
			SysCtlDelay(2000);

			// Take step
			// Note: We need to take double meas at randvillkor (100) !!!!!
			if ( stepCount > 0 && stepCount < 100 ) {
				stepDir = 1;
			}
			else if ( stepCount >= 100 && stepCount < 200) {
				stepDir = 0;
			}
			else {
				stepDir = 1;
				stepCount = 0;

				// Reset busy-flag
				stat_vec &= ~TAKE_MEAS;
			}
			step = takeStep(step, stepDir);

			// Request reading from LIDAR
			reqLidarMeas();

			if ( stat_vec & TAKE_MEAS ) {
				// Restart TIMER1
				enableTimer(TIMER1_BASE, (SYSCLOCK / MEASUREMENT_DELAY));
			}
			else {
				sendUARTDataVector(mapData, MAP_DATA_SIZE);
				stat_vec &= ~BUSY;
			}
		}

		// Check the drive_stop flag, which always should be set unless we should move
		if ( stat_vec & DRIVE_STOP ) {
			// Stop all movement
			SetPWMLevel(0,0);
			halt();

			// MAKE SURE all drive-flags are not set
			stat_vec &= ~(DRIVE_F | DRIVE_L | DRIVE_R | DRIVE_LL | BUSY);
		}
		// Should we drive?
		else if ( stat_vec & DRIVE ) {
			// Remove drive flag
			stat_vec &= ~DRIVE;
			// Increase PWM
			increase_PWM(0,MAX_FORWARD_SPEED,0,MAX_FORWARD_SPEED);
			if ( stat_vec & DRIVE_F ) {
				enableTimer(TIMER2_BASE, DRIVE_FORWARD_TIME);
			}
			else if ( stat_vec & DRIVE_LL ) {
				enableTimer(TIMER2_BASE, DRIVE_TURN_180_TIME);
			}
			else {
				enableTimer(TIMER2_BASE, DRIVE_TURN_TIME);
			}
		}
		if ( !(stat_vec & BUSY) ) {
			// Tasks
			switch ( stat_vec ) {
				case ((uint8_t)DRIVE_F) :
					// Call drive function
					go_forward();
					// Set the drive flag & BUSY
					stat_vec |= DRIVE | BUSY;
					break;
				case ((uint8_t)DRIVE_L) :
					// Call drive-left function
					go_left();
					// Set the drive flag
					stat_vec |= DRIVE | BUSY;
					break;
				case ((uint8_t)DRIVE_R) :
					// Call drive-right function
					go_right();
					// Set the drive flag
					stat_vec |= DRIVE | BUSY;
					break;
				case ((uint8_t)DRIVE_LL) :
					// Call turn 180-degrees function
					go_back();
					// Set the drive flag
					stat_vec |= DRIVE | BUSY;
					break;
				case ((uint8_t)TAKE_MEAS) :
					// Request reading from LIDAR
					reqLidarMeas();
					// Start TIMER1
					enableTimer(TIMER1_BASE, (SYSCLOCK / MEASUREMENT_DELAY)); // if sysclock = 1 s, 1/120 = 8.3 ms
					// We are busy
					stat_vec |= BUSY;
					break;

				default:
					break;
			}
		}
	}
}
Ejemplo n.º 24
0
void game_loop(int * input_key){
	//init game data
	init_game();
	
	//control data
	struct timeval *begin, *end, *tmp;
	unsigned long diff;//in us
	int key = 0; //for temporary storage of input_key, b/c of race conditions
	int reached_bottom = 0;
	
	begin=malloc(sizeof(struct timeval));
	end=malloc(sizeof(struct timeval));
	gettimeofday(begin, NULL);
	diff = 0;
	
	draw();
	while(1){
		//input
		key=*input_key;
		if(key != -1){
			*input_key = -1;//input read->reset
			switch(key){//TODO move block
				case KEY_DOWN:	
					reached_bottom = go_down(&current_tetromino, &world);
					break;
				case KEY_LEFT: 	
					if(!reached_bottom){
						go_left(&current_tetromino, &world);
					}
					break;
				case KEY_RIGHT: 
					if(!reached_bottom){
						go_right(&current_tetromino, &world);
					}
					break;
				case 'x':
					if(!reached_bottom){
						rotate(&current_tetromino, &world, 1);
					}
					break;
				case 'c':
					if(!reached_bottom){
						rotate(&current_tetromino, &world, 0);
					}
					break;
				case 27: /*ESC*/
					switch(menu(input_key)){
						case 0:break;//continue game
						case 1:init_game();break;//new game
						case -1://quit
							free(begin);
							free(end);
							return;
						default:break;
					} 
					gettimeofday(begin, NULL);
					break;
				case 32: /*Space*/
					while(!go_down(&current_tetromino, &world));
					reached_bottom=1;
					diff=tick_time;
					break;
				case -2: 		
					free(begin);
					free(end);
					return;
				default: break;
			}
		}
		
		//game tick & time
		gettimeofday(end, NULL);
		diff+=(end->tv_sec-begin->tv_sec)*1000000 + end->tv_usec-begin->tv_usec;
		tmp=begin;
		begin=end;
		end=tmp;
		
		if(diff >= tick_time){
			diff -= tick_time;
			tick(&reached_bottom);
		}
		
		//gui update
		draw();
		
		//sleep
		usleep(50*1000);
	}
}
Ejemplo n.º 25
0
/**
 * @brief         cmd_ps
 * @param[in]     void
 * @param[in,out] void
 * @return        void
 */
void cmd_ps (void)
{
    uint32_t i;
    uint32_t len; 
    uint32_t task_free_size;
    
    PRINT("\r\n");
    
    PRINT("ID");
    go_right(LINE_SPACE_01 - strlen("ID"));
    
    PRINT("STACK_NAME");
    go_right(LINE_SPACE_02 - strlen("STACK_NAME"));
    
    PRINT("STASK_SIZE");
    go_right(LINE_SPACE_03 - strlen("STASK_SIZE"));	
    
    PRINT("STASK_FREE");
    go_right(LINE_SPACE_04 - strlen("STASK_FREE"));
    
    PRINT("STATE");
    PRINT("\r\n\r\n");
    
    for (i = 0; i < bos_TaskCount; i++)
    {
        PRINT("%d", bos_TaskTable[i].task->task_id);
        len = num_len(bos_TaskTable[i].task->task_id);
        go_right(LINE_SPACE_01 - len);
        
        PRINT("%s", bos_TaskTable[i].task->name);
        len = strlen(bos_TaskTable[i].task->name);
        go_right(LINE_SPACE_02 - len);

        PRINT("%d", bos_TaskTable[i].task->stack_size);
        len = num_len(bos_TaskTable[i].task->stack_size);
        go_right(LINE_SPACE_03 - len);

        task_free_size = task_free(i);
        PRINT("%d", task_free_size);
        len = num_len(task_free_size);
        go_right(LINE_SPACE_04 - len);
        
        switch (bos_TaskTable[i].task->flags)
        {
            case STATE_TERM:
                PRINT("[T]");     // Terminated
            break;
            case STATE_READY:
                PRINT("[R]");     // Ready
            break;
            case STATE_RUN:
                PRINT("[A]");     // Active
            break;
            case STATE_WAIT:
                PRINT("[W]");     // Wait
            break;
            default:
                PRINT("[U]");     // Unknow
            break;
        }
        PRINT("\r\n");
    }
}