[英]Reading in character by character in file
I am currently working on a primitive ants vs zombies game for a class project.我目前正在为一个班级项目开发一个原始的蚂蚁大战僵尸游戏。 We are to read in a 'horde' file that contains characters corresponding to a Zombie that will be invading and an integer(1-9inclusive) that represents a multiple of the previous zombie char.我们将读入一个“horde”文件,该文件包含与将要入侵的僵尸对应的字符和一个表示前一个僵尸字符倍数的整数(包括 1-9)。 What I am having trouble is differentiating between int and char within the file string and how to create multiple objects depending on the int.我遇到的问题是区分文件字符串中的 int 和 char 以及如何根据 int 创建多个对象。 This is what I have so far:这是我到目前为止:
public void readHordeFile(String filename){
try {
file = new FileReader(filename);
} catch (FileNotFoundException e) {
System.out.println("File not found " + e.getMessage());
}
buf = new BufferedReader(file);
try {
zombieString = buf.readLine();
for(int i = 0; i < zombieString.length(); i++){
if(zombieString.charAt(i) == 'S'){
horde.add(new ZombieScientist());
}else if(zombieString.charAt(i) == 'Z'){
horde.add(new StandardZombie());
}else if(zombieString.charAt(i) == 'I'){
horde.add(new InfectedZombie());
}else if(zombieString.charAt(i) == 1){
}
}
} catch (IOException e) {
e.getMessage();
}
}
an example file contains : SZI1示例文件包含:SZI1
I was thinking of just hard-coding each number but I still run into the problem of not knowing how to add multiples of the same object.我正在考虑对每个数字进行硬编码,但我仍然遇到不知道如何添加同一对象的倍数的问题。 I would really appreciate any help.我真的很感激任何帮助。 Thank you all in advance.谢谢大家。
You can still check integer in char format like this.您仍然可以像这样以字符格式检查整数。
else if(zombieString.charAt(i) == '1'){
}
if it is 1,2 or 3 you can check it in this way.如果是 1,2 或 3,您可以通过这种方式检查它。
if you want to add multiple objects you can create a list of objects and add to "horde" object如果你想添加多个对象,你可以创建一个对象列表并添加到“部落”对象
To tell if a code point in a string is numeric you use the Character.isDigit method;要判断字符串中的代码点是否为数字,请使用 Character.isDigit 方法; to get a portion of your string into an int you use the Integer.parseInt method.要将字符串的一部分转换为 int,请使用 Integer.parseInt 方法。
A few things;一些东西; this is un-tested code so take it as more of a hint;这是未经测试的代码,因此将其视为更多提示;
You should probably start by breaking the zombie creation up into its own method, so your loop looks something like;您可能应该首先将僵尸创建分解为它自己的方法,因此您的循环看起来像;
zombieString = buf.readLine();
for(int i = 0; i < zombieString.length(); i++){
Character ch = zombieString.charAt(i);
addZombie(horde, ch);
}
Then you could just save the latest created zombie in a state variable;然后你可以将最新创建的僵尸保存在一个状态变量中;
Character previousZombie = ' ';
zombieString = buf.readLine();
for(int i = 0; i < zombieString.length(); i++){
Character ch = zombieString.charAt(i);
previousZombie = ch;
addZombie(horde, ch);
}
...and add a check if the next character is a digit and if so just add the correct number of the previous zombie. ...并检查下一个字符是否是数字,如果是,则添加前一个僵尸的正确编号。
Character previousZombie = ' ';
zombieString = buf.readLine();
for(int i = 0; i < zombieString.length(); i++){
Character ch = zombieString.charAt(i);
if(!Character.isDigit(ch))
{
previousZombie = ch;
addZombie(horde, ch);
}
else
{
for(int j='1'; j<ch; j++)
addZombie(horde, previousZombie);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.