jeudi 28 janvier 2021

Is there a way to avoid repetition and simplify this?

The followings is the pseudocode, what I am trying to achieve is scanning a block of a 2D array called 'board' and the scanner should return what it detected. The player/user inputs X and Y coordinates and based on that the scanner will launch on the board. The scanner can only scan 9 blocks/values around the centre/drop point.

Algorithm #: inputEntered - actionPerformed (ActionEvent textfInput)

1. SET xVal = user inputs x coordinate
2. SET yVal = user inputs y coordinate 

...

Comment: scans centre (radar drop point)
13.         IF board[xVal][yVal] == 0
14.             SET buttons[xVal][yVal] = "@"
15.         ELSE IF board[xVal][yVal] != 0
16.             SET buttons[xVal][yVal] = "X"
17.         END IF


Comment: scans top of array selected
18.         IF board[xVal-1][yVal] == 0
19.             SET buttons[xVal-1][yVal] = "@"
20.         ELSE IF board[xVal][yVal-1] != 0
21.             SET buttons[xVal-1][yVal] = "X"
22.         END IF 

Comment: scans middle point of array selected
23.         IF board[xVal][yVal] == 0
24.             SET buttons[xVal][yVal] = "@"
25.         ELSE IF board[xVal][yVal] != 0
26.             SET buttons[xVal][yVal] = "X"
27.         END IF

Comment: scans bottom of array selected
28.         IF board[xVal+1][yVal] == 0
29.             SET buttons[xVal+1][yVal] = "@"
30.         ELSE IF board[xVal+1][yVal] != 0
31.             SET buttons[xVal+1][yVal] = "X"
32.         END IF

Comment: scans left of array selected
33.         IF board[xVal][yVal-1] == 0
34.             SET buttons[xVal][yVal-1] = "@"
35.         ELSE IF board[xVal][yVal-1] != 0
36.             SET buttons[xVal][yVal-1] = "X"
37.         END IF

Comment: scans right of array selected
38.         IF board[xVal][yVal+1] == 0
39.             SET buttons[xVal][yVal+1] = "@"
40.         ELSE IF board[xVal][yVal+1] != 0
41.             SET buttons[xVal][yVal+1] = "X"
42.         END IF

Comment: scans top-left of array selected
43.         IF board[xVal-1][yVal-1] == 0
44.             SET buttons[xVal-1][yVal-1] = "@"
45.         ELSE IF board[xVal-1][yVal-1] != 0
46.             SET buttons[xVal-1][yVal-1] = "X"
47.         END IF

Comment: scans bottom-right of array selected
48.         IF board[xVal+1][yVal+1] == 0
49.             SET buttons[xVal+1][yVal+1] = "@"
50.         ELSE IF board[xVal+1][yVal+1] != 0
51.             SET buttons[xVal+1][yVal+1] = "X"
52.         END IF


Comment: scans top-right of array selected
53.         IF board[xVal-1][yVal+1] == 0
54.             SET buttons[xVal-1][yVal+1] = "@"
55.         ELSE IF board[xVal-1][yVal+1] != 0
56.             SET buttons[xVal-1][yVal+1] = "X"
57.         END IF

Comment: scans bottom-left of array selected
58.         IF board[xVal+1][yVal-1] == 0
59.             SET buttons[xVal+1][yVal-1] = "@"
60.         ELSE IF board[xVal+1][yVal-1] != 0
61.             SET buttons[xVal+1][yVal-1] = "X"
62.         END IF
63.     END IF
...

Aucun commentaire:

Enregistrer un commentaire