Java word guessing game - problem

Thread Solved

Join Date: Jun 2008
Posts: 6
Reputation: Potty391 is an unknown quantity at this point 
Solved Threads: 0
Potty391 Potty391 is offline Offline
Newbie Poster

Java word guessing game - problem

 
0
  #1
22 Days Ago
Hi all, I've been working on a Java guessing game, similar to hangman which rewards you with a picture using the Gogga class (developed by the people who wrote my IT textbook for Grade 10, also I removed gogga for this website, since I doubt you'd be able to find it).
Just a word of warning... it could be simplified to a lot less lines of code, but since I may not use for complex things such as arrays, do while, etc. The problem I'm having is at the word guessing part of the program, it asks for the next character and displays the words and picture within the if statements, regardless of whether the input character was incorrect of not (so it carries out the code regardless if the conditions of the "if" is met). I hope you guys know what I'm talking about. Also I seem to not be able to have "else" after the big bunch of if statements. Just for reference, the program is not yet 100% complete since I first wanted to sort out this bug.

  1. /* PAT Assignment --- Word Guessing + Number pattern game
  2.   Charl Potgieter 10Pn
  3. October / November 2009
  4. */
  5.  
  6.  
  7. import javax.swing.*;
  8. import java.util.*; // for random
  9. public class GuessingGame1
  10. {
  11. public static void main (String [] args)
  12. {
  13.  
  14. JOptionPane.showMessageDialog(null, "Welcome to the Guessing Game!");
  15.  
  16. for ( int j = 1; ; j = j ++ )
  17. {
  18.  
  19. String menu, undscore1, undscore2, undscore3, undscore4, undscore5; //have underscores
  20. Random ran = new Random();
  21. char char1, char2, char3, char4, char5, inpChar;
  22.  
  23.  
  24.  
  25.  
  26. String[] options = {"Word Guessing",
  27. "Patterns Game ",
  28. "Exit"
  29. }; // Used to make options to select in the menu
  30.  
  31.  
  32. menu = (String) JOptionPane.showInputDialog (null, " MENU", " Guessing Game", JOptionPane.PLAIN_MESSAGE, null, options, " "); //Starting menu
  33.  
  34. if (menu.equals (options [0]))
  35. {
  36.  
  37. String word = JOptionPane.showInputDialog("Player 1: Please input a word (to a maximum of 5 characters), for single player type: 'random' "); //Ask for input, whether the user wants to make a word, or a random one
  38.  
  39. int rndChoice = (int)(Math.random()*14); // rndChoice is made for the switch to be able to randomly choose a word
  40.  
  41.  
  42. //Code for user word
  43.  
  44.  
  45. if (!word.equals("random"))
  46. {
  47. /* undscore1 = "_";
  48.   undscore2 = "_";
  49.   undscore3 = "_";
  50.   undscore4 = "_";
  51.   undscore5 = "_";
  52. */
  53. String inpWord;
  54.  
  55.  
  56.  
  57. inpWord = JOptionPane.showInputDialog("Please input a character that you want to guess");
  58. inpChar = inpWord.charAt(0);
  59. char1 = word.charAt(0);
  60. char2 = word.charAt(1);
  61. char3 = word.charAt(2);
  62. char4 = word.charAt(3);
  63. char5 = word.charAt(4);
  64.  
  65.  
  66. if (inpChar == char1)
  67. {
  68.  
  69. System.out.println("The word is: "+char1+"____ ");
  70.  
  71.  
  72. }
  73. inpWord = JOptionPane.showInputDialog("Please input a character that you want to guess");
  74. inpChar = inpWord.charAt(0);
  75.  
  76. if(inpChar == char2);
  77. {
  78.  
  79.  
  80. System.out.println("The word is: "+char1+char2+"___" );
  81.  
  82.  
  83.  
  84. }
  85. inpWord = JOptionPane.showInputDialog("Please input a character that you want to guess");
  86. inpChar = inpWord.charAt(0);
  87.  
  88. if(inpChar == char3);
  89. {
  90.  
  91. System.out.println("The word is: "+char1+char2+char3+"__" );
  92.  
  93. }
  94.  
  95. inpWord = JOptionPane.showInputDialog("Please input a character that you want to guess");
  96. inpChar = inpWord.charAt(0);
  97.  
  98. if(inpChar == char4);
  99. {
  100.  
  101.  
  102. System.out.println("The word is: "+char1+char2+char3+char4+"_" );
  103.  
  104. }
  105.  
  106. inpWord = JOptionPane.showInputDialog("Please input a character that you want to guess");
  107. inpChar = inpWord.charAt(0);
  108.  
  109. if(inpChar == char5);
  110. {
  111.  
  112.  
  113. System.out.println("The word is: "+char1+char2+char3+char4+char5 );
  114.  
  115.  
  116. }
  117.  
  118.  
  119. JOptionPane.showMessageDialog(null, "Congratulations, you have won! \n Returning to menu");
  120.  
  121.  
  122. }
  123.  
  124. //Code for random word
  125.  
  126. if (word.equals("random"))
  127. {
  128. switch (rndChoice)
  129. {
  130. case 0:
  131. word = "hello";
  132. break;
  133. case 1:
  134. word = "kevin";
  135. break;
  136. case 2:
  137. word = "charl";
  138. break;
  139. case 3:
  140. word = "county";
  141. break;
  142. case 4:
  143. word = "sweety";
  144. break;
  145. case 5:
  146. word = "motor";
  147. break;
  148. case 6:
  149. word = "edges";
  150. break;
  151. case 7:
  152. word = "works";
  153. break;
  154. case 8:
  155. word = "place";
  156. break;
  157. case 9:
  158. word = "right";
  159. break;
  160. case 10:
  161. word = "dudes";
  162. break;
  163. case 11:
  164. word = "lucky";
  165. break;
  166. case 12:
  167. word = "homes";
  168. break;
  169. }
  170. undscore1 = "_";
  171. undscore2 = "_";
  172. undscore3 = "_";
  173. undscore4 = "_";
  174. undscore5 = "_";
  175. String inpWord;
  176.  
  177. for ( int count = 1; ; count = count ++ )
  178. {
  179.  
  180. inpWord = JOptionPane.showInputDialog("Please input a character that you want to guess");
  181.  
  182.  
  183. inpChar = inpWord.charAt(0);
  184.  
  185. char1 = word.charAt(0);
  186. char2 = word.charAt(1);
  187. char3 = word.charAt(2);
  188. char4 = word.charAt(3);
  189. char5 = word.charAt(4);
  190.  
  191.  
  192.  
  193.  
  194. if (inpChar == char1)
  195. {
  196.  
  197. System.out.println("The word is: "+char1+"____ ");
  198.  
  199.  
  200. }
  201. inpWord = JOptionPane.showInputDialog("Please input a character that you want to guess");
  202. inpChar = inpWord.charAt(0);
  203.  
  204. if(inpChar == char2);
  205. {
  206.  
  207.  
  208. System.out.println("The word is: "+char1+char2+"___" );
  209.  
  210.  
  211. }
  212. inpWord = JOptionPane.showInputDialog("Please input a character that you want to guess");
  213. inpChar = inpWord.charAt(0);
  214.  
  215. if(inpChar == char3);
  216. {
  217.  
  218. System.out.println("The word is: "+char1+char2+char3+"__" );
  219.  
  220. }
  221.  
  222. inpWord = JOptionPane.showInputDialog("Please input a character that you want to guess");
  223. inpChar = inpWord.charAt(0);
  224.  
  225. if(inpChar == char4);
  226. {
  227.  
  228.  
  229. System.out.println("The word is: "+char1+char2+char3+char4+"_" );
  230.  
  231. }
  232.  
  233. inpWord = JOptionPane.showInputDialog("Please input a character that you want to guess");
  234. inpChar = inpWord.charAt(0);
  235.  
  236. if(inpChar == char5);
  237. {
  238.  
  239.  
  240. System.out.println("The word is: "+char1+char2+char3+char4+char5 );
  241.  
  242.  
  243. }
  244. JOptionPane.showMessageDialog(null, "Congratulations, you have won! \n Returning to menu");
  245.  
  246.  
  247. }
  248.  
  249. }
  250. }
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257. if (menu.equals (options [1]))
  258. {
  259. JOptionPane.showMessageDialog(null, "In this game you will be given 10 sequences of numbers that you will have to complete, you only have one try, its that easy!");
  260. int patrn;
  261.  
  262.  
  263. int rndNoChoice = (int)(Math.random()*14); // rndNoChoice is made for the switch to be able to randomly choose a pattern
  264.  
  265. switch (rndNoChoice)
  266. {
  267. case 0:
  268. patrn = Integer.parseInt(JOptionPane.showInputDialog("What is the next number: \n 15 ; 30 ; 45 ...."));
  269. if (patrn == 60)
  270. {
  271. JOptionPane.showMessageDialog(null, "Correct!");
  272. }
  273. else
  274. {
  275. JOptionPane.showMessageDialog(null, "Incorrect!");
  276. }
  277. break;
  278. /////////////////
  279. case 1:
  280. patrn = Integer.parseInt(JOptionPane.showInputDialog("What is the next number: \n 10 ; 20 ; 30 ...."));
  281. if (patrn == 40)
  282. {
  283. JOptionPane.showMessageDialog(null, "Correct!");
  284. }
  285. else
  286. {
  287. JOptionPane.showMessageDialog(null, "Incorrect!");
  288. }
  289.  
  290. break;
  291. //////////////////
  292. case 2:
  293. patrn = Integer.parseInt(JOptionPane.showInputDialog("What is the next number: \n 1 ; 2 ; 3 ...."));
  294. if (patrn == 4)
  295. {
  296. JOptionPane.showMessageDialog(null, "Correct!");
  297. }
  298. else
  299. {
  300. JOptionPane.showMessageDialog(null, "Incorrect!");
  301. }
  302.  
  303. break;
  304. //////////////////
  305. case 3:
  306. patrn = Integer.parseInt(JOptionPane.showInputDialog("What is the next number: \n 7 ; 14 ; 21 ...."));
  307. if (patrn == 28)
  308. {
  309. JOptionPane.showMessageDialog(null, "Correct!");
  310. }
  311. else
  312. {
  313. JOptionPane.showMessageDialog(null, "Incorrect!");
  314. }
  315.  
  316.  
  317. break;
  318. /////////////////
  319. case 4:
  320. patrn = Integer.parseInt(JOptionPane.showInputDialog("What is the next number: \n 8 ; 16 ; 24 ...."));
  321. if (patrn == 32)
  322. {
  323. JOptionPane.showMessageDialog(null, "Correct!");
  324. }
  325. else
  326. {
  327. JOptionPane.showMessageDialog(null, "Incorrect!");
  328. }
  329.  
  330.  
  331. break;
  332. /////////////////
  333. case 5:
  334. patrn = Integer.parseInt(JOptionPane.showInputDialog("What is the next number: \n 2 ; 4 ; 6 ...."));
  335. if (patrn == 8)
  336. {
  337. JOptionPane.showMessageDialog(null, "Correct!");
  338. }
  339. else
  340. {
  341. JOptionPane.showMessageDialog(null, "Incorrect!");
  342. }
  343.  
  344.  
  345. break;
  346. //////////////////
  347. case 6:
  348. patrn = Integer.parseInt(JOptionPane.showInputDialog("What is the next number: \n 12 ; 24 ; 36 ...."));
  349. if (patrn == 48)
  350. {
  351. JOptionPane.showMessageDialog(null, "Correct!");
  352. }
  353. else
  354. {
  355. JOptionPane.showMessageDialog(null, "Incorrect!");
  356. }
  357.  
  358.  
  359. break;
  360. //////////////////
  361. case 7:
  362. patrn = Integer.parseInt(JOptionPane.showInputDialog("What is the next number: \n 1000 ; 750 ; 500 ...."));
  363. if (patrn == 250)
  364. {
  365. JOptionPane.showMessageDialog(null, "Correct!");
  366. }
  367. else
  368. {
  369. JOptionPane.showMessageDialog(null, "Incorrect!");
  370. }
  371.  
  372.  
  373. break;
  374. //////////////////
  375. case 8:
  376. patrn = Integer.parseInt(JOptionPane.showInputDialog("What is the next number: \n 4 ; 8 ; 12 ...."));
  377. if (patrn == 16)
  378. {
  379. JOptionPane.showMessageDialog(null, "Correct!");
  380. }
  381. else
  382. {
  383. JOptionPane.showMessageDialog(null, "Incorrect!");
  384. }
  385.  
  386.  
  387. break;
  388. //////////////////
  389. case 9:
  390. patrn = Integer.parseInt(JOptionPane.showInputDialog("What is the next number: \n 1000 ; 2000 ; 3000 ...."));
  391. if (patrn == 4000)
  392. {
  393. JOptionPane.showMessageDialog(null, "Correct!");
  394. }
  395. else
  396. {
  397. JOptionPane.showMessageDialog(null, "Incorrect!");
  398. }
  399.  
  400.  
  401. break;
  402. //////////////////
  403. case 10:
  404. patrn = Integer.parseInt(JOptionPane.showInputDialog("What is the next number: \n 9 ; 18 ; 27 ...."));
  405. if (patrn == 36)
  406. {
  407. JOptionPane.showMessageDialog(null, "Correct!");
  408. }
  409. else
  410. {
  411. JOptionPane.showMessageDialog(null, "Incorrect!");
  412. }
  413.  
  414.  
  415. break;
  416. /////////////////
  417. case 11:
  418. patrn = Integer.parseInt(JOptionPane.showInputDialog("What is the next number: \n 3 ; 6 ; 9 ...."));
  419. if (patrn == 12)
  420. {
  421. JOptionPane.showMessageDialog(null, "Correct!");
  422. }
  423. else
  424. {
  425. JOptionPane.showMessageDialog(null, "Incorrect!");
  426. }
  427.  
  428.  
  429. break;
  430. /////////////////
  431. case 12:
  432. patrn = Integer.parseInt(JOptionPane.showInputDialog("What is the next number: \n 23 ; 46 ; 69 ...."));
  433. if (patrn == 92)
  434. {
  435. JOptionPane.showMessageDialog(null, "Correct!");
  436. }
  437. else
  438. {
  439. JOptionPane.showMessageDialog(null, "Incorrect!");
  440. }
  441.  
  442.  
  443. break;
  444.  
  445. }
  446.  
  447. //////////////////
  448.  
  449.  
  450. }
  451.  
  452. if (menu.equals (options [2]))
  453. {
  454. JOptionPane.showMessageDialog(null,"Thank you for playing!! \n Credits: \t Charl Potgieter");
  455. }
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464. }
  465. }
  466. }

Thank you for your time and patience,
- Charl aka Potty391
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC