java猜单词游戏_Java_初级编程,猜英文单词游戏

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

21aa0497595712d43a1437159c492580.png

下面的Java程序是我学习Java初期的一个作业:猜英文单词游戏,作为我Blog第一篇随笔,这个程序也可以直接点击http://www.blogjava.net/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 :)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值