java猜单词游戏_Java_初级编程,猜英文单词游戏
大家好,这是我第一次在BlogJava发帖,本人酷爱编程,加入BlogJava社区,就是为了认识结交更多有共同兴趣爱好的朋友,交流彼此学习编程的经验,请大家多多支持我
下面的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 :)
java猜单词游戏_Java_初级编程,猜英文单词游戏相关推荐
- 猜数游戏c语言编程while,【游戏编程】猜数字游戏(C语言)
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 //此游戏规则为:系统随机生成一个整数,然后要你去猜它.系统会提示你是大了还是小了.游戏结束后会生成排行榜,有记录时间 #include #include ...
- 猜数游戏c语言编程_做游戏,学编程(C语言) 10 僵尸危机
这个案例,分享14级同学大一时实现的打僵尸小游戏,电脑点击下图可以看到动图效果: 这个案例的代码不复杂,但是结合了游戏角色的动画效果.射击声音特效,实现后还是挺酷的.对应的游戏素材.分步骤代码,可从百 ...
- c语言经典编程案例猜数字,用c语言编程猜数字
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 //NumberRiddle.c //这个程序还没有做完,用户输入检测部分没有做,不继续了.2009-12-22 //猜数字游戏 //游戏规则: //电脑 ...
- c语言1000以内猜数字,用c语言编程猜数字
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 //NumberRiddle.c //这个程序还没有做完,用户输入检测部分没有做,不继续了.2009-12-22 //猜数字游戏 //游戏规则: //电脑 ...
- 听说玩这些游戏能提升编程能力?
游戏 == 编程 >>> True 电子游戏和编程充满联系.从雅达利兴盛的年代开始,游戏就已经开始影响一代又一代程序员.他们开发游戏,在游戏过程中获得快乐.随着机器学习的兴起,程序员 ...
- java猜单词游戏_9 java基础综合编程练习---猜单词游戏
配套视频教程 image.png 任务目的 1.掌握字符串常用操作 2.掌握随机数的用法 3.掌握控制台基本输入输出 4.掌握循环,分支条件的用法 5.培养编程思维,提高分析问题能力` 任务描述 im ...
- Java黑皮书课后题第7章:***7.35(游戏:猜字词游戏)编写一个猜字词游戏,随机产生一个单词,当用户猜测正确后,正确的字母显示出来。当用户猜出一个单词,显示猜错的次数,并询问用户是否继续猜测
***7.35(游戏:猜字词游戏)编写一个猜字词游戏,随机产生一个单词,当用户猜测正确后,正确的字母显示出来.当用户猜出一个单词,显示猜错的次数,并询问用户是否继续猜测 题目 题目描述与运行示例 破题 ...
- 编程猜单词游戏python_Python实现简单的猜单词小游戏
本文实例为大家分享了Python实现猜单词小游戏的具体代码,供大家参考,具体内容如下 思路 1.一个words列表里存放若干的单词,例如:["extends", "pri ...
- java猜单词游戏_序列应用——猜单词游戏
开发工具:Visual Studio Code 1.游戏介绍 猜单词游戏就是计筧机随机产生一个单词,打乱字母顺序,供玩家去猜测.此游戏采用控制字符界面. 2.程序设计思路 游戏中,可使用序列中的元组存 ...
最新文章
- 区块链的去中心化VS传统互联网的去中心化:技术与治理的双重困境
- Pinterest 谈实战经验:如何在两年内实现零到数百亿的月访问
- Python的open函数文件读写线程不安全,logging模型文件读写线程安全!
- c语言图片合并 代码,帮个忙,合并代码
- 20321关系数据库理论基础
- Android中软键盘(输入法)收起的方法
- python删除指定天数前的文件_python 删除指定时间间隔之前的文件实例
- PyQt5应用与实践
- java执行linux脚本 并返回其执行结果
- Java日历compareTo()方法与示例
- [Comet OJ - Contest #7 D][52D 2417]机器学习题_斜率优化dp
- 帆软报表嵌套在iframe中,HTML的fieldset 定义的为自定义导出按钮
- U盘一键直接安装原版win7
- 纸筒制作机器人_卡纸手工制作方法_机器人DIY制作教程图解
- java弹弹球实验报告_Java程序设计实验报告2(弹球游戏)1
- 《魔兽世界插件》教程—21点扑克游戏 Blackjack
- 【前辈经验】——前端职位描述
- linux修改用户描述的命令,Linux修改用户信息(usermod)
- node.js命令行程序在Windows系统和Linux系统下的部署
- 操作系统文件管理实验