我想用Java创建游戏Battleship,但我不知道如何制作这样的数组:
#standardSQL
WITH dates AS (
SELECT day FROM UNNEST(GENERATE_DATE_ARRAY(DATE '2018-07-05', '2018-07-09', INTERVAL 1 DAY)) AS day
), targets AS (
SELECT DATE '2018-07-06' AS day, 'UK' AS Market, NUMERIC '2.4' AS quantity
UNION ALL SELECT '2018-07-06', "US", 8.4
UNION ALL SELECT '2018-07-06', "US", 1.2
UNION ALL SELECT '2018-07-08', "UK", 3.0
UNION ALL SELECT '2018-07-08', "US", 10.9
), sales AS (
SELECT DATE '2018-07-08' AS day, 'UK' AS Market, 4 AS quantity
UNION ALL SELECT '2018-07-06', 'US', 15
)
SELECT
dates.day AS day,
t.market AS market,
targetQuantity,
quantity
FROM dates
LEFT JOIN (SELECT day, market, SUM(quantity) AS targetQuantity FROM targets GROUP BY day, market) t
ON dates.day = CAST(t.day AS DATE)
LEFT JOIN (SELECT day, market, SUM(quantity) AS quantity FROM sales GROUP BY day, market) s
ON dates.day = CAST(s.day AS DATE) AND t.market = s.market
ORDER BY day, market
我希望可以轻松设置字段的元素。这样我就可以通过用户输入轻松地操作该字段。
例如:
我希望D6成为*
10 - - - - - - - - - -
9 - - - - - - - - - -
8 - - - - - - - - - -
7 - - - - - - - - - -
6 - - - - - - - - - -
5 - - - - - - - - - -
4 - - - - - - - - - -
3 - - - - - - - - - -
2 - - - - - - - - - -
1 - - - - - - - - - -
A B C D E F G H I J
答案 0 :(得分:0)
我建议您实施。这是一个Grid类,它允许您定义String 10x10的网格。如您所见,您可以使用1-10
和列A-J
之间的行来读写元素。 main
方法仅显示用法示例:
public class Grid {
public Grid() {
for (int i = 0; i < elements.length; i++) {
elements[i] = "-";
}
}
private String elements[] = new String[100];
public void print() {
for (int i = 0; i < elements.length; i++) {
if (i % 10 == 0) {
System.out.println();
System.out.print(String.format("%2d",10 - i / 10));
}
System.out.print(" " + elements[i]);
}
System.out.print("\n ");
for (int i = 0; i < 10; i++) {
System.out.print(" " + (char)(i + 'A'));
}
}
public String get(int row, char col) {
return elements[(10-row)*10-('A'-col)];
}
public void set(int row, char col, String value) {
if (row>=1 && row<=10 && col>='A' && col<='J') {
elements[(10-row)*10-('A'-col)]=value;
}
}
public static void main(String[] args) {
Grid grid = new Grid();
grid.set(1, 'A', "x");
grid.set(1, 'B', "t");
grid.set(10, 'A', "y");
grid.set(10, 'J', "r");
grid.set(5, 'J', "r");
grid.set(1, 'J', "x");
grid.set(6, 'D', "*");
grid.print();
}
}
Main
被执行的结果是:
10 y - - - - - - - - r
9 - - - - - - - - - -
8 - - - - - - - - - -
7 - - - - - - - - - -
6 - - - * - - - - - -
5 - - - - - - - - - r
4 - - - - - - - - - -
3 - - - - - - - - - -
2 - - - - - - - - - -
1 x t - - - - - - - x
A B C D E F G H I J
答案 1 :(得分:0)
您是否正在寻找符合这些条件的东西?
API:
课程:
互动:
new Battleground() b
System.out.print(b)
b.set(Position.get(10, 'J'), Tile.IntactShip)
代码:
public class Game
{
public static void main (String[] args) throws java.lang.Exception
{
Game game = new Game();
game.demo();
}
public void demo()
{
Battleground b = new Battleground();
b.set(Position.get(7, 'C'), Tile.IntactShip);
b.set(Position.get(7, 'D'), Tile.IntactShip);
b.set(Position.get(7, 'E'), Tile.IntactShip);
b.set(Position.get(7, 'F'), Tile.IntactShip);
b.set(Position.get(5, 'E'), Tile.IntactShip);
b.set(Position.get(4, 'E'), Tile.IntactShip);
System.out.println(b);
}
}
class Battleground
{
private Tile[][] tiles = new Tile[10][10];
public Battleground()
{
fill(Tile.Unknown);
}
public Tile get(Position pos)
{
return tiles[pos.row-1][pos.column-'A'];
}
public void set(Position pos, Tile tile)
{
tiles[pos.row-1][pos.column-'A'] = tile;
}
public void fill(Tile tile)
{
for (Position p = Position.FIRST; p != null; p = p.next())
set(p, tile);
}
public String toString()
{
StringBuilder sb = new StringBuilder();
for (int row = 10; row >= 1; row--)
{
sb.append((row > 9 ? "" : " ") + row);
for (char col = 'A'; col <= 'J'; col++)
sb.append(" " + tiles[row-1][col-'A']);
sb.append("\n");
}
sb.append(" ");
for (char col = 'A'; col <= 'J'; col++)
sb.append(" " + col);
sb.append("\n");
return sb.toString();
}
}
class Position
{
public static final Position FIRST = get(10, 'A');
public final int row;
public final char column;
private Position(int row, char col)
{
this.row = row;
this.column = col;
}
public static Position get(int row, char col) throws java.lang.IllegalArgumentException
{
if (row < 1 || row > 10 || col < 'A' || col > 'J')
throw new IllegalArgumentException();
return new Position(row, col);
}
public Position next()
{
if (row == 1 && column == 'J')
return null;
if (column == 'J')
return new Position(row-1, 'A');
return new Position(row, (char)(column+1));
}
}
enum Tile
{
Unknown('.'),
Empty('*'),
IntactShip('O'),
DestroyedShip('X');
private char symbol;
Tile(char symbol) {
this.symbol = symbol;
}
public String toString() {
return "" + symbol;
}
}