C++ version of Yahtzee Kata by Jon Jagger. Taken from http://cyber-dojo.com/?id=57FFF6CEE2

This commit is contained in:
emilybache 2012-10-12 15:58:55 +02:00
parent b6cd227450
commit 0dde408d01
5 changed files with 443 additions and 0 deletions

6
Yahtzee/cpp/makefile Normal file
View File

@ -0,0 +1,6 @@
run.tests.output : run.tests
./run.tests
run.tests : *.cpp
g++ -Wall -Werror -O *.cpp -o run.tests

BIN
Yahtzee/cpp/run.tests Executable file

Binary file not shown.

251
Yahtzee/cpp/yahtzee.cpp Normal file
View File

@ -0,0 +1,251 @@
#include "yahtzee.hpp"
#include <string.h>
int Yahtzee::Chance(int d1, int d2, int d3, int d4, int d5)
{
int total = 0;
total += d1;
total += d2;
total += d3;
total += d4;
total += d5;
return total;
}
int Yahtzee::yahtzee(int dice[])
{
int counts[6] = {0,0,0,0,0,0};
for (int i = 0; i != 5; i++)
counts[dice[i]-1]++;
for (int i = 0; i != 6; i++)
if (counts[i] == 5)
return 50;
return 0;
}
int Yahtzee::Ones(int d1, int d2, int d3, int d4, int d5) {
int sum = 0;
if (d1 == 1) sum++;
if (d2 == 1) sum++;
if (d3 == 1) sum++;
if (d4 == 1) sum++;
if (d5 == 1)
sum++;
return sum;
}
int Yahtzee::Twos(int d1, int d2, int d3, int d4, int d5) {
int sum = 0;
if (d1 == 2) sum += 2;
if (d2 == 2) sum += 2;
if (d3 == 2) sum += 2;
if (d4 == 2) sum += 2;
if (d5 == 2) sum += 2;
return sum;
}
int Yahtzee::Threes(int d1, int d2, int d3, int d4, int d5) {
int s;
s = 0;
if (d1 == 3) s += 3;
if (d2 == 3) s += 3;
if (d3 == 3) s += 3;
if (d4 == 3) s += 3;
if (d5 == 3) s += 3;
return s;
}
Yahtzee::Yahtzee()
{
}
Yahtzee::Yahtzee(int d1, int d2, int d3, int d4, int _5)
{
dice = new int[5];
dice[0] = d1;
dice[1] = d2;
dice[2] = d3;
dice[3] = d4;
dice[4] = _5;
}
int Yahtzee::Fours()
{
int sum;
sum = 0;
for (int at = 0; at != 5; at++) {
if (dice[at] == 4) {
sum += 4;
}
}
return sum;
}
int Yahtzee::Fives()
{
int s = 0;
int i;
for (i = 0; i < 5; i++)
if (dice[i] == 5)
s = s + 5;
return s;
}
int Yahtzee::sixes()
{
int sum = 0;
for (int at = 0; at < 5; at++)
if (dice[at] == 6)
sum = sum + 6;
return sum;
}
int Yahtzee::ScorePair(int d1, int d2, int d3, int d4, int d5)
{
int counts[6] = {0,0,0,0,0,0};
counts[d1-1]++;
counts[d2-1]++;
counts[d3-1]++;
counts[d4-1]++;
counts[d5-1]++;
int at;
for (at = 0; at != 6; at++)
if (counts[6-at-1] == 2)
return (6-at)*2;
return 0;
}
int Yahtzee::TwoPair(int d1, int d2, int d3, int d4, int d5)
{
int counts[6] = {0};
counts[d1-1]++;
counts[d2-1]++;
counts[d3-1]++;
counts[d4-1]++;
counts[d5-1]++;
int n = 0;
int score = 0;
for (int i = 0; i < 6; i += 1)
if (counts[6-i-1] == 2) {
n++;
score += (6-i);
}
if (n == 2)
return score * 2;
else
return 0;
}
int Yahtzee::FourOfAKind(int _1, int _2, int d3, int d4, int d5)
{
int * tallies;
tallies = new int[6];
tallies[0] = tallies[1] = tallies[2] = 0;
tallies[3] = tallies[4] = tallies[5] = 0;
tallies[_1-1]++;
tallies[_2-1]++;
tallies[d3-1]++;
tallies[d4-1]++;
tallies[d5-1]++;
for (int i = 0; i < 6; i++)
if (tallies[i] == 4)
return (i+1) * 4;
return 0;
}
int Yahtzee::ThreeOfAKind(int d1, int d2, int d3, int d4, int d5)
{
int * t;
t = new int[6];
t[0] = t[1] = t[2] = 0;
t[3] = t[4] = t[5] = 0;
t[d1-1]++;
t[d2-1]++;
t[d3-1]++;
t[d4-1]++;
t[d5-1]++;
for (int i = 0; i < 6; i++)
if (t[i] == 3)
return (i+1) * 3;
return 0;
}
int Yahtzee::SmallStraight(int d1, int d2, int d3, int d4, int d5)
{
int* tallies =new int[6];
memset(tallies, 0, sizeof(int)*6);
tallies[d1-1] += 1;
tallies[d2-1] += 1;
tallies[d3-1] += 1;
tallies[d4-1] += 1;
tallies[d5-1] += 1;
if (tallies[0] == 1 &&
tallies[1] == 1 &&
tallies[2] == 1 &&
tallies[3] == 1 &&
tallies[4] == 1)
return 15;
return 0;
}
int Yahtzee::LargeStraight(int d1, int d2, int d3, int d4, int d5)
{
int* tallies = new int[6];
memset(tallies, 0, sizeof(*tallies)*6);
tallies[d1-1] += 1;
tallies[d2-1] += 1;
tallies[d3-1] += 1;
tallies[d4-1] += 1;
tallies[d5-1] += 1;
if (tallies[1] == 1 &&
tallies[2] == 1 &&
tallies[3] == 1 &&
tallies[4] == 1
&& tallies[5] == 1)
return 20;
return 0;
}
int Yahtzee::FullHouse(int d1, int d2, int d3, int d4, int d5)
{
int* tallies;
bool _2 = false;
int i;
int _2_at = 0;
bool _3 = false;
int _3_at = 0;
tallies = new int[6];
memset(tallies, 0, sizeof(int)*6);
tallies[d1-1] += 1;
tallies[d2-1] += 1;
tallies[d3-1] += 1;
tallies[d4-1] += 1;
tallies[d5-1] += 1;
for (i = 0; i != 6; i += 1)
if (tallies[i] == 2) {
_2 = true;
_2_at = i+1;
}
for (i = 0; i != 6; i += 1)
if (tallies[i] == 3) {
_3 = true;
_3_at = i+1;
}
if (_2 && _3)
return _2_at * 2 + _3_at * 3;
else
return 0;
}

