コード例 #1
0
ファイル: tic-tac-toe-5x5.c プロジェクト: ijunglee/ooxx
int analyze(char matrix[][size])
{
	// row check
	for(int row=0; row<size; row++){
		row_check(matrix, row);
	}

	// column check
	for(int col=0; col<size; col++){
		col_check(matrix, col);
	}

	// diagonal
	for(int x=0; x<2; x++){
		x_check(matrix, x);
	}
}
コード例 #2
0
ファイル: TETRIS.c プロジェクト: jsburke/Tetris
int main(int argc, char* argv[])
{

	// SDL Event handler
	SDL_Event    event;
	int key_press;

	int game_over, score;
	int game_grid[CELLS] = {0};
	int row_test, last_row, drop_count, lock_in, key_count;
	int first_hold = 1;

	tetromino * falling;
	tetromino * next;
	tetromino * hold;
	tetromino * temp;

	set_rand();
	lock_in = 0;
	drop_count = 1;
	game_over = 0;
	key_count = 0;
	score     = 0;

	falling = tetromino_new(ACTIVE);
	next    = tetromino_new(INACTIVE);
	hold    = tetromino_new(INACTIVE);
	temp    = NULL;
	
	//log = fopen("Log.txt","w");
	//fprintf(log, "Begin Main\n");

	if(init() == 0)
	{//printf(log, "SDL did not initialize.\n" );
		return 0;
	}

	if(load_media() == 0)
	{//printf(log, "Media Loading failed\n" );
		return 0;
	}


	gridRect.x = 0;
	gridRect.y = 0;

	fallingRect.x = 0;
	fallingRect.y = 0;

	prevRect.x = (BLOCK_SIZE*MAXCOL) + PREVIEW_OFFSET;
	prevRect.y = 100;

	scoreRect.x = SCORE_X;
	scoreRect.y = SCORE_Y;

//	Preview = PreviewSurfaces[SQUARE_BLOCK]; // set a dummy default

	//anim = 0;
	//fprintf(log, "Game Loop entry\n" );

	while(game_over == 0)
	{
		/*if(anim ==  0)
			tetromino_drop(falling, game_grid);

		anim = (anim == 200)? 0: anim+1;*/

		drop_count = auto_drop(falling, game_grid, drop_count, score);

		key_press = 100;

		if(SDL_PollEvent(&event) != 0)
		{
			switch(event.type)
			{
				case SDL_QUIT:
					game_over = 1;
				break;

				case SDL_KEYDOWN:
					switch(event.key.keysym.sym)
					{
						case SDLK_UP:
							key_press = UP;
						break;

						case SDLK_DOWN:
							key_press = DOWN;
						break;

						case SDLK_LEFT:
							key_press = LEFT;
						break;

						case SDLK_RIGHT:
							key_press = RIGHT;
						break;

						case SDLK_ESCAPE:
							key_press = ESC;
						break;
						
						case SDLK_SPACE:
							key_press = SPACE;
						break;
					}
				break;
			}
		}

		//if(key_count == 0)
		//{
			switch(key_press)
				{
					case UP:
						tetromino_rotate(falling, game_grid);
					break;
	
					case DOWN:
						tetromino_drop(falling, game_grid);
					break;
	
					case RIGHT:
						tetromino_shift_right(falling, game_grid);
					break;
	
					case LEFT:
						tetromino_shift_left(falling, game_grid);
					break;
	
					case ESC:
					game_over = 1;
					break;
					
					case SPACE: // this still would be nice with visuals...
						if(first_hold == 1)
						{
							temp = hold;
							tetromino_mod(temp);
							hold = falling;
							falling = next;
							tetromino_activate(falling);
							next = temp;
							temp = NULL;
							first_hold = 0;
						}
						else
						{
							temp = falling;
							falling = hold;
							tetromino_activate(falling);
							hold = temp;
							temp = NULL;
						}
					break;
			}
		//}

		key_count = (key_count < KEY_RATE) ? key_count + 1: 0;

		lock_in = (tetromino_lock(falling, game_grid) == 1)? lock_in+1:0;
		
		if(lock_in == 200)
		{
			tetromino_into_grid(falling, game_grid);
			score += falling->shape + 2 * 3;
			score += row_check(game_grid);
			// manage the tetrominos
			temp = falling;
			falling = next;
			tetromino_activate(falling);
			tetromino_mod(temp);
			next = temp;
			temp = NULL;
			lock_in = 0;
		}

		if(grid_row_any(game_grid,0) == 1)
			game_over = 1;

		//blit to screen
		SDL_FillRect(ScreenSurface, 0, 0); // so the images don't trail
		blit_preview(next->shape);	
		blit_grid(game_grid);
		blit_tetromino(falling);
		blit_score(score);
		SDL_UpdateWindowSurface(MainWin);
	}

	game_close();
	free(falling);
	free(next);
	free(hold);
	return 0;
}