将程序更改为面向对象的设计

时间:2017-03-28 19:53:25

标签: java

我需要帮助重新安排一个程序,我必须使用类构造函数方法......等等。这是一场战舰游戏。我不太清楚我需要什么类别,构造和方法。

 import java.util.Scanner;

public class BattleshipLab {

    Scanner input = new Scanner(System.in);
    public static final boolean DEBUG = false;

    public static void breakln() {
        System.out.println("_____________________________________");
        System.out.println("");
    }

    public static void createBoard(String[][] board) {
        for (String[] board1 : board) {
            for (int c = 0; c < board[0].length; c++) {
                board1[c] = "~";
            }
        }
    }

    public static void showBoard(String[][] board) {
        breakln();
        for (String[] board1 : board) {
            if (DEBUG == true) {
                for (int c = 0; c < board[0].length; c++) {
                    System.out.print(" " + board1[c]);
                }
                System.out.println("");
            } else {
                for (int c = 0; c < board[0].length; c++) {
                    if (board1[c].equals("S")) {
                        System.out.print(" " + "~");
                    } else {
                        System.out.print(" " + board1[c]);
                    }
                }
                System.out.println("");
            }
        }
        breakln();
    }

    public static void createShip(String[][] board, int size) {
        if (Math.random() < 0.5) {
            int col = (int) (Math.random() * 5);
            int row = (int) (Math.random() * 7);
            for (int i = 0; i < size; i++) {
                board[row][col + i] = "S";
            }
        } else {
            int col = (int) (Math.random() * 7);
            int row = (int) (Math.random() * 5);
            for (int i = 0; i < size; i++) {
                board[row + i][col] = "S";
            }
        }
    }

    public static int userFire(String[][] board, int hits, int torps) {
        Scanner input = new Scanner(System.in);
        int row, col;
        System.out.println("You have: " + torps + " torpedos left!");
        System.out.println("Select a row to fire in: ");
        row = input.nextInt();
        while (row > 8 || row < 1) // Error checking for row
        {
            System.out.println("Enter a valid row (1 -> 8)");
            row = input.nextInt();
        }
        System.out.println("Select a column to fire in: ");
        col = input.nextInt();
        while (col > 8 || col < 1) // Error checking for column
        {
            System.out.println("Enter a valid col (1 -> 8)");
            col = input.nextInt();
        }
        if (board[row - 1][col - 1].equals("S")) {
            hits++;
            System.out.println("~~~~~~~ HIT ~~~~~~~");
            board[row - 1][col - 1] = "!";
        } else {
            System.out.println("~~~~~~~ MISS ~~~~~~~");
            board[row - 1][col - 1] = "M";
        }
        return hits;
    }

    public static void finall(int hits, int torps) {
        if (hits < 4) {
            System.out.println("Sorry, but you lost because you didn't sink the ship.");
        }
        if (torps < 1) {
            System.out.println("You have lost all your torpedos");
        } else if (hits >= 4) {
            System.out.println("You have beaten the game battleship, Thanks for playing!");
        }
        System.out.println("Good game, well played!");
    }

    public static void main(String[] arg) {
        String[][] board = new String[8][8];
        createBoard(board);
        createShip(board, 4);
        int torps = 15;
        int hits = 0;
        /// Starting real stuff
        while (torps > 0 && hits < 4) {
            showBoard(board);
            hits = userFire(board, hits, torps);
            torps--;
        }
        finall(hits, torps);
    }
}

2 个答案:

答案 0 :(得分:1)

欢迎使用Stack Overflow,Noah!

首先说几句:

  1. 这类问题通常被认为是不清楚你在问什么和/或太宽泛,所以期待downvotes和结束你的问题。你应该真正阅读how to ask a good question
  2. 如果您在使用代码设计时遇到困难,通常最好先拿一支笔和一些纸张并草拟粗略的轮廓。对于将现有程序转换为OOP的具体任务,class diagram将是一个非常好的起点。
  3. 现在已经不在了,我会给你一个正确的方向,看到你是Stack Overflow的新手。

    在您的代码中,我们可以找到两个有趣的方法:

    • createBoard
    • createShip

    请注意它们是如何从create开始的?这是一个非常非常强烈的暗示,至少为这两个对象编写类可能是个好主意:

    • Board
    • Ship

    说到方法,你当前的代码再次提供了一些指示。虽然我不确定是否会发射鱼雷之类的东西,但看起来您可能希望至少将showBoard()转换为show类的方法Board

    现在,您需要的其他类和方法实际上取决于该游戏的规则(我不熟悉它),您希望它具有哪些功能,当然还有您希望如何实现它。可能的其他类可能是Torpedo,但也许这些类只是您Ship类的原始类型成员。另一个候选人可能是TileBoard的基本构建块?

    你必须从这里拿走它,真的。 Java Lessons on Classes and Objects会很有用 我希望这能让你开始。

答案 1 :(得分:0)

好的,当你开始编写OOP时,你应该分析你的问题并将其分解。游戏的哪些部分组成,它们的功能和属性是什么,这些部分如何相互作用?我会帮助你开始,考虑制作电路板和电池类。

在OOP中,您喜欢Java,您需要创建一个您定义的对象(类)的新实例。所以,让我们说你创建了一个类:

  class Shape {
      int length, width;
      String color ="";
  }

要在main中使用Shape,请创建一个新实例

  class Driver {
        public static void main (String [] args){
               //First you specify the type as your object name, then name the       variable and lastly, you do new object. 
               Shape myShape = new Shape ();
       }
  }
相关问题