33
Yahtzee/cpp/yahtzee.hpp Normal file
View File

@ -0,0 +1,33 @@
#ifndef YAHTZEE_INCLUDED
#define YATHZEE_INCLUDED
class Yahtzee
{
public:
static int Chance(int d1, int d2, int d3, int d4, int d5);
static int yahtzee(int dice[]);
static int Ones(int d1, int d2, int d3, int d4, int d5);
static int Twos(int d1, int d2, int d3, int d4, int d5);
static int Threes(int d1, int d2, int d3, int d4, int d5);
protected:
int * dice;
public:
Yahtzee();
Yahtzee(int d1, int d2, int d3, int d4, int _5);
int Fours();
int Fives();
int sixes();
int ScorePair(int d1, int d2, int d3, int d4, int d5);
static int TwoPair(int d1, int d2, int d3, int d4, int d5);
static int FourOfAKind(int _1, int _2, int d3, int d4, int d5);
static int ThreeOfAKind(int d1, int d2, int d3, int d4, int d5);
static int SmallStraight(int d1, int d2, int d3, int d4, int d5);
static int LargeStraight(int d1, int d2, int d3, int d4, int d5);
static int FullHouse(int d1, int d2, int d3, int d4, int d5);
};
#endif

View File

@ -0,0 +1,153 @@
#include "yahtzee.hpp"
#include <cassert>
#include <iostream>
static void Chance_scores_sum_of_all_dice(void)
{
int expected = 15;
int actual = Yahtzee().Chance(2,3,4,5,1);
assert(expected == actual);
assert(16 == Yahtzee().Chance(3,3,4,5,1));
}
static int * ints(int a, int b, int c, int d, int e)
{
int * r = new int[5];
r[0] = a;
r[1] = b;
r[2] = c;
r[3] = d;
r[4] = e;
return r;
}
static void Yahtzee_scores_50(void)
{
int expected = 50;
int actual = Yahtzee().yahtzee(ints(4,4,4,4,4));
assert(expected == actual);
assert(50 == Yahtzee().yahtzee(ints(6,6,6,6,6)));
assert(0 == Yahtzee().yahtzee(ints(6,6,6,6,3)));
}
static void Test_1s()
{
assert(Yahtzee().Ones(1,2,3,4,5) == 1);
assert(2 == Yahtzee().Ones(1,2,1,4,5));
assert(0 == Yahtzee().Ones(6,2,2,4,5));
assert(4 == Yahtzee().Ones(1,2,1,1,1));
}
static void test_2s()
{
assert(4 == Yahtzee().Twos(1,2,3,2,6));
assert(10 == Yahtzee().Twos(2,2,2,2,2));
}
static void test_threes()
{
assert(6 == Yahtzee().Threes(1,2,3,2,3));
assert(12 == Yahtzee().Threes(2,3,3,3,3));
}
static void fours_test()
{
assert(12 == (new Yahtzee(4,4,4,5,5))->Fours());
assert(8 == (new Yahtzee(4,4,5,5,5))->Fours());
assert(4 == (*new Yahtzee(4,5,5,5,5)).Fours());
}
static void fives() {
assert(10 == (new Yahtzee(4,4,4,5,5))->Fives());
assert(15 == Yahtzee(4,4,5,5,5).Fives());
assert(20 == Yahtzee(4,5,5,5,5).Fives());
}
static void sixes_test()
{
assert(0 == Yahtzee(4,4,4,5,5).sixes());
assert(6 == Yahtzee(4,4,6,5,5).sixes());
assert(18 == Yahtzee(6,5,6,6,5).sixes());
}
static void one_pair()
{
assert(6 == Yahtzee().ScorePair(3,4,3,5,6));
assert(10 == Yahtzee().ScorePair(5,3,3,3,5));
assert(12 == Yahtzee().ScorePair(5,3,6,6,5));
}
static void two_Pair()
{
assert(16 == Yahtzee().TwoPair(3,3,5,4,5));
assert(0 == Yahtzee().TwoPair(3,3,5,5,5));
}
static void three_of_a_kind()
{
assert(9 == Yahtzee().ThreeOfAKind(3,3,3,4,5));
assert(15 == Yahtzee().ThreeOfAKind(5,3,5,4,5));
assert(0 == Yahtzee::ThreeOfAKind(3,3,3,3,5));
}
static void four_of_a_knd()
{
assert(12 == Yahtzee::FourOfAKind(3,3,3,3,5));
assert(20 == Yahtzee::FourOfAKind(5,5,5,4,5));
assert(0 == Yahtzee::FourOfAKind(3,3,3,3,3));
}
static void smallStraight()
{
assert(15 == Yahtzee::SmallStraight(1,2,3,4,5));
assert(15 == Yahtzee::SmallStraight(2,3,4,5,1));
assert(0 == Yahtzee().SmallStraight(1,2,2,4,5));
}
static void largeStraight()
{
assert(20 == Yahtzee::LargeStraight(6,2,3,4,5));
assert(20 == Yahtzee().LargeStraight(2,3,4,5,6));
assert(0== Yahtzee::LargeStraight(1,2,2,4,5));
}
static void fullHouse()
{
assert(18 == Yahtzee().FullHouse(6,2,2,2,6));
assert(0 == Yahtzee().FullHouse(2,3,4,5,6));
}
typedef void test();
static test * tests[ ] =
{
Chance_scores_sum_of_all_dice,
Yahtzee_scores_50,
Test_1s,
test_2s,
test_threes,
fours_test,
fives,
sixes_test,
one_pair,
two_Pair,
three_of_a_kind,
four_of_a_knd,
smallStraight,
largeStraight,
fullHouse,
static_cast<test*>(0),
};
int main()
{
size_t at = 0;
while (tests[at])
{
tests[at++]();
std::cout << '.';
}
std::cout << std::endl << at << " tests passed" << std::endl;
return 0;
}