Bug 555 - Wrong input will make the chessboard array overflow
Summary: Wrong input will make the chessboard array overflow
Status: CONFIRMED
Alias: None
Product: Chinese Checker
Classification: Unclassified
Component: Algorithm Component (show other bugs)
Version: unspecified
Hardware: All All
: --- enhancement
Assignee: yutongmen2-c
URL:
Depends on:
Blocks:
 
Reported: 2021-11-11 14:29 HKT by yutongmen2-c
Modified: 2021-11-12 19:30 HKT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description yutongmen2-c 2021-11-11 14:29:26 HKT

    
Comment 1 yutongmen2-c 2021-11-12 19:30:23 HKT
The program does not have an input check at the early phase. A wrong input will lead to program crash. To avoid this, a input check statement is added. The implementation is as follows:


Former partial code:

 System.out.println("Please input start move");
            int start_x = 0;
            int start_y = 0;

            if (in.hasNextInt()) {
                start_x = in.nextInt();
                start_y = in.nextInt();
            }

            Point ps = new Point(start_x, start_y);
            StartMove start_m = new StartMove(ps);



New partial code:

System.out.println("Please input start move");
            int start_x = 0;
            int start_y = 0;

            // if start_x or start_y is out of range [0,8]
            do {
                if (in.hasNextInt()) {
                    start_x = in.nextInt();
                    start_y = in.nextInt();
                }
            } while (start_x < 0 || start_x > 8 || start_y < 0 || start_y > 8);
            Point ps = Graph.getInstance().chessboard[start_x][start_y];