2010年11月23日

          大家好,這是我第一次在BlogJava發帖,本人酷愛編程,加入BlogJava社區,就是為了認識結交更多有共同興趣愛好的朋友,交流彼此學習編程的經驗,請大家多多支持我
          下面的Java程序是我學習Java初期的一個作業:猜英文單詞游戲,作為我Blog第一篇隨筆,這個程序也可以直接點擊http://www.aygfsteel.com/Files/hughmay/Assignment_1.zip下載,然后輸入(import)到Java編輯軟件,如eclipse。直接運行該程序。
          /*
           * Author: ZHUANG JIN YI_91237 
           * This program is a simple game application (GUI) that allows two players compete with each other 
           * by guessing a word from a pool of 10 letters. Players can choose to pass to next player or choose to quit to end this game :)
           * 2010 Copyright Zhuang Jinyi, Jason. 
           */

          import java.util.*;
          import javax.swing.*;

          public class FindYourWords 
          {
          public static void main(String[] args) 
          {
          Scanner console = new Scanner (System.in);
          Random generator = new Random();
          String [] dict= FileUtil.readDictFromFile("words.txt");
          int scorePlayerA = 0;
          int scorePlayerB = 0;
          int sumScoreA = 0;
          int sumScoreB = 0;
          int i;
          int match = 0;
          int match1 = 0;
          int match2 = 0;
          int match3 = 0;
          int[] ranNum = new int[8];
          int[] ranNumVowel = new int[2];
          String junk;
          String[] letters = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "g", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
          String[] vowels = {"a", "e", "i", "o", "u"};
          String[] randomLetter = new String[8];
          String[] randomVowel = new String[2];
          String playerAInput = "";
          String playerBInput = "";
          String[] point1 = {"E" ,"A" ,"O" ,"T", "I", "N", "R", "S", "L", "U"};
          String[] point2 = {"D", "G"};
          String[] point3 = {"C", "M","B", "P"};
          String[] point4 = {"H","F", "W", "Y"}; 
          String[] point5 = {"K"}; 
          String[] point8 = {"J", "X"};  
          String[] point10 = {"Q", "Z"}; 

          JOptionPane.showMessageDialog (null, "-----------------------------------------Game: Find Your Words-----------------------------------------\n" + "=======================Ready to Play? Click 'Ok'=======================", "Find Your Words", JOptionPane.INFORMATION_MESSAGE);
          try
          {
          while (!playerAInput.equalsIgnoreCase("!") && !playerBInput.equalsIgnoreCase("!"))
          {
          if (!playerAInput.equalsIgnoreCase("!") || playerBInput.equalsIgnoreCase("@"))
          {
          //random letters generation  
          for (i = 0; i < ranNum.length; i++ )
          {
          ranNum[i] = generator.nextInt(26);
          randomLetter[i] = letters[ranNum[i]];
          }
          for (i = 0; i < ranNumVowel.length; i++ )
          {
          ranNum[i] = generator.nextInt(5);
          randomVowel[i] = vowels[ranNum[i]];
          }
          //store random letters & prompt user input 
          String[] randomQues = {randomLetter[0], randomVowel[1], randomLetter[1], randomVowel[0], randomLetter[2], randomLetter[3], randomLetter[4], randomLetter[5], randomLetter[6], randomLetter[7]};
          //GUI dialog box 
          String randomLetters = "Letters of Player A: " + randomLetter[0] + " " + randomVowel[1] + " " + randomLetter[1] + " " + randomVowel[0] + " " + randomLetter[2] + " " + randomLetter[3] + " " +  randomLetter[4] + " " + randomLetter[5] + " " + randomLetter[6] + " " + randomLetter[7];
          playerAInput = JOptionPane.showInputDialog (randomLetters + "\n" + "Enter your word (or ‘@’ to pass or ‘!’ to quit): ");
          String[] playerA = new String[playerAInput.length()];
          playerA = playerAInput.split("");
          /*
          for (i = 0; i < playerA.length; i++)
          {
          System.out.println(playerA[i]);
          }
          System.out.println(playerA.length);
          */
          while (!playerAInput.equalsIgnoreCase("@") && !playerAInput.equalsIgnoreCase("!"))
          {
          //checking validity part 
          scorePlayerA = 0;
          match = 0;
          match1 = 0;
          for (i = 0; i < dict.length; i++)
          {
          if (dict[i].equalsIgnoreCase(playerAInput))
          {
          match += 1;
          }
          else
          {
          match += 0;
          }
          }
          if (match >= 1)
          {
          for (i = 0; i < playerA.length; i++)
          {
          for (int m = 0; m < randomQues.length; m++)
          {
          if (playerA[i].equalsIgnoreCase(randomQues[m]))
          {
          match1 += 1;
          break;
          }
          }
          }
          if (match1 >= (playerA.length - 1))
          {
          //Giving points to players
          for (i = 0; i < playerA.length; i++)
          {
          for (int m = 0; m < point1.length; m++)
          {
          if (point1[m].equalsIgnoreCase(playerA[i]))
          {
          scorePlayerA += 1;
          }
          }
          }
          for (i = 0; i < playerA.length; i++)
          {
          for (int m = 0; m < point2.length; m++)
          {
          if (point2[m].equalsIgnoreCase(playerA[i]))
          {
          scorePlayerA += 2;
          }
          }
          }
          for (i = 0; i < playerA.length; i++)
          {
          for (int m = 0; m < point3.length; m++)
          {
          if (point3[m].equalsIgnoreCase(playerA[i]))
          {
          scorePlayerA += 3;
          }
          }
          }
          for (i = 0; i < playerA.length; i++)
          {
          for (int m = 0; m < point4.length; m++)
          {
          if (point4[m].equalsIgnoreCase(playerA[i]))
          {
          scorePlayerA += 4;
          }
          }
          }
          for (i = 0; i < playerA.length; i++)
          {
          for (int m = 0; m < point5.length; m++)
          {
          if (point5[m].equalsIgnoreCase(playerA[i]))
          {
          scorePlayerA += 5;
          }
          }
          }
          for (i = 0; i < playerA.length; i++)
          {
          for (int m = 0; m < point8.length; m++)
          {
          if (point8[m].equalsIgnoreCase(playerA[i]))
          {
          scorePlayerA += 8;
          }
          }
          }
          for (i = 0; i < playerA.length; i++)
          {
          for (int m = 0; m < point10.length; m++)
          {
          if (point10[m].equalsIgnoreCase(playerA[i]))
          {
          scorePlayerA += 10;
          }
          }
          }
          sumScoreA += scorePlayerA;
          /*
          System.out.println("Total score for word: " + scorePlayerA);
          System.out.println("Total score for game: " + sumScoreA);
          */
          String result = "Total score for word: " + scorePlayerA + "\n" + "Total score for game: " + sumScoreA;
          JOptionPane.showMessageDialog(null, result, "This Round Result :)", JOptionPane.INFORMATION_MESSAGE);
          break;
          }
          else
          {
          JOptionPane.showMessageDialog(null, "Error : A valid word is formed but one or more letter(s) used are not yours.", "Error :(", JOptionPane.QUESTION_MESSAGE);
          playerAInput = JOptionPane.showInputDialog (randomLetters + "\n" + "Enter your word (or ‘@’ to pass or ‘!’ to quit): ");
          playerA = playerAInput.split("");
          }
          }
          else
          {
          JOptionPane.showMessageDialog(null, "Error : An invalid word is formed.", "Error :(", JOptionPane.ERROR_MESSAGE);
          playerAInput = JOptionPane.showInputDialog (randomLetters + "\n" + "Enter your word (or ‘@’ to pass or ‘!’ to quit): ");
          playerA = playerAInput.split("");
          }
          }
          }
          if (!playerAInput.equalsIgnoreCase("!") || playerAInput.equalsIgnoreCase("@"))
          {
          //random letters generation 
          for (i = 0; i < ranNum.length; i++ )
          {
          ranNum[i] = generator.nextInt(26);
          randomLetter[i] = letters[ranNum[i]];
          }
          for (i = 0; i < ranNumVowel.length; i++ )
          {
          ranNum[i] = generator.nextInt(5);
          randomVowel[i] = vowels[ranNum[i]];
          }
          //store random letters & prompt user input 
          String[] randomQues1 = {randomLetter[0], randomVowel[1], randomLetter[1], randomVowel[0], randomLetter[2], randomLetter[3], randomLetter[4], randomLetter[5], randomLetter[6], randomLetter[7]};
          //GUI dialog box 
          String randomLetters1 = "Letters of Player B: " + randomLetter[0] + " " + randomVowel[1] + " " + randomLetter[1] + " " + randomVowel[0] + " " + randomLetter[2] + " " + randomLetter[3] + " " +  randomLetter[4] + " " + randomLetter[5] + " " + randomLetter[6] + " " + randomLetter[7];
          playerBInput = JOptionPane.showInputDialog (randomLetters1 + "\n" + "Enter your word (or ‘@’ to pass or ‘!’ to quit): ");
          String[] playerB = new String[playerBInput.length()];
          playerB = playerBInput.split("");

          while (!playerBInput.equalsIgnoreCase("@") && !playerBInput.equalsIgnoreCase("!"))
          {
          //checking validity part 
          scorePlayerB = 0;
          match2 = 0;
          match3 = 0;
          for (i = 0; i < dict.length; i++)
          {
          if (dict[i].equalsIgnoreCase(playerBInput))
          {
          match2 += 1;
          }
          else
          {
          match2 += 0;
          }
          }
          if (match2 >= 1)
          {
          for (i = 0; i < playerB.length; i++)
          {
          for (int m = 0; m < randomQues1.length ; m++)
          {
          if (playerB[i].equalsIgnoreCase(randomQues1[m]))
          {
          match3 += 1;
          break;
          }
          }
          }
          if (match3 >= (playerB.length - 1))
          {
          //Giving points to players
          for (i = 0; i < playerB.length; i++)
          {
          for (int m = 0; m < point1.length; m++)
          {
          if (point1[m].equalsIgnoreCase(playerB[i]))
          {
          scorePlayerB += 1;
          }
          }
          }
          for (i = 0; i < playerB.length; i++)
          {
          for (int m = 0; m < point2.length; m++)
          {
          if (point2[m].equalsIgnoreCase(playerB[i]))
          {
          scorePlayerB += 2;
          }
          }
          }
          for (i = 0; i < playerB.length; i++)
          {
          for (int m = 0; m < point3.length; m++)
          {
          if (point3[m].equalsIgnoreCase(playerB[i]))
          {
          scorePlayerB += 3;
          }
          }
          }
          for (i = 0; i < playerB.length; i++)
          {
          for (int m = 0; m < point4.length; m++)
          {
          if (point4[m].equalsIgnoreCase(playerB[i]))
          {
          scorePlayerB += 4;
          }
          }
          }
          for (i = 0; i < playerB.length; i++)
          {
          for (int m = 0; m < point5.length; m++)
          {
          if (point5[m].equalsIgnoreCase(playerB[i]))
          {
          scorePlayerB += 5;
          }
          }
          }
          for (i = 0; i < playerB.length; i++)
          {
          for (int m = 0; m < point8.length; m++)
          {
          if (point8[m].equalsIgnoreCase(playerB[i]))
          {
          scorePlayerB += 8;
          }
          }
          }
          for (i = 0; i < playerB.length; i++)
          {
          for (int m = 0; m < point10.length; m++)
          {
          if (point10[m].equalsIgnoreCase(playerB[i]))
          {
          scorePlayerB += 10;
          }
          }
          }
          sumScoreB += scorePlayerB;
          String result1 = "Total score for word: " + scorePlayerB + "\n" + "Total score for game: " + sumScoreB;
          JOptionPane.showMessageDialog(null, result1, "This Round Result :)", JOptionPane.INFORMATION_MESSAGE);
          break;
          }
          else
          {
          JOptionPane.showMessageDialog(null, "Error : A valid word is formed but one or more letter(s) used are not yours.", "Error :(", JOptionPane.QUESTION_MESSAGE);
          playerBInput = JOptionPane.showInputDialog (randomLetters1 + "\n" + "Enter your word (or ‘@’ to pass or ‘!’ to quit): ");
          playerB = playerBInput.split("");
          }
          }
          else
          {
          JOptionPane.showMessageDialog(null, "Error : An invalid word is formed.", "Error :(", JOptionPane.ERROR_MESSAGE);
          playerBInput = JOptionPane.showInputDialog (randomLetters1 + "\n" + "Enter your word (or ‘@’ to pass or ‘!’ to quit): ");
          playerB = playerBInput.split("");
          }
          }
          }
          }
          //Calculate and compare the final result
          if (sumScoreA > sumScoreB)
          {
          String finalResult = "Total score for Player A: " + sumScoreA + "\n" + "Total score for Player B: " + sumScoreB + "\n" + "Player A wins!";
          JOptionPane.showMessageDialog (null, "(: *********************Final Result********************* :)\n" + finalResult, "Final Result :)", JOptionPane.INFORMATION_MESSAGE);
          }
          else if (sumScoreA < sumScoreB)
          {
          String finalResult = "Total score for Player A: " + sumScoreA + "\n" + "Total score for Player B: " + sumScoreB + "\n" + "Player B wins!";
          JOptionPane.showMessageDialog (null, "(: *********************Final Result********************* :)\n" + finalResult, "Final Result :)", JOptionPane.INFORMATION_MESSAGE);
          }
          else if (sumScoreA == sumScoreB)
          {
          String finalResult = "Total score for Player A: " + sumScoreA + "\n" + "Total score for Player B: " + sumScoreB + "\n" + "The result is a draw!";
          JOptionPane.showMessageDialog (null, "(: *********************Final Result********************* :)\n" + finalResult, "Final Result :)", JOptionPane.INFORMATION_MESSAGE);
          }
          }
          catch (Exception e)
          {
          junk = console.nextLine();
          String errorMessage = "Error, " + junk + " is not acceptable!" + "\n" + "Sorry, Game Over" + "\n" + "Please run this program again :)";
          JOptionPane.showMessageDialog (null, errorMessage, "Error Message :(", JOptionPane.INFORMATION_MESSAGE);
          }
          }
          }
          PS: good night :)

          posted @ 2010-11-23 22:40 浦風 閱讀(979) | 評論 (1)編輯 收藏


          posts - 1, comments - 1, trackbacks - 0, articles - 1

          Copyright © 浦風

          主站蜘蛛池模板: 扎兰屯市| 苏尼特右旗| 民勤县| 西林县| 玉田县| 天水市| 阿勒泰市| 洞口县| 彭水| 双城市| 奉节县| 通渭县| 荥经县| 紫阳县| 金昌市| 宜州市| 云南省| 镇平县| 成武县| 滕州市| 大厂| 剑河县| 岳阳市| 玉环县| 鄂伦春自治旗| 武功县| 泰安市| 锡林浩特市| 尚志市| 革吉县| 霍林郭勒市| 辽中县| 商丘市| 天峻县| 宜昌市| 钦州市| 通渭县| 张北县| 闽侯县| 集安市| 高唐县|