int main(int argc, char* argv[]) {
	// set up board
	PositionType board[BoardSize][BoardSize];
	int numberOfBombs = atoi(argv[1]);
	setBoard(board, numberOfBombs);

	// game loop
	int gameState, row, column;
	do {
		// ask for guess
		printf("Your move?: \n");
		scanf("%d %d", &row, &column);
		printf("\n");

		// process move
		gameState = processGuess(board, row - 1, column - 1);
		if (gameState == -1) {
			printf("Invalid move!\n\n");
		} else if (gameState == 0) {
			printf("You've already played there!\n\n");
		}

		// show the board
		displayBoard(board);
		printf("\n");
	} while (gameState != 2 && hasWon(board) == 0);

	// game over, check for win or lose
	if (gameState != 2) {
		printf("YOU WIN!\n\n");
	} else {
		printf("YOU LOSE!\n\n");
	}

	return 0;
}
int main(int argc, char* argv[]){

	
	if(argc==1){
		printf("Incorrect number of command line arguments\n");
		printf("Correct usage: ./a.out <number of bombs>\n");
		return -1;	
	}
	PositionType board[BoardSize][BoardSize];
	int numberOfBomb=atoi(argv[1]);
	int totalNumberOfSafePlace=BoardSize*BoardSize-numberOfBomb;
	
	setBoard(board,numberOfBomb);
	displayBoard(board);
	printf("Game begins, there are total %d grids, and there are %d bombs there, %d total safe grids.\n", BoardSize*BoardSize, numberOfBomb, totalNumberOfSafePlace);
	int row;
	int col;
	printf("Enter a row and col:\n");
	scanf("%d %d",&row,&col);
	
	int result;

	while(1){
		
		result=processGuess( board, row , col );
		
		
		switch(result){
				case -1:
					printf("Invalid position.\n");
					
					break;
				case 0:
					printf("That position has already been picked.\n");
					
					break;				
				case 1:
					totalNumberOfSafePlace--;
					break;
				case 2:
					printf("Game over, you lose\n");
					displayBoard(board);
					return -1;
				
		}
		
		displayBoard(board);
		if(totalNumberOfSafePlace==0){
			break;
		}
		printf("Total Number Of Safe Place Left: %d\n",totalNumberOfSafePlace);
		printf("Enter a row and col:\n");
		scanf("%d %d",&row,&col);
		

	}

	
	printf("You win!\n");
	

	return 0;

}
Example #3
0
//Main code running the game
int main(int argc, char* argv[])
{
	//Make sure coreect number of command line arguments
	if(argc != 2)
	{
		printf("Incorrect number of command line arguemnts");
		return 0;
	}
	
	//Assign argument to an int
	int numBomb = atoi(argv[1]);
	
	//Create the board
	PositionType board[BoardSize][BoardSize];
	setBoard(board, numBomb);
	

	//Play Game
	int run = 1;
	int notBomb = BoardSize * BoardSize - numBomb;
	while(run == 1)
	{
		displayBoard(board);

		//Ask for a guess
		int row, col;
		printf("\n\nPlease select a row: ");
		scanf("%d", &row);
		printf("\nPlease select a column: ");
		scanf("%d", &col);
		//Fix offset
		row = row-1;
		col = col-1;

		//process guess according to rules defined in processGuess function and change spots on board accordingly
		switch (processGuess(board, row, col))
		{
			case 0:
				printf("\nSPOT WAS ALREADY UNCOVERED\n");
				break;
			case 1:
				board[row][col] = uncovered;
				notBomb--;
				break;
			case 2:
				board[row][col] = uncoveredBomb;
				displayBoard(board);
				printf("\n\nBOMB FOUND\nGAME OVER");
				run = 0;
				break;
			default:
				printf("\nINVALID SELECTION\n");
				break;
		}
		
		//Check win condition
		if(notBomb == 0)
		{
			printf("\nALL BOMBS CLEARED\nYOU WIN");
			run = 0;
		}

	}
}