Yahtzee का खेल खेलते हैं


18

याहत्ज़ी खेल में, खिलाड़ी 5 6-पक्षीय पासा को प्रति बार तीन बार घुमाते हैं, संभवतः रोल के बीच पासा बचाते हैं, और फिर एक श्रेणी का चयन करते हैं जिसे वे अपने रोल के लिए उपयोग करना चाहते हैं। यह तब तक जारी रहता है जब तक कि अधिक श्रेणियां नहीं होती हैं (जो 13 मोड़ के बाद होती है)। फिर, खिलाड़ियों का स्कोर लंबा हो जाता है, और सबसे अधिक स्कोर वाले खिलाड़ी जीत जाते हैं।

श्रेणियां इस प्रकार हैं ("पासा का योग" का अर्थ है निर्दिष्ट पासा पर पिप्स की संख्या को जोड़ना):

  • ऊपरी भाग
    • इक्के : 1 पाइप दिखा पासा का योग
    • Twos : 2 पिप्स दिखाने वाले पासा का योग
    • पेड़ : 3 पिप्स दिखाने वाले पासा का योग
    • चौके : 4 पिप्स दिखाने वाले पासा का योग
    • फाइव्स : 5 पिप्स दिखाने वाले पासा का योग
    • छक्के : 6 पिप्स दिखाने वाले पासा का योग
  • निचला भाग
    • तीन का एक प्रकार : एक ही मूल्य के साथ 3 पासा, स्कोर सभी पासा का योग है
    • एक प्रकार का चार : एक ही मूल्य के साथ 4 पासा, स्कोर सभी पासा का योग है
    • पूर्ण घर : एक मूल्य के साथ 3 पासा और दूसरे के साथ 2, स्कोर 25 है
    • छोटे सीधे : 4 अनुक्रमिक पासा, स्कोर 30 है
    • बड़े सीधे : 5 अनुक्रमिक पासा, स्कोर 40 है
    • Yahtzee : एक ही मूल्य के साथ सभी 5 पासा, स्कोर 50 है
    • संभावना : पासा के किसी भी संयोजन, स्कोर सभी पासा का योग है

श्रेणी विकल्पों के बारे में कुछ नियम हैं:

  • यदि कोई खिलाड़ी ऐसी श्रेणी चुनता है जो उनके रोल से मेल नहीं खाती है, तो उन्हें उस श्रेणी के लिए 0 का स्कोर प्राप्त होता है।
  • यदि कोई खिलाड़ी ऊपरी भाग में कम से कम 63 अंक अर्जित करता है, तो उन्हें 35 बोनस अंक प्राप्त होते हैं।
  • यदि किसी खिलाड़ी ने याहत्ज़ी को रोल किया है, लेकिन याहत्ज़ी की श्रेणी पहले से ही ले ली गई है (एक और याहत्ज़ी द्वारा - एक मिस के लिए 0 में भरना गिनती नहीं है), तो उन्हें 100 अंक का बोनस प्राप्त होता है। यह बोनस पहले के बाद प्रत्येक याहत्ज़ी के लिए प्रदान किया जाता है।
    • इसके अतिरिक्त, खिलाड़ी को अभी भी एक श्रेणी भरने के लिए चुनना होगा। उन्हें अपने रोल के अनुरूप ऊपरी सेक्शन की श्रेणी चुननी होगी (जैसे 5 6 के रोल को छक्के की श्रेणी में रखा जाना चाहिए)। यदि संबंधित ऊपरी अनुभाग श्रेणी का पहले ही उपयोग किया जा चुका है, तो याहटज़ी का उपयोग निचले तबके की श्रेणी के लिए किया जा सकता है (इस मामले में, फुल हाउस, स्मॉल स्ट्रेट, या लार्ज स्ट्रेट को चुनने पर अंक 0 की बजाय सामान्य राशि प्राप्त होती है)। यदि सभी निचली श्रेणी की श्रेणियों को ले लिया जाता है, तो याहत्ज़ी को अप्रयुक्त ऊपरी खंड श्रेणी में लागू किया जा सकता है, जिसमें 0 का स्कोर होता है।

चुनौती

इस चुनौती में, प्रतियोगी यहत्ज़ी के 1000 खेल खेलेंगे। प्रत्येक गेम के अंत में, सबमिशन (रों) जिसने सबसे अधिक अंक प्राप्त किए हैं उसे 1 अंक प्राप्त होगा। सभी खेलों के समाप्त होने के बाद, सबसे अधिक अंकों के साथ सबमिशन जीत जाएगा। यदि कोई टाई है, तो अतिरिक्त खेल केवल बंधे हुए सबमिशन के साथ खेला जाएगा जब तक कि टाई टूट न जाए।

नियंत्रक

