barcaisrio 2020-10-29 04:36 采纳率: 0%
浏览 50

mock object(模拟对象)这么做是否正确?模拟对象只能在测试代码不能在开发代码中是什么意思?

创建了一个名为PlayerController的类,该类处理玩家的输入和输出。 我想知您到目前为止我做的是否正确(有一个PLayerControllerTest类)。
我们老师说模拟对象只能在测试代码不能在开发代码中。

PlayerController:

package gradeTesting;

import java.util.ArrayList;
import java.util.Scanner;

public class playerController {

    private ArrayList<Player> players;
    private int boardRows;
    private int boardCols;
    private boolean over;
    private static mainView view;
    private ScannerInput in;

    private boolean isOver() {return over;}

    public playerController(int boardRows, int boardCols, ScannerInput in) {
        players = new ArrayList<Player>();
        this.boardRows = boardRows;
        this.boardCols = boardCols;
        over = false;
        view = new mainView(this);
        this.in = in;
    }

    public void createPlayer() {
        Board b = new Board(boardRows, boardCols);

        Player p = new Player(players.size()+1, b);



        try {
            /*
             * read and create number of ships
             */
            //int nShips = Integer.parseInt(in.nextLine());
            int nShips = 1;
            boolean insert;

            for(int i=0; i<nShips; i++) {
                insert = false;

                while(!insert) {

                    ArrayList<Coordinate> shipCoords = new ArrayList<Coordinate>();
                    int x = 0;
                    int y = 0;
                    System.out.println("Ship " + i + " coordinates x, y should be separated by coma and a space between different entries(x,y w,z ....)");
                    String text = in.nextLine();
                    /*
                     * read and create coordinates for each ship
                     */
                    for(String coordi : text.split(" ")) {
                        String []xy = coordi.split(",");
                        x = Integer.parseInt(xy[0]);
                        y = Integer.parseInt(xy[1]);
                        shipCoords.add(new Coordinate(x,y));
                    }

                    //add ship to player
                    try {
                        p.addShip(shipCoords.size(), shipCoords);
                        insert = true;
                    }catch(Exception e) {
                        e.printStackTrace();
                    }
                }

            }

            //add player to list of players
            players.add(p);

        }catch(Exception e) {
            e.printStackTrace();
        }

        System.out.println();

    }

    public void play() {
        int turn = 0;
        Scanner in = new Scanner(System.in);
        Player p1 = players.get(0);
        Player p2 = players.get(1);
        boolean correct = false;

        while(!isOver() && players.size()>1) {
            correct = false;

            while(!correct) {

                view.printMenu(p1);

                correct = selectAction(p1, p2);

                if(correct) {

                    Player temp = p1;
                    p1=p2;
                    p2 = temp;
                    correct = true;

                }else {
                    correct = false;                    
                }

            }
        }

        view.playerWinner(players.get(0));

    }

    public boolean selectAction(Player p1, Player p2) {

        Scanner in = new Scanner(System.in);
        int action = Integer.parseInt(in.nextLine());
        boolean res = false;

        switch(action) {
            case 1:
                printBoard(p1);
                res = false;
            case 2:
                printEnemiBoard(p1);
                res = false;
            case 3:
                res = attack(p1, p2);
            /*case 4:
                surrender(p1);
                res = true;*/
        }

        return res; 
    }

    public void printBoard(Player p) {
        view.printBoard(p.getBoard()); 
    }

    public void printEnemiBoard(Player p) {
        view.printBoard(p.getEnemiBoard());
    }

    public boolean attack(Player p1, Player p2) {

        Scanner in = new Scanner(System.in);

        try {

            System.out.println("coordinate to attack (coordinate comma separated x,y): ");
            System.out.println();

            String[] xy = in.nextLine().split(",");
            Coordinate attack = new Coordinate(Integer.parseInt(xy[0]), Integer.parseInt(xy[1]));
            boolean success = p2.attacked(attack);

            if(success) {
                p1.setEnemi(attack);
                view.hitBoat(xy[0], xy[1]);

                if(!p2.isAlive()) {
                    players.remove(1);
                    view.playerEliminated(p2);
                    over = players.size()<=1;
                }

            }

            return true;

        }catch(Exception e) {
            if(e.getMessage().equals("OutR"))
            e.printStackTrace();
            return false;
        }

    }

    public void surrender(Player p1) {
        players.remove(0);
        view.playerEliminated(p1);
        over = players.size()<=1;
    }


    public static void main(String[] args) {

        ScannerInput in = new ScannerConsole(System.in);

        playerController controller = new playerController(10,10, in);
        controller.view.prepareGame();

        controller.play();

    }

}

PLayerControllerTest:

package gradeTesting;

import org.junit.Test;

public class playerControllerTest {


    @Test
    public void test() {
        ConsoleInput ci = new ConsoleInput();
        playerController pc = new playerController(10,10, ci);

        //create player 1
        pc.createPlayer();

        //create player 2
        pc.createPlayer();

    }

}
  • 写回答

1条回答 默认 最新

  • 冰点. 全栈领域优质创作者 2023-06-14 18:11
    关注

    模拟对象只能在测试代码不能在开发代码中是什么意思?意思是你的测试的代码应该写在测试类里面,包括你造数据的代码

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料