Esempio n. 1
0
File: main.c Progetto: iruel01/2048
int main(int argc, char* argv[]) {
	struct board board;
	char input[1024];
	int status; // Game status.
	int valid;

	// Print legal shenanigains.
	printf("\t2048 (implemented in C)  Copyright (C) 2014  Wade T. Cline\n"
	       "\tThis program comes with ABSOLUTELY NO WARRANTY. This is\n"
	       "\tfree software, and you are welcome to redistribute it\n"
	       "\tunder certain conditions. See the file 'COPYING' in the\n"
	       "\tsource code for details.\n\n");

	// Set up board.
	board_init(&board);
	
	// Play the game.
	while (!(status = board_done(&board))) {
		// Print the board.
		board_print(&board);

		// Get the player's move.
		valid = 0;
		memset((void*)input, 0, sizeof(input));
		write(STDOUT_FILENO, (void*)"> ", 3);
		if (read(STDIN_FILENO, (void*)input, sizeof(input) - 1)
		    == -1) {
			perror("Error reading input");
			break;
		}
		input[strlen(input) - 1] = 0;
		if (!strcmp(input, "u") || !strcmp(input, "up")) {
			valid = board_move_up(&board);
		} else if (!strcmp(input, "d") || !strcmp(input, "down")) {
			valid = board_move_down(&board);
		} else if (!strcmp(input, "l") || !strcmp(input, "left")) {
			valid = board_move_left(&board);
		} else if (!strcmp(input, "r") || !strcmp(input, "right")) {
			valid = board_move_right(&board);
		} else {
			printf("Don't understand input: %s.\n", input);
			continue;
		}

		// Prepare for user's next move.
		if (valid) {
			board_plop(&board);
		} else {
			printf("Invalid move.\n");
		}
	}
	
	// Print the final board.
	printf("Game over, you %s!", (status < 0) ? "LOSE" : "WIN");
	board_print(&board);

	// Return success.
	return EXIT_SUCCESS;
}
Esempio n. 2
0
File: main.c Progetto: Octoate/2048
main() {
	struct board board;
	unsigned char input;
	int status; // Game status.
	int valid;
	unsigned char idx;
	
	initGraphics();
	
	//PutSpriteMode0((void*)boardnumber[2], 0, 0, BOARDNUMBER_WIDTH, BOARDNUMBER_HEIGHT);
	
	//for (idx = 0; idx < 10; idx++)
	//{
		//PutSpriteMode0((void*)number[idx], idx * NUMBER_WIDTH, BOARDNUMBER_HEIGHT + 1, NUMBER_WIDTH, NUMBER_HEIGHT);
	//}
	//GetChar_CPC();
	
	// Print legal shenanigains.
	//printf("\t2048 (implemented in C)  Copyright (C) 2014  Wade T. Cline\r\n"
	//       "\tThis program comes with ABSOLUTELY NO WARRANTY. This is\r\n"
	//       "\tfree software, and you are welcome to redistribute it\r\n"
	//       "\tunder certain conditions. See the file 'COPYING' in the\r\n"
	//       "\tsource code for details.\r\n\r\n");

	// Set up board.
	board_init(&board);
	
	
	// Play the game.
	while (!(status = board_done(&board))) {
		// Print the board.
		//printf("Score: %u \r\n", score);
		board_print(&board);

		// Get the player's move.
		valid = 0;
		input = GetChar_CPC();
		
		switch (input)
		{
			case 0x0B:	//Joystick up
			case 0x51:	//Q
			case 0x71:	//q
			case 0xF0:	//Up
				valid = board_move_up(&board);
				break;
			
			case 0x0A:	//Joystick down
			case 0x41:	//A
			case 0x61:	//a
			case 0xF1:	//Down
				valid = board_move_down(&board);
				break;
				
			case 0x08:	//Joystick left
			case 0x4F:	//O
			case 0x6F:	//o
			case 0xF2:	//Left
				valid = board_move_left(&board);
				break;
				
			case 0xFD:	//Joystick right
			case 0x50:	//P
			case 0x70:	//p
			case 0xF3:	//Right
				valid = board_move_right(&board);
				break;
			default:
				//printf("Don't understand input: 0x%x.\r\n", input);
				continue;
		}

		// Prepare for user's next move.
		if (valid) {
			board_plop(&board);
		} else {
			//printf("Invalid move.\r\n");
		}
	}
	
	// Print the final board.
	//printf("Game over, you %s!\r\n", (status < 0) ? "LOSE" : "WIN");
	board_print(&board);

	// Return success.
	return 0;
}
Esempio n. 3
0
File: main.c Progetto: clinew/2048
int main(int argc, char* argv[]) {
	struct arguments arguments;
	struct board board;
	char buffer[256];
	int format;
	char input;
	char* message;
	int status; // Game status.
	struct termios term_settings;
	int valid;

	// Parse arguments.
	message = arguments_parse(&arguments, argc, argv);
	if (message) {
		usage_print(message);
	}

	// Apply arguments.
	valid = 1; // Hack; overload to determine whether to play or quit.
	if (arguments.flags & ARGUMENTS_VERSION) {
		printf("%s\n", VERSION);
		valid = 0;
	}
	if (arguments.flags & ARGUMENTS_LEGAL) {
		printf("%s\n", legal);
		valid = 0;
	}
	if (arguments.flags & ARGUMENTS_HELP) {
		usage_print(NULL);
	}
	if (!valid) {
		exit(EXIT_SUCCESS);
	}
	if (arguments.flags & ARGUMENTS_MODE) {
		if (arguments.mode == mode_format) {
			setup_signal_handlers();
			enter_alternate_buffer();
			enter_format_mode(&term_settings);
			format = 1;
		} else if (arguments.mode == mode_plain) {
			format = 0;
		}
	} else if (isatty(STDOUT_FILENO) && isatty(STDIN_FILENO)) {
		setup_signal_handlers();
		enter_alternate_buffer();
		enter_format_mode(&term_settings);
		format = 1;
	}
	if (arguments.flags & ARGUMENTS_SEED) {
		srand(arguments.seed);
	} else {
		srand(time(NULL));
	}

	// Set up the board.
	board_init(&board);
	if (!format) {
		fputs(legal, stdout);
	}

	// Play the game.
play:
	valid = 1;
	while (!(status = board_done(&board))) {
		// Set up screen for next move.
		// Sorry about this ugly call.
		screen_print(&board, valid ? (format ? "\n\n" : "") :
			"\nInvalid move.\n", format);

		// Get the player's move.
		input = yoink(format);

		// Process player's move.
		if (input == 'w' || input == 'k') {
			valid = board_move_up(&board);
		} else if (input == 's' || input == 'j') {
			valid = board_move_down(&board);
		} else if (input == 'a' || input == 'h') {
			valid = board_move_left(&board);
		} else if (input == 'd' || input == 'l') {
			valid = board_move_right(&board);
		} else if (input == 'n') {
			// Start a new game (or not) based on user input.
			printf("Start a new game? [y/N] ");
			input = yoink(format);
			if (input == 'y' || input == 'Y') {
				board_reset(&board);
			}
			continue;
		} else if (input == '?') {
			help_print();
			if (format) {
				printf("\nPress any key to continue.");
				input = yoink(format);
			}
			continue;
		} else {
			valid = 0;
		}

		// End player's move.
		if (valid) {
			board_plop(&board);
		}
	}

	// Print the final board.
	snprintf(buffer, sizeof(buffer),
		"\nGame over, you %s!\n\nPlay again? [y/n]\n",
		(status < 0) ? "LOSE" : "WIN");
	screen_print(&board, buffer, format);

	// Check for new game.
	while ((input = yoink(format)) != 'y' && input != 'Y' &&
		 input != 'n' && input != 'N') {
		 screen_print(&board, buffer, format);
	}
	if (input == 'y' || input == 'Y') {
		board_reset(&board);
		goto play;
	}

	// Restore the terminal.
	if (format) {
		restore_mode();
		leave_alternate_buffer();
	}

	// Free the board.
	board_free(&board);

	// Return success.
	return EXIT_SUCCESS;
}