Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > Java программирование > Общие вопросы по Java, Java SE, Kotlin
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 11.01.2018, 03:18   #1
Augustus
 
Регистрация: 10.01.2018
Сообщений: 7
По умолчанию Добавить статистику к массиву

Здравствуйте, мне нужен совет, как реализовать следующее:

Задача: Игра симулирует рандомно броски 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;
    }
}
Augustus вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц

Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск


Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
вывести статистику (html) theliera Фриланс 12 12.11.2013 20:41
вывести статистику theliera HTML и CSS 3 01.11.2013 11:25
засчитывает в статистику? scroyler Работа с сетью в Delphi 3 03.03.2012 18:31
отобразить статистику мыши PE3K!u' Фриланс 8 05.01.2011 18:06
Собрать статистику счетчика. Djonson PHP 1 01.03.2008 18:35