इस GitHub रिपॉजिटरी पर पूरा कंट्रोलर कोड पाया जा सकता है । यहां सार्वजनिक इंटरफेस हैं जिनके साथ खिलाड़ी बातचीत करेंगे:

public interface ScorecardInterface {

    // returns an array of unused categories
    Category[] getFreeCategories();

    // returns the current total score
    int getScore();

    // returns the current Yahtzee bonus
    int getYahtzeeBonus();

    // returns the current Upper Section bonus
    int getUpperBonus();

    // returns the current Upper Section total
    int getUpperScore();

}
public interface ControllerInterface {

    // returns the player's scorecard (cloned copy, so don't try any funny business)
    ScorecardInterface getScoreCard(Player p);

    // returns the current scores for all players, in no particular order
    // this allows players to compare themselves with the competition,
    //  without allowing them to know exactly who has what score (besides their own score),
    //  which (hopefully) eliminates any avenues for collusion or sabotage
    int[] getScores();

}
public enum Category {
    ACES,
    TWOS,
    THREES,
    FOURS,
    FIVES,
    SIXES,
    THREE_OF_A_KIND,
    FOUR_OF_A_KIND,
    FULL_HOUSE,
    SMALL_STRAIGHT,
    LARGE_STRAIGHT,
    YAHTZEE,
    CHANCE;

    // determines if the category is part of the upper section
    public boolean isUpper() {
        // implementation
    }

    // determines if the category is part of the lower section
    public boolean isLower() {
        // implementation
    }

    // determines if a given set of dice fits for the category
    public boolean matches(int[] dice) {
        // implementation
    }

    // calculates the score of a set of dice for the category
    public int getScore(int[] dice) {
        // implementation
    }

    // returns all categories that fit the given dice
    public static Category[] getMatchingCategories(int[] dice) {
        // implementation
    }
}
public class TurnChoice {

    // save the dice with the specified indexes (0-4 inclusive)
    public TurnChoice(int[] diceIndexes) {
        // implementation
    }

    // use the current dice for specified category
    public TurnChoice(Category categoryChosen) {
        // implementation
    }

}

public abstract class Player {

    protected ControllerInterface game;

    public Player(ControllerInterface game) {
        this.game = game;
    }

    public String getName() {
        return this.getClass().getSimpleName();
    }

    // to be implemented by players
    // dice is the current roll (an array of 5 integers in 1-6 inclusive)
    // stage is the current roll stage in the turn (0-2 inclusive)
    public abstract TurnChoice turn(int[] dice, int stage);

}

इसके अतिरिक्त, कुछ उपयोगिता विधियाँ हैं Util.java। वे मुख्य रूप से नियंत्रक कोड को सरल बनाने के लिए हैं, लेकिन वे खिलाड़ियों द्वारा उपयोग किया जा सकता है यदि वे चाहें।

नियम

  • Scorecard.getScoresसभी खिलाड़ियों के वर्तमान स्कोर को देखने के लिए विधि का उपयोग करने के अलावा खिलाड़ियों को किसी भी तरह से बातचीत करने की अनुमति नहीं है । इसमें अन्य खिलाड़ियों के साथ मिलीभगत करना या सिस्टम के उन हिस्सों में छेड़छाड़ करना शामिल है जो सार्वजनिक इंटरफ़ेस का हिस्सा नहीं हैं।
  • यदि कोई खिलाड़ी अवैध कदम उठाता है, तो उन्हें टूर्नामेंट में प्रतिस्पर्धा करने की अनुमति नहीं दी जाएगी। टूर्नामेंट को चलाने से पहले अवैध चालों के कारण किसी भी मुद्दे को हल किया जाना चाहिए।
  • यदि टूर्नामेंट चलाने के बाद अतिरिक्त सबमिशन किए जाते हैं, तो एक नया टूर्नामेंट नए सबमिशन (रों) के साथ चलाया जाएगा, और जीतने वाले को तदनुसार अपडेट किया जाएगा। हालाँकि, मैं नए टूर्नामेंट को चलाने की कोई गारंटी नहीं देता।
  • सबमिशन कंट्रोलर कोड में किसी भी कीड़े का शोषण नहीं कर सकता है जो इसे वास्तविक गेम नियमों से विचलित करने का कारण बनता है। मुझे (एक टिप्पणी में और / या GitHub मुद्दे में) बग को इंगित करें, और मैं उन्हें ठीक कर दूंगा।
  • जावा के प्रतिबिंब उपकरणों का उपयोग करना मना है।
  • कोई भी भाषा जो JVM पर चलती है, या जावा या JVM बायटेकोड (जैसे स्काला या जाइथॉन) के लिए संकलित की जा सकती है, जब तक आप जावा के साथ इंटरफेस करने के लिए आवश्यक किसी भी अतिरिक्त कोड की आपूर्ति करते हैं।

