首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >战舰-java小游戏

战舰-java小游戏
EN

Stack Overflow用户
提问于 2012-05-18 23:19:19
回答 3查看 6.4K关注 0票数 0

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

代码语言:javascript
复制
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;
            }
        }
    }
}
EN

回答 3

Stack Overflow用户

发布于 2012-05-18 23:27:15

首先,您没有正确地对此建模(IMO)

我会更多地使用java.awt.Rectangle。我会先把木板做成一个矩形,然后把每艘船也做成一个矩形。因为Rectangle (实际上来自Shape )有方法包含(...)你应该能够快速测试你的矩形是否重叠。

至于在棋盘上标记镜头,也许你的船需要被定义为不仅仅是矩形--给它们提供被击中的点的功能。可以使用java.awt.Point处理命中/未命中的镜头

票数 4
EN

Stack Overflow用户

发布于 2012-05-18 23:24:20

我认为你在这里问了两个问题,我将两个都回答。

  1. 要将一艘船放到场地上,并确保它们不重叠,请检查在任何一个方块中是否有一艘船,您正试图将新船放入其中。创建一个二维布尔数组,在其中保存哪个正方形是一艘船。
  2. 对于用户输入,您尝试过什么,以及运行时遇到了什么问题?没有任何东西可以抓住,我不能给你任何东西来工作。我建议让用户给出两个坐标:船的起点和终点。处理这些数据。
票数 0
EN

Stack Overflow用户

发布于 2012-05-18 23:26:40

您似乎没有使用数据结构来跟踪您在地图中的“已填充”位置。这样,您就可以比较或“验证”地图中未填充的位置。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10655328

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档