-
Notifications
You must be signed in to change notification settings - Fork 245
Expand file tree
/
Copy pathTicTacToeViewModel.java
More file actions
55 lines (38 loc) · 1.2 KB
/
Copy pathTicTacToeViewModel.java
File metadata and controls
55 lines (38 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.acme.tictactoe.viewmodel;
import android.databinding.ObservableArrayMap;
import android.databinding.ObservableField;
import com.acme.tictactoe.model.Board;
import com.acme.tictactoe.model.Player;
public class TicTacToeViewModel implements ViewModel {
private Board model;
public final ObservableArrayMap<String, String> cells = new ObservableArrayMap<>();
public final ObservableField<String> winner = new ObservableField<>();
public TicTacToeViewModel() {
model = new Board();
}
@Override
public void onCreate() {
}
@Override
public void onPause() {
}
@Override
public void onResume() {
}
@Override
public void onDestroy() {
}
public void onResetSelected() {
model.restart();
winner.set(null);
cells.clear();
}
public void onClickedCellAt(int row, int col) {
String key = "" + row + col;
if (!cells.containsKey(key)) {
Player playerThatMoved = model.mark(row, col);
cells.put(key, playerThatMoved == null ? null : playerThatMoved.toString());
winner.set(model.getWinner() == null ? null : model.getWinner().toString());
}
}
}