main.go (17721B)
1 package main 2 3 import ( 4 "bufio" 5 "fmt" 6 "math/rand" 7 "os" 8 "os/exec" 9 "runtime" 10 "strconv" 11 "time" 12 "unicode" 13 ) 14 15 // Be able to clear screen 16 func clearScreen() { 17 var cmd *exec.Cmd 18 if runtime.GOOS == "windows" { 19 cmd = exec.Command("cmd", "/c", "cls") 20 21 } else { 22 cmd = exec.Command("clear") 23 } 24 cmd.Stdout = os.Stdout 25 cmd.Run() 26 } 27 28 // handle file errors 29 func handleError(err error) { 30 if err != nil { 31 panic(err) 32 } 33 } 34 35 // be able to read files 36 func read(name string) { 37 file, err := os.Open(name) 38 handleError(err) 39 40 scanner := bufio.NewScanner(file) 41 42 for scanner.Scan() { 43 fmt.Println(scanner.Text()) 44 } 45 46 } 47 48 // be able to proceed in story and lets user have time to read in story 49 func proceed() { 50 reader := NewCinReader() 51 fmt.Print("\nEnter the letter C to continue: ") 52 unicode.ToUpper(reader.ReadCharacterSet([]rune("Cc"))) 53 clearScreen() 54 } 55 56 func menu() { 57 reader := NewCinReader() 58 59 for { 60 // Display Menu 61 read("menu.txt") 62 63 // Ask user to pick option 64 fmt.Print("\nEnter the corresponding letter to select an option (p = Play | a = About): ") 65 option := unicode.ToUpper(reader.ReadCharacterSet([]rune("PpAa"))) 66 67 // Option user, display about game section 68 if option == 'A' { 69 clearScreen() 70 read("about.txt") 71 fmt.Print("\nEnter the letter P when you are ready to play: ") 72 reader.ReadCharacterSet([]rune("Pp")) 73 } 74 clearScreen() 75 break 76 } 77 } 78 79 type data struct { 80 name string 81 age int 82 nation string 83 pieces int 84 } 85 86 func (explorer *data) intro() { 87 reader := NewCinReader() 88 // Ask user for explorer's info 89 fmt.Println("Before we begin this adventure, please fill in the upcoming prompts.") 90 fmt.Print("\nYour Explorer's name: ") 91 explorer.name = reader.ReadString(false) 92 fmt.Print("\nYour Explorer's Age: ") 93 explorer.age = reader.ReadIntegerRange(1, 150) 94 95 // Display Explorer nations to represent for user to input 96 fmt.Println(` 97 a = Netherlands 98 b = England 99 c = France 100 d = Portugal 101 e = Spain 102 `) 103 104 // Grab input of Explorer Nation 105 fmt.Print("Enter the letter corresponding above to the nation you will represent as an explorer: ") 106 nationChoice := unicode.ToUpper(reader.ReadCharacterSet([]rune("AaBbCcDdEe"))) 107 108 // Set the explorer's nation 109 switch nationChoice { 110 case 'A': 111 explorer.nation = "Dutch" 112 case 'B': 113 explorer.nation = "English" 114 case 'C': 115 explorer.nation = "French" 116 case 'D': 117 explorer.nation = "Portuguese" 118 case 'E': 119 explorer.nation = "Spanish" 120 } 121 122 // Clear screen and display intro 123 clearScreen() 124 fmt.Printf("%s, a "+strconv.Itoa(explorer.age)+" year old explorer representing the "+explorer.nation+" crown. %s has been given the grand task to find Fountain of Youth, a rumored area existing in the new world which gives youth if you drink of the fountain of water, and it's treasure holds. You as the player must help %s find the Fountain of Youth and it's treasure before any of the other nations discover it. Back to the story, %s traveled to La Havana, Cuba, a famous trade port and launch point of explorers adventuring into the New World, to talk around to see if anyone had an information on a clue on finding this Fountain of Youth. While in La Havana, Cuba, %s decided to explore and talk to any other sailors or people in the local La Havana bar. While at the bar, %s meets this old white hair and bearded sailor with a black eyepatch on his right eye named Jean Ango, a French sailor who aside in Christopher Colombus's first voyage to the New World.", explorer.name, explorer.name, explorer.name, explorer.name, explorer.name, explorer.name) 125 126 // Ask user if they want to proceed in story 127 // If yes, clear screen and display following text 128 proceed() 129 130 fmt.Println(`Jean Ango: 131 So I hear you want to find the Fountain of Youth. During my years as an active sailor I have heard great stories from other sailors about it wanting to find it granting them youth and other things. While in my youth, I have done a bit of research on this topic, I might be able to help you. In order in finding this Fountain of Youth you desire to find, you need to get this 4 pieces of a whole map leading to the treasure scattered around the islands of the Carribbean as follows: 132 133 1. George Town, Cayman Islands 134 2. Kingston, Jamaica 135 3. Santo Domingo, Domincan Republic 136 4. Port-au-Prince, Haiti 137 138 But beware, there might be other explorers or pirates looking for what you desire as well to find. I have heard rumors Ponce de León, a Spanish explorer, is close to finding it, and has a big size crew with him. You might need to fight to death against León for the treasure. Good luck on your journey explorer, safe travels. 139 140 `) 141 proceed() 142 } 143 144 // Guessing Game 145 func guessValid() bool { 146 // Setup the random number generator 147 seed := rand.NewSource(time.Now().UnixNano()) 148 RandomNumberGenerator := rand.New(seed) 149 150 // Pick a random number from 1-25 and store it. 151 randomNumber := RandomNumberGenerator.Intn(25) + 1 152 153 // User gets variable which holds 10 guesses. 154 var tries int = 10 155 156 // Ask the user to type a number from 1-25 to guess the random number and store it. 157 reader := NewCinReader() 158 for i := 0; i < 10; i++ { 159 fmt.Printf("Guesses remaining: %d", tries) 160 fmt.Print("\nType a number from 1 - 25 to guess the random number generate: ") 161 guess := reader.ReadIntegerRange(1, 25) 162 if guess == randomNumber { 163 // Guess was Correct! 164 fmt.Println("Correct number! You won!\n") 165 return true 166 } else { 167 // Guess was Incorrect! 168 fmt.Println("Incorrect number.\n") 169 tries -= 1 170 } 171 } 172 return false 173 } 174 175 func (explorer *data) cay() { 176 // Display Progress of map pieces found 177 fmt.Printf("You have found %d out of the 4 pieces of the map. \n\n", explorer.pieces) 178 179 // Display Cayman Island Intro 180 fmt.Println("First, " + explorer.name + " begins traveling to George Town, Cayman Islands. While there he meets a English pirate captain named William Aleyn at a local bar in Georgetown.") 181 proceed() 182 fmt.Println(`William Aleyn: So you told me you are looking for the piece for the Fountain of Youth lad. I have a piece of the map but I have a challenge for you. The challenge is you have to have to guess my number which is between 1 - 25. You have 10 guesses to attempt to guess my number correct. If you lose and do not guess the number correct, a new number will be generated and 10 guesses will be given again. 183 `) 184 proceed() 185 186 // Start guessing game 187 for { 188 if guessValid() == true { 189 break 190 } 191 clearScreen() 192 } 193 194 // Add piece of map to track progress 195 fmt.Println(`William Aleyn: Good work. You have guessed my number correct. Here is the first piece of the map to the Fountain of Youth.`) 196 explorer.pieces += 1 197 proceed() 198 } 199 200 func (explorer *data) battle(emName string, health, damage, emHealth, emDamage int) bool { 201 202 // proceed() 203 reader := NewCinReader() 204 205 const ( 206 kAttack rune = 'A' 207 kBlock rune = 'B' 208 kStam rune = 'S' 209 ) 210 211 var ( 212 round int = 1 213 usrStam int = 0 214 cpuStam int = 0 215 usrMove rune 216 cpuMove rune 217 ) 218 219 for { 220 // Print Player & Enemy stats 221 fmt.Printf("%s vs %s \n\n", explorer.name, emName) 222 fmt.Printf("%s's Health: %d\n", explorer.name, health) 223 fmt.Printf("%s's Damage: %d\n\n", explorer.name, damage) 224 //fmt.Println(emName + fmt.Sprintf("'s Health: %d\n", emHealth) + emName + fmt.Sprintf("'s Damage: %d\n\n", emDamage)) 225 fmt.Printf("%s's Health: %d\n", emName, health) 226 fmt.Printf("%s's Damage: %d\n\n", emName, emDamage) 227 228 // Generate User Move 229 switch usrStam { 230 case 0: 231 fmt.Print("Enter the letter of the move you want to perform. (S = Stamina | B = Block): ") 232 usrMove = unicode.ToUpper(reader.ReadCharacterSet([]rune("SsBb"))) 233 case 1: 234 fmt.Print("Enter the letter of the move you want to perform. (A = Attack | B = Block): ") 235 usrMove = unicode.ToUpper(reader.ReadCharacterSet([]rune("AaBb"))) 236 } 237 238 switch usrMove { 239 case kAttack: 240 fmt.Println("You attempt to attack.") 241 usrStam = 0 242 case kBlock: 243 fmt.Println(fmt.Sprintf("You have blocked any incoming possible attack against from %s.", emName)) 244 case kStam: 245 fmt.Println("You attempt to regain stamina.") 246 usrStam = 1 247 } 248 249 //fmt.Println(round) 250 // Generate Computer Move 251 if round == 1 || (usrStam == 0 && cpuStam == 0) { 252 fmt.Println(emName + " Regains Stamina.") 253 round, cpuMove, cpuStam = 2, 'S', 1 254 } else { 255 256 // Loop until AI gets a valid move 257 for { 258 // AI Randomly Picks Move 259 ability := []rune{'A', 'B', 'S'} 260 cpuMove = ability[rand.Intn(len(ability))] 261 262 //fmt.Printf("%c\n", cpuMove) 263 if cpuMove == 'A' && cpuStam == 1 { 264 fmt.Println(emName + " attacks.") 265 cpuStam = 0 266 break 267 } else if cpuMove == 'B' { 268 fmt.Println(emName + " blocks.") 269 break 270 } else if cpuMove == 'S' && cpuStam == 0 { 271 fmt.Println(emName + " regains Stamina.") 272 cpuStam = 1 273 break 274 } 275 } 276 } 277 278 // Generate Outcomes of Fight 279 fmt.Println() 280 if usrMove == kAttack && cpuMove == kAttack { 281 // Draw Continue: User Attack = Computer Attack 282 fmt.Println("You both attack each other. No damage was made.") 283 } else if usrMove == kAttack && cpuMove == kBlock { 284 // Continue: User Attack = Computer Block 285 fmt.Println(emName + "has successfully blocked you.") 286 } else if usrMove == kAttack && cpuMove == kStam { 287 // Continue: User Attack = Computer Regains 288 fmt.Println(explorer.name + " have successfully attacked " + emName) 289 fmt.Printf("-%d damage.\n", damage) 290 emHealth -= damage 291 } else if usrMove == kBlock && cpuMove == kAttack { 292 // Continue: User Block = Computer Attack 293 fmt.Println(explorer.name + " has successfully blocked " + emName + "'s attack.") 294 } else if usrMove == kBlock && cpuMove == kBlock { 295 // Continue: User Block = Computer Block 296 fmt.Println("Continue on.") 297 } else if usrMove == kBlock && cpuMove == kStam { 298 // Continue: User Block = Computer Regains 299 fmt.Println("Continue on.") 300 } else if usrMove == kStam && cpuMove == kAttack { 301 // Continue: User Regains = Computer Throws 302 fmt.Println(emName + " has successfully attacked " + explorer.name) 303 fmt.Printf("-%d damage.\n", damage) 304 health -= damage 305 } else if usrMove == kStam && cpuMove == kBlock { 306 // Continue: User Regains = Computer Blocks 307 fmt.Println("Continue on.") 308 } else if usrMove == kStam && cpuMove == kStam { 309 // Continue: User Regains = Computer Regains 310 fmt.Println("Continue on! You both have regained stamina.") 311 } 312 313 // Check if game ended 314 if emHealth == 0 { 315 return true 316 } else if health == 0 { 317 // If explorer fails, restart game. 318 clearScreen() 319 fmt.Println("You have been eliminated. Try again.") 320 proceed() 321 return false 322 } 323 proceed() 324 } 325 } 326 327 // Jamaica Island 328 func (explorer *data) jam() { 329 // Display Progress of map pieces found 330 fmt.Printf("You have found %d out of the 4 pieces of the map. \n\n", explorer.pieces) 331 332 // Display Jamaica Intro 333 fmt.Printf("After heading getting the first piece of the map from the Cayman Islands, %s traveled in his ship to the second location to find the next map piece, Kingston, Jamaica. While in Kingston, Jamaica, %s travels around and finds man by the name of James Alday in a local market in Kingston who was a tobacco farmer who claims he has a piece of the map. But in order to get the piece you have beat him a fighting brawl.\n", explorer.name, explorer.name) 334 335 // Start Battle 336 proceed() 337 fmt.Println(`Welcome to the fighting gamemode, a gamemode which you will use to battle other enemies against your explorer. 338 339 How to play: 340 341 You will be the following 2 of 3 options each turn to in the battle as it progresses. 342 343 A = Attack: 344 With the attack ability you can attack and do damage to the enemy but in order to throw you need one stamina point. Note: If you and the enemy at the same time attack, none of you do any damage to each other. 345 346 B = Block: 347 With the block ability you can block any attacks the other enemy does to you. 348 349 S = Regain Stamina: 350 With the regain stamina ability you can regain a stamina point in which you could use in the next round to attack the enemy. 351 352 How to Win: 353 In order to win, you must take all their health points away. 354 `) 355 proceed() 356 357 // Start Battle 358 for { 359 if explorer.battle("James Alday", 100, 50, 50, 50) == true { 360 break 361 } 362 clearScreen() 363 } 364 365 // Add piece of map to track progress 366 clearScreen() 367 fmt.Println("James Alday: I lost. Here is the next piece of the map.") 368 explorer.pieces += 1 369 } 370 371 // be able to answer in the quiz minigame 372 func answer() rune { 373 reader := NewCinReader() 374 fmt.Print("\nEnter the corresponding letter to the option you wish to answer in: ") 375 answer := unicode.ToUpper(reader.ReadCharacterSet([]rune("AaBbCcDd"))) 376 clearScreen() 377 return answer 378 } 379 380 func quiz() int { 381 // create var which holds score 382 var score int = 0 383 // Ask question and collect answer 384 // If answer correct, add to total score. 385 fmt.Println(`What is the capital of Cuba? 386 a. La Havana 387 b. Madrid 388 c. Berlin 389 d. Washington D.C. 390 `) 391 if answer() == 'A' { 392 score += 1 393 } 394 395 fmt.Println(`What is the smallest country in the world? 396 a. Denmark 397 b. USA 398 c. Vatican City 399 d. Japan 400 `) 401 if answer() == 'C' { 402 score += 1 403 } 404 405 fmt.Println(`Which religion dominated the Middle Ages? 406 a. Protestantism 407 b. Buddhism 408 c. Catholicism 409 d. Gnosticism 410 `) 411 if answer() == 'C' { 412 score += 1 413 } 414 415 fmt.Println(`Which animal symbolizes good luck in Europe? 416 a. Ladybug 417 b. Lion 418 c. Camel 419 d. Cow 420 `) 421 if answer() == 'A' { 422 score += 1 423 } 424 425 fmt.Println(`What is the name that used to refer to a group of frogs? 426 a. Herd 427 b. Frogs 428 c. Army 429 d. Colony 430 `) 431 if answer() == 'C' { 432 score += 1 433 } 434 435 return score 436 437 } 438 439 func (explorer *data) dom() { 440 // Display Progress of map pieces found 441 fmt.Printf("You have found %d out of the 4 pieces of the map. \n\n", explorer.pieces) 442 443 // Display Domincan Republic Intro 444 fmt.Printf("Next, %s traveled in his ship to the third location to find the next map piece in Santo Domingo, Domincan Republic. While in Santo Domingo, %s runs into a Catholic monk named Baldassarre Cossa who has an some piece of the map.\n\n", explorer.name, explorer.name) 445 fmt.Println(`Baldassarre Cossa: Well hear you are looking for this piece. I will give it to you, but I am a fool who will ask you to fight me for it. Simply I desire you must get 4 out of 5 questions in trivia I will give you. I will not give my piece to fools who are not smart and will probably lose the piece of the map easily. If you fail, you must try again the trivia. Now let's begin...`) 446 proceed() 447 448 // Start Trivia 449 for { 450 if quiz() >= 4 { 451 break 452 } 453 clearScreen() 454 } 455 456 fmt.Println("Baldassarre Cossa: Well here is your map piece. You are an educated person. Well done.") 457 explorer.pieces += 1 458 459 } 460 461 func (explorer *data) hai() { 462 // Display Progress of map pieces found 463 fmt.Printf("You have found %d out of the 4 pieces of the map. \n\n", explorer.pieces) 464 465 // Display Domincan Republic Intro 466 fmt.Printf("Next, %s traveled in his ship to the forth and final location to find the last map piece in Port-au-Prince, Haiti. While in Santo Domingo, %s runs into a Catholic monk named Baldassarre Cossa who has an some piece of the map.\n\n", explorer.name, explorer.name) 467 fmt.Println(`Magnus Heinason: So you have all the pieces of the map but I still have the last one. You will have to beat me in a brawl for this one. I am not going to give up with an easy fight.`) 468 proceed() 469 470 // Start Battle 471 for { 472 if explorer.battle("Magnus Heinason", 100, 50, 100, 50) == true { 473 break 474 } 475 clearScreen() 476 } 477 478 // Add piece of map to track progress 479 clearScreen() 480 fmt.Println("Magnus Heinason: You fight well " + explorer.name + ". Now take this last piece of the map and get out here before any other seek this piece of the map.") 481 explorer.pieces += 1 482 } 483 484 func (explorer *data) final() { 485 // Display Progress of map pieces found 486 fmt.Printf("Nice job! You have found %d out of the 4 pieces of the map. \n\n", explorer.pieces) 487 488 fmt.Println("After getting all the pieces of the map, " + explorer.name + " traveled to an unknown territory which is now present day St. Augustine, Florida.") 489 fmt.Println("Next, " + explorer.name + " reads the clues and eventually you find the Fountain of Youth and it's glorious treasure.") 490 fmt.Println("But wait!!! You quickly discover a Spanish explorer by the name of Ponce de León there as well nearby the Fountain of Youth.") 491 fmt.Println("Ponce de León: So you want this treasure and this Fountain of Youth as well. Well you will have to fight me until the last breathe of my life. I will give my life to protect and take this treasure as much as I please. Get ready to die " + explorer.name + ".") 492 493 // Start Final Battle 494 for { 495 if explorer.battle("Ponce de León", 100, 50, 150, 50) == true { 496 break 497 } 498 clearScreen() 499 } 500 501 // Display treasure ascii and ending text 502 clearScreen() 503 read("treasure.txt") 504 fmt.Println(explorer.name + " has slain Ponce de León. You are victorious. Men and women shall hear and tell great tales about the great explorer name of " + explorer.name + ". The treasure and the Fountain of Youth is " + explorer.name + "'s to keep.\n\n") 505 fmt.Println("Thanks for playing as the legendary " + explorer.name + " in the great story of the Fountain of Youth! I hope you have enjoyed.") 506 } 507 508 func main() { 509 // Create a CinReader 510 reader := NewCinReader() 511 512 // Create Needed Variables 513 var ( 514 again rune = 'Y' 515 explorer data 516 ) 517 518 // Loop until the user no longer wants to play 519 // This is the Game Loop 520 for again == 'Y' { 521 explorer.pieces = 0 522 menu() 523 explorer.intro() 524 explorer.cay() 525 explorer.jam() 526 explorer.dom() 527 explorer.hai() 528 explorer.final() 529 530 // Prompt user to see if the want to continue to play 531 fmt.Print("Would you like to play again? (Y / N) ") 532 again = unicode.ToUpper(reader.ReadCharacterSet([]rune("YyNn"))) 533 fmt.Println() 534 } 535 }