我在VBA上没有找到很多答案,但是我正在制作一个Minesweeper 5x5板,并试图在一个地雷周围放置一些单元,以显示有多少地雷在与之接触。我可以使中间的9个起作用,即带有“ X”的空格:
package com.test.resttemplate;
import java.io.IOException;
import java.util.Map;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
@SpringBootApplication
public class RestTempaltetestApplication {
private static RestTemplate restTemplate;
@SuppressWarnings("unchecked")
public static void main(String[] args) {
SpringApplication.run(RestTempaltetestApplication.class, args);
final HttpHeaders headers = new HttpHeaders();
headers.add("materialId", "122344");
Map<String, Object> map;
try {
map = (Map<String, Object>) callRestTemplateExchange("https://alloyui.com/io/data/states.json", headers, HttpMethod.GET, null, Map.class);
System.out.println("Keyssss :"+map.keySet()+"values"+map.values());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public static Object callRestTemplateExchange(String url, HttpHeaders headers, HttpMethod method, Object body,
Class<?> claszz) throws Exception {
try {
System.out.println("Reached inside === ");
ResponseEntity<Object> response = null;
HttpEntity<?> requestEntity = new HttpEntity<Object>(headers);
System.out.println("Reached === 2");
if (null != body) {
System.out.println("Request === " + convertObjectToString(body));
requestEntity = new HttpEntity<Object>(body, headers);
} else {
requestEntity = new HttpEntity<Object>(headers);
}
System.out.println("Reached === 3");
response = (ResponseEntity<Object>) restTemplate.exchange(url, method, requestEntity, claszz);
if(null!=response.getBody()){
System.out.println("response === " + response.getBody());
}else
System.out.println("response is null");
System.out.println("Reached === 4");
return response.getBody();
} catch (HttpClientErrorException e) {
throw new Exception();
}
}
public final static String convertObjectToString(Object object) {
if (null != object) {
ObjectMapper objectMapper = new ObjectMapper();
try {
Object json = objectMapper.readValue(objectMapper.writeValueAsString(object), Object.class);
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
} catch (JsonMappingException e) {
} catch (IOException e) {
}
}
return null;
}
}
但是我很难让相邻的细胞也能计数。这是我的代码:
-----
-XXX-
-XXX-
-XXX-
-----
我将索引设置为2到4,因为否则Excel会向我抛出错误“ 9”:下标超出范围。
任何帮助将不胜感激。
答案 0 :(得分:1)
您无需为当前单元格周围的每个单元格设置数组或偏移量。 只需为扫雷器创建较大的射程,并为较小的(3 x 3)射程计算周围的炸弹。在循环中进行其余操作。
请参见下面的代码,所有内容均在注释中进行了说明。随时更改中心单元地址和扫雷范围,以进行更大的扫雷游戏。
Sub myminesweeper()
Dim rng As Range, r2 As Range, c1 As Range, c2 As Range, center_cell As Range, irng As Range
Dim cnt As Integer
Set center_cell = Range("E7") 'set center cell of minesweeper range
Set rng = Range(center_cell.Offset(-3, -3), center_cell.Offset(3, 3)) 'set minesweeper range
'(simply change the offset numbers)
'currently it is 7 x 7
For Each c1 In rng 'loop through cells in minesweeper range
cnt = 0 'reset the counter
If c1.Value = "X" Then GoTo Skip 'skip calculation if there is mine
Set r2 = Range(c1.Offset(-1, -1), c1.Offset(1, 1)) 'set 3 x 3 range to check mines
For Each c2 In r2 'loop through cells in 3 x 3 range
Set irng = Application.Intersect(c2, rng) 'check if the cell is within minesweeper range
If Not irng Is Nothing Then 'if the cell is in range
If c2.Value = "X" Then cnt = cnt + 1 'check if there is a mine. if so, add 1 to cnt.
End If
Next c2
c1.Value = cnt 'set the cell's value to total count of mines around it (cnt)
Skip:
Next
End Sub
答案 1 :(得分:0)
在测试单元之前,先测试一下您是否处于边界:
For i = 1 To 5
For j = 1 To 5
If BombArray(i, j) <> "X" Then
BombArray(i, j) = 0
If j <> 1 And i <> 5 Then
If BombArray(i + 1, j - 1) = "X" Then BombArray(i, j) = BombArray(i, j) + 1
End If
If i <> 5 Then
If BombArray(i + 1, j) = "X" Then BombArray(i, j) = BombArray(i, j) + 1
End If
'Etc.
'...
End If
Next j
Next i