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
256 changes: 256 additions & 0 deletions hangman.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
/* Jared Wasserman -- hangman.c */

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>

int memberq(char guessedLetters[],int size,char character){
int guessed=0;
int x;
for(x=0;x<size;x++){
if(character==guessedLetters[x]){
guessed=1;
break;
}
}
return guessed;
}

int correct(char word[],char guessedLetters[]){
int k;
int correct=0;
for(k=0;k<strlen(word);k++){
if(word[k]!=' '){
if(!memberq(guessedLetters,26,word[k])){
correct++;
}
}
}
if(correct>0){
return 1;
}else{
return 0;
}
}

int print(char word[],char guessedLetters[26],int numOfGuess,int incorrectGuesses){
int i;
printf("\n");
for(i=0;i<strlen(word);i++){
int guessed=0;
int x;
for(x=0;x<26;x++){
if(word[i]==guessedLetters[x]){
guessed=1;
break;
}
}

if(guessed){
printf("%c",word[i]);
}else if(word[i]==' '){
printf(" ");
}else{
printf("_");
}
}
printf("\n\n");
printf("Guessed Leters: \n");
for(i=0;i<numOfGuess;i++){
printf("%c ",guessedLetters[i]);
}
printf("\n\n");
switch(incorrectGuesses){
case 0:
printf(" +===+\n");
printf(" | |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf("=======\n");
break;

case 1:
printf(" +===+\n");
printf(" | |\n");
printf(" O |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf("=======\n");
break;

case 2:
printf(" +===+\n");
printf(" | |\n");
printf(" O |\n");
printf(" | |\n");
printf(" |\n");
printf(" |\n");
printf("=======\n");
break;

case 3:
printf(" +===+\n");
printf(" | |\n");
printf(" O |\n");
printf(" /| |\n");
printf(" |\n");
printf(" |\n");
printf("=======\n");
break;

case 4:
printf(" +===+\n");
printf(" | |\n");
printf(" O |\n");
printf(" /|\\ |\n");
printf(" |\n");
printf(" |\n");
printf("=======\n");
break;

case 5:
printf(" +===+\n");
printf(" | |\n");
printf(" O |\n");
printf(" /|\\ |\n");
printf(" / |\n");
printf(" |\n");
printf("=======\n");
break;

case 6:
printf(" +===+\n");
printf(" | |\n");
printf(" O |\n");
printf(" /|\\ |\n");
printf(" / \\ |\n");
printf(" |\n");
printf("=======\n");
break;

case 7:
printf(" +===+\n");
printf(" | |\n");
printf(" O |\n");
printf(" /|\\ |\n");
printf(" / \\ |\n");
printf(" |\n");
printf("=======\n");
break;

}
return 0;
}

char input(char guessedLetters[26]){
char wordOrLetter;
while(1){
printf("Would you like to guess a letter or a word?(l/w): ");
wordOrLetter = getchar();
if(isalpha(wordOrLetter)){
if(wordOrLetter=='w'||wordOrLetter=='W'){
break;
}else if(wordOrLetter=='l'||wordOrLetter=='L'){
break;
}
}
printf("Invalid input. ");
while(getchar()!='\n');
}
wordOrLetter=tolower(wordOrLetter);
char c;
if(wordOrLetter=='w'){
return '#';
}else{
while(getchar()!='\n');
while(1){
printf("Enter your guess: ");
c = getchar();
if(isalpha(c)){
if(memberq(guessedLetters,26,tolower(c))){
printf("You already guessed that character. ");
while(getchar()!='\n');
}else{
break;
}
}else{
printf("Invalid input. ");
while(getchar()!='\n');
}
}
}
return c;
}


int main(){

char words[][20]={"sky","apple","friday","hello","hello world","hangman","bob","word","other","test"};
int incorrectGuesses=0;
char guessedLetters[26];
int numOfGuess=0;
srand(time(NULL));

char word[20]="";
strcat(word,words[(rand()%10)]);

printf("\nWelcome To Hangman!\n");
printf("\nYou are trying to guess: \n");
int i;
for(i=0;i<strlen(word);i++){
if(word[i]==' '){
printf(" ");
}else{
printf("_");
}
}
printf("\n\n");

while(1){
if(incorrectGuesses!=6&&correct(word,guessedLetters)){
char userInput = input(guessedLetters);
char wordGuess[100];

if(userInput=='#'){
printf("Please enter your guess for the word: ");
while(getchar()!='\n');
fgets(wordGuess,sizeof(wordGuess),stdin);
wordGuess[strlen(wordGuess)-1]='\0';
int j;
for(j=0;j<=strlen(wordGuess);j++){
wordGuess[j] = tolower(wordGuess[j]);
}
if(strcmp(wordGuess,word)==0){
printf("\nCongrats! You guessed the word correctly!\n\n");
break;
}else{
printf("\nSorry! You lose. You guessed %s. The word was %s.\n\n",wordGuess,word);
break;
}
}else{
if(memberq(word,(sizeof(word)/sizeof(word[0])),tolower(userInput))){
}else{
incorrectGuesses++;
}
guessedLetters[numOfGuess]=tolower(userInput);
numOfGuess++;
print(word,guessedLetters,numOfGuess,incorrectGuesses);
}
while(getchar()!='\n');
printf("\n");
}else if(incorrectGuesses==6){
printf("Sorry! You lose. You guessed ran out of guesses. The word was %s.\n\n",word);
break;
}else{
printf("Congrats! You guessed the word correctly!\n\n");
break;
}
}

return 0;
}
92 changes: 92 additions & 0 deletions minesweeper.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* Jared Wasserman -- minesweeper.c */
/*This program takes the dimensions of a grid from the user and a probalility for a mine to be in any given square. Then in returns the board for the end of the game.*/
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main(){
srand(time(NULL));
int m;
int n;
float p;

/*Getting And Checking Input*/
int returnValue;
do{
printf("Enter m dimension: ");
returnValue = scanf("%d",&m);
if(returnValue==0){
printf("Invalid Input. ");
while(getchar()!='\n');
}
}while(returnValue==0);
while(getchar()!='\n');
do{
printf("Enter n dimension: ");
returnValue = scanf("%d",&n);
if(returnValue==0){
printf("Invalid Input. ");
while(getchar()!='\n');
}
}while(returnValue==0);
while(getchar()!='\n');
do{
printf("Enter probalility (between 0 & 1): ");
returnValue = scanf("%f",&p);
if(returnValue==0||p<=0||p>=1){
printf("Invalid Input. ");
while(getchar()!='\n');
}
}while(returnValue==0||p<=0||p>=1);

int p2=(100*p);
int arr[m][n];
int x;
int y;

/*Placing The Mines*/
for(x=0;x<m;x++){
for(y=0;y<n;y++){
if((rand()%101)<=p2){
arr[x][y]=-1;
}else{
arr[x][y]=0;
}
}
}

/*Counting The Number Of Mines*/
for(x=0;x<m;x++){
for(y=0;y<n;y++){
if(arr[x][y]!=-1){
int count=0;
int sur[8][2]={{x,y-1},{x,y+1},{x+1,y-1},{x+1,y},{x+1,y+1},{x-1,y-1},{x-1,y},{x-1,y+1}};
int a;
for(a=0;a<8;a++){
if(sur[a][0]<0||sur[a][0]>(m-1)||sur[a][1]<0||sur[a][1]>(n-1)){
/*Out of bounds*/
}else{
if(arr[sur[a][0]][sur[a][1]]==-1){
count++;
}
}
}
arr[x][y]=count;
}
}
}

/*Printing The Board*/
for(x=0;x<m;x++){
for(y=0;y<n;y++){
if(arr[x][y]==-1){
printf(" *");
}else{
printf(" %d",arr[x][y]);
}
}
printf("\n");
}

return 0;
}