gpt4 book ai didi

java - 战舰-java游戏

转载 作者:行者123 更新时间:2023-12-01 19:04:28 26 4
gpt4 key购买 nike

我正在学习创建一个java游戏,对java来说还是个新手。现在我想创建一个战舰游戏。但现在我被困在这里了。现在,当我随机放置船作为电脑板时,有时它会与之前的船重叠,因此游戏变得不平衡。其次,在我收到玩家的输入后,如何将输入值放入棋盘中。这是我的代码:

import javax.swing.JOptionPane;
import java.util.Random;
import java.util.Scanner;

public class Battleship
{
public static void main (String args[]){
String map [][][]=new String [10][10][2];
int row =0,col=0;

//initPlayerMap(map);
//printMap(map,true);// true to printout the map
placeShips(map); // place the shipe at computer map
initMap(map,"~", true);
initMap(map,"#",false);
placeShips(map); // place the shipe at computer map
printMap(map,true);
System.out.println("Now enter your coordinate of the boom");
row = getInput("Please enter row: ");
col = getInput("Please enter col: ");
printMap(map,false); // computer map
hitShip(row,col);

}

private static void hitShip (int row, int col){
if (map[startFrom++][colOrRow][1]== map[row][col][1]){
System.out.println("abc");
}
else
{
System.out.println("darn!");
}
}
private static void initMap(String map[][][] , String initChar, boolean player){
//the 0 in 3rd dimension is representing player map and 1 for computer
int mapNo= (player?0:1);
for (int i = 0 ; i < 10; i ++)
for (int j=0; j<10; j++)
map [i][j][mapNo]= initChar;
}

private static void printMap(String map[][][], boolean player){
int whichMap=0;
if (!player)
whichMap=1;

System.out.println(" 0\t1\t2\t3\t4\t5\t6\t7\t8\t9 ");
for (int i = 0 ; i < 10; i ++){
System.out.print(i+" ");
for (int j=0; j<10; j++){
System.out.print(map [i][j][whichMap]+ "\t");
}
System.out.print("\n");
}
}// end of printMap method

public static int getInput(String message){
Scanner sc = new Scanner (System.in);
System.out.print(message);
return sc.nextInt();
}

private static void placeShips(String[][][] grid)
{
char[] shipType = { 'B' , 'C' , 'F' , 'M' };
int[] shipSize = { 5 , 4 , 3 , 2 };
int[] shipNums = { 1 , 2 , 4 , 4 };

for (int x = 0 ; x < shipType.length ; x++)
for (int y = 1 ; y <= shipNums[x] ; y++)
{

String shipName = shipType[x]+""+y;
placeShip(grid,shipName,shipSize[x]);
}
}

private static void placeShip(String[][][] map, String shipName, int size){
int direction = (int)(Math.random()*2);// 0 or 1
int colOrRow = (int)(Math.random()*map.length); // pick
int startFrom =(int)(Math.random()*(map.length-size)); // which cell?


// placing the ship
for(int i=0; i < size; i++){
// weather is vertical or horizontal
// vertical
if (direction == 0 ){
map[startFrom++][colOrRow][1] = shipName;

}
else {
map[colOrRow][startFrom++][1] = shipName;
}
}
}
}

最佳答案

首先,你还没有正确建模(IMO)

我会更多地使用 java.awt.Rectangle。我首先将棋盘制作为矩形,然后将每艘船也制作为矩形。因为 Rectangle(实际上来自 Shape)具有 contains(...) 方法,所以您应该能够快速测试矩形是否重叠。

就在棋盘上标记射击而言,也许您的飞船需要定义的不仅仅是矩形 - 为它们提供已击中点的功能。您可以使用 java.awt.Point 进行命中/未命中镜头

关于java - 战舰-java游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10655328/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com