从C转换到Java后,我了解到Java包含许多可以为您完成工作的功能,可以这么说,不像C中您必须手动完成任务。
我目前正在设计一款OOP棋盘游戏,其中允许多个玩家挑选一个在整个游戏中代表它们的游戏。我已将游戏片存放在一个阵列中,然后询问玩家的数量以选择一个游戏片。然而,由于显而易见的原因,他们不允许选择与他们之前的球员相同的比赛。因此,我的问题是有一个功能,允许我从阵列中删除一个挑选的游戏片,或者我必须手动执行此操作,可以这么说。如果需要,我的代码如下:
{{1}}
答案 0 :(得分:2)
我会为当前玩家介绍一个新的List<String>
可用选项:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
...
List<String> availableOptions = new ArrayList<>(
Arrays.asList(potential_player_pieces)
);
并在选择完成后删除所选元素:
for (int i = 0; i < players; ++i) {
System.out.println("Player " + i + " pick a playing piece: " + availableOptions);
availableOptions.remove(player_pieces[i] = reader.nextLine());
}
您也可能将阵列的初始化时间缩短为:
String[] potentialPlayerPieces = new String[] {"*", "|", ..., "<"};
请注意,我已将该变量重命名为 Javaish 。
答案 1 :(得分:1)
我想在你的案例中建议HashMap
。我认为这种数据结构是有效的,符合您的目的。
Map<String, Integer> pieceMap = new HashMap<String, Integer>();
// HashMaps stores the value as a key value format.
// Here the keys are the piece options
// And the 0 is indicating that, primarily the piece is not used.
pieceMap.put("*", 0);
pieceMap.put("|", 0);
pieceMap.put("?", 0);
pieceMap.put("@", 0);
pieceMap.put("&", 0);
pieceMap.put("¬", 0);
pieceMap.put("!", 0);
pieceMap.put("%", 0);
pieceMap.put("<\n", 0);
String[] player_pieces = new String[players + 1];
for (int i = 0; i < players; i++) {
System.out.print("Player " + i + " pick a playing piece:"); // Asks the players the question
printAvailablePieces(pieceMap);
String piecePlayed = reader.nextLine();
if(pieceMap.containsKey(piecePlayed) && pieceMap.get(piecePlayed).equals(0)) {
// The piece has not used yet.
pieceMap.put(piecePlayed, 1);
player_pieces[i] = piecePlayed;
} else {
// The piece was played before
System.out.println("Please play a different piece");
i--; // Take the user input again.
}
}
public void printAvailablePieces(HashMap<String, Integer> pieceMap) {
for (Map.Entry<String, Integer> entry : pieceMap.entrySet())
if(entry.getValue().equals(0)) System.out.print(entry.getKey() + " ");
}
希望有所帮助。
答案 2 :(得分:0)
答案 3 :(得分:0)
使用List和HashMap
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class Temp {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> potential_player_pieces = new ArrayList<String>();//Array storing the playing pieces available
potential_player_pieces.add("*");
potential_player_pieces.add("|");
potential_player_pieces.add( "?");
potential_player_pieces.add( "@");
potential_player_pieces.add( "&");
potential_player_pieces.add( "¬");
potential_player_pieces.add( "!");
potential_player_pieces.add( "%");
potential_player_pieces.add( "<\n");
Collections.sort(potential_player_pieces);
System.out.println(potential_player_pieces);
int length=potential_player_pieces.size();
Scanner reader= new Scanner(System.in);
Map<Integer,String> player_pieces = new HashMap<Integer,String>();//String to store the playing pieces that the players have chosen
for (int i=1; i<=length; i++)//Loops to the number of players and asks them what playing piece they want
{
System.out.print("Player " + i + " pick a playing piece: ");//Asks the players the question
for (int j=0; j<potential_player_pieces.size(); j++){
if(j==0)
{
System.out.print(potential_player_pieces.get(0)+"\n");
player_pieces.put(i, potential_player_pieces.get(0));
potential_player_pieces.remove(0);
}
if(potential_player_pieces.size()>0)
System.out.println(potential_player_pieces.get(j)); //Displays the possible playing pieces
}
//Saves the player chioces to the array made above
}
System.out.println(player_pieces);
}
}