Advertisement
Guest User

NEW

a guest
Jan 24th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.34 KB | None | 0 0
  1. ! GAME OF HANGMAN BY DAVE AHL, DIGITAL
  2. ! BASED ON A BASIC PROGRAM WRITTEN BY KEN AUPPERLE
  3. ! HALF HALLOW HILLS H.S. DIX HILLS NY
  4. ! CONVERTED TO FORTRAN 77 BY M.WIRTH, APRIL 2012
  5. ! REENGINEERED INTO FORTRAN 90/95 BY JEFFREY-DAVID KAPP, JANUARY 2017
  6. program hangman
  7.  
  8. character :: gallows(12,12)
  9. character :: display(20), letters(26), guess, response
  10. character(20) :: answer, word_guess
  11. integer :: used_words(50)
  12. integer :: q, incorrect_guesses, i, j, num_words, total_guesses, r, length, guess_valid
  13. real :: rnd
  14.  
  15. character(20), dimension(50) :: dict = [character(20) :: 'gum','sin','for','cry','lug','bye','fly','ugly', &
  16. 'each','from','work','talk','with','self', &
  17. 'pizza','thing','feign','fiend','elbow','fault', &
  18. 'dirty','budget','spirit','quaint','maiden', &
  19. 'escort','pickax','example','tension','quinine', &
  20. 'kidney','replica','sleeper','triangle', &
  21. 'kangaroo','mahogany','sergeant','sequence', &
  22. 'moustache','dangerous','scientist','different', &
  23. 'quiescent','magistrate','erroneously', &
  24. 'loudspeaker','phytotoxic','matrimonial', &
  25. 'parasympathomimetic','thigmotropism']
  26.  
  27. write (*,*) "The Game of Hangman"
  28. !word count initialisation
  29. count=1
  30. num_words=50
  31.  
  32. do while ( count < num_words ) !master loop
  33. !start; variable initialisation
  34. !most variables reinitalised after each word
  35. do i = 1,12
  36. do j = 1,12
  37. gallows(i,j) = " "
  38. end do
  39. end do
  40. do i = 1,20
  41. display(i) = "-"
  42. end do
  43. do i = 1,26
  44. letters(i) = " "
  45. end do
  46. do i = 1,50
  47. used_words(i) = 0
  48. end do
  49. do i = 1,12
  50. gallows(i,1) = "x"
  51. end do
  52. do j = 1,7
  53. gallows(1,j) = "x"
  54. end do
  55. gallows(2,7) = "x"
  56. incorrect_guesses = 0
  57. word_complete = 0
  58. !end; variable initialisation
  59.  
  60. do i = 1,50 !picking a word - sub routine
  61. rnd=rand()
  62. q=ceiling(rnd*50)
  63. if (used_words(q) == 0) then
  64. used_words(q) = 1
  65. count=count+1
  66. total_guesses=0
  67. exit
  68. end if
  69. end do
  70. answer = dict(q)
  71. length = len_trim(answer)
  72. write (*,*) display(1:l)
  73.  
  74. do while ( incorrect_guesses < 10) !word loop
  75. guess_valid = 0
  76. r=0
  77. do while ( guess_valid == 0) !guess validation loop
  78. call display_letters(letters)
  79. write (*,*) " "
  80. write (*,*) "What is your guess? "
  81. read (*,*) guess
  82. do i = 1,26 !guess validation loop
  83. if (letters(i) == " ") then
  84. total_guesses=total_guesses+1
  85. letters(i) = guess
  86. guess_valid = 1
  87. exit
  88. else if (letters(i) == guess) then
  89. write (*,*) "You guessed that letter before."
  90. exit
  91. end if
  92. end do
  93. end do
  94.  
  95.  
  96. do i = 1,length !checking if the user's guess is in the word
  97. if (answer(i:i) == guess) then
  98. display(i) = guess
  99. r=r+1
  100. end if
  101. end do
  102. if (r == 0) then !if the user's guess isnt present in the word
  103. write (*,*) "Sorry, that letter isn't in the word."
  104. incorrect_guesses=incorrect_guesses+1;
  105. call update_gallows(incorrect_guesses, gallows) !give the poor victim another body part
  106. if (incorrect_guesses == 10) then
  107. write (*,*) "Sorry, you loose. The word was ", answer
  108. write (*,*) "You missed that one."
  109. end if
  110. else !else it is in the word
  111. do i = 1,length !check if word has been completed
  112. if (display(i) == "-") then
  113. !jump out of loop if it hasn't been
  114. exit
  115. end if
  116. end do
  117. if ( i < length ) then !word's not done
  118. write (*,*) display(1:length)
  119. write (*,*) "What is your guess for the word? "
  120. read (*,*) word_guess
  121. if (answer .eq. word_guess) then
  122. write (*,*) "Right! It took you ",total_guesses," guesses."
  123. exit
  124. else
  125. write (*,*) "Wrong. Try another letter."
  126. end if
  127. else !it is done
  128. write (*,*) "You found the word: ", answer
  129. exit
  130. end if
  131. end if
  132. end do !end word loop
  133.  
  134. if (count >= num_words) then
  135. write (*,*) "You did all the words"
  136. write (*,*) "Ending..."
  137. else
  138. write (*,*) "Do you want another word? (y/n) "
  139. read (*,*) response
  140. if (response == "n") then
  141. write (*,*) "It's been fun! bye for now."
  142. exit
  143. end if
  144. end if
  145. end do !end master loop
  146. end
  147.  
  148. !sub routine - updating the hangee with extra body parts and informing the player
  149. subroutine update_gallows( incorrect_guesses_s, gallows_s )
  150. integer :: incorrect_guesses_s
  151. character :: gallows_s(12,12)
  152. integer :: i, j
  153. select case (incorrect_guesses_s)
  154. case (1)
  155. write (*,*) "First we draw a head."
  156. gallows_s(3,6) = "-"; gallows_s(3,7) = "-"; gallows_s(3,8) = "-"; gallows_s(4,5) = "("; !first guess
  157. gallows_s(4,6) = "."
  158. gallows_s(4,8) = "."; gallows_s(4,9) = ")"; gallows_s(5,6) = "-"; gallows_s(5,7) = "-";
  159. gallows_s(5,8) = "-"
  160. case (2)
  161. write (*,*) "Now we draw a body."
  162. do i = 6,9 !second guess
  163. gallows_s(i,7) = "x"
  164. end do
  165. case (3)
  166. write (*,*) "Next we draw an arm."
  167. do i = 4,7 !third guess
  168. gallows_s(i,i-1) = "\"
  169. end do
  170. case (4)
  171. write (*,*) "This time it's the other arm."
  172. gallows_s(4,11) = "/"; gallows_s(5,10) = "/"; gallows_s(6,9) = "/"; gallows_s(7,8) = "/";
  173. case (5)
  174. write (*,*) "Now, let's draw the right leg."
  175. gallows_s(10,6) = "/"; gallows_s(11,5) = "/";
  176. case (6)
  177. write (*,*) "This time we draw the left leg."
  178. gallows_s(10,8) = "\"; gallows_s(11,9) = "\";
  179. case (7)
  180. write (*,*) "Now we put up a hand."
  181. gallows_s(3,11) = "\";
  182. case (8)
  183. write (*,*) "Next the other hand."
  184. gallows_s(3,3) = "/"
  185. case (9)
  186. write (*,*) "Now we draw one foot."
  187. gallows_s(12,10) = "\"; gallows_s(12,11) = "-";
  188. case (10)
  189. write (*,*) "Here's the other foot -- you're hung!!."
  190. gallows_s(12,3) = "-"; gallows_s(12,4) = "/"
  191. end select
  192. do i = 1,12
  193. write (*,*) (gallows_s(i,j),j=1,12)
  194. end do
  195. return
  196. end subroutine
  197. !---
  198.  
  199. !sub routine - displaying guessed letters
  200. subroutine display_letters( letters_s )
  201. character :: letters_s(26)
  202. integer :: i
  203. write (*,*) "Here are the letters you used: "
  204. do i = 1,26
  205. if (letters_s(i) == ' ') then
  206. exit
  207. else
  208. write (*,'(aa$)') letters_s(i),"," !I have no idea what this is doing
  209. end if
  210. end do
  211. return
  212. end subroutine
  213. !---
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement