有什么方法可以更好地格式化我的代码吗?我对编程非常陌生,我想学习这个程序工作的最佳方法。例如:如果单词不包含在我的Words.java中,那么告诉用户他们试图输入的单词不存在。
import java.util.Scanner;
public class WordleApplication
{
static String word = Words.words[(int)(Math.random() * Words.words.length)];
static Scanner s = new Scanner(System.in);
static String guess = "";
static int g = 6;
static String p1Name = "";
static String p2Name = "";
static int p1Score = 0;
static int p2Score = 0;
public static void main(String[] args)
{
Rules.rules();
// System.out.println("Please enter your name Player 1");
// Player p1 = new Player(s.nextLine(), 0);
// System.out.println(p1.getName());
}
public static void player(){
System.out.println("Player 1 Enter your name: ");
Player p1 = new Player(s.nextLine(), p1Score);
p1Name = p1.getName();
System.out.println();
System.out.println("Player 2 Enter your name: ");
Player p2 = new Player(s.nextLine(), p2Score);
p2Name = p2.getName();
p1Game();
p2Game();
}
public static void p1Game() {
System.out.println("Alright " + p1Name + " you're up!");
while (!guess.equals(word) && g > 0) {
System.out.println("\nGuesses Left: " + g);
guess = s.nextLine().toLowerCase();
if (guess.length() != 5) {
System.out.println("Must be 5 characters");
continue;
}
for (int i = 0; i < word.length(); i++) {
boolean didBreak = false;
for (int j = 0; j < word.length(); j++) {
if (guess.charAt(i) == word.charAt(i)) {
System.out.print("Y");
didBreak = true;
break;
}
if (guess.charAt(i) == word.charAt(j)) {
System.out.print("C");
didBreak = true;
break;
}
}
if (!didBreak) {
System.out.print("X");
}
}
System.out.println("\n" + word); //test
g--;
if(guess.equals(word)){
p1Score += 10;
System.out.println("Nicely done! You have " + p1Score + " points!");
p2Game();
}
if(g == 0){
System.out.println("\nThe word was: " + word);
p2Game();
}
}
}
public static void p2Game() {
System.out.println("Alright " + p2Name + " you're up!");
while (!guess.equals(word) && g > 0) {
System.out.println("\nGuesses Left: " + g);
guess = s.nextLine().toLowerCase();
if (guess.length() != 5) {
System.out.println("Must be 5 characters");
continue;
}
for (int i = 0; i < word.length(); i++) {
boolean didBreak = false;
for (int j = 0; j < word.length(); j++) {
if (guess.charAt(i) == word.charAt(i)) {
System.out.print("Y");
didBreak = true;
break;
}
if (guess.charAt(i) == word.charAt(j)) {
System.out.print("C");
didBreak = true;
break;
}
}
if (!didBreak) {
System.out.print("X");
}
}
System.out.println("\n" + word); //test
g--;
if(guess.equals(word)){
System.out.println("Nicely done!");
p2Score += 10;
}
if(g == 0){
System.out.println("\nThe word was: " + word);
}
}
}
public static void results(){
System.out.println("test");
}
}只是用来向用户描述什么的规则。
import java.util.Scanner;
public class Rules {
public static void rules() {
Scanner s = new Scanner(System.in);
String enterkey;
System.out.println("Welcome to Wordle!");
System.out.println("Each player will be given 6 tries to guess a 5 letter word.");
System.out.println("If you can't guess the word in 6 tries. GAME OVER :(");
System.out.println("Do you have what it takes to guess the word?");
System.out.println("Go ahead and press y when you're ready!");
while(true){
enterkey = s.nextLine();
if(!enterkey.equals("y")){
System.out.println("Please press y to continue...");
continue;
}
if(enterkey.equals("y")){
WordleApplication.player();
}
}
}
}public class Player {
protected String name;
protected int score;
public Player(String name, int score) {
this.name = name;
this.score = 10;
}
public String getName() {
return name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public String toString() {
return name + ": " + score;
}
}public class Words {
public static String[] words = {
"abuse",
"adult",
"agent",
"anger",
"apple",
"award",
"basis",
"beach",
"birth",
"block",
"blood",
"board",
"brain",
"bread",
"break",
"brown",
"buyer",
"cause",
"chain",
"chair",
"chest",
"chief",
"child",
"china",
"claim",
"class",
"clock",
"coach",
"coast",
"court",
"cover",
"cream",
"crime",
"cross",
"crowd",
"crown",
"cycle",
"dance",
"death",
"depth",
"doubt",
"draft",
"drama",
"dream",
"dress",
"drink",
"drive",
"earth",
"enemy",
"entry",
"error",
"event",
"faith",
"fault",
"field",
"fight",
"final",
"floor",
"focus",
"force",
"frame",
"frank",
"front",
"fruit",
"glass",
"grant",
"grass",
"green",
"group",
"guide",
"heart",
"henry",
"horse",
"hotel",
"house",
"image",
"index",
"input",
"issue",
"japan",
"jones",
"judge",
"knife",
"laura",
"layer",
"level",
"lewis",
"light",
"limit",
"lunch",
"major",
"march",
"match",
"metal",
"model",
"money",
"month",
"motor",
"mouth",
"music",
"night",
"noise",
"north",
"novel",
"nurse",
"offer",
"order",
"other",
"owner",
"panel",
"paper",
"party",
"peace",
"peter",
"phase",
"phone",
"piece",
"pilot",
"pitch",
"place",
"plane",
"plant",
"plate",
"point",
"pound",
"power",
"press",
"price",
"pride",
"prize",
"proof",
"queen",
"radio",
"range",
"ratio",
"reply",
"right",
"river",
"round",
"route",
"rugby",
"scale",
"scene",
"scope",
"score",
"sense",
"shape",
"share",
"sheep",
"sheet",
"shift",
"shirt",
"shock",
"sight",
"simon",
"skill",
"sleep",
"smile",
"smith",
"smoke",
"sound",
"south",
"space",
"speed",
"spite",
"sport",
"squad",
"staff",
"stage",
"start",
"state",
"steam",
"steel",
"stock",
"stone",
"store",
"study",
"stuff",
"style",
"sugar",
"table",
"taste",
"terry",
"theme",
"thing",
"title",
"total",
"touch",
"tower",
"track",
"trade",
"train",
"trend",
"trial",
"trust",
"truth",
"uncle",
"union",
"unity",
"value",
"video",
"visit",
"voice",
"waste",
"watch",
"water",
"while",
"white",
"whole",
"woman",
"world",
"youth",
"alcon",
"aught",
"hella",
"one’s",
"ought",
"thame",
"there",
"thine",
"thine",
"where",
"which",
"whose",
"whoso",
"yours",
"yours",
"admit",
"adopt",
"agree",
"allow",
"alter",
"apply",
"argue",
"arise",
"avoid",
"begin",
"blame",
"break",
"bring",
"build",
"burst",
"carry",
"catch",
"cause",
"check",
"claim",
"clean",
"clear",
"climb",
"close",
"count",
"cover",
"cross",
"dance",
"doubt",
"drink",
"drive",
"enjoy",
"enter",
"exist",
"fight",
"focus",
"force",
"guess",
"imply",
"issue",
"judge",
"laugh",
"learn",
"leave",
"let’s",
"limit",
"marry",
"match",
"occur",
"offer",
"order",
"phone",
"place",
"point",
"press",
"prove",
"raise",
"reach",
"refer",
"relax",
"serve",
"shall",
"share",
"shift",
"shoot",
"sleep",
"solve",
"sound",
"speak",
"spend",
"split",
"stand",
"start",
"state",
"stick",
"study",
"teach",
"thank",
"think",
"throw",
"touch",
"train",
"treat",
"trust",
"visit",
"voice",
"waste",
"watch",
"worry",
"would",
"write",
"above",
"acute",
"alive",
"alone",
"angry",
"aware",
"awful",
"basic",
"black",
"blind",
"brave",
"brief",
"broad",
"brown",
"cheap",
"chief",
"civil",
"clean",
"clear",
"close",
"crazy",
"daily",
"dirty",
"early",
"empty",
"equal",
"exact",
"extra",
"faint",
"false",
"fifth",
"final",
"first",
"fresh",
"front",
"funny",
"giant",
"grand",
"great",
"green",
"gross",
"happy",
"harsh",
"heavy",
"human",
"ideal",
"inner",
"joint",
"large",
"legal",
"level",
"light",
"local",
"loose",
"lucky",
"magic",
"major",
"minor",
"moral",
"naked",
"nasty",
"naval",
"other",
"outer",
"plain",
"prime",
"prior",
"proud",
"quick",
"quiet",
"rapid",
"ready",
"right",
"roman",
"rough",
"round",
"royal",
"rural",
"sharp",
"sheer",
"short",
"silly",
"sixth",
"small",
"smart",
"solid",
"sorry",
"spare",
"steep",
"still",
"super",
"sweet",
"thick",
"third",
"tight",
"total",
"tough",
"upper",
"upset",
"urban",
"usual",
"vague",
"valid",
"vital",
"white",
"whole",
"wrong",
"young",
"afore",
"after",
"bothe",
"other",
"since",
"slash",
"until",
"where",
"while",
"aback",
"abaft",
"aboon",
"about",
"above",
"accel",
"adown",
"afoot",
"afore",
"afoul",
"after",
"again",
"agape",
"agogo",
"agone",
"ahead",
"ahull",
"alife",
"alike",
"aline",
"aloft",
"alone",
"along",
"aloof",
"aloud",
"amiss",
"amply",
"amuck",
"apace",
"apart",
"aptly",
"arear",
"aside",
"askew",
"awful",
"badly",
"bally",
"below",
"canny",
"cheap",
"clean",
"clear",
"coyly",
"daily",
"dimly",
"dirty",
"ditto",
"drily",
"dryly",
"dully",
"early",
"extra",
"false",
"fatly",
"feyly",
"first",
"fitly",
"forte",
"forth",
"fresh",
"fully",
"funny",
"gaily",
"gayly",
"godly",
"great",
"haply",
"heavy",
"hella",
"hence",
"hotly",
"icily",
"infra",
"intl.",
"jildi",
"jolly",
"laxly",
"lento",
"light",
"lowly",
"madly",
"maybe",
"never",
"newly",
"nobly",
"oddly",
"often",
"other",
"ought",
"party",
"piano",
"plain",
"plonk",
"plumb",
"prior",
"queer",
"quick",
"quite",
"ramen",
"rapid",
"redly",
"right",
"rough",
"round",
"sadly",
"secus",
"selly",
"sharp",
"sheer",
"shily",
"short",
"shyly",
"silly",
"since",
"sleek",
"slyly",
"small",
"so-so",
"sound",
"spang",
"srsly",
"stark",
"still",
"stone",
"stour",
"super",
"tally",
"tanto",
"there",
"thick",
"tight",
"today",
"tomoz",
"truly",
"twice",
"under",
"utter",
"verry",
"wanly",
"wetly",
"where",
"wrong",
"wryly",
"abaft",
"aboon",
"about",
"above",
"adown",
"afore",
"after",
"along",
"aloof",
"among",
"below",
"circa",
"cross",
"furth",
"minus",
"neath",
"round",
"since",
"spite",
"under",
"until",
"aargh",
"adieu",
"adios",
"alack",
"aloha",
"avast",
"bakaw",
"basta",
"begad",
"bless",
"blige",
"brava",
"bravo",
"bring",
"chook",
"damme",
"dildo",
"ditto",
"frick",
"fudge",
"golly",
"gratz",
"hallo",
"hasta",
"havoc",
"hella",
"hello",
"howay",
"howdy",
"hullo",
"huzza",
"jesus",
"kapow",
"loose",
"lordy",
"marry",
"mercy",
"night",
"plonk",
"psych",
"quite",
"salve",
"skoal",
"sniff",
"sooey",
"there",
"thiam",
"thwap",
"tough",
"twirp",
"viola",
"vivat",
"wacko",
"wahey",
"whist",
"wilma",
"wirra",
"woops",
"wowie",
"yecch",
"yeeha",
"yeesh",
"yowch",
"zowie"
};
}发布于 2022-05-14 02:43:20
好吧,它会传到p2Game,因为你对它说了好几遍。
在player()中,您调用p1Game,然后调用p2Game,这是很酷的,这是可行的。当p1Game完成时,它返回到player,下一行是p2Game,这可能是您想要的。
但是,您可以在p2Game中多次调用p1Game,这可能是造成问题的原因。在p1Game中调用p2Game的两次可能是不必要的。
现在发生的是,玩家1猜测正确的单词。你说“干得好!”然后打电话给p2Game。玩家2有一个尝试,当那个结束时,控制回到p1Game进行,它推出p1Game最终完成,回到player和p2Game得到另一个调用。
因此,在p2Game方法中丢弃对p1Game的调用。
在更好地组织代码方面。是的,有几件事:
p1Game和p2Game看上去几乎一模一样。如果您发现自己正在编写这样的代码,那么您应该考虑另一种方法。有一个叫做"DRY --当您使用像Java这样的面向对象语言时不要重复Yourself"static“的编程原则。不如在1986年写BASIC。--通常,main方法是唯一的static方法。
public static void main()
{
WordleApplication wa = new WordleApplication();
wa.player();
}您可以将static从您的player方法中删除。
更改p1Game方法,以便它接受一个Player对象作为参数:
public static void OneGame(Player p) {
System.out.println("Alright " + p.getName() + " you're up!");
while (!guess.equals(word) && g > 0) {
System.out.println("\nGuesses Left: " + g);
...看看它是如何使用“you‘s up”行中的名称参数的吗?将其扩展到整个方法中。
因此,在player方法的底部,而不是
p1Game()
p2Game()你可以代替
OneGame (p1);
OneGame (p2);这意味着您可以完全删除p2Game。这很好,因为少代码就是更好的代码。
静态变量也是不好的形式。它们基本上是应用程序中的“全局变量”,一旦应用程序变得更大,就很难跟踪全局变量。最好是尽量减少谁可以修改变量。g就是一个例子。这是游戏中只需要的方法。因此,如果将它移到OneGame方法的开头,并将其设置为6,那么就很好了。事实上,两名球员之间似乎有6次猜测。如果玩家1进行5次猜测,玩家2只会得到1次猜测。
所以
guess和g,可能还有word,除非您希望双方猜测同一个单词?p1Name参数的getName()方法,而不是p变量。使用getScore和setScore代替p1Score
看来你已经在这方面下了很大功夫了。我觉得再多一点,一切都会好起来的。
祝好运。
https://stackoverflow.com/questions/72236544
复制相似问题