首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >(JS)重构帮助-使用JavaScript进行法克尔评分

(JS)重构帮助-使用JavaScript进行法克尔评分
EN

Stack Overflow用户
提问于 2021-02-26 02:54:09
回答 1查看 65关注 0票数 0

我已经用React创建了一个用于玩骰子游戏Farkle的应用程序。我在一个javascript对象中组织了这些组合和它们的点值,方法是手动键入每个组合(只保留几个,我肯定错过了几个)。我想重构成更高效的东西,并用我可能错过的组合来填补空白。我刚开始编程,不知道从哪里开始,也不知道如何开始,所以我想我应该在这里寻求任何建议。

这个游戏是关于掷6个骰子,并根据他们的点值选择骰子。下面是我的应用使用的评分规则:

评分

1- 10分

5-5分

三个1- 10分

三个2- 20分

三个三分球- 30分

三个4- 40分

三个5- 50分

三个6- 60分

1-2-3-4-5-6 - 300分

3对150分(包括4对独一无二和1对)

请注意,得分组合只有在单次投掷时才算数。

(例如:如果玩家掷出一个1并将其放在一边,然后在下一次投掷中掷出两个1,他们只得30分,而不是100分。)

有时,单轮投篮会提供多种得分方式。

例如,球员滚动1-2-4-5-5-5可以获得以下分数之一:

1得10分

1得15分,5得5分

三个5得50分

1和3个5得60分

下面是我的js对象在我的应用程序中的样子:

代码语言:javascript
复制
#combinations.js
const combinations = {
      '1':10,
      '5':5,
      '1, 1':20,
      '5, 5':10,
    //triplets
      '1, 1, 1':10,
      '2, 2, 2':20,
      '3, 3, 3':30,
      '4, 4, 4':40,
      '5, 5, 5':50,
      '6, 6, 6':60,
    //triplets + one offs
      '1, 2, 2, 2':30,
      '1, 3, 3, 3':40,
      '1, 4, 4, 4':50,
//.. so on and so forth
}
export default combinations

对于如何更有效地管理这些组合,任何在正确方向上的推动都将受到极大的赞赏。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-27 03:16:34

我在GitHub上搜索JS farkle应用程序,发现了这个解决方案:

