Ejemplo n.º 1
0
void ConnectFour::update() {
	
	//new cursor position
	const int new_x = mouse.x - Tile::TILE_LENGTH/2;
	
	switch(state) {
		case PLAYING:
				if(-Tile::BORDER_LENGTH < new_x && new_x + cursor.w < WIDTH+Tile::BORDER_LENGTH) {
					cursor.x = new_x;
				}
		case GAMEOVER:
			for(auto& f: falling) {
				if(f) {
					f->update();
			
					//remove tokens that reached the goal
					if(f->is_done()) {
						delete f;
						f = nullptr;
					}
				}
			}
			
			//can only be in state PLAYING if there is at least one possible move
			if(PLAYING == state && computer == current) {
				calculate_move();
			}
			break;
		default:
			break;
	}
}
Ejemplo n.º 2
0
int main(void)
{
	struct chessboard *c = get_new_board();
	int tmp, sx, sy, dx, dy;

	while (1) {
		print_chessboard(c);
		/* Calcluate white's suggested move */
		tmp = calculate_move(c, 0, MOVE_DEPTH);
		sx = tmp & 0xff;
		sy = (tmp & 0xff00) >> 8;
		dx = (tmp & 0xff0000) >> 16;
		dy = (tmp & 0xff000000) >> 24;
		printf("Computer suggests (%d,%d) -> (%d,%d)\n", sx, sy, dx, dy);

no_clear:
		printf("Enter move: ");
		tmp = scanf("%d %d %d %d", &sx, &sy, &dx, &dy);
		if (tmp != 4)
			goto no_clear;

		if (sx == -1)
			return 0;

		tmp = execute_move(c, sx, sy, dx, dy);
		if (tmp) {
			printf("Error: %s\n", get_error_string(tmp));
			goto no_clear;
		} else {
			printf("Move succeeded!\n");
		}

		/* Calcluate black's move */
		tmp = calculate_move(c, 1, MOVE_DEPTH);
		sx = tmp & 0xff;
		sy = (tmp & 0xff00) >> 8;
		dx = (tmp & 0xff0000) >> 16;
		dy = (tmp & 0xff000000) >> 24;
		printf("Black moves (%d,%d) -> (%d,%d)\n", sx, sy, dx, dy);
		tmp = execute_move(c, sx, sy, dx, dy);
		if (tmp)
			fatal("Computer tried to make an illegal move: %s\n", get_error_string(tmp));
	}

	return 0;
}
Ejemplo n.º 3
0
void HandleEvent(SDL_Event event, celestial_object *spaceship, list *bullet_list, celestial_object *bullet, celestial_object *big_bullet, Uint8 *keystates,list *asteroid_list, int *delay_array)
{



	/* ################# MENU ################# */
	if (state == menu) {

		if (keystates[SDLK_UP]){
			choice = 0;
		}
		else if (keystates[SDLK_DOWN]){
			choice = 1;
		}
		else if (keystates[SDLK_LEFT]){
			choice = 2;
		}
		else if (keystates[SDLK_RIGHT]){
			choice = 3;
		}


		if (keystates[SDLK_RETURN] && choice==0) {
			spaceship_state = INVINCIBLE;
			time_begin_invincibility = SDL_GetTicks();
			state = init;
			Mix_HaltMusic();
		}

		else if (keystates[SDLK_RETURN] && choice==1) {
			quit = true;
		}
		else if (keystates[SDLK_RETURN] && choice==2){
			training=true;
			gameover=false;
			level=0;
			nb_of_asteroids_at_startup=2;
			state=init;
		}

		else if (keystates[SDLK_RETURN] && choice==3) {
			state=options;
		}

	}
	/* ################# OPTIONS ################# */

	if(state == options){
		if (keystates[SDLK_UP]){
			choice = 4;
		}
		else if (keystates[SDLK_DOWN]){
			choice = 5;
		}
		if (keystates[SDLK_RETURN] && choice == 4) {
			state = music;
		}
		else if (keystates[SDLK_RETURN] && choice == 5){
			state = highscoremenu;
		}
		if (keystates[SDLK_BACKSPACE]){
			state = menu;
			choice = 3;
		}
	}
	/* ################# HIGHSCORE ################# */
	if (state == highscoremenu)
	{
		if (keystates[SDLK_BACKSPACE]){
			state = options;
		}
	}

	/* ################# MUSIC ################# */
	if(state==music){


		readmusic(choicemusic,mainmusic,epicbattle,battle3,battle4,battle5);

		if (delay_array[K_UP] == GO && keystates[SDLK_UP]){
			Mix_HaltMusic();
			choicemusic = choicemusic-1;
			readmusic(choicemusic,mainmusic,epicbattle,battle3,battle4,battle5);
			if (choicemusic<1){
				choicemusic=6;
			}
		}
		else if (delay_array[K_DOWN] == GO && keystates[SDLK_DOWN]){
			Mix_HaltMusic();
			choicemusic = choicemusic+1;
			readmusic(choicemusic,mainmusic,epicbattle,battle3,battle4,battle5);

			if (choicemusic>6){
				choicemusic=1;
				Mix_PlayMusic(mainmusic, 0);
			}
		}
		choicememory=choicemusic;
		if (keystates[SDLK_BACKSPACE]){
			state = options;
			Mix_HaltMusic();
			Mix_PlayMusic(Game_of_Thrones_8_bit, -1);
		}
	}
	/* ################# PLAY ################# */

	if(state==play){

		if (keystates[SDLK_BACKSPACE]){
			gameover=true;
			state=init;
			godmod=false;
			desactivatescore=false;
			training=false;
		}


		if (!pause) {

			/* Shoot */
			if (keystates[SDLK_SPACE] && (spaceship_state != DESTROY)) {
				if (godmod) {
					triple_fire(bullet_list, spaceship, big_bullet);
					Mix_PlayChannel(1, laser, 0);
				}

				if (delay_array[K_SPACE] == GO) {
					if (level <= 3)
						fire(bullet_list, spaceship, bullet);
					else if (level > 3 && level <= 6)
						fire(bullet_list, spaceship, big_bullet);
					else if (level > 6 && level <= 9)
						triple_fire(bullet_list, spaceship, bullet);
					else 
						triple_fire(bullet_list, spaceship, big_bullet);
					Mix_PlayChannel(1, laser, 0);
				}
			}


			/* go forward right */
			if (delay_array[K_UP] == GO && delay_array[K_RIGHT] == GO && keystates[SDLK_UP] && keystates[SDLK_RIGHT]) {
				sprite_turn_left(spaceship);
				calculate_move(spaceship, FORWARD);
			}

			/* go forward left */
			else if (delay_array[K_UP] == GO && delay_array[K_LEFT] == GO && keystates[SDLK_UP] && keystates[SDLK_LEFT]) {
				sprite_turn_right(spaceship);
				calculate_move(spaceship, FORWARD);
			}

			/* go forward */
			else if (delay_array[K_UP] == GO && keystates[SDLK_UP]) {
				calculate_move(spaceship, FORWARD);
			}

			/* go back left */
			else if (delay_array[K_DOWN] == GO && delay_array[K_LEFT] == GO && keystates[SDLK_DOWN] && keystates[SDLK_LEFT]) {
				sprite_turn_right(spaceship);
				calculate_move(spaceship, BACK);
			}

			/* go back right */
			else if (delay_array[K_DOWN] == GO && delay_array[K_RIGHT] == GO && keystates[SDLK_DOWN] && keystates[SDLK_RIGHT]) {
				sprite_turn_left(spaceship);
				calculate_move(spaceship, BACK);
			}

			/* go back */
			else if(delay_array[K_DOWN] == GO && keystates[SDLK_DOWN]) {
				calculate_move(spaceship, BACK);
			}


			/* go left on the spaceship's sprite */
			else if (delay_array[K_RIGHT] && keystates[SDLK_RIGHT]) {
				sprite_turn_left(spaceship);
			}

			/* go right on the spaceship's sprite */
			else if (delay_array[K_LEFT] && keystates[SDLK_LEFT]) {
				sprite_turn_right(spaceship);
			}

			/* invicible mod */
			if (delay_array[K_i] == GO && keystates[SDLK_i]){
				godmod = !godmod;
				desactivatescore = !desactivatescore;
				score = 0;
				if (godmod) {
					Mix_HaltMusic();
					Mix_PlayMusic(this_is_sparta, -1);
				}
				else {
					Mix_HaltMusic();
					readmusic(choicemusic,mainmusic,epicbattle,battle3,battle4,battle5);
				}
			}

			/*win mod*/
			if (delay_array[K_w] == GO &&  keystates[SDLK_w]){
				list_free(asteroid_list);
			}

		}

		if (keystates[SDLK_p]){
			pause = true;
			Mix_PauseMusic();
		}

		if (keystates[SDLK_RETURN] && pause) {
			pause = false;
			Mix_ResumeMusic();
		}

	}
	/* ################# END ################# */
	if(state==end){

		if (keystates[SDLK_RETURN]){
			Mix_HaltMusic();
			state=init;
		}
	}


	switch (event.type) {

		case SDL_QUIT:
			quit = true;
			break;

		case SDL_KEYDOWN:

			switch (event.key.keysym.sym) {

				/* quit when press press escape or q */
				case SDLK_ESCAPE:
					quit = true;
					break;
				case SDLK_q:
					quit = true;
					break;

					/* nothing to do for other keys */
				default:
					break;
			}
	}
}