Skip to content

Commit 3ac4c37

Browse files
committed
Make mvp
1 parent 8baa3c5 commit 3ac4c37

File tree

7 files changed

+70
-17
lines changed

7 files changed

+70
-17
lines changed

app/src/main/java/com/andlvovsky/lines/activity/MainActivity.java

+31-5
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,30 @@
55
import android.widget.Button;
66
import android.widget.TableLayout;
77
import android.widget.TableRow;
8+
import android.widget.TextView;
89

910
import androidx.appcompat.app.AppCompatActivity;
1011

1112
import com.andlvovsky.lines.R;
1213
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;
1317
import com.andlvovsky.lines.meta.GameConstants;
1418
import com.andlvovsky.lines.view.CellView;
1519

1620
import java.util.ArrayList;
1721
import java.util.List;
1822

1923
public class MainActivity extends AppCompatActivity {
20-
LinesGameController controller = LinesGameController.INSTANCE;
24+
private LinesGameController controller = LinesGameController.INSTANCE;
25+
private LinesGame game = LinesGame.INSTANCE;
2126

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<>();
2532

2633
@Override
2734
protected void onCreate(Bundle savedInstanceState) {
@@ -32,18 +39,23 @@ protected void onCreate(Bundle savedInstanceState) {
3239

3340
public void restart() {
3441
controller.startGame();
42+
hintTextView.setText("");
43+
repaintGame();
3544
}
3645

3746
private void init() {
3847
setViews();
3948
createCells();
4049
setListeners();
50+
hintTextView.setText("");
4151
controller.startGame();
4252
}
4353

4454
private void setViews() {
4555
restartButton = findViewById(R.id.restartButton);
56+
scoreTextView = findViewById(R.id.scoreTextView);
4657
boardLayout = findViewById(R.id.boardTableLayout);
58+
hintTextView = findViewById(R.id.hintTextView);
4759
}
4860

4961
private void createCells() {
@@ -87,8 +99,22 @@ public void onClick(View v) {
8799
cell.setOnClickListener(new View.OnClickListener() {
88100
@Override
89101
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+
}
91111
}
92112
});
93113
}
114+
115+
private void repaintGame() {
116+
for(CellView cell: cells)
117+
cell.invalidate();
118+
scoreTextView.setText("Score: " + game.getScore());
119+
}
94120
}

app/src/main/java/com/andlvovsky/lines/analyzer/BoardAnalyzer.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ private BoardAnalyzer() {
2424

2525
public static boolean isValidMove(Board board, Move move) {
2626
return !(move.iFrom() == move.iTo() && move.jFrom() == move.jTo()) &&
27-
board.isEmpty(move.iFrom(), move.jFrom()) &&
27+
!board.isEmpty(move.iFrom(), move.jFrom()) &&
2828
board.isEmpty(move.iTo(), move.jTo()) &&
2929
BoardGraphAlgorithms.doesPathExist(board, move);
3030
}
@@ -60,7 +60,7 @@ public static int countEmptyCells(Board board) {
6060
}
6161

6262
public static boolean isInBoard(Coordinates coord) {
63-
return coord.i > 0 && coord.i < SIZE && coord.j > 0 && coord.j < SIZE;
63+
return coord.i >= 0 && coord.i < SIZE && coord.j >= 0 && coord.j < SIZE;
6464
}
6565

6666
private static List<Coordinates> findLongest(Board board, Coordinates transition) {

app/src/main/java/com/andlvovsky/lines/analyzer/BoardGraphAlgorithms.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ private static boolean bfs(Board board, Coordinates from, Coordinates to) {
3838
return true;
3939
nextVisited.addAll(findPossibleNextCells(board, coord, used));
4040
}
41-
lastVisited = nextVisited;
41+
lastVisited.clear();
42+
lastVisited.addAll(nextVisited);
4243
nextVisited.clear();
4344
}
4445
return false;
@@ -48,14 +49,17 @@ private static List<Coordinates> findPossibleNextCells(Board board, Coordinates
4849
List<Coordinates> nextCoords = new ArrayList<>();
4950
for (Coordinates step: validSteps) {
5051
Coordinates next = new Coordinates(coord.i + step.i, coord.j + step.j);
51-
if (isValidCell(board, next))
52+
if (isValidCell(board, next, used)) {
5253
nextCoords.add(next);
54+
used[next.i][next.j] = true;
55+
}
5356
}
5457
return nextCoords;
5558
}
5659

57-
private static boolean isValidCell(Board board, Coordinates coord) {
60+
private static boolean isValidCell(Board board, Coordinates coord, boolean[][] used) {
5861
return BoardAnalyzer.isInBoard(coord) &&
59-
board.isEmpty(coord.i, coord.j);
62+
board.isEmpty(coord.i, coord.j) &&
63+
!used[coord.i][coord.j];
6064
}
6165
}

app/src/main/java/com/andlvovsky/lines/board/Board.java

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.andlvovsky.lines.board;
22

3+
import com.andlvovsky.lines.game.Move;
4+
35
import static com.andlvovsky.lines.meta.GameConstants.SIZE;
46

57
public class Board implements Cloneable {
@@ -31,6 +33,11 @@ public void addBall(int i, int j, int color) {
3133
cell.setBallColor(color);
3234
}
3335

36+
public void moveBall(Move move) {
37+
addBall(move.iTo(), move.jTo(), getColor(move.iFrom(), move.jFrom()));
38+
clearCell(move.iFrom(), move.jFrom());
39+
}
40+
3441
@Override
3542
public Object clone() {
3643
Board board = new Board();

app/src/main/java/com/andlvovsky/lines/controller/LinesGameController.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.andlvovsky.lines.controller;
22

3+
import com.andlvovsky.lines.exception.InvalidMoveException;
34
import com.andlvovsky.lines.game.LinesGame;
45
import com.andlvovsky.lines.game.Move;
56
import com.andlvovsky.lines.util.Coordinates;
@@ -21,9 +22,14 @@ public void startGame() {
2122
public void chooseCell(int i, int j) {
2223
chosenCells.add(new Coordinates(i, j));
2324
if (chosenCells.size() == 2) {
24-
game.makeMove(new Move(chosenCells.get(0).i, chosenCells.get(0).j,
25-
chosenCells.get(1).i, chosenCells.get(1).j));
26-
chosenCells.clear();
25+
try {
26+
game.makeMove(new Move(chosenCells.get(0).i, chosenCells.get(0).j,
27+
chosenCells.get(1).i, chosenCells.get(1).j));
28+
} catch (InvalidMoveException e) {
29+
// ignore
30+
} finally {
31+
chosenCells.clear();
32+
}
2733
}
2834
}
2935

app/src/main/java/com/andlvovsky/lines/game/LinesGame.java

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.andlvovsky.lines.analyzer.BoardAnalyzer;
44
import com.andlvovsky.lines.board.Board;
5+
import com.andlvovsky.lines.exception.GameOverException;
56
import com.andlvovsky.lines.exception.InvalidMoveException;
67
import com.andlvovsky.lines.generator.BallsGenerator;
78

@@ -17,6 +18,7 @@ public void start() {
1718
score = 0;
1819
nextColors = BallsGenerator.generateNextColors();
1920
BallsGenerator.placeFirstBalls(board);
21+
score += BoardAnalyzer.removeBallsAndCalculateAddingScore(board);
2022
}
2123

2224
public int getNextColor(int pos) {
@@ -34,9 +36,12 @@ public Board getBoard() {
3436
public void makeMove(Move move) {
3537
if (!BoardAnalyzer.isValidMove(board, move))
3638
throw new InvalidMoveException();
39+
board.moveBall(move);
3740
score += BoardAnalyzer.removeBallsAndCalculateAddingScore(board);
3841
BallsGenerator.placeNextBalls(board, nextColors);
3942
score += BoardAnalyzer.removeBallsAndCalculateAddingScore(board);
43+
if (BoardAnalyzer.countEmptyCells(board) == 0)
44+
throw new GameOverException();
4045
nextColors = BallsGenerator.generateNextColors();
4146
}
4247
}

app/src/main/res/layout/activity_main.xml

+8-3
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,21 @@
2828
android:layout_height="wrap_content"
2929
android:orientation="horizontal">
3030

31-
<Space
31+
<TextView
32+
android:id="@+id/hintTextView"
3233
android:layout_width="match_parent"
3334
android:layout_height="wrap_content"
34-
android:layout_weight="1" />
35+
android:layout_weight="1"
36+
android:text="Game Over!"
37+
android:textAlignment="center"
38+
android:textColor="@color/colorAccent"
39+
android:textSize="24sp" />
3540

3641
<Button
3742
android:id="@+id/restartButton"
3843
android:layout_width="match_parent"
3944
android:layout_height="wrap_content"
40-
android:layout_weight="3"
45+
android:layout_weight="1"
4146
android:text="RESTART GAME" />
4247
</LinearLayout>
4348

0 commit comments

Comments
 (0)