在我的课堂上,我们被分配了一个名为Battleship的CodeHS任务。我被困在7号的第2部分,即Location类。练习如下:
要编写的下一个类是Location.java文件。 Location类存储一个网格位置的信息。
一个位置有两个定义属性
1)这个地方有船吗?
2)这个位置的状态是什么?
这个状态是这个位置是否被认可,我们受到了打击,还是错过了。
public static final int UNGUESSED = 0;
public static final int HIT = 1;
public static final int MISSED = 2;
这些是您需要为Location类实现的方法。
// Location constructor.
public Location()
// Was this Location a hit?
public boolean checkHit()
// Was this location a miss?
public boolean checkMiss()
// Was this location unguessed?
public boolean isUnguessed()
// Mark this location a hit.
public void markHit()
// Mark this location a miss.
public void markMiss()
// Return whether or not this location has a ship.
public boolean hasShip()
// Set the value of whether this location has a ship.
public void setShip(boolean val)
// Set the status of this Location.
public void setStatus(int status)
// Get the status of this Location.
public int getStatus()
我的工作如下:
public class Location
{
private int location;
private int hit;
private int miss;
private boolean val;
private int status;
//Implement the Location class here
public static final int UNGUESSED = 0;
public static final int HIT = 1;
public static final int MISSED = 2;
// Location constructor.
public Location(int location){
this.location = location;
}
// Was this Location a hit?
public boolean checkHit(){
if(location == HIT)
return true;
return false;
}
// Was this location a miss?
public boolean checkMiss(){
if(location == MISSED)
return true;
return false;
}
// Was this location unguessed?
public boolean isUnguessed(){
if(location == UNGUESSED)
return true;
return false;
}
// Mark this location a hit.
public void markHit(int hit){
this.hit = hit;
}
// Mark this location a miss.
public void markMiss(int miss){
this.miss = miss;
}
// Return whether or not this location has a ship.
public boolean hasShip(){
return val;
}
// Set the value of whether this location has a ship.
public void setShip(boolean val){
if(status == HIT)
val = true;
else
val = false;
}
// Set the status of this Location.
public void setStatus(int status){
this.status = status;
}
// Get the status of this Location.
public int getStatus(){
if(status == HIT)
return HIT;
else if (status == MISSED)
return MISSED;
else if(status == UNGUESSED)
return UNGUESSED;
}
}
虽然如果我在其他地方出错,我真的不会感到惊讶,但我的主要问题是setShip布尔方法。我怎么能把它设置为真(有一个船在那个位置)或假(没有船)。我所拥有的是我最好的猜测,但只有在"射击"之后进行测试才有效。我认为这项练习希望在此之前进行测试。
答案 0 :(得分:1)
感谢帮助人员。使用提供的反馈,我将类修复为以下内容,现在它可以正常工作。
public class Location
{
private int status;
private boolean ship;
//Implement the Location class here
public static final int UNGUESSED = 0;
public static final int HIT = 1;
public static final int MISSED = 2;
// Location constructor.
public Location(){
status = UNGUESSED;
ship = false;
}
// Was this Location a hit?
public boolean checkHit(){
if(status == HIT)
return true;
return false;
}
// Was this location a miss?
public boolean checkMiss(){
if(status == MISSED)
return true;
return false;
}
// Was this location unguessed?
public boolean isUnguessed(){
if(status == UNGUESSED)
return true;
return false;
}
// Mark this location a hit.
public void markHit(){
status = HIT;
}
// Mark this location a miss.
public void markMiss(){
status = MISSED;
}
// Return whether or not this location has a ship.
public boolean hasShip(){
return ship;
}
// Set the value of whether this location has a ship.
public void setShip(boolean val){
ship = val;
}
// Set the status of this Location.
public void setStatus(int status){
this.status = status;
}
// Get the status of this Location.
public int getStatus(){
return status;
}
}
答案 1 :(得分:0)
你有一个使用hasShip()返回的私有布尔值。
你的setShip(val)方法应该用val设置类变量,而不是根据统计数据来计算。
这只是一个简单的setter方法。
编辑:
就像
// Mark this location a hit.
public void markHit(int hit){
this.hit = hit;
}
答案 2 :(得分:0)
首先:不要改变你从练习中获得的方法签名(例如,构造函数不应该有参数,同样适用于row counter
和df1 = pd.DataFrame(columns=['System', 'Sub System', 'Tidal Zone'])
row=0
for i in k.keys():
df1.set_value(row, 'System', df[df['Concat_Code']==i].AquaticSettingName.values[0])
for j in k[i]:
df1.set_value(row, 'Sub System', df[df['Concat_Code']==i+'.'+str(j)].AquaticSettingName.values[0])
for v in k[i][j]:
try:
df1.set_value(row, 'Tidal Zone', df[df['Concat_Code']==i+'.'+str(j)+'.'+str(v)].AquaticSettingName.values[0])
except:
df1.set_value(row, 'Tidal Zone', '')
row=row+1
import numpy as np
df1 = df1.replace(np.nan,' ', regex=True)
方法。
您不需要将markHit
和markMiss
保存在两个不同的变量中,因为某个位置无法同时被点击和遗漏,因此我们使用单个变量int miss
。
我非常确定你的问题1和2是(或多或少)相同的东西,因为如果有一个命中,反之亦然,那么你只有一个属性{{ 1}}所以每个位置默认为int hit
,您可以从那里读取所有内容。
您需要做的就是更改status
和int status = 0
方法的状态变量,然后相应地调用UNCHECKED
。
代码示例:
markMiss