代码语言:javascript
复制
// check array for all score combinations
function combinations(array) {
    // this array stores unused dice
    var unused = [];
    
    // make a copy of array, to avoid changing it (theseKeepers and thisRoll are global variables)
    for (var i = 0; i < array.length; i++) {
        unused.push(array[i]);
    }
    
    // store number of dice in array
    var l = unused.length;
    
    // stores score to return (in case of multiple combinations)
    var score = 0;
    
    // if there are 6..
    if (l == 6) {
        // check for six of a kind
        for (var x = 1; x <= 6; x++) {
            var contains = containsAll([x,x,x,x,x,x], unused);

            // retrieve result from structured array
            if (contains.result == true) {
                // return score and empty array
                return {score: 600,
                        extra: []
                };
            }           
        }
        
        // check for a straight
        var contains = containsAll([1,2,3,4,5,6], unused);

        // retrieve result from structured array
        if (contains.result == true) {
            // return score and empty array
            return {score: 150,
                    extra: []
            };
        }
        
        // check for 2 three of a kinds
        for (var x = 1; x <= 6; x++) {
            for (var y = 1; y <= 6; y++) {
                var contains = containsAll([x,x,x,y,y,y], unused);
                
                // retrieve result from structured array
                if (contains.result == true) {
                    // return score and empty array
                    return {score: 200,
                            extra: []
                    };
                }
            }
        }
        
        // check for a full house
        for (var x = 1; x <= 6; x++) {
            for (var y = 1; y <= 6; y++) {
                var contains = containsAll([x,x,y,y,y,y], unused);
                
                // retrieve result from structured array
                if (contains.result == true) {
                    // return score and empty array
                    return {score: 250,
                            extra: []
                    };
                }
            }
        }
        
        // check for three pairs
        for (var x = 1; x <= 6; x++) {
            for (var y = 1; y <= 6; y++) {
                for (var z = 1; z <= 6; z++) {
                    var contains = containsAll([x,x,y,y,z,z], unused);
        
                    // retrieve result from structured array
                    if (contains.result == true) {
                        // return score and empty array
                        return {score: 150,
                                extra: []
                        };
                    }
                }
            }
        }
    }
    
    // if there are five or more..
    if (l >= 5) {
        // check for five of a kind
        for (var x = 1; x <= 6; x++) {
            var contains = containsAll([x,x,x,x,x], unused);
        
            // retrieve result from structured array
            if (contains.result == true) {
                score += 300;
            
                // retrieve unused array from structured array
                unused = contains.array;
    
                // reset length
                l = unused.length;
            }
        }
    }
    
    // if there are four or more..
    if (l >= 4) {
        // check for four of a kind
        for (var x = 1; x <= 6; x++) {
            var contains = containsAll([x,x,x,x], unused);
        
            // retrieve result from structured array
            if (contains.result == true) {
                score += 200;
            
                // retrieve unused array from structured array
                unused = contains.array;
    
                // reset length
                l = unused.length;
            }
        }
    }
    
    //  if there are three or more..
    if (l >= 3) {
        // check for three of a kind
        for (var x = 1; x <= 6; x++) {
            var contains = containsAll([x,x,x], unused);
        
            // retrieve result from structured array
            if (contains.result == true) {
                // if they are 2s, 3s, 4s, 5s, or 6s..
                if (x > 1) {
                    // multiply by 100
                    score += x * 10;
                } else { // otherwise..
                    // they are 1s
                    score += 100;
                }
            
                // retrieve unused array from structured array
                unused = contains.array;

                // reset length
                l = unused.length;
            }
        }
    }
    
    // if there are two or more..
    if (l >= 2) {
        // check for two ones
        var contains = containsAll([1,1], unused);
        
        // retrieve result from structured array
        if (contains.result == true) {
            score += 20;
            
            // retrieve unused array from structured array
            unused = contains.array;
    
            // reset length
            l = unused.length;
        }
    }
    
    // if there are still two or more..
    if (l >= 2) {
        // check for two fives
        var contains = containsAll([5,5], unused);
        
        // retrieve result from structured array
        if (contains.result == true) {
            score += 10;
            
            // retrieve unused array from structured array
            unused = contains.array;
    
            // reset length
            l = unused.length;
        }
    }
    
    // if there is one or more..
    if (l >= 1) {
        // check for a one
        var contains = containsAll([1], unused);
        
        // retrieve result from structured array
        if (contains.result == true) {
            score += 10;
            
            // retrieve unused array from structured array
            unused = contains.array;
    
            // reset length
            l = unused.length;
        }
    }
    
    // if there is still one or more..
    if (l >= 1) {
        // check for a five
        var contains = containsAll([5], unused);
        
        // retrieve result from structured array
        if (contains.result == true) {
            score += 5;
            
            // retrieve unused array from structured array
            unused = contains.array;
    
            // reset length
            l = unused.length;
        }
    }
    
    // return additive score and array with unused dice
    return {score: score,
            extra: unused
    };
}

// takes in score combination and unused array; determines whether combination exists in unused array
function containsAll(combination, unused) {
    // l is defined up front for speed
    var l = combination.length;
    
    // stores unused dice
    var un = [];
    
    // make a copy of unused array, to avoid changing it
    for (var i = 0; i < unused.length; i++) {
        un.push(unused[i]);
    }
    
    // this array stores used dice
    var used = [];
    
    for (var i = 0; i < l; i++)  {
        // store index of combination die in unused array
        var c = un.indexOf(combination[i]);
        
        // if unused doesn't contain this combination die..
        if (c == -1) {
            return false;
        } else { // otherwise..
            // add die to used array
            used.push(un[c]);
            
            // remove die from unused (since most combinations contain multiples)
            un.splice(c,1);
        }
    }
    
    // take out scored dice
    unused = difference(unused, used);
    
    // score combination exists in array; return true and unused array
    return {
        result: true,
        array: unused
    };
}
// takes in unused and used arrays; returns difference
function difference(unused, used) {
    // this array stores unused dice
    var un = [];
    
    // make a copy of unused array, to avoid changing it
    for (var i = 0; i < unused.length; i++) {
        un.push(unused[i]);
    }
    
    // this array stores used dice
    var u = [];
    
    // make a copy of used array, to avoid changing it
    for (var i = 0; i < used.length; i++) {
        u.push(used[i]);
    }
    
    for (i = 0; i < u.length; i++) {
        // store first index of used number in unused
        var j = un.indexOf(u[i]);
        
        // if used number is found (indexOf() returns -1 if no match is found)..
        if (j > -1) {
            // remove it from unused
            un.splice(j, 1);
        }
    }
    
    // return unused (which now only has values that aren't in used array)
    return un;
}

向GitHub用户@danielbmckay发出巨大的呼喊

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66374736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档