From c0256786af29c4d0a1c062d2578a0ab3f3eccedd Mon Sep 17 00:00:00 2001 From: elberdev Date: Fri, 19 Jun 2015 21:43:52 -0400 Subject: [PATCH 1/6] first commit --- README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..17ca639 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# blah From 8c363ac881e892e6197f7bf7c81fbc662db7643b Mon Sep 17 00:00:00 2001 From: elberdev Date: Fri, 19 Jun 2015 21:45:59 -0400 Subject: [PATCH 2/6] first commit --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 17ca639..9db14e6 100644 --- a/README.md +++ b/README.md @@ -1 +1,2 @@ # blah +# blah From e3906836293ab82250191508b38d58b6aa8380a5 Mon Sep 17 00:00:00 2001 From: elberdev Date: Fri, 19 Jun 2015 21:51:39 -0400 Subject: [PATCH 3/6] uploading files --- .../HangPerson.xcodeproj/project.pbxproj | 76 ++++---- HangPerson/HangPerson/main.m | 169 +++++++++++++++++- 2 files changed, 204 insertions(+), 41 deletions(-) diff --git a/HangPerson/HangPerson.xcodeproj/project.pbxproj b/HangPerson/HangPerson.xcodeproj/project.pbxproj index 03c9452..eb02b4d 100644 --- a/HangPerson/HangPerson.xcodeproj/project.pbxproj +++ b/HangPerson/HangPerson.xcodeproj/project.pbxproj @@ -7,11 +7,11 @@ objects = { /* Begin PBXBuildFile section */ - 8D3685681B2F1C00002A5FA6 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D3685671B2F1C00002A5FA6 /* main.m */; }; + 3BCFA2721B2E09C10015D60B /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 3BCFA2711B2E09C10015D60B /* main.m */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ - 8D3685621B2F1C00002A5FA6 /* CopyFiles */ = { + 3BCFA26C1B2E09C10015D60B /* CopyFiles */ = { isa = PBXCopyFilesBuildPhase; buildActionMask = 2147483647; dstPath = /usr/share/man/man1/; @@ -23,12 +23,12 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ - 8D3685641B2F1C00002A5FA6 /* HangPerson */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = HangPerson; sourceTree = BUILT_PRODUCTS_DIR; }; - 8D3685671B2F1C00002A5FA6 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; + 3BCFA26E1B2E09C10015D60B /* HangPerson */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = HangPerson; sourceTree = BUILT_PRODUCTS_DIR; }; + 3BCFA2711B2E09C10015D60B /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ - 8D3685611B2F1C00002A5FA6 /* Frameworks */ = { + 3BCFA26B1B2E09C10015D60B /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( @@ -38,26 +38,26 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ - 8D36855B1B2F1C00002A5FA6 = { + 3BCFA2651B2E09C10015D60B = { isa = PBXGroup; children = ( - 8D3685661B2F1C00002A5FA6 /* HangPerson */, - 8D3685651B2F1C00002A5FA6 /* Products */, + 3BCFA2701B2E09C10015D60B /* HangPerson */, + 3BCFA26F1B2E09C10015D60B /* Products */, ); sourceTree = ""; }; - 8D3685651B2F1C00002A5FA6 /* Products */ = { + 3BCFA26F1B2E09C10015D60B /* Products */ = { isa = PBXGroup; children = ( - 8D3685641B2F1C00002A5FA6 /* HangPerson */, + 3BCFA26E1B2E09C10015D60B /* HangPerson */, ); name = Products; sourceTree = ""; }; - 8D3685661B2F1C00002A5FA6 /* HangPerson */ = { + 3BCFA2701B2E09C10015D60B /* HangPerson */ = { isa = PBXGroup; children = ( - 8D3685671B2F1C00002A5FA6 /* main.m */, + 3BCFA2711B2E09C10015D60B /* main.m */, ); path = HangPerson; sourceTree = ""; @@ -65,13 +65,13 @@ /* End PBXGroup section */ /* Begin PBXNativeTarget section */ - 8D3685631B2F1C00002A5FA6 /* HangPerson */ = { + 3BCFA26D1B2E09C10015D60B /* HangPerson */ = { isa = PBXNativeTarget; - buildConfigurationList = 8D36856B1B2F1C00002A5FA6 /* Build configuration list for PBXNativeTarget "HangPerson" */; + buildConfigurationList = 3BCFA2751B2E09C10015D60B /* Build configuration list for PBXNativeTarget "HangPerson" */; buildPhases = ( - 8D3685601B2F1C00002A5FA6 /* Sources */, - 8D3685611B2F1C00002A5FA6 /* Frameworks */, - 8D3685621B2F1C00002A5FA6 /* CopyFiles */, + 3BCFA26A1B2E09C10015D60B /* Sources */, + 3BCFA26B1B2E09C10015D60B /* Frameworks */, + 3BCFA26C1B2E09C10015D60B /* CopyFiles */, ); buildRules = ( ); @@ -79,53 +79,53 @@ ); name = HangPerson; productName = HangPerson; - productReference = 8D3685641B2F1C00002A5FA6 /* HangPerson */; + productReference = 3BCFA26E1B2E09C10015D60B /* HangPerson */; productType = "com.apple.product-type.tool"; }; /* End PBXNativeTarget section */ /* Begin PBXProject section */ - 8D36855C1B2F1C00002A5FA6 /* Project object */ = { + 3BCFA2661B2E09C10015D60B /* Project object */ = { isa = PBXProject; attributes = { LastUpgradeCheck = 0630; - ORGANIZATIONNAME = "Mike Kavouras"; + ORGANIZATIONNAME = "Elber Carneiro"; TargetAttributes = { - 8D3685631B2F1C00002A5FA6 = { + 3BCFA26D1B2E09C10015D60B = { CreatedOnToolsVersion = 6.3.2; }; }; }; - buildConfigurationList = 8D36855F1B2F1C00002A5FA6 /* Build configuration list for PBXProject "HangPerson" */; + buildConfigurationList = 3BCFA2691B2E09C10015D60B /* Build configuration list for PBXProject "HangPerson" */; compatibilityVersion = "Xcode 3.2"; developmentRegion = English; hasScannedForEncodings = 0; knownRegions = ( en, ); - mainGroup = 8D36855B1B2F1C00002A5FA6; - productRefGroup = 8D3685651B2F1C00002A5FA6 /* Products */; + mainGroup = 3BCFA2651B2E09C10015D60B; + productRefGroup = 3BCFA26F1B2E09C10015D60B /* Products */; projectDirPath = ""; projectRoot = ""; targets = ( - 8D3685631B2F1C00002A5FA6 /* HangPerson */, + 3BCFA26D1B2E09C10015D60B /* HangPerson */, ); }; /* End PBXProject section */ /* Begin PBXSourcesBuildPhase section */ - 8D3685601B2F1C00002A5FA6 /* Sources */ = { + 3BCFA26A1B2E09C10015D60B /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 8D3685681B2F1C00002A5FA6 /* main.m in Sources */, + 3BCFA2721B2E09C10015D60B /* main.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXSourcesBuildPhase section */ /* Begin XCBuildConfiguration section */ - 8D3685691B2F1C00002A5FA6 /* Debug */ = { + 3BCFA2731B2E09C10015D60B /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; @@ -167,7 +167,7 @@ }; name = Debug; }; - 8D36856A1B2F1C00002A5FA6 /* Release */ = { + 3BCFA2741B2E09C10015D60B /* Release */ = { isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; @@ -202,14 +202,14 @@ }; name = Release; }; - 8D36856C1B2F1C00002A5FA6 /* Debug */ = { + 3BCFA2761B2E09C10015D60B /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { PRODUCT_NAME = "$(TARGET_NAME)"; }; name = Debug; }; - 8D36856D1B2F1C00002A5FA6 /* Release */ = { + 3BCFA2771B2E09C10015D60B /* Release */ = { isa = XCBuildConfiguration; buildSettings = { PRODUCT_NAME = "$(TARGET_NAME)"; @@ -219,24 +219,24 @@ /* End XCBuildConfiguration section */ /* Begin XCConfigurationList section */ - 8D36855F1B2F1C00002A5FA6 /* Build configuration list for PBXProject "HangPerson" */ = { + 3BCFA2691B2E09C10015D60B /* Build configuration list for PBXProject "HangPerson" */ = { isa = XCConfigurationList; buildConfigurations = ( - 8D3685691B2F1C00002A5FA6 /* Debug */, - 8D36856A1B2F1C00002A5FA6 /* Release */, + 3BCFA2731B2E09C10015D60B /* Debug */, + 3BCFA2741B2E09C10015D60B /* Release */, ); defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; - 8D36856B1B2F1C00002A5FA6 /* Build configuration list for PBXNativeTarget "HangPerson" */ = { + 3BCFA2751B2E09C10015D60B /* Build configuration list for PBXNativeTarget "HangPerson" */ = { isa = XCConfigurationList; buildConfigurations = ( - 8D36856C1B2F1C00002A5FA6 /* Debug */, - 8D36856D1B2F1C00002A5FA6 /* Release */, + 3BCFA2761B2E09C10015D60B /* Debug */, + 3BCFA2771B2E09C10015D60B /* Release */, ); defaultConfigurationIsVisible = 0; }; /* End XCConfigurationList section */ }; - rootObject = 8D36855C1B2F1C00002A5FA6 /* Project object */; + rootObject = 3BCFA2661B2E09C10015D60B /* Project object */; } diff --git a/HangPerson/HangPerson/main.m b/HangPerson/HangPerson/main.m index 948dd70..6220d51 100644 --- a/HangPerson/HangPerson/main.m +++ b/HangPerson/HangPerson/main.m @@ -2,17 +2,180 @@ // main.m // HangPerson // -// Created by Michael Kavouras on 6/15/15. -// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// Created by Elber Carneiro on 6/14/15. +// Copyright (c) 2015 Elber Carneiro. All rights reserved. // #import +// This function returns the size of a string array as long as the last +// element in the array is explicitly set to NULL +size_t arraySize(char *wordArray[]) { + size_t count = 0; + while (wordArray[count] != NULL) { + count++; + } + return count; +} + +// This function prints the "title screen" +void titleScreen() { + printf("\n Let's play a game of HANGPERSON!\n"); + usleep(1e6); +} + +// This function sets up the difficulty +int difficultySetup(int maxWrongGuesses) { + while (maxWrongGuesses == 0) { + int difficulty; + printf("\n Choose your difficulty in a scale from 1 to 3,\n"); + printf(" 3 being the most difficult: "); + scanf("%d%*c", &difficulty); + + switch (difficulty) { + case 1: + maxWrongGuesses = 10; + break; + case 2: + maxWrongGuesses = 8; + break; + case 3: + maxWrongGuesses = 6; + break; + default: + printf("\n\n Sorry, you did not input a number from 1 to 3.\n"); + break; + } + } + + return maxWrongGuesses; +} + int main(int argc, const char * argv[]) { @autoreleasepool { + +//***************** OUR VARIABLES ************************************************// + + char *wordPool[] = {"decapitated", "slum", "slush", "fuzzy", "sloppy", + "slept", "quarry", "nozzle", "shank", "quartz", "iterate", "hangman", + "illustrious", "cockiness", "doddering", "huffing", "staff", + "puzzling", "blabbering", "zigzagging", "jazzier", "jagged", + "zine", "fazed", "queuing", "slap", "hangman", "solutions", + "technologies", "catacombs", "inserted", "animal", "carpet", + "lazybones", "molasses", "carnival", "compass", "jelly", + "compact", "renaissance", "fantastic", "killer", "somber", + "animal", "arrow", "sloppy", "super", "superfluous", NULL}; + + // get the length of the wordPool array + size_t wordPoolSize = arraySize(wordPool); + // will hold the player's guesses + char letterGuess; + // will keep track of how many wrong guesses + int wrongGuesses = 0; + // will be used to set difficulty + int maxWrongGuesses = 0; + // keep track of total letters filled in correctly. will let program + // know when the player wins + int correctLetters = 0; - // code goes here... +//********************************************************************************// + + titleScreen(); + maxWrongGuesses = difficultySetup(maxWrongGuesses); + + // initial setup + printf("\n Okay, let's begin!\n\n"); + usleep(1e6); + printf(" You have a maximum of %d wrong guesses\n", maxWrongGuesses); + printf(" before you lose.\n\n"); + usleep(1e6); + printf(" What word am I thinking of?\n"); + + // pick a random index from the wordpool + srand((int) time(NULL)); + int randomWordPoolIndex = rand()%wordPoolSize; + + // temp variable to hold word we pluck out of the word pool + char *temp = wordPool[randomWordPoolIndex]; + + // setup a separate string to hold the display of the word in the game + // this is the size it needs to be in order to have spaces between + // each letter + char board[(strlen(temp) * 2) - 1]; + + // fill board string with alternating uderscores and spaces + for (int i = 0; i < strlen(temp); i++) { + // all indexes that are multiples of 2 will hold underscores + board[i * 2] = '_'; + // everything else in between will hold spaces. the if statement + // ensures there is no space following the last underscore + if (i < strlen(temp) - 1) { + board[i * 2 + 1] = ' '; + } + + } + + // game logic. keeps running as long as you have not used up all your + // guesses and have not guessed all the letters + while (correctLetters < strlen(temp) && wrongGuesses < maxWrongGuesses) { + + // print inital board + printf("\n\n %s", board); + + printf("\n\n\n Guess a letter: "); + scanf("%c%*c", &letterGuess); + + // set up a boolean switch tell the program what to print in + // case of a correct or incorrect guess + BOOL correctGuess = 0; + + // check if the letter is in the word + for (int i = 0; i < strlen(temp); i++) { + if (letterGuess == temp[i]) { + + // if it is, update the display + board[i*2] = letterGuess; + // update conditionals for the while loop + correctLetters++; + correctGuess = 1; + + } + } + + // print different things depending on whether the guess was + // correct or not. remind the player how many guesses they + // already used. + if (correctGuess == 0) { + printf("\n\n Sorry, that's not in the word.\n"); + wrongGuesses++; + if (wrongGuesses == 1) { + printf(" That's %d wrong guess!", wrongGuesses); + } else { + printf(" That's %d wrong guesses!", wrongGuesses); + } + } + } + + // print the board one last time before the end + printf("\n\n %s\n\n\n", board); + + // print different ending message depending on result + if (correctLetters == strlen(temp)) { + printf(" YOU GOT IT!\n\n"); + printf(" HURRAY FOR YOU.\n"); + printf(" A COOL IMAGINARY VICTORY SCREEN APPEARS.\n"); + } else { + printf(" YOU ARE OUT OF GUESSES!\n"); + printf("\n The word was: \n"); + usleep(1e6); + printf(" <<<<< %s >>>>>\n", temp); + printf("\n\n YOU LOST!\n"); + printf(" YOU KILLED AN IMAGINARY STICK FIGURE.\n"); + printf(" GOOD GOING.\n\n"); + } + + } return 0; } From 2907b836af28522ca43c4b81771c932f52a9ba15 Mon Sep 17 00:00:00 2001 From: elberdev Date: Fri, 19 Jun 2015 21:55:35 -0400 Subject: [PATCH 4/6] changed readme --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9db14e6..933cf97 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,5 @@ -# blah -# blah +# hanbperson +# +This is my current working version. I'd like to add more visual jazz later. Also, I began refactoring my code into functions but did not finish, hence, some of the working guts are in separate functions while others are in main. + +Cheers. From 35a23b44bc425f7e4d1449364d0f93614548dc76 Mon Sep 17 00:00:00 2001 From: elberdev Date: Fri, 19 Jun 2015 21:56:26 -0400 Subject: [PATCH 5/6] changed readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 933cf97..28303f6 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# hanbperson +# hangperson # This is my current working version. I'd like to add more visual jazz later. Also, I began refactoring my code into functions but did not finish, hence, some of the working guts are in separate functions while others are in main. From 030b52ae662fbdd0d175b79cf4b21d3c936521b9 Mon Sep 17 00:00:00 2001 From: elberdev Date: Sat, 20 Jun 2015 10:18:45 -0400 Subject: [PATCH 6/6] lots of changes/refactoring --- HangPerson/HangPerson/main.m | 202 +++++++++++++++++++++++++---------- 1 file changed, 144 insertions(+), 58 deletions(-) diff --git a/HangPerson/HangPerson/main.m b/HangPerson/HangPerson/main.m index 6220d51..cc1706e 100644 --- a/HangPerson/HangPerson/main.m +++ b/HangPerson/HangPerson/main.m @@ -24,7 +24,7 @@ void titleScreen() { usleep(1e6); } -// This function sets up the difficulty +// This function sets up the game difficulty int difficultySetup(int maxWrongGuesses) { while (maxWrongGuesses == 0) { int difficulty; @@ -34,13 +34,13 @@ int difficultySetup(int maxWrongGuesses) { switch (difficulty) { case 1: - maxWrongGuesses = 10; + maxWrongGuesses = 12; break; case 2: - maxWrongGuesses = 8; + maxWrongGuesses = 10; break; case 3: - maxWrongGuesses = 6; + maxWrongGuesses = 8; break; default: printf("\n\n Sorry, you did not input a number from 1 to 3.\n"); @@ -48,13 +48,127 @@ int difficultySetup(int maxWrongGuesses) { } } + printf("\n Okay, let's begin!\n\n"); + usleep(1e6); + printf(" You have a maximum of %d wrong guesses\n", maxWrongGuesses); + printf(" before you lose.\n\n"); + usleep(1e6); + return maxWrongGuesses; } +// This function picks a random word from the wordPool +char * wordSetup(size_t wordPoolSize, char *wordPool[]) { + + printf(" What word am I thinking of?\n"); + + // pick a random index from the wordpool + srand((int) time(NULL)); + int randomWordPoolIndex = rand()%wordPoolSize; + + // temp variable to hold word we pluck out of the word pool + char *gameWord = wordPool[randomWordPoolIndex]; + return gameWord; +} + +// This function builds the game board +char * getGameBoard(char * gameWord, size_t gameWordSize) { + // board will be the size of temp with extra spaces between each + // character + int boardSize = ((int) gameWordSize * 2 - 1); + char board[boardSize]; + + // fill board string with alternating uderscores and spaces + for (int i = 0; i < gameWordSize; i++) { + // all indexes that are multiples of 2 will hold underscores + board[i * 2] = '_'; + // everything else in between will hold spaces. the if statement + // ensures there is no space following the last underscore + if (i < gameWordSize - 1) { + board[i * 2 + 1] = ' '; + } else { + board[i * 2 + 1] = '\0'; + } + } + + char * returnBoard = board; + return returnBoard; +} + +//void gameOn(int correctLetters, size_t gameWordSize, int wrongGuesses, +// int maxWrongGuesses, char * board, char letterGuess, +// char * gameWord) { +// // game logic. keeps running as long as you have not used up all your +// // guesses and have not guessed all the letters of the word +// while (correctLetters < gameWordSize && wrongGuesses < maxWrongGuesses) { +// +// // print inital board +// printf("\n\n %s", board); +// +// printf("\n\n\n Guess a letter: "); +// scanf("%c%*c", &letterGuess); +// +// // set up a boolean switch tell the program what to print in +// // case of a correct or incorrect guess +// BOOL correctGuess = 0; +// +// // check if the letter is in the word +// for (int i = 0; i < gameWordSize; i++) { +// if (letterGuess == gameWord[i]) { +// +// // if it is, update the display +// board[i*2] = letterGuess; +// // update conditionals for the while loop +// correctLetters++; +// correctGuess = 1; +// +// } +// } +// +// // print different things depending on whether the guess was +// // correct or not. remind the player how many guesses they +// // already used. +// if (correctGuess == 0) { +// printf("\n\n Sorry, that's not in the word.\n"); +// wrongGuesses++; +// if (wrongGuesses == 1) { +// printf(" That's %d wrong guess!", wrongGuesses); +// } else { +// printf(" That's %d wrong guesses!", wrongGuesses); +// } +// +// } +// } +//} + +// This function prints the end of game results +void endGame(int correctLetters, size_t gameWordSize, char * gameWord) { + + // print different ending message depending on result + if (correctLetters == gameWordSize) { + printf("\n The word was: \n"); + usleep(1e6); + printf("\n <<<<< %s >>>>>\n", gameWord); + usleep(1e6); + printf("\n\n YOU GOT IT!\n\n"); + printf(" HURRAY FOR YOU.\n"); + printf(" A COOL IMAGINARY VICTORY SCREEN APPEARS.\n"); + } else { + printf(" YOU ARE OUT OF GUESSES!\n"); + printf("\n The word was: \n"); + usleep(1e6); + printf("\n <<<<< %s >>>>>\n", gameWord); + usleep(1e6); + printf("\n\n YOU LOST!\n"); + printf(" YOU KILLED AN IMAGINARY STICK FIGURE.\n"); + printf(" GOOD GOING.\n\n"); + } +} + int main(int argc, const char * argv[]) { @autoreleasepool { -//***************** OUR VARIABLES ************************************************// + //********* OUR VARIABLES ************************************************// char *wordPool[] = {"decapitated", "slum", "slush", "fuzzy", "sloppy", "slept", "quarry", "nozzle", "shank", "quartz", "iterate", "hangman", @@ -64,12 +178,19 @@ int main(int argc, const char * argv[]) { "technologies", "catacombs", "inserted", "animal", "carpet", "lazybones", "molasses", "carnival", "compass", "jelly", "compact", "renaissance", "fantastic", "killer", "somber", - "animal", "arrow", "sloppy", "super", "superfluous", NULL}; + "animal", "arrow", "sloppy", "super", "superfluous", "frugal", + "innovator", "serpent", "wisdom", "technique", "breakthrough", + "meditation", "thought", "durable", "succeeded", + "supercalifragilisticexpialadocious", NULL}; // get the length of the wordPool array size_t wordPoolSize = arraySize(wordPool); // will hold the player's guesses char letterGuess; + // will hold the random word we pick and the display board + char *gameWord, *board; + // this value will be used very often for loops + size_t gameWordSize; // will keep track of how many wrong guesses int wrongGuesses = 0; // will be used to set difficulty @@ -78,46 +199,27 @@ int main(int argc, const char * argv[]) { // know when the player wins int correctLetters = 0; -//********************************************************************************// - titleScreen(); - maxWrongGuesses = difficultySetup(maxWrongGuesses); + //********* OUR GAME BEGINS **********************************************// - // initial setup - printf("\n Okay, let's begin!\n\n"); - usleep(1e6); - printf(" You have a maximum of %d wrong guesses\n", maxWrongGuesses); - printf(" before you lose.\n\n"); - usleep(1e6); - printf(" What word am I thinking of?\n"); + // print title screen + titleScreen(); - // pick a random index from the wordpool - srand((int) time(NULL)); - int randomWordPoolIndex = rand()%wordPoolSize; + // setup the difficulty + maxWrongGuesses = difficultySetup(maxWrongGuesses); - // temp variable to hold word we pluck out of the word pool - char *temp = wordPool[randomWordPoolIndex]; + // pick a random word from the wordPool + gameWord = wordSetup(wordPoolSize, wordPool); - // setup a separate string to hold the display of the word in the game - // this is the size it needs to be in order to have spaces between - // each letter - char board[(strlen(temp) * 2) - 1]; + // calculate size of gameWord to use with loops + gameWordSize = (int) strlen(gameWord); - // fill board string with alternating uderscores and spaces - for (int i = 0; i < strlen(temp); i++) { - // all indexes that are multiples of 2 will hold underscores - board[i * 2] = '_'; - // everything else in between will hold spaces. the if statement - // ensures there is no space following the last underscore - if (i < strlen(temp) - 1) { - board[i * 2 + 1] = ' '; - } - - } + // create the game display board + board = getGameBoard(gameWord, gameWordSize); // game logic. keeps running as long as you have not used up all your - // guesses and have not guessed all the letters - while (correctLetters < strlen(temp) && wrongGuesses < maxWrongGuesses) { + // guesses and have not guessed all the letters of the word + while (correctLetters < gameWordSize && wrongGuesses < maxWrongGuesses) { // print inital board printf("\n\n %s", board); @@ -130,8 +232,8 @@ int main(int argc, const char * argv[]) { BOOL correctGuess = 0; // check if the letter is in the word - for (int i = 0; i < strlen(temp); i++) { - if (letterGuess == temp[i]) { + for (int i = 0; i < gameWordSize; i++) { + if (letterGuess == gameWord[i]) { // if it is, update the display board[i*2] = letterGuess; @@ -157,25 +259,9 @@ int main(int argc, const char * argv[]) { } } - // print the board one last time before the end - printf("\n\n %s\n\n\n", board); - - // print different ending message depending on result - if (correctLetters == strlen(temp)) { - printf(" YOU GOT IT!\n\n"); - printf(" HURRAY FOR YOU.\n"); - printf(" A COOL IMAGINARY VICTORY SCREEN APPEARS.\n"); - } else { - printf(" YOU ARE OUT OF GUESSES!\n"); - printf("\n The word was: \n"); - usleep(1e6); - printf(" <<<<< %s >>>>>\n", temp); - printf("\n\n YOU LOST!\n"); - printf(" YOU KILLED AN IMAGINARY STICK FIGURE.\n"); - printf(" GOOD GOING.\n\n"); - } + // print game results + endGame(correctLetters, gameWordSize, gameWord); - } return 0; }