Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
LD28 entry: One roll Yahtzee
import 'dart:html';
import 'dart:math' show Random;
ButtonElement rollButton;
ButtonElement resetButton;
DivElement currentRoll;
DivElement savedDice;
TableElement scoreTable;
TableElement chosenScoreTable;
SpanElement rollCountDisplay;
SpanElement totalScoreDisplay;
class Game{
static int savedCount = 0;
static int rollCount = 0;
static List<Score> scores = [];
static List<Score> chosenScores = [];
static List<ButtonElement> scoreButtons = [];
static int totalScore = 0;
static int totalUpperSecScore = 0;
static bool upperSecBonusApplied = false;
static bool gameOver = false;
static void initScores(){
scores.clear();
for (int i = 1; i <= 6; i++){
NumScore s = new NumScore(i);
scores.add(s);
}
scores.add(new NOfAKindScore(3));
scores.add(new NOfAKindScore(4));
scores.add(new SmallStraightScore());
scores.add(new LargeStraightScore());
scores.add(new FullHouseScore());
scores.add(new YahtzeeScore());
scores.add(new ChanceScore());
}
static void roll(Event e){
if (rollCount < 1){
Random diceGen = new Random();
currentRoll.children.clear();
for (int i = 0; i < 5 - savedCount; i++) {
DivElement die = new DivElement();
die..text = (diceGen.nextInt(6)+1).toString()
..className = 'die'
..onClick.listen(Game.saveDie);
currentRoll.children.add(die);
}
rollCount++;
rollCountDisplay.text = (1-rollCount).toString();
updateScoreDisplays();
}
rollButton.disabled = (rollCount == 1 || savedCount == 5);
for (ButtonElement i in scoreButtons){
i.disabled = false;
}
}
static void saveDie(Event e){
DivElement die = e.target;
if (die.parent == currentRoll){
currentRoll.children.remove(die);
savedDice.children.add(die);
savedCount++;
} else {
savedDice.children.remove(die);
currentRoll.children.add(die);
savedCount--;
}
rollButton.disabled = (savedCount == 5 || rollCount == 1);
}
static void resetBoard(){
rollCountDisplay.text = '1';
if (totalUpperSecScore >= 63 && !upperSecBonusApplied) {
totalScore += 35;
SpanElement notifier = new SpanElement();
notifier.text = ' (includes Upper Section bonus - 35)';
totalScoreDisplay.parent.children.add(notifier);
upperSecBonusApplied = true;
}
totalScoreDisplay.text = totalScore.toString();
currentRoll.children.clear();
savedDice.children.clear();
rollCount = 0;
savedCount = 0;
rollButton.disabled = false;
for (ButtonElement i in scoreButtons){
i.disabled = true;
}
}
static void reset(Event e){
chosenScores = [];
chosenScoreTable.children.clear();
chosenScoreTable.innerHtml = '<tr><th>Name</th><th>Score</th></tr>';
totalScore = 0;
totalUpperSecScore = 0;
upperSecBonusApplied = false;
if (gameOver){
querySelector('.gameover').remove();
}
resetBoard();
initScores();
updateScoreDisplays();
gameOver = false;
}
static void updateScoreDisplays(){
scoreTable.children.clear();
scoreTable.innerHtml = '<tr><th>Name</th><th>Score</th></tr>';
List<int> dice = diceList();
for (Score i in scores){
i.display(dice: dice);
}
if (scores.length == 0 || (scores.length == 1 && scores[0]._scoreName == "Yahtzee" && scores[0]._oneScored)){
Game.endGame();
}
}
static void endGame(){
rollButton.disabled = true;
gameOver = true;
SpanElement gameOverElem = new SpanElement();
gameOverElem..innerHtml = "<br>Game over!"
..className = 'gameover';
querySelector('.rollbtn').children.add(gameOverElem);
}
}
void main() {
//find stuff, init listeners
currentRoll = querySelector('#current_roll');
savedDice = querySelector('#saved_dice');
scoreTable = querySelector('#score_table');
chosenScoreTable = querySelector('#chosen_scores');
rollButton = querySelector('#roll');
rollButton.onClick.listen(Game.roll);
resetButton = querySelector('#reset');
resetButton.onClick.listen(Game.reset);
rollCountDisplay = querySelector('#rollcount');
totalScoreDisplay = querySelector('#currentscore');
Game.resetBoard();
Game.initScores();
Game.updateScoreDisplays();
}
List<int> diceList() {
List<int> l = [];
for (Element e in querySelectorAll('.die')) {
l.add(int.parse(e.text));
}
return l;
}
int numOfOccurencesInList(List l, Object o){
int count = 0;
for(var i in l){
if (i == o){
count++;
}
}
return count;
}
class Score {
String _scoreName;
bool _oneScored = false; //used by Yahtzee
int calcScore(List<int> l){
return 0;
}
void display({List<int> dice: null}){
if (dice == null) dice = diceList();
TableRowElement row = new TableRowElement();
int score = calcScore(dice);
TableCellElement buttonCell = new TableCellElement();
ButtonElement button = new ButtonElement();
button..onClick.listen(this.selectScore)
..text = 'Select Score'
..disabled = true;
Game.scoreButtons.add(button);
buttonCell.children.add(button);
row..innerHtml = '<td>$_scoreName</td><td>$score</td>'
..children.add(buttonCell);
scoreTable.children.add(row);
}
void selectScore(Event e){
Element row = e.target.parent.parent;
scoreTable.children.remove(row);
chosenScoreTable.children.add(row);
row.children.remove(e.target.parent);
Game.scores.remove(this);
Game.chosenScores.add(this);
Game.totalScore += this.calcScore(diceList());
Game.resetBoard();
Game.updateScoreDisplays();
}
}
class NumScore extends Score {
var _points;
NumScore(int num){
Map names = {1:"Ones",2:"Twos",3:"Threes",
4:"Fours",5:"Fives",6:"Sixes"};
_scoreName = names[num];
_points = num;
}
int calcScore(List<int> l) => numOfOccurencesInList(l, _points)*_points;
void selectScore(Event e){
Game.totalUpperSecScore += this.calcScore(diceList());
super.selectScore(e);
}
}
class ChanceScore extends Score {
String _scoreName = 'Chance';
int calcScore(List<int> l){
int score = 0;
for (int i in l){
score += i;
}
return score;
}
}
class NOfAKindScore extends Score {
int _n;
NOfAKindScore(int n){
_n = n;
Map names = {3: "Three of a Kind", 4: "Four of a Kind"};
_scoreName = names[n];
}
int calcScore(List<int> l){
for (int i = 1; i <= 6; i++){
if (numOfOccurencesInList(l, i) >= _n){
int score = 0;
for (int i in l){
score += i;
}
return score;
}
}
return 0;
}
}
class YahtzeeScore extends Score {
String _scoreName = "Yahtzee";
int calcScore(List<int> l){
for (int i = 1; i <= 6; i++){
if (numOfOccurencesInList(l, i) == 5){
return _oneScored ? 100 : 50;
}
}
return 0;
}
void selectScore(Event e){
if (this.calcScore(diceList()) == 0) {
super.selectScore(e);
return;
}
Element row = e.target.parent.parent;
chosenScoreTable.children.add(row);
row.children.remove(e.target.parent);
Game.chosenScores.add(this);
Game.totalScore += this.calcScore(diceList());
_oneScored = true;
Game.resetBoard();
Game.updateScoreDisplays();
}
}
class FullHouseScore extends Score {
String _scoreName = "Full House";
int calcScore(List<int> l){
if (l.toSet().length == 2){
for (int i = 1; i <= 6; i++){
if (numOfOccurencesInList(l, i) == 3){
return 25;
}
}
}
return 0;
}
}
class LargeStraightScore extends Score {
String _scoreName = "Large Straight";
int calcScore(List<int> l){
l = l.toSet().toList(growable: false);
l.sort();
if (l.length < 5) return 0;
if (l[4] - l[0] == 4) return 40;
return 0;
}
}
class SmallStraightScore extends Score {
String _scoreName = "Small Straight";
int calcScore(List<int> l){
l = l.toSet().toList(growable: false);
l.sort();
if (l.length < 4) return 0;
for (int i = 0; i < l.length-3; i++){
if (l[i+3] - l[i] == 3) return 30;
}
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.