Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions hangman.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

void choice1(char man[], int manSize, char blanks[], int answerSize, char answer[]){
char guess[answerSize];
printf("\n%s", man);
printf("\n%s", blanks);
printf("\nEnter your guess: ");
scanf("%s", &guess);
for(int i = 0; i<strlen(guess); i++)
guess[i]=tolower(guess[i]);
if(strcmp(guess, answer)==0){
printf("\nYOU WON!!!!!!!!\n");
}
else{
printf("\nSORRY YOU LOSE!\n");
printf("The correct word was %s.\n", answer);
}
}

int spotOfLetter(char answer[], int answerLength, char guess){
for(int j=0;j<answerLength; j++)
if(guess==answer[j])
return j;
return -1;
}/*returns spot in answer where letter is or -1 if letter is not in answer*/

int main(){
char man[100] = {'*', '*', '*', '*', '*', '|', '\n', '|',' ', ' ', ' ', ' ', /*12*/'O', '\n', '|', ' ', ' ', ' ', /*18*/ '\\',/*19*/ '|',/*20*/ '/', '\n', '|', ' ', ' ' ,' ' ,/*26*/ '/', ' ',/*28*/ '\\'};
/*creates man figure marks spots in array that are part of the man*/
int locationInMan=0;
int manCharSpots[] = {12, 18, 19, 20, 26, 28};/*locations of body parts*/
printf("\n%s\n", man);
printf("The goal of the game is to try to guess the word chosen\nby the computer. You may choose to either guess a single letter \nat a time or guess the whole word. However, BEWARE, if you choose\nto guess the whole word and guess wrong, you automatically lose.\nGOOD LUCK :)\n");
/*gives directions*/

char words[][100] = {"benchmark", "blacksmith", "background", "aftershock", "clipboard", "complain", "consumable", "decorate", "smart", "discourage"};
/*creates array of possible answers*/
srand(time(NULL));
int r = rand();
r = r%10;
char answer[100];
strcpy(answer,words[r]);/*puts the answer into 'answer'*/

printf("\n%s", answer);/*for TESTING*/

char blanks[100];
int k;
for(k = 0; k<strlen(answer); k++)
blanks[k]='_';
blanks[k]='\0';
/*creates string made of blanks*/

while(1){
int lOrW=2;
char guess[100];
printf("\nIf you would like to guess the whole word,");
printf(" enter 1.\nIf you would like to only guess a ");
printf("letter, enter 0.\n");

scanf("%d", &lOrW);/*gets input for letter or word choice*/
printf("\nchoice = %d", lOrW);

while(lOrW!=0 && lOrW!=1){
printf("\nInvalid option!\nEnter a valid choice: ");
while(getchar()!='\n');/*clears input buffer*/
scanf("%d", &lOrW);
}
if(lOrW==1){
choice1(man, strlen(man), blanks, strlen(blanks), answer);
/*takes care of whole word choice*/
break;
}
if(lOrW==0){
printf("\n\nYou chose to enter one letter!");
printf("\n\n%s\n\n",man);/*prints man*/
printf("\n\n%s\n\n", blanks);/*prints blanks*/
while(getchar()!='\n');/*clears input buffer*/
printf("\nEnter your guess: ");
char l = getchar();
int spot = spotOfLetter(answer, strlen(answer), l);
if(spot<0){
if(locationInMan>4){
printf("\n\n%s\n\n", man);
printf("\nSorry you lose!\n");
printf("\nThe correct answer was %s\n", answer);
break;
}
printf("\n\nSorry, you guessed wrong!\n");
man[manCharSpots[locationInMan]]=' ';/*deletes character in man*/
locationInMan++;
printf("\n\n%s\n\n", man);
}
if(spot>=0){
printf("\n\nYou guessed right!");
blanks[spot]=answer[spot];
printf("\n\n%s\n\n", blanks);
}
if(blanks[strlen(blanks)-1]!='_'){
printf("\nCongratulations, you won!");
printf("\nThe correct word was %s\n", answer);
break;
}
}
}
return 0;
}
110 changes: 110 additions & 0 deletions minesweeper.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


int main(){
int dim1, dim2;
float prob;

char line[100];
while(1){
printf("Enter first dimension: ");
fgets(line, sizeof(line), stdin);
if(sscanf(line, "%d", &dim1))
break;
printf("\nYou did not enter a valid dimension.\n");
}/*checks for invalid input*/
while(1){
printf("Enter second dimension: ");
fgets(line, sizeof(line), stdin);
if(sscanf(line, "%d", &dim2))
break;
printf("\nYou did not enter a valid dimension.\n");
}/*checks for invalid input*/

while(1){
printf("Enter probability of mines: ");
fgets(line, sizeof(line), stdin);
if(sscanf(line, "%f", &prob)&&prob>0 && prob<1)
break;
printf("\nYou did not enter a valid probability.\n");
}/*checks for invalid input*/
prob*=10;/*makes prob a value between 1 and 9*/

srand(time(NULL)); /*initializes generator*/

int board[dim1][dim2];

for(int r=0; r<dim1; r++){
for(int c=0; c<dim2; c++){
int r = rand();
r%=10;
}
}/*puts values between 0-9 in board*/

char printBoard[dim1+2][dim2+2];/*creates printable board*/

for(int r=1; r<dim1+1; r++){
for(int c=1; c<dim2+1; c++){
if(prob>board[r][c]){
printBoard[r][c] = ' ';
}
else printBoard[r][c] = '*';
}
}/*fills in printableBoard*/

int count = 0;
for(int r=0; r<dim1+2; r++){
for(int c=0; c<dim2+2; c++){
if(printBoard[r][c]!='*'){
if(printBoard[r+1][c]=='*')
count++;
if(printBoard[r-1][c]=='*')
count++;
if(printBoard[r+1][c+1]=='*')
count++;
if(printBoard[r+1][c-1]=='*')
count++;
if(printBoard[r][c+1]=='*')
count++;
if(printBoard[r][c-1]=='*')
count++;
if(printBoard[r-1][c-1]=='*')
count++;
if(printBoard[r-1][c+1]=='*')
count++;

board[r][c]=count;
printBoard[r][c]=count + '0';
count=0;
}
}
}/*puts numbers into non-mine spaces*/

char userBoard[dim1][dim2];
for(int r =0; r<dim1; r++){
for(int c = 0; c<dim2; c++){
userBoard[r][c]= printBoard[r+1][c+1];
}
}/*sets board dimensions back to regular*/

for(int r = 0; r<dim1; r++){
for(int c = 0; c<dim2; c++){
if(userBoard[r][c]!='*'){
char temp[100] = "_";
strcat(temp, userBoard[r][c]);
printf("\n\n%c", userBoard[r][c]);
}
}
}

printf("\n");

for(int r=0; r<dim1; r++){
for(int c=0; c<dim2; c++)
printf("%c", userBoard[r][c]);
printf("\n");
}
return 0;
}