अंतिम टिप्पणियाँ

यदि कोई उपयोगिता विधि है, तो आप मुझे नियंत्रक में जोड़ना चाहेंगे, बस टिप्पणियों में पूछें और / या GitHub पर एक मुद्दा बनाएं, और मैं इसे जोड़ दूंगा, यह मानते हुए कि यह नियम तोड़ने या जानकारी को उजागर करने की अनुमति नहीं देता है कौन से खिलाड़ी प्रिवी नहीं हैं। यदि आप इसे स्वयं लिखना चाहते हैं और GitHub पर पुल अनुरोध बनाना चाहते हैं, तो और भी बेहतर!


ACES? तुम्हारा मतलब है ONES? ये पासे हैं, कार्ड नहीं।
mbomb007


मुझे याद नहीं है यह देखकर कहा जाता है कि जब मैंने इसे खेला है, लेकिन ठीक है।
mbomb007

क्या किसी दिए गए श्रेणी के लिए स्कोर पाने के लिए एक विधि है जो पासा का एक सेट है?
mbomb007

@ mbomb007 नहीं, लेकिन मैं निश्चित रूप से एक बना सकता हूं :)
Mego

जवाबों:


4

DummyPlayer

package mego.yahtzee;
import java.util.Random;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class DummyPlayer extends Player {

    public DummyPlayer(ControllerInterface game) {
        super(game);
    }

    @Override
    public TurnChoice turn(int[] dice, int stage) {
        Category[] choices = game.getScoreCard(this).getFreeCategories();
        Category choice = choices[new Random().nextInt(choices.length)];
        if(IntStream.of(dice).allMatch(die -> die == dice[0])) {
            if(Stream.of(choices).filter(c -> c == Category.YAHTZEE).count() > 0) {
                choice = Category.YAHTZEE;
            } else if(Stream.of(choices).filter(c -> c == Util.intToUpperCategory(dice[0])).count() > 0) {
                choice = Util.intToUpperCategory(dice[0]);
            } else {
                choices = Stream.of(game.getScoreCard(this).getFreeCategories()).filter(c -> c.isLower()).toArray(Category[]::new);
                if(choices.length > 0) {
                    choice = choices[new Random().nextInt(choices.length)];
                } else {
                    choices = game.getScoreCard(this).getFreeCategories();
                    choice = choices[new Random().nextInt(choices.length)];
                }
            }
        }
        return new TurnChoice(choice);
    }

}

यह खिलाड़ी युट्ज़ी नियंत्रक में मौजूद उपकरणों का उपयोग करने के लिए एक मूल रूपरेखा के रूप में सेवा करने के लिए यहां है। यह जब भी संभव हो याहत्ज़ी का चयन करता है, और सख्त जोकर नियमों का अनुपालन करते हुए, अन्यथा यादृच्छिक विकल्प बनाता है।


1

इक्के और रातें

खैर, मुझे लगता है कि मैं हाल ही में कितना व्यस्त था धन्यवाद की तुलना में यह बहुत लंबा समय लगा।

package mego.yahtzee;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import static mego.yahtzee.Category.*;

public class AcesAndEights extends Player {
    private Category[] freeCategories, matchingCategories, usableCategories;

    public AcesAndEights(ControllerInterface game) {
        super(game);
    }

