5
5
import android .widget .Button ;
6
6
import android .widget .TableLayout ;
7
7
import android .widget .TableRow ;
8
+ import android .widget .TextView ;
8
9
9
10
import androidx .appcompat .app .AppCompatActivity ;
10
11
11
12
import com .andlvovsky .lines .R ;
12
13
import com .andlvovsky .lines .controller .LinesGameController ;
14
+ import com .andlvovsky .lines .exception .GameOverException ;
15
+ import com .andlvovsky .lines .exception .InvalidMoveException ;
16
+ import com .andlvovsky .lines .game .LinesGame ;
13
17
import com .andlvovsky .lines .meta .GameConstants ;
14
18
import com .andlvovsky .lines .view .CellView ;
15
19
16
20
import java .util .ArrayList ;
17
21
import java .util .List ;
18
22
19
23
public class MainActivity extends AppCompatActivity {
20
- LinesGameController controller = LinesGameController .INSTANCE ;
24
+ private LinesGameController controller = LinesGameController .INSTANCE ;
25
+ private LinesGame game = LinesGame .INSTANCE ;
21
26
22
- Button restartButton ;
23
- TableLayout boardLayout ;
24
- List <CellView > cells = new ArrayList <>();
27
+ private Button restartButton ;
28
+ private TextView scoreTextView ;
29
+ private TextView hintTextView ;
30
+ private TableLayout boardLayout ;
31
+ private List <CellView > cells = new ArrayList <>();
25
32
26
33
@ Override
27
34
protected void onCreate (Bundle savedInstanceState ) {
@@ -32,18 +39,23 @@ protected void onCreate(Bundle savedInstanceState) {
32
39
33
40
public void restart () {
34
41
controller .startGame ();
42
+ hintTextView .setText ("" );
43
+ repaintGame ();
35
44
}
36
45
37
46
private void init () {
38
47
setViews ();
39
48
createCells ();
40
49
setListeners ();
50
+ hintTextView .setText ("" );
41
51
controller .startGame ();
42
52
}
43
53
44
54
private void setViews () {
45
55
restartButton = findViewById (R .id .restartButton );
56
+ scoreTextView = findViewById (R .id .scoreTextView );
46
57
boardLayout = findViewById (R .id .boardTableLayout );
58
+ hintTextView = findViewById (R .id .hintTextView );
47
59
}
48
60
49
61
private void createCells () {
@@ -87,8 +99,22 @@ public void onClick(View v) {
87
99
cell .setOnClickListener (new View .OnClickListener () {
88
100
@ Override
89
101
public void onClick (View v ) {
90
- controller .chooseCell (cell .iCoord (), cell .jCoord ());
102
+ try {
103
+ controller .chooseCell (cell .iCoord (), cell .jCoord ());
104
+ } catch (InvalidMoveException e ) {
105
+ // ignore
106
+ } catch (GameOverException e ) {
107
+ hintTextView .setText ("Game Over!" );
108
+ } finally {
109
+ repaintGame ();
110
+ }
91
111
}
92
112
});
93
113
}
114
+
115
+ private void repaintGame () {
116
+ for (CellView cell : cells )
117
+ cell .invalidate ();
118
+ scoreTextView .setText ("Score: " + game .getScore ());
119
+ }
94
120
}
0 commit comments