Beispiel #1
0
Datei: life.c Projekt: zellcht/c
int main(int argc, char *argv[]){
  int x, y, i, j;
  FILE *file;
  char iniBoard[MAX_SIZE][MAX_SIZE];
  int status[MAX_SIZE][MAX_SIZE];
  SDL_Simplewin sw;
  SDL_Rect rectangle;

  file = NULL;
  Neill_SDL_Init(&sw);
  initial(iniBoard, status);
  file = fopen(argv[1], "r");
  /* fopen returns NULL pointer on failure */
  if (file == NULL){
    printf("Could not open file. \n");
  }
  else {
    printf("File (%s) opened. \n", argv[1]);
    fscanf(file, "%d %d", &x, &y);
    printf("Row: %d Column: %d \n", x, y);
    char board[y][x];
    if(fgetc(file) != EOF){
      for(j = 0; j < y; j++){
	for(i = 0; i < x + 1; i++){
	  board[j][i] = fgetc(file);
	}
      }
    }
    /* Closing file */
    fclose(file);
    /* Input the board which read from the txt file to a 50x50 board*/
    inputBoard(x, y, board, iniBoard);
    do{
      /* Sleep for a short time */
      SDL_Delay(MILLISECONDDELAY);
      SDL_RenderClear(sw.renderer);   
      /* Draw the actual board */
      drawBoard(iniBoard, sw, rectangle, x, y);      
      /* Update window */
      SDL_RenderPresent(sw.renderer);
      SDL_UpdateWindowSurface(sw.win); 
      /* Get the neighbours' status */
      getNeighbours(x, y, iniBoard, status);
      /* Calculate next generation */
      nextGen(x, y, iniBoard, status);
      Neill_SDL_Events(&sw);
    }while(!sw.finished);
    /* Clear up graphics subsystems */
    atexit(SDL_Quit);
  }
  return 0;
}
/* Display the maze in an SDL GUI */
void display_SDL(max_maze array, maze_dimensions dims){
	
	// Fit the maze to the window height by scaling each cell's dimensions
	int rectsize = WHEIGHT / dims.height; 
	int window_offset = (WWIDTH-WHEIGHT)/2;
	int cx=0, cy =0;
	
	SDL_Simplewin win;
	SDL_Rect rectangle;
	rectangle.w = rectsize;
	rectangle.h = rectsize;
	Neill_SDL_Init(&win);

	do {
		// Draw the array graphically.
		for (int i = 0; i < dims.height; ++i) {
			for (int j = 0; j < dims.width; ++j) {

				if ( array[i][j] == '#' ) {
					Neill_SDL_SetDrawColour(&win,255,255,255); // Set colour to WHITE
					
					// Draw rectangle for each array cell, centred in the window
					rectangle.x = (j * rectsize) + window_offset ;
					rectangle.y = i * rectsize;
					SDL_RenderFillRect(win.renderer, &rectangle);
				} 
				if ( array[i][j] == '.') {	
					Neill_SDL_SetDrawColour(&win,255,128,0); // Set colour to ORANGE

					cx = (j*rectsize) + window_offset + (rectsize/2);
					cy = (i*rectsize) + (rectsize/2);
					Neill_SDL_RenderFillCircle(win.renderer, cx, cy, RADIUS);
				}
			}
		}

		// Update window.
		SDL_RenderPresent(win.renderer);
		SDL_UpdateWindowSurface(win.win);
		Neill_SDL_Events(&win); 		// Check for user kill

		SDL_Delay(MILLISECONDDELAY); 	// Wait a short time

	} while(!win.finished);

	// Cleans up.
	atexit(SDL_Quit);
}
int main(void)
{

   SDL_Simplewin sw;
   SDL_Rect rectangle;
   rectangle.w = RECTSIZE;
   rectangle.h = RECTSIZE;

   Neill_SDL_Init(&sw);

   do{

      // Sleep for a short time
      SDL_Delay(MILLISECONDDELAY);

      // Choose a random colour, a mixture of red, green and blue.
      Neill_SDL_SetDrawColour(&sw,
                             rand()%SDL_8BITCOLOUR, rand()%SDL_8BITCOLOUR,
                             rand()%SDL_8BITCOLOUR);

      // Filled Rectangle, fixed size, random position
      rectangle.x = rand()%(WWIDTH-RECTSIZE);
      rectangle.y = rand()%(WHEIGHT-RECTSIZE);
      SDL_RenderFillRect(sw.renderer, &rectangle);

      // Unfilled Rectangle, fixed size, random position
      rectangle.x = rand()%(WWIDTH-RECTSIZE);
      rectangle.y = rand()%(WHEIGHT-RECTSIZE);
      SDL_RenderDrawRect(sw.renderer, &rectangle);

      // Update window - no graphics appear on some devices until this is finished
      SDL_RenderPresent(sw.renderer);
      SDL_UpdateWindowSurface(sw.win); 
      
      // Has anyone pressed ESC or killed the SDL window ?
      // Must be called frequently - it's the only way of escaping 
      Neill_SDL_Events(&sw);

   }while(!sw.finished);


   // Clear up graphics subsystems
   atexit(SDL_Quit);

   return 0;

}
Beispiel #4
0
int main(int argc, char *argv[])
{
	board_struct player_board;
	board_struct *final_board = NULL;

	full_empty hashing_array[VERY_LARGE_NUMBER] = {empty};

	success_failure success_index = unsuccessful;
	int row_coordinate, col_coordinate;
	clock_t start = clock();
	
	weighting_board pagoda_board;

	SDL_Simplewin sw;

	check_unsigned_long_long();

	check_input_arguments(argc, argv, &row_coordinate, &col_coordinate, &success_index);

	initialise_player_board(&player_board);

	initialise_pagoda_board(pagoda_board, row_coordinate, col_coordinate);

	if(success_index != successful){
		final_board = make_a_move(row_coordinate, col_coordinate, &player_board, &player_board, &success_index, hashing_array, pagoda_board);
	}
	else{
		final_board = &player_board;
	}

	print_time_taken(start);

	Neill_SDL_Init(&sw);

	if(success_index == successful || success_index == symmetric_successful){
		print_successful_boards(&player_board, &sw, row_coordinate, col_coordinate, success_index);
	}

	free_all_boards(final_board);

	atexit(SDL_Quit);

	return(0);
}