- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我写了战舰游戏的代码。有两个玩家,人类玩家和计算机玩家。他们都有单独的板。一开始,我会自动将船只随机放置在两个棋盘上,然后获取坐标并将船只放置在对方的棋盘上。如果坐标中的位置击中对方玩家的船只,我会向屏幕发送一条消息。如果击沉船只,屏幕上会显示字符“s”,如果击中则仅显示“*”,如果无法击中则显示“x”。当人类玩家的时间到时,我如何在收到坐标后对计算机玩家的棋盘进行更改?
public static int numRows = 10;
public static int numCols = 10;
public static int playerShips;
public static int computerShips;
public static String[][] grid = new String[numRows][numCols];
public static int[][] missedGuesses = new int[numRows][numCols];
public static void main(String[] args) {
System.out.println("Welcome to Amiral Batti game");
System.out.println("\nComputer: ");
deployComputerShips();
System.out.println("\n");
System.out.println("\nHuman: ");
deployPlayerShips();
do {
Battle();
}
while(players.playerShips != 0 && players.computerShips != 0);
gameOver();
}
public static int FIELD_SIZE = 10;
public static void deployPlayerShips() {
Random random = new Random();
int[][] field = new int[FIELD_SIZE][FIELD_SIZE];
for (int i = 5; i > 0; i--) {
int x = random.nextInt(field.length);
int y = random.nextInt(field.length);
boolean vertical = random.nextBoolean();
if (vertical) {
if (y + i > FIELD_SIZE) {
y -= i;
}
} else if (x + i > FIELD_SIZE) {
x -= i;
}
boolean isFree = true;
if (vertical) {
for (int m = y; m < y + i; m++) {
if (field[m][x] != 0) {
isFree = false;
break;
}
}
} else {
for (int n = x; n < x + i; n++) {
if (field[y][n] != 0) {
isFree = false;
break;
}
}
}
if (!isFree) {
i++;
continue;
}
if (vertical) {
for (int m = Math.max(0, x - 1); m < Math.min(FIELD_SIZE, x + 2); m++) {
for (int n = Math.max(0, y - 1); n < Math.min(FIELD_SIZE, y + i + 1); n++) {
field[n][m] = 9;
}
}
} else {
for (int m = Math.max(0, y - 1); m < Math.min(FIELD_SIZE, y + 2); m++) {
for (int n = Math.max(0, x - 1); n < Math.min(FIELD_SIZE, x + i + 1); n++) {
field[m][n] = 9;
}
}
}
for (int j = 0; j < i; j++) {
field[y][x] = i;
if (vertical) {
y++;
} else {
x++;
}
}
}
System.out.print(" ");
System.out.println("0 1 2 3 4 5 6 7 8 9");
char[][] map = new char[FIELD_SIZE][FIELD_SIZE];
for (int i = 0; i < FIELD_SIZE; i++) {
for (int j = 0; j < FIELD_SIZE; j++) {
map[i][j] = field[i][j] == 0 || field[i][j] == 9 ? '.' : 'o';
}
}
Arrays.stream(map)
.forEach(m -> System.out.println(Arrays.toString(m).replace(",", "")));
}
public static void deployComputerShips() {
Random random = new Random();
int[][] field = new int[FIELD_SIZE][FIELD_SIZE];
for (int i = 5; i > 0; i--) {
int x = random.nextInt(field.length);
int y = random.nextInt(field.length);
boolean vertical = random.nextBoolean();
if (vertical) {
if (y + i > FIELD_SIZE) {
y -= i;
}
} else if (x + i > FIELD_SIZE) {
x -= i;
}
boolean isFree = true;
if (vertical) {
for (int m = y; m < y + i; m++) {
if (field[m][x] != 0) {
isFree = false;
break;
}
}
} else {
for (int n = x; n < x + i; n++) {
if (field[y][n] != 0) {
isFree = false;
break;
}
}
}
if (!isFree) {
i++;
continue;
}
if (vertical) {
for (int m = Math.max(0, x - 1); m < Math.min(FIELD_SIZE, x + 2); m++) {
for (int n = Math.max(0, y - 1); n < Math.min(FIELD_SIZE, y + i + 1); n++) {
field[n][m] = 9;
}
}
} else {
for (int m = Math.max(0, y - 1); m < Math.min(FIELD_SIZE, y + 2); m++) {
for (int n = Math.max(0, x - 1); n < Math.min(FIELD_SIZE, x + i + 1); n++) {
field[m][n] = 9;
}
}
}
for (int j = 0; j < i; j++) {
field[y][x] = i;
if (vertical) {
y++;
} else {
x++;
}
}
}
System.out.print(" ");
System.out.println("0 1 2 3 4 5 6 7 8 9");
char[][] map = new char[FIELD_SIZE][FIELD_SIZE];
for (int i = 0; i < FIELD_SIZE; i++) {
for (int j = 0; j < FIELD_SIZE; j++) {
map[i][j] = field[i][j] == 0 || field[i][j] == 9 ? '.' : 'o';
}
}
Arrays.stream(map)
.forEach(m -> System.out.println(Arrays.toString(m).replace(",", "")));
}
public static void Battle(){
playerTurn();
computerTurn();
printBoard();
System.out.println();
System.out.println("Your ships: " + players.playerShips + " | Computer ships: " + players.computerShips);
System.out.println();
}
public static void playerTurn(){
Scanner scn = new Scanner(System.in);
System.out.println("\nHuman's turn: ");
int x = -1, y = -1;
do {
Scanner input = new Scanner(System.in);
System.out.print("Enter row number: ");
x = scn.nextInt();
System.out.print("Enter column number: ");
y = scn.nextInt();
if ((x >= 0 && x < numRows) && (y >= 0 && y < numCols)){
if (grid[x][y].equals("o")){
System.out.println("You sunk the ship!");
grid[x][y] = "s";
--players.computerShips;
}
else if (grid[x][y].equals(".")) {
System.out.println("You missed");
grid[x][y] = "x";
}
}
else if ((x < 0 || x >= numRows) || (y < 0 || y >= numCols))
System.out.println("You can't place ships outside the " + numRows + " by " + numCols + " grid");
}
while((x < 0 || x >= numRows) || (y < 0 || y >= numCols));
}
public static void computerTurn(){
System.out.println("\nComputer's turn: ");
int x = -1, y = -1;
do {
x = (int)(Math.random()*10);
y = (int)(Math.random()*10);
System.out.println("Enter row number: "+x);
System.out.println("Enter column number: "+y);
if ((x >= 0 && x < numRows) && (y >= 0 && y < numCols)){
if (grid[x][y].equals("o")){
System.out.println("The Computer sunk one of your ships!");
grid[x][y] = "s";
--players.playerShips;
++players.computerShips;
}
else if (grid[x][y].equals(".")) {
System.out.println("Computer missed");
grid[x][y] = "x";
if(missedGuesses[x][y] != 1)
missedGuesses[x][y] = 1;
}
}
}
while((x < 0 || x >= numRows) || (y < 0 || y >= numCols));
}
public static void gameOver(){
System.out.println("Your ships: " + players.playerShips + " | Computer ships: " + players.computerShips);
if(players.playerShips > 0 && players.computerShips <= 0)
System.out.println("You won the battle! ");
else
System.out.println("You lost the battle! ");
System.out.println();
}
public static void printBoard(){
System.out.print(" ");
System.out.println("0123456789");
for(int x = 0; x < grid.length; x++) {
System.out.print(x);
for (int y = 0; y < grid[x].length; y++){
System.out.print(grid[x][y]);
}
System.out.println();
}
System.out.println();
}
最佳答案
好的,这是一个更正确的版本。我可以做更多,但是......然后一半的代码将被重写,我不认为这样做有什么意义(对你没有帮助)。因此,我坚持认为它远非完美,但至少它解决了您的问题。我做了以下更正:
打包沙箱;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
// ATTENTION: low-quality code based on original source posted in question. Not fully functional and has many issues. But it answers the original question.
public class Warships {
private static final int numRows = 10;
private static final int numCols = 10;
private static final int NB_SHIPS = 5;
private static int playerShips = NB_SHIPS;
private static int computerShips = NB_SHIPS;
private static final int[][] missedGuesses = new int[numRows][numCols];
private static final Random random = new Random(System.currentTimeMillis());
public static final int FIELD_SIZE = 10;
private static final int[][] playerField = new int[FIELD_SIZE][FIELD_SIZE];
private static final int[][] computerField = new int[FIELD_SIZE][FIELD_SIZE];
public static void main(String[] args) {
System.out.println("Welcome to Amiral Batti game");
System.out.println("\nComputer: ");
deployPlayersShips(computerField);
System.out.println("\n");
System.out.println("\nHuman: ");
deployPlayersShips(playerField);
Scanner scn = new Scanner(System.in);
do {
battle(scn);
} while (playerShips != 0 && computerShips != 0);
gameOver();
}
public static void deployPlayersShips(int[][] field) {
for (int i = NB_SHIPS; i > 0; i--) {
int x = random.nextInt(field.length);
int y = random.nextInt(field.length);
boolean vertical = random.nextBoolean();
(...)
}
printBoard2(field);
}
public static void battle(Scanner scn) {
playerTurn(scn);
computerTurn();
System.out.println("\nComputer: ");
printBoard2(computerField);
System.out.println("\nHuman: ");
printBoard2(playerField);
System.out.println();
System.out.println("Your ships: " + playerShips + " | Computer ships: " + computerShips);
System.out.println();
}
public static void playerTurn(Scanner scn) {
System.out.println("\nHuman's turn: ");
int x = -1, y = -1;
// MAIN ISSUE WAS HERE -- Add correct loop on invalid input
do {
System.out.print("Enter row number: ");
x = scn.nextInt();
System.out.print("Enter column number: ");
y = scn.nextInt();
} while ((x < 0 || x >= numRows) || (y < 0 || y >= numCols));
// MAIN ISSUE WAS HERE -- Removed invalid loop on update field
if (computerField[x][y] != 0 && computerField[x][y] != 9) {
System.out.println("You sunk the ship!");
computerField[x][y] = 1;//"s";
computerShips--;
} else if (".".equals(computerField[x][y])) {
System.out.println("You missed");
computerField[x][y] = 2; //"x";
}
}
public static void computerTurn() {
System.out.println("\nComputer's turn: ");
int x = random.nextInt(FIELD_SIZE);
int y = random.nextInt(FIELD_SIZE);
System.out.println("Enter row number: " + x);
System.out.println("Enter column number: " + y);
// MAIN ISSUE WAS HERE -- Removed invalid loop on input/rand
if ((x >= 0 && x < numRows) && (y >= 0 && y < numCols)) {
if (playerField[x][y] != 0 && playerField[x][y] != 9) {
System.out.println("The Computer sunk one of your ships!");
playerField[x][y] = 1;//"s";
playerShips--;
} else if (".".equals(playerField[x][y])) {
System.out.println("Computer missed");
playerField[x][y] = 2; //"x";
if (missedGuesses[x][y] != 1) {
missedGuesses[x][y] = 1;
}
}
}
}
public static void gameOver() {
System.out.println("Your ships: " + playerShips + " | Computer ships: " + computerShips);
if (playerShips > 0 && computerShips <= 0) {
System.out.println("You won the battle! ");
} else {
System.out.println("You lost the battle! ");
}
System.out.println();
}
public static void printBoard2(int[][] field) {
System.out.print(" ");
System.out.println("0 1 2 3 4 5 6 7 8 9");
char[][] map = new char[FIELD_SIZE][FIELD_SIZE];
for (int i = 0; i < FIELD_SIZE; i++) {
for (int j = 0; j < FIELD_SIZE; j++) {
switch(field[i][j]) {
case 0:
case 9: map[i][j] = '.';
break;
case 1: map[i][j] = 's';
break;
case 2: map[i][j] = 'x';
break;
default: map[i][j] = 'o';
break;
}
}
}
Arrays.stream(map)
.forEach(m -> System.out.println(Arrays.toString(m).replace(",", "")));
}
}
此时我让你做剩下的事情。许多错误仍然存在。在再次请求帮助之前,请花时间真正完成并仔细检查您的代码。并继续进行培训,以便在 OOP、编码规则和最佳实践方面有改进的空间。祝你好运。
关于java - Java 中的战舰游戏板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62093845/
我们将 Azure Boards(与敏捷流程相关的项目)定义为“功能”>“史诗”>“任务”>“用户故事”。 在我们的Azure Boards(Boards >Board)中,它仅显示Epic和Feat
我正在编写一个 C++ 井字游戏,这是我目前拥有的: #include using namespace std; int main() { board *b; b->draw();
这是一个足够简单的问题。 看完documentation for ion-pane它指出: A simple container that fits content, with no side eff
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 4年前关闭。 Improve this
我正在用 javascript 对 arduino 开发板进行编程。我正在尝试使用 johnny-five 库连接多个 arduino 板。我关注了johnny-five documentation我
在我的 Java 类(class)中,我们正在学习《Java 基础知识》一书的第 4 章。我正在做项目 4-11,它是一个黑色和红色的棋盘格,但是我得到随机颜色,我试图按照本书教我们使用 ColorP
我正在制作一个数独板 GUI,它应该看起来像这样 http://www.sudoku.4thewww.com/Grids/grid.jpg 由于某种原因,它只显示最后一个 3*3 板。如果有人能告诉我
我正在开发一款带有二维阵列(游戏板)的新游戏。每个单元格/图 block 都有一定数量的点。 我想实现的是一个算法能找到核心最高的最短路径。 所以我首先实现了 Dijkstra 算法(下面的源代码)来
更新:(2015-10-16)[已解决!]-使用trigger()并通过slice()限制为50个引脚固定。 非常感谢Abhas Tandon通过提供使用 $(this).trigger('click
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 7年前关闭。 Improve this questi
var size = 8; var board = ""; for (var y = 0; y x= (x+y) % 2 = 关于javasc
我正在制作一个简单的游戏,需要我创建一个由用户定义大小的棋盘。 我一直在编写一个函数,该函数应该返回我将在我的游戏中使用的棋盘(矩阵),但我似乎无法让它工作。 我尝试使用嵌套的 for 循环方法在 m
我正在尝试让板模板引擎与 express.js 一起工作。我最初的尝试是这样的: app.register('.html', { compile: function (str, options
我正在测试 Azure Boards Rest API。我目前可以成功创建、删除和获取项目,但我似乎无法在列之间移动它们。 这是我的要求https://{{AzureBoardsToken}}@{{A
我想用 trello api 归档一个板/列表,但我找不到解决方案。 与 https://trello.com/docs/api/list/#post-1-lists-idlist-archiveal
我上传了 sketch到一个 Arduino Uno,它的循环是这样的: void loop(){ Serial.println("Hello, World!"); } 所以,现在,我无法再上
我想要进行一个查询,显示结构 Epic -> 功能 -> 发布 -> 用户故事 -> 任务,以及特定迭代路径下的所有待处理任务 我尝试使用工作项树,但它只显示到 mu 用户故事 我的 Azure De
我在 python 中使用来自 Opencv 的 Charuco 标记。我之前使用的是 Aruco 开发板,我可以选择创建一个带有 id 偏移量(例如:偏移量为 40)的开发板。 from cv2 i
我不知道如何将另一个 View 中的辅助 Anchorpane 设置到主 View 的边界(在 fxml 代码中,它将是名为 holderPane 并且有灰色区域),这样当窗口展开时,它也会随之拉伸(
如何使用包含列、行和堆栈(包含 4、3、2、1)的 3D 通用数组制作一 block 板。 这是我声明的: private int row, col, stack; int[][][] array3D
我是一名优秀的程序员,十分优秀!