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..cc1706e 100644 --- a/HangPerson/HangPerson/main.m +++ b/HangPerson/HangPerson/main.m @@ -2,16 +2,265 @@ // 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 game 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 = 12; + break; + case 2: + maxWrongGuesses = 10; + break; + case 3: + maxWrongGuesses = 8; + break; + default: + printf("\n\n Sorry, you did not input a number from 1 to 3.\n"); + break; + } + } + + 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 ************************************************// + + 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", "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 + int maxWrongGuesses = 0; + // keep track of total letters filled in correctly. will let program + // know when the player wins + int correctLetters = 0; + + + //********* OUR GAME BEGINS **********************************************// + + // print title screen + titleScreen(); + + // setup the difficulty + maxWrongGuesses = difficultySetup(maxWrongGuesses); + + // pick a random word from the wordPool + gameWord = wordSetup(wordPoolSize, wordPool); + + // calculate size of gameWord to use with loops + gameWordSize = (int) strlen(gameWord); + + // 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 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); + } + + } + } - // code goes here... + // print game results + endGame(correctLetters, gameWordSize, gameWord); } return 0; diff --git a/README.md b/README.md new file mode 100644 index 0000000..28303f6 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# 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. + +Cheers.