Здравствуйте, мне нужен совет, как реализовать следующее:
Задача: Игра симулирует рандомно броски 5 игровых кубиков 20 раз, считывает значения, анализирует их и выдает сообщения, о том, имеется ли выигрышная комбинация, или нет.
С игрой уже все понятно, проблема в том, как к этому "прикрутить" статистику типа:
"Статистика игры после ___ бросков:
| Результат: | Количество |
Grand 1
Poker 2
Full House 2
Sequence 0
You lose! 5
"
Это лучше реализовать через одномерный массив?
Код:
public class DiceGame {
public static void main(String args[]) {
Integer[][] throws = throwing();
String[] haende = checkHand(throws);;
System.out.println("Programm DiceGame\n\n Throw dices!:");
String leftAlignFormat = "| %-5d | %-9d | %-9d | %-9d | %-9d | %-9d | %-13s |%n";
System.out.format("+-------+-----------+-----------+-----------+-----------+-----------+---------------+%n");
System.out.format("| Game | Dice 1 | Dice 2 | Dice 3 | Dice 4 | Dice 5 | Results |%n");
System.out.format("+-------+-----------+-----------+-----------+-----------+-----------+---------------+%n");
for (int i = 0; i < throws.length; i++) {
System.out.format(leftAlignFormat, i+1, < throws[i][0], < throws[i][1], throws[i][2], throws[i][3], throws[i][4], haende[i]);
}
System.out.format("+-------+-----------+-----------+-----------+-----------+-----------+---------------+%n");
}
public static Integer[][] throwing() {
Integer[][] throws = new Integer[20][5];
for (int i = 0; i < throws.length; i++) {
for (int j = 0; j < throws[i].length; j++) {
double z = Math.random() * 6;
int throw = (int)z;
throw = throw + 1;
throws [i][j] = throw;
}
}
return throws;
}
public static String[] checkHand(Integer[][] _throws) {
String[] haende = new String[20];
for (int i = 0; i < haende.length; i++) {
if (checkGrand(_throws [i])) {
haende[i] = "Grand";
} else if (checkPoker(_throws[i])) {
haende[i] = "Poker";
} else if (checkFullHouse(_throws[i])) {
haende[i] = "Full House";
} else if (checkSequence(_throws[i])) {
haende[i] = "Sequence";
} else {
haende[i] = "You lose!";
}
}
return haende;
}
public static boolean checkGrand(Integer[] _throws) {
if (
_throws[0] == _throws [1] &&
_throws[1] == _throws [2] &&
_throws[2] == _throws [3] &&
_throws[3] == _throws [4]
) {
return true;
}
return false;
}
public static boolean checkPoker(Integer[] _throws) {
for (int i = 3; i < _throws.length; i++) {
int count = 0;
for (int j = 0; j < i; j++) {
if (_throws[i] == _throws[j]) {
count++;
}
}
if (count == 3) {
return true;
}
}
return false;
}
public static boolean checkFullHouse(Integer[] _throws) {
int doubleCheck = 0;
for (int i = 2; i < _throws.length; i++) {
int count = 0;
for (int j = 0; j < i; j++) {
if (_throws[i] == _throws[j]) {
count++;
doubleCheck = _throws[i];
}
}
if (count == 2) {
System.out.println(doubleCheck);
for (int k = 1; k < _throws.length; k++) {
int count2 = 0;
for (int l = 0; l < k; l++) {
if (_throws[k] == _throws[l] && _throws[k] != doubleCheck) {
count2++;
System.out.println(_throws[k]);
System.out.println(_throws[l]);
}
}
if (count2 == 1) {
return true;
}
}
return false;
}
count = 0;
}
return false;
}
public static boolean checkSequence(Integer[] _throws) {
for (int i = 1; i < 3; i++) {
if (_throws[i] != (_throws[i+1]-1)) {
return false;
}
}
if (_throws[0] != _throws[1]-1 && _throws[3] != _throws[4]-1) {
return false;
}
return true;
}
}