scoring for farkle dice game

Started by
2 comments, last by dvessey2008 13 years, 6 months ago
what is the best way to check six dice for scoring for the dice game farkle in C++? Would the best way be to use a ton of if statements such as

if dieOne == 1 and dieTwo == 1 and dieThree == 1 and dieFour == 1 and dieFive == 1 and dieSix == 1
player gets 8,000pts

I know this is not properly coded it is just psuedo code for my design purposes. My only concern is it would take hundreds of if statemnets to check all dice possibilities. I was just wondering if there is a better way.
Advertisement
Well the first thing I would do is get rid of all your dieN's and just make a proper array of die. Then you can iterate over them properly in a for loop with much less typing :)

Just looking up the rules real quick (http://en.wikipedia.org/wiki/Farkle#Standard_scoring) they don't look that complicated.

Something like this:

Die die[6];int score = 0;int counters[6]; //sides 1-6 indexed as 0-5 here for simplicityfor(uint i=0;i<6;i++){  if(die==0) score+=100;  if(die==4) score+=50;  counters[die]++;}if(counters[0]>3) score+=1000; //more than 3 1's is 1000 points...etc...
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
From glancing at the rules, you only have three different scoring possibilities. Karwost's answer is on the right track, but won't handle some cases like duplicate flushes. There's a better way:

1) roll an array of 6 dice (e.g. result = 4,3,1,1,6,1)
2) sort the results by value (e.g. = 1,1,1,3,4,6)
3) iterate array, for each value "n" doing:
-- 3a) add 50 pts for every 5, and 100 pts for every 1.
-- 3b) Increment a "flush" ctr if value of n = value of n-1. Clear ctr if not.
-- 3c) Increment a "straight" ctr if value of n = 1+ value of n-1. Clear if not.
-- 3d) If flush ctr=3, add 100 * val of die (+100 if val=1, -150 if val=5), and clear ctr.

At end of loop, if straight ctr=6, clear score and set = 1000.

The +100 for val=1, -150 for val-5 may look a bit odd, but that's to adjust the double-scoring the individual dice within a flush as individual 1's and 5's.
Thanks for the help! I understand how arrays work, I just always get tripped up when implementing my own arrays. I understand now! Appreciate it!

This topic is closed to new replies.

Advertisement