-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
138 lines (119 loc) · 3.85 KB
/
Game.java
File metadata and controls
138 lines (119 loc) · 3.85 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package ru.spbau.mit.alyokhina.TicTacToe;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
/**
* Class for key actions of game
*/
public class Game {
/**
* GridPane for current game
*/
protected GridPane gridPane;
/**
* Number playing
*/
protected int numberGames = 0;
/**
* Statistics for current game
*/
protected Statistics statistics;
/**
* Constructor
*/
public Game(GridPane gridPane, Statistics statistics) {
this.gridPane = gridPane;
this.statistics = statistics;
gridPane.getChildren().clear();
CreateElements.createButtonToMainActivity(gridPane);
}
/**
* Translated text from buttons array in string array
*/
protected String[] toStringArray(Button[] table) {
String[] tableString = new String[table.length];
for (int i = 0; i < table.length; i++) {
tableString[i] = table[i].getText();
}
return tableString;
}
/**
* Check the state of the game
*
* @param tableString state board, "" - if cell is empty
* @return "X" - if X wins , "O" - if O wins, "" - if state is unknown, else - draw
*/
public static String check(String[] tableString) {
boolean existEmpty = false;
for (int i = 0; i < tableString.length; i++) {
if (tableString[i].equals("")) {
existEmpty = true;
}
}
for (int i = 0; i < 9; i += 3) {
if (!tableString[i].equals("") && tableString[i].equals(tableString[i + 1]) && tableString[i + 1].equals(tableString[i + 2])) {
return tableString[i];
}
}
for (int i = 0; i < 3; i++) {
if (!tableString[i].equals("") && tableString[i].equals(tableString[i + 3]) && tableString[i + 3].equals(tableString[i + 6])) {
return tableString[i];
}
}
if (!tableString[0].equals("") && tableString[0].equals(tableString[4]) && tableString[4].equals(tableString[8])) {
return tableString[0];
}
if (!tableString[2].equals("") && tableString[2].equals(tableString[4]) && tableString[4].equals(tableString[6])) {
return tableString[2];
}
if (existEmpty) {
return "";
} else {
return "draw";
}
}
/**
* /**
* Check the state of the game
*
* @param tableString state board, "" - if cell is empty
* @param isPreliminaryCheck, true - if real state, false - if we want check move
* @return "X" - if X wins , "O" - if O wins, "" - if state is unknown, else - draw
*/
protected String check(String[] tableString, boolean isPreliminaryCheck) {
String result = check(tableString);
if (!isPreliminaryCheck && !result.equals("")) {
setResult(result);
}
return result;
}
/**
* Print result games
*/
private void setResult(String win) {
numberGames++;
CreateElements.setResult(gridPane, statistics, win);
CreateElements.createButtonReplay(gridPane, statistics, this::goBack);
}
/**
* For return in this activity
*/
protected void goBack(Statistics statistics) {
this.statistics = statistics;
gridPane.getChildren().clear();
CreateElements.createButtonToMainActivity(gridPane);
menu();
}
/**
* Symbol to which the player move
*/
public String getCompSymb(int player) {
return (numberGames % 2 == player) ? "X" : "O";
}
/**
* Before games we should choose characteristics game
*/
public void menu() {
CreateElements.createButton(gridPane, 50, 200, 200, 70, "Легкий");
CreateElements.createButton(gridPane, 50, 200, 200, 170, "Сложный");
}
}