    @Override
    public TurnChoice turn(int[] dice, int stage) {
        List<Integer> holdIndices = new java.util.ArrayList<>();

        freeCategories = game.getScoreCard(this).getFreeCategories();

        matchingCategories = Category.getMatchingCategories(dice);
        Arrays.sort(matchingCategories);

        usableCategories = Arrays.stream(freeCategories)
                                 .filter(this::isMatchingCategory)
                                 .toArray(Category[]::new);
        Arrays.sort(usableCategories);

        if (isMatchingCategory(YAHTZEE))
            return doYahtzeeProcess(dice);

        if (isUsableCategory(FULL_HOUSE))
            return new TurnChoice(FULL_HOUSE);

        if (stage == 0 || stage == 1) {
            if (isMatchingCategory(THREE_OF_A_KIND)) {
                int num = 0;
                for (int i : dice) {
                    if (Util.count(Util.boxIntArray(dice), i) >= 3) {
                        num = i;
                        break;
                    }
                }
                for (int k = 0; k < 5; k++) {
                    if (dice[k] == num)
                        holdIndices.add(k);
                }
                return new TurnChoice(toIntArray(holdIndices.toArray(new Integer[0])));
            }

            if (isFreeCategory(LARGE_STRAIGHT) || isFreeCategory(SMALL_STRAIGHT)) {
                if (isUsableCategory(LARGE_STRAIGHT))
                    return new TurnChoice(LARGE_STRAIGHT);

                if (isMatchingCategory(SMALL_STRAIGHT)) {
                    if (!isFreeCategory(LARGE_STRAIGHT))
                        return new TurnChoice(SMALL_STRAIGHT);

                    int[] arr = Arrays.stream(Arrays.copyOf(dice, 5))
                                      .distinct()
                                      .sorted()
                                      .toArray();
                    List<Integer> l = Arrays.asList(Util.boxIntArray(dice));
                    if (Arrays.binarySearch(arr, 1) >= 0 && Arrays.binarySearch(arr, 2) >= 0) {
                        holdIndices.add(l.indexOf(1));
                        holdIndices.add(l.indexOf(2));
                        holdIndices.add(l.indexOf(3));
                        holdIndices.add(l.indexOf(4));
                    }
                    else if (Arrays.binarySearch(arr, 2) >= 0 && Arrays.binarySearch(arr, 3) >= 0) {
                        holdIndices.add(l.indexOf(2));
                        holdIndices.add(l.indexOf(3));
                        holdIndices.add(l.indexOf(4));
                        holdIndices.add(l.indexOf(5));
                    }
                    else {
                        holdIndices.add(l.indexOf(3));
                        holdIndices.add(l.indexOf(4));
                        holdIndices.add(l.indexOf(5));
                        holdIndices.add(l.indexOf(6));
                    }
                    return new TurnChoice(toIntArray(holdIndices.toArray(new Integer[0])));
                }
            }

            if (isFreeCategory(FULL_HOUSE)) {
                int o = 0, t = o;
                for (int k = 1; k <= 6; k++) {
                    if (Util.count(Util.boxIntArray(dice), k) == 2) {
                        if (o < 1)
                            o = k;
                        else
                            t = k;
                    }
                }

                if (o > 0 && t > 0) {
                    for (int k = 0; k < 5; k++) {
                        if (dice[k] == o || dice[k] == t)
                            holdIndices.add(k);
                    }
                    return new TurnChoice(toIntArray(holdIndices.toArray(new Integer[0])));
                }
            }
        }
        else {
            Arrays.sort(freeCategories, Comparator.comparingInt((Category c) -> c.getScore(dice))
                                                  .thenComparingInt(this::getPriority)
                                                  .reversed());
            return new TurnChoice(freeCategories[0]);
        }

        return new TurnChoice(new int[0]);
    }

    private TurnChoice doYahtzeeProcess(int[] dice) {
        if (isUsableCategory(YAHTZEE))
            return new TurnChoice(YAHTZEE);

        Category c = Util.intToUpperCategory(dice[0]);
        if (isUsableCategory(c))
            return new TurnChoice(c);

        Category[] arr = Arrays.stream(freeCategories)
                               .filter(x -> x.isLower())
                               .sorted(Comparator.comparing(this::getPriority)
                                                 .reversed())
                               .toArray(Category[]::new);
        if (arr.length > 0)
            return new TurnChoice(arr[0]);

        Arrays.sort(freeCategories, Comparator.comparingInt(this::getPriority));
        return new TurnChoice(freeCategories[0]);
    }

    private boolean isFreeCategory(Category c) {
        return Arrays.binarySearch(freeCategories, c) >= 0;
    }

    private boolean isMatchingCategory(Category c) {
        return Arrays.binarySearch(matchingCategories, c) >= 0;
    }

    private boolean isUsableCategory(Category c) {
        return Arrays.binarySearch(usableCategories, c) >= 0;
    }

    private int getPriority(Category c) {
        switch (c) {
            case YAHTZEE: return -3;        // 50 points
            case LARGE_STRAIGHT: return -1; // 40 points
            case SMALL_STRAIGHT: return -2; // 30 points
            case FULL_HOUSE: return 10;     // 25 points
            case FOUR_OF_A_KIND: return 9;  // sum
            case THREE_OF_A_KIND: return 8; // sum
            case SIXES: return 7;
            case FIVES: return 6;
            case FOURS: return 5;
            case THREES: return 4;
            case TWOS: return 3;
            case ACES: return 2;
            case CHANCE: return 1;          // sum
        }
        throw new RuntimeException();
    }

    private int[] toIntArray(Integer[] arr) {
        int[] a = new int[arr.length];
        for (int k = 0; k < a.length; k++)
            a[k] = arr[k];
        return a;
    }
}

बॉट पासा में पैटर्न की तलाश करता है जो कुछ श्रेणियों से मेल खा सकता है और आवश्यक लोगों को पकड़ सकता है। यह तुरंत उच्च प्राथमिकता वाले मिलान श्रेणी का चयन करता है यदि कोई मिल जाए; अन्यथा यह एक श्रेणी चुनता है जो उच्चतम स्कोर प्राप्त करता है। औसतन प्रति गेम लगभग 200 अंक।

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.