大家好,这是我第一次在BlogJava发帖,本人酷爱编程,加入BlogJava社区,就是为了认识结交更多有共同兴趣爱好的朋友,交流彼此学习编程的经验,请大家多多支持我
/*
* 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 :)