代码有点'有点'。但我不希望列表的最后一个数字添加到另一个列表的第一个数字。我很难解释所以我会告诉你更进一步!
与'M'相邻的所有零都应该改变。但是有些人的数字太高了。
继承人代码:
hiddenfield = [[0, 0, 0, 0, 0, 'M', 0, 0, 0, 'M'],
[0, 0, 0, 'M', 0, 0, 0, 'M', 'M', 'M'],
[0, 0, 'M', 0, 'M', 0, 0, 'M', 0, 'M'],
[0, 0, 0, 0, 'M', 0, 0, 0, 0, 0],
[0, 0, 'M', 0, 'M', 0, 0, 0, 0, 0],
[0, 0, 0, 'M', 0, 'M', 'M', 0, 0, 0],
['M', 0, 0, 'M', 0, 0, 0, 'M', 0, 0],
[0, 0, 0, 'M', 0, 0, 0, 0, 0, 0],
[0, 'M', 'M', 0, 'M', 0, 0, 0, 0, 'M'],
['M', 0, 0, 'M', 0, 0, 0, 0, 'M', 0]]
def update_numbers(x, y, hiddenfield):
try:
if hiddenfield[x][y] != 'M':
hiddenfield[x][y] += 1
except IndexError:
pass
def numbersinhiddefield(indexes):
indexes = [[i,j] for i,row in enumerate(indexes) for j,elem in enumerate(row) if elem == 'M']
loop = 0
while loop < len(indexes):
update_numbers(indexes[loop][0]+1,indexes[loop][1], hiddenfield)
update_numbers(indexes[loop][0]-1,indexes[loop][1], hiddenfield)
update_numbers(indexes[loop][0],indexes[loop][1]+1, hiddenfield)
update_numbers(indexes[loop][0],indexes[loop][1]-1, hiddenfield)
update_numbers(indexes[loop][0]+1,indexes[loop][1]+1, hiddenfield)
update_numbers(indexes[loop][0]-1,indexes[loop][1]-1, hiddenfield)
update_numbers(indexes[loop][0]+1,indexes[loop][1]-1, hiddenfield)
update_numbers(indexes[loop][0]-1,indexes[loop][1]+1, hiddenfield)
loop += 1
def showMineFieldHidden(hiddenfield):
border = list(range(0,len(hiddenfield)))
row = [' ']+border
i = 0
for rows in [border]+hiddenfield:
print(row[i], end=' ')
i += 1
for lines in rows:
print(lines, end=' ')
print()
numbersinhiddefield(hiddenfield)
showMineFieldHidden(hiddenfield)
但我得出的不仅仅是正确的:
0 1 2 3 4 5 6 7 8 9
0 0 0 1 1 2 M 2 2 4 M
1 0 1 2 M 3 2 3 M M M
2 0 1 M 4 M 2 2 M 5 M
3 0 2 2 5 M 3 1 1 2 1
4 0 1 M 4 M 4 2 1 0 0
5 1 2 3 M 4 M M 2 1 1
6 M 1 3 M 4 2 3 M 1 1
7 2 3 4 M 3 1 1 1 2 2
8 2 M M 4 M 1 0 1 2 M
9 M 3 3 M 3 2 1 1 M 4 <-- this is supposed to be a 2.
^
this is supposed to be a 1 and the one next to it a zero
我认为它是从边界添加到较低的列表?
非常感谢som的帮助!
答案 0 :(得分:1)
您的问题是当行asia/kolkatta
中有'M'时,您可以使用它来添加行public String cinverttoutc(String time) {
String currenttimeformat = "";
String twelvehourformat = "";
try {
Calendar calendar = new GregorianCalendar();
TimeZone timeZone = calendar.getTimeZone();
String Timezonename = timeZone.getID();
TimeZone tz = TimeZone.getTimeZone(Timezonename);
localtimezone = tz.getID();
Log.i("localtimezone",localtimezone);
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dt = new Date();
String dateis = sdf.format(dt);
String dateandtime = dateis + " " + time;
final SimpleDateFormat sdf1 = new SimpleDateFormat(DATEFORMAT);
SimpleDateFormat twelv`enter code here`ehour = new SimpleDateFormat("hh:mm aa");
TimeZone utcZone = TimeZone.getTimeZone(localtimezone);
sdf1.setTimeZone(utcZone);
Date myDate = sdf1.parse(dateandtime);
sdf1.setTimeZone(TimeZone.getDefault());
String Currentformat = sdf1.format(myDate);
DateFormat readFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat timeFormat = new SimpleDateFormat("hh:mm aa");
Date now = readFormat.parse(Currentformat);
currenttimeformat = timeFormat.format(now);
} catch (ParseException e) {
}
return currenttimeformat;
}
的计数。索引0
是引用最后一行的另一种方法。类似于列-1
,您可以添加到列-1
的计数。
这意味着顶行的'M'计算底行,而左列的'M'计算右列。
如果修改0
以检查坐标,则应该能够避免重复计算。这也可能看起来比异常处理更清晰。
-1