gpt4 book ai didi

java - 从数组中删除一个单词

转载 作者:行者123 更新时间:2023-11-29 21:08:59 24 4
gpt4 key购买 nike

我根据此处的教程构建了一个刽子手游戏。它从游戏数组中随机选择一个单词。它被设置为不连续两次选择同一个词。问题是它会在玩几个游戏时多次选择同一个词。有没有一种方法可以标记或标记一个词,以便在使用一次后,它不会在同一游戏 session 中再次使用?

我想我需要一个 ArrayList,但我想不出代码。我一直在搜索和尝试了一个星期,但我就是做不到。我真的很努力地自己寻找答案。

完整代码

package com.slideproud.hangman;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;



public class GameActivity extends Activity {

//the words
private String[] words;
//random for word selection
private Random rand;
//store the current word
private String currWord;
//the layout holding the answer
private LinearLayout wordLayout;
//text views for each letter in the answer
private TextView[] charViews;
//letter button grid
private GridView letters;
//body part images
private ImageView[] bodyParts;
//total parts
private int numParts = 6;
//current part
private int currPart;
//num chars in word
private int numChars;
//num correct so far
private int numCorr;
//help
private AlertDialog helpAlert;

public GameActivity() {
}
//body part images



@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);

//read answer words in
Resources res = getResources();
ArrayList<String> words = new ArrayList<String>();
words.addAll(Arrays.asList(res.getStringArray(R.array.words)));
//initialize random
rand = new Random();
//initialize word


//read answer words in
// Resources res = getResources();
// words = res.getStringArray(R.array.words);
//initialize random
//rand = new Random();
//initialize word
// currWord = "";

//get answer area
wordLayout = (LinearLayout) findViewById(R.id.word);

//get letter button grid
letters = (GridView) findViewById(R.id.letters);

//get body part images
bodyParts = new ImageView[numParts];
bodyParts[0] = (ImageView) findViewById(R.id.head);
bodyParts[1] = (ImageView) findViewById(R.id.body);
bodyParts[2] = (ImageView) findViewById(R.id.arm1);
bodyParts[3] = (ImageView) findViewById(R.id.arm2);
bodyParts[4] = (ImageView) findViewById(R.id.leg1);
bodyParts[5] = (ImageView) findViewById(R.id.leg2);




//start gameplay
playGame();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.action_help:
showHelp();
return true;
}
return super.onOptionsItemSelected(item);
}

private void playGame() {
//choose a word
int random = rand.nextInt(words.length);
String newWord = words.get(random);
words.remove(random);
currWord = newWord;


//play a new game
//private void playGame() {

//choose a word
//String newWord = words[rand.nextInt(words.length)];
//make sure not same word as last time
// while (newWord.equals(currWord)) newWord = words[rand.nextInt(words.length)];
//update current word
//newWord.remove(0);
//currWord = newWord;

//create new array for character text views
charViews = new TextView[currWord.length()];

//remove any existing letters
wordLayout.removeAllViews();
//loop through characters
for (int c = 0; c < currWord.length(); c++) {
charViews[c] = new TextView(this);
charViews[c].setText(""+currWord.charAt(c));

charViews[c].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
charViews[c].setGravity(Gravity.CENTER);
charViews[c].setTextColor(Color.WHITE);
charViews[c].setBackgroundResource(R.drawable.letter_bg);
//add to layout
wordLayout.addView(charViews[c]);
}


//reset adapter
LetterAdapter ltrAdapt = new LetterAdapter(this);
letters.setAdapter(ltrAdapt);

//start part at zero
currPart = 0;
//set word length and correct choices
numChars = currWord.length();
numCorr = 0;

//hide all parts
for (int p = 0; p < numParts; p++) {
bodyParts[p].setVisibility(View.INVISIBLE);
}
}

//letter pressed method
public void letterPressed(View view) {
//find out which letter was pressed
String ltr = ((TextView) view).getText().toString();
char letterChar = ltr.charAt(0);
//disable view
view.setEnabled(false);
view.setBackgroundResource(R.drawable.letter_down);
//check if correct
boolean correct = false;
for (int k = 0; k < currWord.length(); k++) {
if (currWord.charAt(k) == letterChar) {
correct = true;
numCorr++;
charViews[k].setTextColor(Color.BLACK);
}
}
//check in case won
if (correct) {
if (numCorr == numChars) {
//disable all buttons
disableBtns();
//let user know they have won, ask if they want to play again
AlertDialog.Builder winBuild = new AlertDialog.Builder(this);
winBuild.setTitle("YAY");
winBuild.setMessage("You win!\n\nThe answer was:\n\n" + currWord);
winBuild.setPositiveButton("Play Again",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
GameActivity.this.playGame();
}
}
);
winBuild.setNegativeButton("Exit",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
GameActivity.this.finish();
}
}
);
winBuild.show();
}
}
//check if user still has guesses
else if (currPart < numParts) {
//show next part
bodyParts[currPart].setVisibility(View.VISIBLE);
currPart++;
} else {
//user has lost
disableBtns();
//let the user know they lost, ask if they want to play again
AlertDialog.Builder loseBuild = new AlertDialog.Builder(this);
loseBuild.setTitle("OOPS");
loseBuild.setMessage("You lose!\n\nThe answer was:\n\n" + currWord);
loseBuild.setPositiveButton("Play Again",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
GameActivity.this.playGame();
}
}
);
loseBuild.setNegativeButton("Exit",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
GameActivity.this.finish();
}
}
);
loseBuild.show();
}
}

//disable letter buttons
public void disableBtns() {
int numLetters = letters.getChildCount();
for (int l = 0; l < numLetters; l++) {
letters.getChildAt(l).setEnabled(false);
}
}

//show help information
public void showHelp() {
AlertDialog.Builder helpBuild = new AlertDialog.Builder(this);
helpBuild.setTitle("Help");
helpBuild.setMessage("Guess the word by selecting the letters.\n\n"
+ "You only have 6 wrong selections then it's game over!");
helpBuild.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
helpAlert.dismiss();
}
}
);
helpAlert = helpBuild.create();
helpBuild.show();
}

public void setWords(String[] words) {
this.words = words;
}
}

这是我的错误错误:任务 ':HangMan:compileDebugJava' 执行失败。

Compilation failed; see the compiler error output for details. ...\AndroidStudioProjects\HangMan\src\main\java\com\slideproud\hangman\GameActivity.java Error:(128, 31) cannot find symbol method get(int) Error:(129, 14) cannot find symbol method remove(int)

这是我的警告

errors pic

最佳答案

你应该这样使用ArrayList:

Resources res = getResources();
ArrayList<String> words = new ArrayList<String>;
words.addAll(Arrays.asList(res.getStringArray(R.array.words)));
//initialize random
rand = new Random();
//initialize word

然后拿到之后就可以去掉了:

private void playGame() {
//choose a word
int random = rand.nextInt(words.size ());
String newWord = words.get(random);
words.remove(random);
currWord = newWord;

编辑:

您必须更改 words 的范围,而不是:

public class GameActivity extends Activity {

//the words
private String[] words;
//random for word selection
private Random rand;

必须是:

public class GameActivity extends Activity {

//the words
ArrayList<String> words;
//random for word selection
private Random rand;

在你的 onCreate() 中,你只需初始化单词:

Resources res = getResources();
words = new ArrayList<String>;
words.addAll(Arrays.asList(res.getStringArray(R.array.words)));
//initialize random
rand = new Random();
//initialize word

关于java - 从数组中删除一个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23573033/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com