Removed all the code except for the GildedRose kata from this repo

This commit is contained in:
emilybache 2013-06-10 14:42:41 +02:00
parent b31a5436cc
commit 567c27854a
112 changed files with 35 additions and 4801 deletions

View File

@ -1 +0,0 @@
*.pyc

View File

@ -1,40 +0,0 @@
This Kata was originally created by Terry Hughes (http://twitter.com/#!/TerryHughes). It is already on GitHub [here](https://github.com/NotMyself/GildedRose). See also [Bobby Johnson's description of the kata](http://iamnotmyself.com/2011/02/13/refactor-this-the-gilded-rose-kata/).
I translated the original C# into a few other languages, (with a little help from my friends!), and slightly changed the starting position. This means I've actually done a small amount of refactoring already compared with the original form of the kata, and made it easier to get going with writing tests by giving you one failing unit test to start with. I also added test fixtures for Text-Based approval testing with TextTest (see [the TextTests](https://github.com/emilybache/Refactoring-Katas/tree/master/GildedRose/texttests))
As Bobby Johnson points out in his article ["Why Most Solutions to Gilded Rose Miss The Bigger Picture"](http://iamnotmyself.com/2012/12/07/why-most-solutions-to-gilded-rose-miss-the-bigger-picture/), it'll actually give you
better practice at handling a legacy code situation if you do this Kata in the original C#. However, I think this kata
is also really useful for practicing writing good tests using different frameworks and approaches, and the small changes I've made help with that. I think it's also interesting to compare what the refactored code and tests look like in different programming languages.
I wrote this article ["Writing Good Tests for the Gilded Rose Kata"](http://emilybache.blogspot.se/2013/03/writing-good-tests-for-gilded-rose-kata.html) about how you could use this kata in a [coding dojo](https://leanpub.com/codingdojohandbook).
## How to use this Kata
The simplest way is to just clone the code and start hacking away improving the design. You'll want to look at the ["Gilded Rose Requirements"](https://github.com/emilybache/Refactoring-Katas/blob/master/GildedRose/GildedRoseRequirements.txt) which explains what the code is for. I strongly advise you that you'll also need some tests if you want to make sure you don't break the code while you refactor.
You could write some unit tests yourself, using the requirements to identify suitable test cases. I've provided a failing unit test in a popular test framework as a starting point for most languages.
Alternatively, use the "Text-Based" tests provided in this repository. (Read more about that in the next section)
Whichever testing approach you choose, the idea of the exercise is to do some deliberate practice, and improve your skills at designing test cases and refactoring. The idea is not to re-write the code from scratch, but rather to practice designing tests, taking small steps, running the tests often, and incrementally improving the design.
## Text-Based Approval Testing
This is a testing approach which is very useful when refactoring legacy code. Before you change the code, you run it, and gather the output of the code as a plain text file. You review the text, and if it correctly describes the behaviour as you understand it, you can "approve" it, and save it as a "Golden Master". Then after you change the code, you run it again, and compare the new output against the Golden Master. Any differences, and the test fails.
It's basically the same idea as "assertEquals(expected, actual)" in a unit test, except the text you are comparing is typically much longer, and the "expected" value is saved from actual output, rather than being defined in advance.
Typically a piece of legacy code may not produce suitable textual output from the start, so you may need to modify it before you can write your first text-based approval test. That could involve inserting log statements into the code, or just writing a "main" method that executes the code and prints out what the result is afterwards. It's this latter approach we are using here to test GildedRose.
The Text-Based tests in this repository are designed to be used with the tool "TextTest" (http://texttest.org). This tool helps you to organize and run text-based tests. There is more information in the README file in the "texttests" subdirectory.
## Get going quickly using Cyber-Dojo
I've also set this kata up on [cyber-dojo](http://cyber-dojo.com) for several languages, so you can get going really quickly:
- [JUnit, Java](http://cyber-dojo.com/forker/fork/751DD02C4C?avatar=snake&tag=4)
- [C#](http://cyber-dojo.com/forker/fork/107907AD1E?avatar=alligator&tag=13)
- [Ruby](http://cyber-dojo.com/forker/fork/A8943EAF92?avatar=hippo&tag=9)
- [RSpec, Ruby](http://cyber-dojo.com/forker/fork/8E58B0AD16?avatar=raccoon&tag=3)
- [Python](http://cyber-dojo.com/forker/fork/297041AA7A?avatar=lion&tag=4)
- [Cucumber, Java](http://cyber-dojo.com/forker/fork/0F82D4BA89?avatar=gorilla&tag=45) - for this one I've also written some step definitions for you

Binary file not shown.

View File

@ -1,13 +1,40 @@
# Refactoring Katas
This Kata was originally created by Terry Hughes (http://twitter.com/#!/TerryHughes). It is already on GitHub [here](https://github.com/NotMyself/GildedRose). See also [Bobby Johnson's description of the kata](http://iamnotmyself.com/2011/02/13/refactor-this-the-gilded-rose-kata/).
Can you refactor? In really small steps? Can you turn some, frankly, ugly code into a paradigm of elegant, readable, extensible design?
I translated the original C# into a few other languages, (with a little help from my friends!), and slightly changed the starting position. This means I've actually done a small amount of refactoring already compared with the original form of the kata, and made it easier to get going with writing tests by giving you one failing unit test to start with. I also added test fixtures for Text-Based approval testing with TextTest (see [the TextTests](https://github.com/emilybache/GildedRose-Refactoring-Kata/tree/master/texttests))
This is a collection of starting files for when practicing refactoring.
As Bobby Johnson points out in his article ["Why Most Solutions to Gilded Rose Miss The Bigger Picture"](http://iamnotmyself.com/2012/12/07/why-most-solutions-to-gilded-rose-miss-the-bigger-picture/), it'll actually give you
better practice at handling a legacy code situation if you do this Kata in the original C#. However, I think this kata
is also really useful for practicing writing good tests using different frameworks and approaches, and the small changes I've made help with that. I think it's also interesting to compare what the refactored code and tests look like in different programming languages.
Contents so far:
I wrote this article ["Writing Good Tests for the Gilded Rose Kata"](http://emilybache.blogspot.se/2013/03/writing-good-tests-for-gilded-rose-kata.html) about how you could use this kata in a [coding dojo](https://leanpub.com/codingdojohandbook).
- Tennis Kata in Java, Python, Objective-C and C++.
- Gilded Rose Kata in C++, C#, Java, Python, Smalltalk, C and Ruby. (a C# version together with a starting project is already available on github [here](https://github.com/NotMyself/GildedRose))
- Yahtzee Kata in C#, Java, C++ and Python (the C#, C++ and Java versions are also available in Jon Jagger's Cyberdojo, see [his blog post](http://jonjagger.blogspot.co.uk/2012/05/yahtzee-cyber-dojo-refactoring-in-java.html))
## How to use this Kata
For more information about these and other katas, you may be interested in my book [The Coding Dojo Handbook](http://leanpub.com/codingdojohandbook)
The simplest way is to just clone the code and start hacking away improving the design. You'll want to look at the ["Gilded Rose Requirements"](https://github.com/emilybache/GildedRose-Refactoring-Kata/tree/master/GildedRoseRequirements.txt) which explains what the code is for. I strongly advise you that you'll also need some tests if you want to make sure you don't break the code while you refactor.
You could write some unit tests yourself, using the requirements to identify suitable test cases. I've provided a failing unit test in a popular test framework as a starting point for most languages.
Alternatively, use the "Text-Based" tests provided in this repository. (Read more about that in the next section)
Whichever testing approach you choose, the idea of the exercise is to do some deliberate practice, and improve your skills at designing test cases and refactoring. The idea is not to re-write the code from scratch, but rather to practice designing tests, taking small steps, running the tests often, and incrementally improving the design.
## Text-Based Approval Testing
This is a testing approach which is very useful when refactoring legacy code. Before you change the code, you run it, and gather the output of the code as a plain text file. You review the text, and if it correctly describes the behaviour as you understand it, you can "approve" it, and save it as a "Golden Master". Then after you change the code, you run it again, and compare the new output against the Golden Master. Any differences, and the test fails.
It's basically the same idea as "assertEquals(expected, actual)" in a unit test, except the text you are comparing is typically much longer, and the "expected" value is saved from actual output, rather than being defined in advance.
Typically a piece of legacy code may not produce suitable textual output from the start, so you may need to modify it before you can write your first text-based approval test. That could involve inserting log statements into the code, or just writing a "main" method that executes the code and prints out what the result is afterwards. It's this latter approach we are using here to test GildedRose.
The Text-Based tests in this repository are designed to be used with the tool "TextTest" (http://texttest.org). This tool helps you to organize and run text-based tests. There is more information in the README file in the "texttests" subdirectory.
## Get going quickly using Cyber-Dojo
I've also set this kata up on [cyber-dojo](http://cyber-dojo.com) for several languages, so you can get going really quickly:
- [JUnit, Java](http://cyber-dojo.com/forker/fork/751DD02C4C?avatar=snake&tag=4)
- [C#](http://cyber-dojo.com/forker/fork/107907AD1E?avatar=alligator&tag=13)
- [Ruby](http://cyber-dojo.com/forker/fork/A8943EAF92?avatar=hippo&tag=9)
- [RSpec, Ruby](http://cyber-dojo.com/forker/fork/8E58B0AD16?avatar=raccoon&tag=3)
- [Python](http://cyber-dojo.com/forker/fork/297041AA7A?avatar=lion&tag=4)
- [Cucumber, Java](http://cyber-dojo.com/forker/fork/0F82D4BA89?avatar=gorilla&tag=45) - for this one I've also written some step definitions for you

View File

@ -1,30 +0,0 @@
# Tennis Refactoring Kata
Imagine you work for a consultancy company, and one of your colleagues has been doing some work for the Tennis Society. The contract is for 10 hours billable work, and your colleague has spent 8.5 hours working on it. Unfortunately he has now fallen ill. He says he has completed the work, and the tests all pass. Your boss has asked you to take over from him. She wants you to spend an hour or so on the code so she can bill the client for the full 10 hours. She instructs you to tidy up the code a little and perhaps make some notes so you can give your colleague some feedback on his chosen design.
There are three versions of this refactoring kata, each with their own design smells and challenges. I suggest you start with the first one. The test suite provided is fairly comprehensive, and fast to run. You should not need to change the tests, only run them often as you refactor.
## Tennis Kata
Tennis has a rather quirky scoring system, and to newcomers it can be a little difficult to keep track of. The tennis society has contracted you to build a scoreboard to display the current score during tennis games.
Your task is to write a “TennisGame” class containing the logic which outputs the correct score as a string for display on the scoreboard. When a player scores a point, it triggers a method to be called on your class letting you know who scored the point. Later, you will get a call “score()” from the scoreboard asking what it should display. This method should return a string with the current score.
You can read more about Tennis scores [here](http://en.wikipedia.org/wiki/Tennis#Scoring) which is summarized below:
1. A game is won by the first player to have won at least four points in total and at least two points more than the opponent.
2. The running score of each game is described in a manner peculiar to tennis: scores from zero to three points are described as "love", "fifteen", "thirty", and "forty" respectively.
3. If at least three points have been scored by each player, and the scores are equal, the score is "deuce".
4. If at least three points have been scored by each side and a player has one more point than his opponent, the score of the game is "advantage" for the player in the lead.
You need only report the score for the current game. Sets and Matches are out of scope.
# Get going quickly with Cyber-dojo
As an alternative to downloading the code, click one of the links below to create a new cyber-dojo to work in, then press "start" to get going coding.
- [Python](http://cyber-dojo.com/forker/fork/FFEB8EE18C?avatar=cheetah&tag=4)
- [Ruby](http://cyber-dojo.com/forker/fork/9197D6B12C?avatar=cheetah&tag=4)
- [Java](http://cyber-dojo.com/forker/fork/426FA07B60?avatar=raccoon&tag=3)
- [C++](http://cyber-dojo.com/forker/fork/CD6FC41518?avatar=deer&tag=45)
- [C#](http://cyber-dojo.com/forker/fork/672E047F5D?avatar=buffalo&tag=8)

View File

@ -1,171 +0,0 @@
#include "all_tests.hpp"
/* change this to the version of tennis you want to work on */
#include "tennis1.cc"
#include <cassert>
void test_LoveAll_0_0()
{
assert("Love-All" == tennis_score(0, 0));
}
void test_FifteenAll_1_1()
{
assert("Fifteen-All" == tennis_score(1, 1));
}
void test_ThirtyAll_2_2()
{
assert("Thirty-All" == tennis_score(2, 2));
}
void test_Deuce_3_3()
{
assert("Deuce" == tennis_score(3, 3));
}
void test_Deuce_4_4()
{
assert("Deuce" == tennis_score(4, 4));
}
void test_FifteenLove_1_0()
{
assert("Fifteen-Love" == tennis_score(1, 0));
}
void test_LoveFifteen_0_1()
{
assert("Love-Fifteen" == tennis_score(0, 1));
}
void test_ThirtyLove_2_0()
{
assert("Thirty-Love" == tennis_score(2, 0));
}
void test_LoveThirty_0_2()
{
assert("Love-Thirty" == tennis_score(0, 2));
}
void test_FortyLove_3_0()
{
assert("Forty-Love" == tennis_score(3, 0));
}
void test_LoveForty_0_3()
{
assert("Love-Forty" == tennis_score(0, 3));
}
void test_Winforplayer1_4_0()
{
assert("Win for player1" == tennis_score(4, 0));
}
void test_Winforplayer2_0_4()
{
assert("Win for player2" == tennis_score(0, 4));
}
void test_ThirtyFifteen_2_1()
{
assert("Thirty-Fifteen" == tennis_score(2, 1));
}
void test_FifteenThirty_1_2()
{
assert("Fifteen-Thirty" == tennis_score(1, 2));
}
void test_FortyFifteen_3_1()
{
assert("Forty-Fifteen" == tennis_score(3, 1));
}
void test_FifteenForty_1_3()
{
assert("Fifteen-Forty" == tennis_score(1, 3));
}
void test_Winforplayer1_4_1()
{
assert("Win for player1" == tennis_score(4, 1));
}
void test_Winforplayer2_1_4()
{
assert("Win for player2" == tennis_score(1, 4));
}
void test_FortyThirty_3_2()
{
assert("Forty-Thirty" == tennis_score(3, 2));
}
void test_ThirtyForty_2_3()
{
assert("Thirty-Forty" == tennis_score(2, 3));
}
void test_Winforplayer1_4_2()
{
assert("Win for player1" == tennis_score(4, 2));
}
void test_Winforplayer2_2_4()
{
assert("Win for player2" == tennis_score(2, 4));
}
void test_Advantageplayer1_4_3()
{
assert("Advantage player1" == tennis_score(4, 3));
}
void test_Advantageplayer2_3_4()
{
assert("Advantage player2" == tennis_score(3, 4));
}
void test_Advantageplayer1_5_4()
{
assert("Advantage player1" == tennis_score(5, 4));
}
void test_Advantageplayer2_4_5()
{
assert("Advantage player2" == tennis_score(4, 5));
}
void test_Advantageplayer1_15_14()
{
assert("Advantage player1" == tennis_score(15, 14));
}
void test_Advantageplayer2_14_15()
{
assert("Advantage player2" == tennis_score(14, 15));
}
void test_Winforplayer1_6_4()
{
assert("Win for player1" == tennis_score(6, 4));
}
void test_Winforplayer2_4_6()
{
assert("Win for player2" == tennis_score(4, 6));
}
void test_Winforplayer1_16_14()
{
assert("Win for player1" == tennis_score(16, 14));
}
void test_Winforplayer2_14_16()
{
assert("Win for player2" == tennis_score(14, 16));
}

View File

@ -1,33 +0,0 @@
void test_LoveAll_0_0();
void test_FifteenAll_1_1();
void test_ThirtyAll_2_2();
void test_Deuce_3_3();
void test_Deuce_4_4();
void test_FifteenLove_1_0();
void test_LoveFifteen_0_1();
void test_ThirtyLove_2_0();
void test_LoveThirty_0_2();
void test_FortyLove_3_0();
void test_LoveForty_0_3();
void test_Winforplayer1_4_0();
void test_Winforplayer2_0_4();
void test_ThirtyFifteen_2_1();
void test_FifteenThirty_1_2();
void test_FortyFifteen_3_1();
void test_FifteenForty_1_3();
void test_Winforplayer1_4_1();
void test_Winforplayer2_1_4();
void test_FortyThirty_3_2();
void test_ThirtyForty_2_3();
void test_Winforplayer1_4_2();
void test_Winforplayer2_2_4();
void test_Advantageplayer1_4_3();
void test_Advantageplayer2_3_4();
void test_Advantageplayer1_5_4();
void test_Advantageplayer2_4_5();
void test_Advantageplayer1_15_14();
void test_Advantageplayer2_14_15();
void test_Winforplayer1_6_4();
void test_Winforplayer2_4_6();
void test_Winforplayer1_16_14();
void test_Winforplayer2_14_16();

View File

@ -1,79 +0,0 @@
test_cases = [dict(p1Points=0, p2Points=0, score="Love-All"),
dict(p1Points=1, p2Points=1, score="Fifteen-All"),
dict(p1Points=2, p2Points=2, score="Thirty-All"),
dict(p1Points=3, p2Points=3, score="Deuce"),
dict(p1Points=4, p2Points=4, score="Deuce"),
dict(p1Points=1, p2Points=0, score="Fifteen-Love"),
dict(p1Points=0, p2Points=1, score="Love-Fifteen"),
dict(p1Points=2, p2Points=0, score="Thirty-Love"),
dict(p1Points=0, p2Points=2, score="Love-Thirty"),
dict(p1Points=3, p2Points=0, score="Forty-Love"),
dict(p1Points=0, p2Points=3, score="Love-Forty"),
dict(p1Points=4, p2Points=0, score="Win for player1"),
dict(p1Points=0, p2Points=4, score="Win for player2"),
dict(p1Points=2, p2Points=1, score="Thirty-Fifteen"),
dict(p1Points=1, p2Points=2, score="Fifteen-Thirty"),
dict(p1Points=3, p2Points=1, score="Forty-Fifteen"),
dict(p1Points=1, p2Points=3, score="Fifteen-Forty"),
dict(p1Points=4, p2Points=1, score="Win for player1"),
dict(p1Points=1, p2Points=4, score="Win for player2"),
dict(p1Points=3, p2Points=2, score="Forty-Thirty"),
dict(p1Points=2, p2Points=3, score="Thirty-Forty"),
dict(p1Points=4, p2Points=2, score="Win for player1"),
dict(p1Points=2, p2Points=4, score="Win for player2"),
dict(p1Points=4, p2Points=3, score="Advantage player1"),
dict(p1Points=3, p2Points=4, score="Advantage player2"),
dict(p1Points=5, p2Points=4, score="Advantage player1"),
dict(p1Points=4, p2Points=5, score="Advantage player2"),
dict(p1Points=15, p2Points=14, score="Advantage player1"),
dict(p1Points=14, p2Points=15, score="Advantage player2"),
dict(p1Points=6, p2Points=4, score="Win for player1"),
dict(p1Points=4, p2Points=6, score="Win for player2"),
dict(p1Points=16, p2Points=14, score="Win for player1"),
dict(p1Points=14, p2Points=16, score="Win for player2"),
]
def create_testcase_dicts():
testcase_dicts = []
for test in test_cases:
cleaned = test["score"]
cleaned = cleaned.replace("-", "")
cleaned = cleaned.replace(" ", "")
test["cleaned"] = cleaned
test["testcase_name"] = "%(cleaned)s_%(p1Points)s_%(p2Points)s" % test
testcase_dicts.append(test)
return testcase_dicts
testcase_dicts = create_testcase_dicts()
# This is not currently being used
gtest_template = """\
TEST(TennisTest, %(testcase_name)s) {
EXPECT_EQ("%(score)s", tennis_score(%(p1Points)s, %(p2Points)s));
}
"""
template = """\
void test_%(testcase_name)s()
{
assert("%(score)s" == tennis_score(%(p1Points)s, %(p2Points)s));
}
"""
# test cases for all_tests.cpp
for test in testcase_dicts:
print template % test
# test_suite.tests.cpp
for test in testcase_dicts:
print " test_%(testcase_name)s," % test
# all_tests.hpp
for test in testcase_dicts:
print "void test_%(testcase_name)s();" % test

View File

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

View File

@ -1,61 +0,0 @@
#include <string>
const std::string tennis_score(int p1Score, int p2Score) {
std::string score = "";
int tempScore=0;
if (p1Score==p2Score)
{
switch (p1Score)
{
case 0:
score = "Love-All";
break;
case 1:
score = "Fifteen-All";
break;
case 2:
score = "Thirty-All";
break;
case 3:
score = "Deuce";
break;
default:
score = "Deuce";
break;
}
}
else if (p1Score>=4 || p2Score>=4)
{
int minusResult = p1Score-p2Score;
if (minusResult==1) score ="Advantage player1";
else if (minusResult ==-1) score ="Advantage player2";
else if (minusResult>=2) score = "Win for player1";
else score ="Win for player2";
}
else
{
for (int i=1; i<3; i++)
{
if (i==1) tempScore = p1Score;
else { score+="-"; tempScore = p2Score;}
switch(tempScore)
{
case 0:
score+="Love";
break;
case 1:
score+="Fifteen";
break;
case 2:
score+="Thirty";
break;
case 3:
score+="Forty";
break;
}
}
}
return score;
}

View File

@ -1,92 +0,0 @@
#include <string>
const std::string tennis_score(int p1Score, int p2Score) {
std::string score = "";
std::string P1res = "";
std::string P2res = "";
if (p1Score == p2Score && p1Score < 4)
{
if (p1Score==0)
score = "Love";
if (p1Score==1)
score = "Fifteen";
if (p1Score==2)
score = "Thirty";
if (p1Score==3)
score = "Forty";
score += "-All";
}
if (p1Score==p2Score && p1Score>2)
score = "Deuce";
if (p1Score > 0 && p2Score==0)
{
if (p1Score==1)
P1res = "Fifteen";
if (p1Score==2)
P1res = "Thirty";
if (p1Score==3)
P1res = "Forty";
P2res = "Love";
score = P1res + "-" + P2res;
}
if (p2Score > 0 && p1Score==0)
{
if (p2Score==1)
P2res = "Fifteen";
if (p2Score==2)
P2res = "Thirty";
if (p2Score==3)
P2res = "Forty";
P1res = "Love";
score = P1res + "-" + P2res;
}
if (p1Score>p2Score && p1Score < 4)
{
if (p1Score==2)
P1res="Thirty";
if (p1Score==3)
P1res="Forty";
if (p2Score==1)
P2res="Fifteen";
if (p2Score==2)
P2res="Thirty";
score = P1res + "-" + P2res;
}
if (p2Score>p1Score && p2Score < 4)
{
if (p2Score==2)
P2res="Thirty";
if (p2Score==3)
P2res="Forty";
if (p1Score==1)
P1res="Fifteen";
if (p1Score==2)
P1res="Thirty";
score = P1res + "-" + P2res;
}
if (p1Score > p2Score && p2Score >= 3)
{
score = "Advantage player1";
}
if (p2Score > p1Score && p1Score >= 3)
{
score = "Advantage player2";
}
if (p1Score>=4 && p2Score>=0 && (p1Score-p2Score)>=2)
{
score = "Win for player1";
}
if (p2Score>=4 && p1Score>=0 && (p2Score-p1Score)>=2)
{
score = "Win for player2";
}
return score;
}

View File

@ -1,17 +0,0 @@
#include <string>
const std::string tennis_score(int p1, int p2) {
std::string s;
std::string p1N = "player1";
std::string p2N = "player2";
if (p1 < 4 && p2 < 4 && !(p1 == 3 && p2 == 3)) {
std::string p[4] = {"Love", "Fifteen", "Thirty", "Forty"};
s = p[p1];
return (p1 == p2) ? s + "-All" : s + "-" + p[p2];
} else {
if (p1 == p2)
return "Deuce";
s = p1 > p2 ? p1N : p2N;
return ((p1-p2)*(p1-p2) == 1) ? "Advantage " + s : "Win for " + s;
}
}

View File

@ -1,55 +0,0 @@
#include "all_tests.hpp"
#include <cassert>
#include <iostream>
typedef void test();
static test * tests[ ] =
{
test_LoveAll_0_0,
test_FifteenAll_1_1,
test_ThirtyAll_2_2,
test_Deuce_3_3,
test_Deuce_4_4,
test_FifteenLove_1_0,
test_LoveFifteen_0_1,
test_ThirtyLove_2_0,
test_LoveThirty_0_2,
test_FortyLove_3_0,
test_LoveForty_0_3,
test_Winforplayer1_4_0,
test_Winforplayer2_0_4,
test_ThirtyFifteen_2_1,
test_FifteenThirty_1_2,
test_FortyFifteen_3_1,
test_FifteenForty_1_3,
test_Winforplayer1_4_1,
test_Winforplayer2_1_4,
test_FortyThirty_3_2,
test_ThirtyForty_2_3,
test_Winforplayer1_4_2,
test_Winforplayer2_2_4,
test_Advantageplayer1_4_3,
test_Advantageplayer2_3_4,
test_Advantageplayer1_5_4,
test_Advantageplayer2_4_5,
test_Advantageplayer1_15_14,
test_Advantageplayer2_14_15,
test_Winforplayer1_6_4,
test_Winforplayer2_4_6,
test_Winforplayer1_16_14,
test_Winforplayer2_14_16,
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;
}

View File

@ -1,27 +0,0 @@
using System.Reflection;
using System.Runtime.CompilerServices;
// Information about this assembly is defined by the following attributes.
// Change them to the values specific to your project.
[assembly: AssemblyTitle("Tennis")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("emily")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
[assembly: AssemblyVersion("1.0.*")]
// The following attributes are used to specify the signing key for the assembly,
// if desired. See the Mono documentation for more information about signing.
//[assembly: AssemblyDelaySign(false)]
//[assembly: AssemblyKeyFile("")]

View File

@ -1,12 +0,0 @@
using System;
namespace Tennis
{
class MainClass
{
public static void Main (string[] args)
{
Console.WriteLine ("Hello World!");
}
}
}

View File

@ -1,48 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="3.5" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{F059601E-EF1A-4148-A2A4-1E1697CC69EA}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>Tennis</RootNamespace>
<AssemblyName>Tennis</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<DebugSymbols>True</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>False</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
<Externalconsole>True</Externalconsole>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<DebugType>none</DebugType>
<Optimize>True</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
<Externalconsole>True</Externalconsole>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="nunit.framework" />
</ItemGroup>
<ItemGroup>
<Compile Include="Main.cs" />
<Compile Include="AssemblyInfo.cs" />
<Compile Include="TennisTest.cs" />
<Compile Include="TennisGame1.cs" />
<Compile Include="TennisGame2.cs" />
<Compile Include="TennisGame3.cs" />
<Compile Include="TennisGame.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -1,12 +0,0 @@
using System;
namespace Tennis
{
public interface TennisGame
{
void WonPoint (string playerName);
string GetScore ();
}
}

View File

@ -1,89 +0,0 @@
using System;
using NUnit.Framework;
namespace Tennis
{
class TennisGame1 : TennisGame
{
private int m_score1 = 0;
private int m_score2 = 0;
private string player1Name;
private string player2Name;
public TennisGame1 (string player1Name, string player2Name)
{
this.player1Name = player1Name;
this.player2Name = player2Name;
}
public void WonPoint (string playerName)
{
if (playerName == "player1")
m_score1 += 1;
else
m_score2 += 1;
}
public string GetScore ()
{
String score = "";
int tempScore=0;
if (m_score1==m_score2)
{
switch (m_score1)
{
case 0:
score = "Love-All";
break;
case 1:
score = "Fifteen-All";
break;
case 2:
score = "Thirty-All";
break;
case 3:
score = "Forty-All";
break;
default:
score = "Deuce";
break;
}
}
else if (m_score1>=4 || m_score2>=4)
{
int minusResult = m_score1-m_score2;
if (minusResult==1) score ="Advantage player1";
else if (minusResult ==-1) score ="Advantage player2";
else if (minusResult>=2) score = "Win for player1";
else score ="Win for player2";
}
else
{
for (int i=1; i<3; i++)
{
if (i==1) tempScore = m_score1;
else { score+="-"; tempScore = m_score2;}
switch(tempScore)
{
case 0:
score+="Love";
break;
case 1:
score+="Fifteen";
break;
case 2:
score+="Thirty";
break;
case 3:
score+="Forty";
break;
}
}
}
return score;
}
}
}

View File

@ -1,144 +0,0 @@
using System;
namespace Tennis
{
public class TennisGame2 : TennisGame
{
public int P1point = 0;
public int P2point = 0;
public string P1res = "";
public string P2res = "";
private string player1Name;
private string player2Name;
public TennisGame2 (string player1Name, string player2Name)
{
this.player1Name = player1Name;
this.player2Name = player2Name;
}
public string GetScore(){
string score = "";
if (P1point == P2point && P1point < 4)
{
if (P1point==0)
score = "Love";
if (P1point==1)
score = "Fifteen";
if (P1point==2)
score = "Thirty";
if (P1point==3)
score = "Forty";
score += "-All";
}
if (P1point==P2point && P1point>3)
score = "Deuce";
if (P1point > 0 && P2point==0)
{
if (P1point==1)
P1res = "Fifteen";
if (P1point==2)
P1res = "Thirty";
if (P1point==3)
P1res = "Forty";
P2res = "Love";
score = P1res + "-" + P2res;
}
if (P2point > 0 && P1point==0)
{
if (P2point==1)
P2res = "Fifteen";
if (P2point==2)
P2res = "Thirty";
if (P2point==3)
P2res = "Forty";
P1res = "Love";
score = P1res + "-" + P2res;
}
if (P1point>P2point && P1point < 4)
{
if (P1point==2)
P1res="Thirty";
if (P1point==3)
P1res="Forty";
if (P2point==1)
P2res="Fifteen";
if (P2point==2)
P2res="Thirty";
score = P1res + "-" + P2res;
}
if (P2point>P1point && P2point < 4)
{
if (P2point==2)
P2res="Thirty";
if (P2point==3)
P2res="Forty";
if (P1point==1)
P1res="Fifteen";
if (P1point==2)
P1res="Thirty";
score = P1res + "-" + P2res;
}
if (P1point > P2point && P2point >= 3)
{
score = "Advantage player1";
}
if (P2point > P1point && P1point >= 3)
{
score = "Advantage player2";
}
if (P1point>=4 && P2point>=0 && (P1point-P2point)>=2)
{
score = "Win for player1";
}
if (P2point>=4 && P1point>=0 && (P2point-P1point)>=2)
{
score = "Win for player2";
}
return score;
}
public void SetP1Score(int number){
for (int i = 0; i < number; i++)
{
P1Score();
}
}
public void SetP2Score(int number){
for (int i = 0; i < number; i++)
{
P2Score();
}
}
public void P1Score(){
P1point++;
}
public void P2Score(){
P2point++;
}
public void WonPoint(string player) {
if (player == "player1")
P1Score();
else
P2Score();
}
}
}

View File

@ -1,41 +0,0 @@
using System;
namespace Tennis
{
public class TennisGame3 : TennisGame
{
private int p2;
private int p1;
private string p1N;
private string p2N;
public TennisGame3 (string player1Name, string player2Name)
{
this.p1N = player1Name;
this.p2N = player2Name;
}
public string GetScore() {
string s;
if (p1 < 4 && p2 < 4) {
string[] p = new String[]{"Love", "Fifteen", "Thirty", "Forty"};
s = p[p1];
return (p1 == p2) ? s + "-All" : s + "-" + p[p2];
} else {
if (p1 == p2)
return "Deuce";
s = p1 > p2 ? p1N : p2N;
return ((p1-p2)*(p1-p2) == 1) ? "Advantage " + s : "Win for " + s;
}
}
public void WonPoint(string playerName) {
if (playerName == "player1")
this.p1 += 1;
else
this.p2 += 1;
}
}
}

View File

@ -1,115 +0,0 @@
using System;
using NUnit.Framework;
namespace Tennis
{
[TestFixture(0, 0, "Love-All")]
[TestFixture( 1, 1, "Fifteen-All" )]
[TestFixture( 2, 2, "Thirty-All")]
[TestFixture( 3, 3, "Forty-All")]
[TestFixture( 4, 4, "Deuce")]
[TestFixture( 1, 0, "Fifteen-Love")]
[TestFixture( 0, 1, "Love-Fifteen")]
[TestFixture( 2, 0, "Thirty-Love")]
[TestFixture( 0, 2, "Love-Thirty")]
[TestFixture( 3, 0, "Forty-Love")]
[TestFixture( 0, 3, "Love-Forty")]
[TestFixture( 4, 0, "Win for player1")]
[TestFixture( 0, 4, "Win for player2")]
[TestFixture( 2, 1, "Thirty-Fifteen")]
[TestFixture( 1, 2, "Fifteen-Thirty")]
[TestFixture( 3, 1, "Forty-Fifteen")]
[TestFixture( 1, 3, "Fifteen-Forty")]
[TestFixture( 4, 1, "Win for player1")]
[TestFixture( 1, 4, "Win for player2")]
[TestFixture( 3, 2, "Forty-Thirty")]
[TestFixture( 2, 3, "Thirty-Forty")]
[TestFixture( 4, 2, "Win for player1")]
[TestFixture( 2, 4, "Win for player2")]
[TestFixture( 4, 3, "Advantage player1")]
[TestFixture( 3, 4, "Advantage player2")]
[TestFixture( 5, 4, "Advantage player1")]
[TestFixture( 4, 5, "Advantage player2")]
[TestFixture( 15, 14, "Advantage player1")]
[TestFixture( 14, 15, "Advantage player2")]
[TestFixture( 6, 4, "Win for player1")]
[TestFixture( 4, 6, "Win for player2")]
[TestFixture( 16, 14, "Win for player1")]
[TestFixture( 14, 16, "Win for player2")]
public class TennisTest
{
private int player1Score;
private int player2Score;
private string expectedScore;
public TennisTest(int player1Score, int player2Score, string expectedScore) {
this.player1Score = player1Score;
this.player2Score = player2Score;
this.expectedScore = expectedScore;
}
[Test]
public void checkTennisGame1() {
TennisGame1 game = new TennisGame1("player1", "player2");
checkAllScores(game);
}
[Test]
public void checkTennisGame2() {
TennisGame2 game = new TennisGame2("player1", "player2");
checkAllScores(game);
}
[Test]
public void checkTennisGame3() {
TennisGame3 game = new TennisGame3("player1", "player2");
checkAllScores(game);
}
public void checkAllScores(TennisGame game) {
int highestScore = Math.Max(this.player1Score, this.player2Score);
for (int i = 0; i < highestScore; i++) {
if (i < this.player1Score)
game.WonPoint("player1");
if (i < this.player2Score)
game.WonPoint("player2");
}
Assert.AreEqual(this.expectedScore, game.GetScore());
}
}
[TestFixture()]
public class ExampleGameTennisTest
{
public void RealisticTennisGame(TennisGame game)
{
String[] points = {"player1", "player1", "player2", "player2", "player1", "player1"};
String[] expected_scores = {"Fifteen-Love", "Thirty-Love", "Thirty-Fifteen", "Thirty-All", "Forty-Thirty", "Win for player1"};
for (int i = 0; i < 6; i++) {
game.WonPoint(points[i]);
Assert.AreEqual(expected_scores[i], game.GetScore());
}
}
[Test()]
public void CheckGame1()
{
TennisGame1 game = new TennisGame1("player1", "player2");
RealisticTennisGame(game);
}
[Test()]
public void CheckGame2()
{
TennisGame2 game = new TennisGame2("player1", "player2");
RealisticTennisGame(game);
}
[Test()]
public void CheckGame3()
{
TennisGame3 game = new TennisGame3("player1", "player2");
RealisticTennisGame(game);
}
}
}

View File

@ -1,5 +0,0 @@
public interface TennisGame {
void wonPoint(String playerName);
String getScore();
}

View File

@ -1,79 +0,0 @@
public class TennisGame1 implements TennisGame {
private int m_score1 = 0;
private int m_score2 = 0;
private String player1Name;
private String player2Name;
public TennisGame1(String player1Name, String player2Name) {
this.player1Name = player1Name;
this.player2Name = player2Name;
}
public void wonPoint(String playerName) {
if (playerName == "player1")
m_score1 += 1;
else
m_score2 += 1;
}
public String getScore() {
String score = "";
int tempScore=0;
if (m_score1==m_score2)
{
switch (m_score1)
{
case 0:
score = "Love-All";
break;
case 1:
score = "Fifteen-All";
break;
case 2:
score = "Thirty-All";
break;
case 3:
score = "Forty-All";
break;
default:
score = "Deuce";
break;
}
}
else if (m_score1>=4 || m_score2>=4)
{
int minusResult = m_score1-m_score2;
if (minusResult==1) score ="Advantage player1";
else if (minusResult ==-1) score ="Advantage player2";
else if (minusResult>=2) score = "Win for player1";
else score ="Win for player2";
}
else
{
for (int i=1; i<3; i++)
{
if (i==1) tempScore = m_score1;
else { score+="-"; tempScore = m_score2;}
switch(tempScore)
{
case 0:
score+="Love";
break;
case 1:
score+="Fifteen";
break;
case 2:
score+="Thirty";
break;
case 3:
score+="Forty";
break;
}
}
}
return score;
}
}

View File

@ -1,137 +0,0 @@
public class TennisGame2 implements TennisGame
{
public int P1point = 0;
public int P2point = 0;
public String P1res = "";
public String P2res = "";
private String player1Name;
private String player2Name;
public TennisGame2(String player1Name, String player2Name) {
this.player1Name = player1Name;
this.player2Name = player2Name;
}
public String getScore(){
String score = "";
if (P1point == P2point && P1point < 4)
{
if (P1point==0)
score = "Love";
if (P1point==1)
score = "Fifteen";
if (P1point==2)
score = "Thirty";
if (P1point==3)
score = "Forty";
score += "-All";
}
if (P1point==P2point && P1point>3)
score = "Deuce";
if (P1point > 0 && P2point==0)
{
if (P1point==1)
P1res = "Fifteen";
if (P1point==2)
P1res = "Thirty";
if (P1point==3)
P1res = "Forty";
P2res = "Love";
score = P1res + "-" + P2res;
}
if (P2point > 0 && P1point==0)
{
if (P2point==1)
P2res = "Fifteen";
if (P2point==2)
P2res = "Thirty";
if (P2point==3)
P2res = "Forty";
P1res = "Love";
score = P1res + "-" + P2res;
}
if (P1point>P2point && P1point < 4)
{
if (P1point==2)
P1res="Thirty";
if (P1point==3)
P1res="Forty";
if (P2point==1)
P2res="Fifteen";
if (P2point==2)
P2res="Thirty";
score = P1res + "-" + P2res;
}
if (P2point>P1point && P2point < 4)
{
if (P2point==2)
P2res="Thirty";
if (P2point==3)
P2res="Forty";
if (P1point==1)
P1res="Fifteen";
if (P1point==2)
P1res="Thirty";
score = P1res + "-" + P2res;
}
if (P1point > P2point && P2point >= 3)
{
score = "Advantage player1";
}
if (P2point > P1point && P1point >= 3)
{
score = "Advantage player2";
}
if (P1point>=4 && P2point>=0 && (P1point-P2point)>=2)
{
score = "Win for player1";
}
if (P2point>=4 && P1point>=0 && (P2point-P1point)>=2)
{
score = "Win for player2";
}
return score;
}
public void SetP1Score(int number){
for (int i = 0; i < number; i++)
{
P1Score();
}
}
public void SetP2Score(int number){
for (int i = 0; i < number; i++)
{
P2Score();
}
}
public void P1Score(){
P1point++;
}
public void P2Score(){
P2point++;
}
public void wonPoint(String player) {
if (player == "player1")
P1Score();
else
P2Score();
}
}

View File

@ -1,36 +0,0 @@
public class TennisGame3 implements TennisGame {
private int p2;
private int p1;
private String p1N;
private String p2N;
public TennisGame3(String p1N, String p2N) {
this.p1N = p1N;
this.p2N = p2N;
}
public String getScore() {
String s;
if (p1 < 4 && p2 < 4) {
String[] p = new String[]{"Love", "Fifteen", "Thirty", "Forty"};
s = p[p1];
return (p1 == p2) ? s + "-All" : s + "-" + p[p2];
} else {
if (p1 == p2)
return "Deuce";
s = p1 > p2 ? p1N : p2N;
return ((p1-p2)*(p1-p2) == 1) ? "Advantage " + s : "Win for " + s;
}
}
public void wonPoint(String playerName) {
if (playerName == "player1")
this.p1 += 1;
else
this.p2 += 1;
}
}

View File

@ -1,98 +0,0 @@
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class TennisTest {
private int player1Score;
private int player2Score;
private String expectedScore;
public TennisTest(int player1Score, int player2Score, String expectedScore) {
this.player1Score = player1Score;
this.player2Score = player2Score;
this.expectedScore = expectedScore;
}
@Parameters
public static Collection<Object[]> getAllScores() {
return Arrays.asList(new Object[][] {
{ 0, 0, "Love-All" },
{ 1, 1, "Fifteen-All" },
{ 2, 2, "Thirty-All"},
{ 3, 3, "Forty-All"},
{ 4, 4, "Deuce"},
{ 1, 0, "Fifteen-Love"},
{ 0, 1, "Love-Fifteen"},
{ 2, 0, "Thirty-Love"},
{ 0, 2, "Love-Thirty"},
{ 3, 0, "Forty-Love"},
{ 0, 3, "Love-Forty"},
{ 4, 0, "Win for player1"},
{ 0, 4, "Win for player2"},
{ 2, 1, "Thirty-Fifteen"},
{ 1, 2, "Fifteen-Thirty"},
{ 3, 1, "Forty-Fifteen"},
{ 1, 3, "Fifteen-Forty"},
{ 4, 1, "Win for player1"},
{ 1, 4, "Win for player2"},
{ 3, 2, "Forty-Thirty"},
{ 2, 3, "Thirty-Forty"},
{ 4, 2, "Win for player1"},
{ 2, 4, "Win for player2"},
{ 4, 3, "Advantage player1"},
{ 3, 4, "Advantage player2"},
{ 5, 4, "Advantage player1"},
{ 4, 5, "Advantage player2"},
{ 15, 14, "Advantage player1"},
{ 14, 15, "Advantage player2"},
{ 6, 4, "Win for player1"},
{ 4, 6, "Win for player2"},
{ 16, 14, "Win for player1"},
{ 14, 16, "Win for player2"},
});
}
public void checkAllScores(TennisGame game) {
int highestScore = Math.max(this.player1Score, this.player2Score);
for (int i = 0; i < highestScore; i++) {
if (i < this.player1Score)
game.wonPoint("player1");
if (i < this.player2Score)
game.wonPoint("player2");
}
assertEquals(this.expectedScore, game.getScore());
}
@Test
public void checkAllScoresTennisGame1() {
TennisGame1 game = new TennisGame1("player1", "player2");
checkAllScores(game);
}
@Test
public void checkAllScoresTennisGame2() {
TennisGame2 game = new TennisGame2("player1", "player2");
checkAllScores(game);
}
@Test
public void checkAllScoresTennisGame3() {
TennisGame3 game = new TennisGame3("player1", "player2");
checkAllScores(game);
}
}

View File

@ -1,3 +0,0 @@
.idea
Tennis.xcodeproj/project.xcworkspace/xcuserdata/*
Tennis.xcodeproj/xcuserdata/*

View File

@ -1,305 +0,0 @@
// !$*UTF8*$!
{
archiveVersion = 1;
classes = {
};
objectVersion = 46;
objects = {
/* Begin PBXBuildFile section */
CACF63F14142F5803F786695 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CACF69F4ABB8AC23884C74C3 /* Foundation.framework */; };
CACF6414A388566B43A9BCB5 /* TennisGame.m in Sources */ = {isa = PBXBuildFile; fileRef = CACF649AAFA398A61A600CB7 /* TennisGame.m */; };
CACF66A873EB3C4C2A1B7D78 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CACF6C52F7301D6A288FECCA /* UIKit.framework */; };
CACF6871FE27A26189B71C29 /* TennisGame1.m in Sources */ = {isa = PBXBuildFile; fileRef = CACF6BF242B4F5F91193BEC7 /* TennisGame1.m */; };
CACF68FFD67D0173EDF11BF4 /* TennisTests.m in Sources */ = {isa = PBXBuildFile; fileRef = CACF6D897D4F49BCD1B41947 /* TennisTests.m */; };
CACF6C5876E4687F7A65AF54 /* SenTestingKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CACF6A13AA71A254E309FB03 /* SenTestingKit.framework */; };
CACF6E1993921B2EC1995F0E /* TennisGame2.m in Sources */ = {isa = PBXBuildFile; fileRef = CACF64EDAB086A5F18E393EE /* TennisGame2.m */; };
CACF6F3B5CEDAF9DFE1B8A64 /* TennisGame3.m in Sources */ = {isa = PBXBuildFile; fileRef = CACF6BAD45E82D978466F92B /* TennisGame3.m */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
CACF61291A7012C7769FCE61 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
CACF6419833B61F7E3D89075 /* TennisGame3.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TennisGame3.h; sourceTree = "<group>"; };
CACF649AAFA398A61A600CB7 /* TennisGame.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TennisGame.m; sourceTree = "<group>"; };
CACF64EDAB086A5F18E393EE /* TennisGame2.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TennisGame2.m; sourceTree = "<group>"; };
CACF665E8EA24EE215E5498C /* TennisTests.octest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = TennisTests.octest; sourceTree = BUILT_PRODUCTS_DIR; };
CACF6771858FAF6F6F4650A6 /* TennisGame2.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TennisGame2.h; sourceTree = "<group>"; };
CACF69F4ABB8AC23884C74C3 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
CACF6A13AA71A254E309FB03 /* SenTestingKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SenTestingKit.framework; path = Library/Frameworks/SenTestingKit.framework; sourceTree = DEVELOPER_DIR; };
CACF6A4205D54CCF3AEA2B65 /* TennisTests.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TennisTests.h; sourceTree = "<group>"; };
CACF6A964A59A72222255B2C /* TennisGame.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TennisGame.h; sourceTree = "<group>"; };
CACF6B7F36643E0787BABC39 /* TennisGame1.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TennisGame1.h; sourceTree = "<group>"; };
CACF6BAD45E82D978466F92B /* TennisGame3.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TennisGame3.m; sourceTree = "<group>"; };
CACF6BF242B4F5F91193BEC7 /* TennisGame1.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TennisGame1.m; sourceTree = "<group>"; };
CACF6C52F7301D6A288FECCA /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
CACF6D897D4F49BCD1B41947 /* TennisTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TennisTests.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
CACF6A7D79290F13325B461F /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
CACF6C5876E4687F7A65AF54 /* SenTestingKit.framework in Frameworks */,
CACF66A873EB3C4C2A1B7D78 /* UIKit.framework in Frameworks */,
CACF63F14142F5803F786695 /* Foundation.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
CACF60033C37E3B5C11CC734 /* Frameworks */ = {
isa = PBXGroup;
children = (
CACF6C52F7301D6A288FECCA /* UIKit.framework */,
CACF69F4ABB8AC23884C74C3 /* Foundation.framework */,
CACF61291A7012C7769FCE61 /* CoreGraphics.framework */,
CACF6A13AA71A254E309FB03 /* SenTestingKit.framework */,
);
name = Frameworks;
sourceTree = "<group>";
};
CACF652B01231CADE96100D1 = {
isa = PBXGroup;
children = (
CACF67CF0393B97F4DC41E0B /* Tennis */,
CACF65D28A08CBB9A2C540F5 /* TennisTests */,
CACF6E7D48293F4D43E7A6DC /* Products */,
CACF60033C37E3B5C11CC734 /* Frameworks */,
);
sourceTree = "<group>";
};
CACF65D28A08CBB9A2C540F5 /* TennisTests */ = {
isa = PBXGroup;
children = (
CACF6A4205D54CCF3AEA2B65 /* TennisTests.h */,
CACF6D897D4F49BCD1B41947 /* TennisTests.m */,
);
path = TennisTests;
sourceTree = "<group>";
};
CACF67CF0393B97F4DC41E0B /* Tennis */ = {
isa = PBXGroup;
children = (
CACF6BF242B4F5F91193BEC7 /* TennisGame1.m */,
CACF6B7F36643E0787BABC39 /* TennisGame1.h */,
CACF64EDAB086A5F18E393EE /* TennisGame2.m */,
CACF6771858FAF6F6F4650A6 /* TennisGame2.h */,
CACF6BAD45E82D978466F92B /* TennisGame3.m */,
CACF6419833B61F7E3D89075 /* TennisGame3.h */,
CACF649AAFA398A61A600CB7 /* TennisGame.m */,
CACF6A964A59A72222255B2C /* TennisGame.h */,
);
path = Tennis;
sourceTree = "<group>";
};
CACF6E7D48293F4D43E7A6DC /* Products */ = {
isa = PBXGroup;
children = (
CACF665E8EA24EE215E5498C /* TennisTests.octest */,
);
name = Products;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
CACF6DE0B90C4D92A2B3D5E6 /* TennisTests */ = {
isa = PBXNativeTarget;
buildConfigurationList = CACF68FD0CC2343E15B3AA80 /* Build configuration list for PBXNativeTarget "TennisTests" */;
buildPhases = (
CACF6FE60766CDED5F49C391 /* Sources */,
CACF6A7D79290F13325B461F /* Frameworks */,
CACF6814909F4B826FA2E0CA /* Resources */,
CACF6F4E118C6D42BF7A89A2 /* ShellScript */,
);
buildRules = (
);
dependencies = (
);
name = TennisTests;
productName = TennisTests;
productReference = CACF665E8EA24EE215E5498C /* TennisTests.octest */;
productType = "com.apple.product-type.bundle";
};
/* End PBXNativeTarget section */
/* Begin PBXProject section */
CACF6DA3161729BC17499E03 /* Project object */ = {
isa = PBXProject;
attributes = {
ORGANIZATIONNAME = "Stefan van den Oord";
};
buildConfigurationList = CACF6959B8F6F7EC1647AAB0 /* Build configuration list for PBXProject "Tennis" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
en,
);
mainGroup = CACF652B01231CADE96100D1;
productRefGroup = CACF6E7D48293F4D43E7A6DC /* Products */;
projectDirPath = "";
projectRoot = "";
targets = (
CACF6DE0B90C4D92A2B3D5E6 /* TennisTests */,
);
};
/* End PBXProject section */
/* Begin PBXResourcesBuildPhase section */
CACF6814909F4B826FA2E0CA /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXResourcesBuildPhase section */
/* Begin PBXShellScriptBuildPhase section */
CACF6F4E118C6D42BF7A89A2 /* ShellScript */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "# Run the unit tests in this test bundle.\n\"${SYSTEM_DEVELOPER_DIR}/Tools/RunUnitTests\"\n";
};
/* End PBXShellScriptBuildPhase section */
/* Begin PBXSourcesBuildPhase section */
CACF6FE60766CDED5F49C391 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
CACF68FFD67D0173EDF11BF4 /* TennisTests.m in Sources */,
CACF6F3B5CEDAF9DFE1B8A64 /* TennisGame3.m in Sources */,
CACF6E1993921B2EC1995F0E /* TennisGame2.m in Sources */,
CACF6871FE27A26189B71C29 /* TennisGame1.m in Sources */,
CACF6414A388566B43A9BCB5 /* TennisGame.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXSourcesBuildPhase section */
/* Begin XCBuildConfiguration section */
CACF601C65BCC0FBE3D880E3 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_LOADER = "";
FRAMEWORK_SEARCH_PATHS = (
"\"$(SDKROOT)/Developer/Library/Frameworks\"",
"\"$(DEVELOPER_LIBRARY_DIR)/Frameworks\"",
);
GCC_PRECOMPILE_PREFIX_HEADER = YES;
PRODUCT_NAME = "$(TARGET_NAME)";
TEST_HOST = "";
WRAPPER_EXTENSION = octest;
};
name = Release;
};
CACF6452D3F349731DB85BE9 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 6.1;
OTHER_CFLAGS = "-DNS_BLOCK_ASSERTIONS=1";
SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
};
name = Release;
};
CACF6A641D1372E5C5478EB2 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_LOADER = "";
FRAMEWORK_SEARCH_PATHS = (
"\"$(SDKROOT)/Developer/Library/Frameworks\"",
"\"$(DEVELOPER_LIBRARY_DIR)/Frameworks\"",
);
GCC_PRECOMPILE_PREFIX_HEADER = YES;
PRODUCT_NAME = "$(TARGET_NAME)";
TEST_HOST = "";
WRAPPER_EXTENSION = octest;
};
name = Debug;
};
CACF6FD238549A86B0925D65 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = NO;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
);
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 6.1;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
};
/* End XCBuildConfiguration section */
/* Begin XCConfigurationList section */
CACF68FD0CC2343E15B3AA80 /* Build configuration list for PBXNativeTarget "TennisTests" */ = {
isa = XCConfigurationList;
buildConfigurations = (
CACF601C65BCC0FBE3D880E3 /* Release */,
CACF6A641D1372E5C5478EB2 /* Debug */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
CACF6959B8F6F7EC1647AAB0 /* Build configuration list for PBXProject "Tennis" */ = {
isa = XCConfigurationList;
buildConfigurations = (
CACF6452D3F349731DB85BE9 /* Release */,
CACF6FD238549A86B0925D65 /* Debug */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
rootObject = CACF6DA3161729BC17499E03 /* Project object */;
}

View File

@ -1,7 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "self:Tennis.xcodeproj">
</FileRef>
</Workspace>

View File

@ -1,68 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "CACF6DE0B90C4D92A2B3D5E6"
BuildableName = "TennisTests.octest"
BlueprintName = "TennisTests"
ReferencedContainer = "container:Tennis.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "CACF6DE0B90C4D92A2B3D5E6"
BuildableName = "TennisTests.octest"
BlueprintName = "TennisTests"
ReferencedContainer = "container:Tennis.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
allowLocationSimulation = "YES">
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>

View File

@ -1,9 +0,0 @@
#import <Foundation/Foundation.h>
@interface TennisGame : NSObject
- (id)initWithPlayer1:(NSString *)player1 player2:(NSString *)player2;
- (void)wonPoint:(NSString *)playerName;
- (NSString *)score;
@end

View File

@ -1,10 +0,0 @@
#import "TennisGame.h"
@implementation TennisGame
- (id)initWithPlayer1:(NSString *)player1 player2:(NSString *)player2 { return [super init]; }
- (void)wonPoint:(NSString *)playerName {}
- (NSString *)score { return nil; }
@end

View File

@ -1,5 +0,0 @@
#import <Foundation/Foundation.h>
#import "TennisGame.h"
@interface TennisGame1 : TennisGame
@end

View File

@ -1,92 +0,0 @@
#import "TennisGame1.h"
@interface TennisGame1 ()
@property(nonatomic, copy) NSString *player1;
@property(nonatomic, copy) NSString *player2;
@end
@implementation TennisGame1 {
int score1;
int score2;
}
- (id)initWithPlayer1:(NSString *)player1 player2:(NSString *)player2 {
self = [super init];
if (self) {
self.player1 = player1;
self.player2 = player2;
score1 = 0;
score2 = 0;
}
return self;
}
- (void)wonPoint:(NSString *)playerName {
if ([playerName isEqualToString:@"player1"])
score1 += 1;
else
score2 += 1;
}
- (NSString *)score {
NSString *score = @"";
int tempScore=0;
if (score1 == score2)
{
switch (score1)
{
case 0:
score = @"Love-All";
break;
case 1:
score = @"Fifteen-All";
break;
case 2:
score = @"Thirty-All";
break;
case 3:
score = @"Forty-All";
break;
default:
score = @"Deuce";
break;
}
}
else if (score1>=4 || score2>=4)
{
int minusResult = score1-score2;
if (minusResult==1) score = @"Advantage player1";
else if (minusResult ==-1) score = @"Advantage player2";
else if (minusResult>=2) score = @"Win for player1";
else score = @"Win for player2";
}
else
{
for (int i=1; i<3; i++)
{
if (i==1) tempScore = score1;
else { score = [NSString stringWithFormat:@"%@-", score]; tempScore = score2; }
switch(tempScore)
{
case 0:
score = [NSString stringWithFormat:@"%@Love", score];
break;
case 1:
score = [NSString stringWithFormat:@"%@Fifteen", score];
break;
case 2:
score = [NSString stringWithFormat:@"%@Thirty", score];
break;
case 3:
score = [NSString stringWithFormat:@"%@Forty", score];
break;
}
}
}
return score;
}
@end

View File

@ -1,5 +0,0 @@
#import <Foundation/Foundation.h>
#import "TennisGame.h"
@interface TennisGame2 : TennisGame
@end

View File

@ -1,147 +0,0 @@
#import "TennisGame2.h"
@interface TennisGame2 ()
@property(nonatomic, copy) NSString *player2Name;
@property(nonatomic, copy) NSString *player1Name;
@end
@implementation TennisGame2 {
int P1point;
NSString *P1res;
int P2point;
NSString *P2res;
}
- (id)initWithPlayer1:(NSString *)player1Name player2:(NSString *)player2Name {
self = [super initWithPlayer1:player1Name player2:player2Name];
if (self) {
self.player1Name = player1Name;
self.player2Name = player2Name;
}
return self;
}
- (NSString *)score {
NSString *score = @"";
if (P1point == P2point && P1point < 4)
{
if (P1point==0)
score = @"Love";
if (P1point==1)
score = @"Fifteen";
if (P1point==2)
score = @"Thirty";
if (P1point==3)
score = @"Forty";
score = [NSString stringWithFormat:@"%@-All", score];
}
if (P1point==P2point && P1point>3)
score = @"Deuce";
if (P1point > 0 && P2point==0)
{
if (P1point==1)
P1res = @"Fifteen";
if (P1point==2)
P1res = @"Thirty";
if (P1point==3)
P1res = @"Forty";
P2res = @"Love";
score = [NSString stringWithFormat:@"%@-%@", P1res, P2res];
}
if (P2point > 0 && P1point==0)
{
if (P2point==1)
P2res = @"Fifteen";
if (P2point==2)
P2res = @"Thirty";
if (P2point==3)
P2res = @"Forty";
P1res = @"Love";
score = [NSString stringWithFormat:@"%@-%@", P1res, P2res];
}
if (P1point>P2point && P1point < 4)
{
if (P1point==2)
P1res=@"Thirty";
if (P1point==3)
P1res=@"Forty";
if (P2point==1)
P2res=@"Fifteen";
if (P2point==2)
P2res=@"Thirty";
score = [NSString stringWithFormat:@"%@-%@", P1res, P2res];
}
if (P2point>P1point && P2point < 4)
{
if (P2point==2)
P2res=@"Thirty";
if (P2point==3)
P2res=@"Forty";
if (P1point==1)
P1res=@"Fifteen";
if (P1point==2)
P1res=@"Thirty";
score = [NSString stringWithFormat:@"%@-%@", P1res, P2res];
}
if (P1point > P2point && P2point >= 3)
{
score = @"Advantage player1";
}
if (P2point > P1point && P1point >= 3)
{
score = @"Advantage player2";
}
if (P1point>=4 && P2point>=0 && (P1point-P2point)>=2)
{
score = @"Win for player1";
}
if (P2point>=4 && P1point>=0 && (P2point-P1point)>=2)
{
score = @"Win for player2";
}
return score;
}
-(void)setP1Score:(int)number {
for (int i = 0; i < number; i++)
{
[self P1Score];
}
}
- (void)setP2Score:(int)number {
for (int i = 0; i < number; i++)
{
[self P2Score];
}
}
- (void)P1Score {
P1point++;
}
- (void)P2Score {
P2point++;
}
- (void)wonPoint:(NSString *)playerName {
if ([playerName isEqualToString:@"player1"])
[self P1Score];
else
[self P2Score];
}
@end

View File

@ -1,5 +0,0 @@
#import <Foundation/Foundation.h>
#import "TennisGame.h"
@interface TennisGame3 : TennisGame
@end

View File

@ -1,46 +0,0 @@
#import "TennisGame3.h"
@interface TennisGame3 ()
@end
@implementation TennisGame3 {
int p1;
int p2;
NSString *p1N;
NSString *p2N;
}
- (id)initWithPlayer1:(NSString *)ap1N player2:(NSString *)ap2N {
self = [super initWithPlayer1:ap1N player2:ap2N];
if (self) {
p1N = ap1N;
p2N = ap2N;
p1 = 0;
p2 = 0;
}
return self;
}
- (NSString *)score {
NSString *s;
if (p1 < 4 && p2 < 4) {
NSArray* p = @[@"Love", @"Fifteen", @"Thirty", @"Forty"];
s = p[p1];
return (p1 == p2) ? [NSString stringWithFormat:@"%@-All",s] : [NSString stringWithFormat:@"%@-%@",s,p[p2]];
} else {
if (p1 == p2)
return @"Deuce";
s = p1 > p2 ? p1N : p2N;
return ((p1-p2)*(p1-p2) == 1) ? [NSString stringWithFormat:@"Advantage %@",s] : [NSString stringWithFormat:@"Win for %@",s];
}
}
- (void)wonPoint:(NSString *)playerName {
if ([playerName isEqualToString:@"player1"])
p1 += 1;
else
p2 += 1;
}
@end

View File

@ -1,5 +0,0 @@
#import <SenTestingKit/SenTestingKit.h>
@interface TennisTests : SenTestCase
- (id)initWithInvocation:(NSInvocation *)invocation scores:(NSArray *)scores;
@end

View File

@ -1,121 +0,0 @@
#import <SenTestingKit/SenTestingKit.h>
#import "TennisTests.h"
#import "TennisGame1.h"
#import "TennisGame2.h"
#import "TennisGame3.h"
@interface TennisTests()
+ (NSArray*)parameters;
@end
@implementation TennisTests (Parametrized)
+ (id)defaultTestSuite {
SenTestSuite *testSuite = [[SenTestSuite alloc] initWithName:NSStringFromClass(self)];
NSArray *allScores = [self parameters];
for (NSArray *scores in allScores) {
[self addTestWithScores:scores toTestSuite:testSuite];
}
return testSuite;
}
+ (void)addTestWithScores:(NSArray *)scores toTestSuite:(SenTestSuite *)testSuite {
NSArray *testInvocations = [self testInvocations];
for (NSInvocation *testInvocation in testInvocations) {
SenTestCase *test = [[TennisTests alloc] initWithInvocation:testInvocation scores:scores];
[testSuite addTest:test];
}
}
@end
@implementation TennisTests {
int player1Score;
int player2Score;
NSString *expectedScore;
}
+ (NSArray*)parameters {
return @[
@[ @0, @0, @"Love-All"],
@[ @1, @1, @"Fifteen-All"],
@[ @2, @2, @"Thirty-All"],
@[ @3, @3, @"Forty-All"],
@[ @4, @4, @"Deuce"],
@[ @1, @0, @"Fifteen-Love"],
@[ @0, @1, @"Love-Fifteen"],
@[ @2, @0, @"Thirty-Love"],
@[ @0, @2, @"Love-Thirty"],
@[ @3, @0, @"Forty-Love"],
@[ @0, @3, @"Love-Forty"],
@[ @4, @0, @"Win for player1"],
@[ @0, @4, @"Win for player2"],
@[ @2, @1, @"Thirty-Fifteen"],
@[ @1, @2, @"Fifteen-Thirty"],
@[ @3, @1, @"Forty-Fifteen"],
@[ @1, @3, @"Fifteen-Forty"],
@[ @4, @1, @"Win for player1"],
@[ @1, @4, @"Win for player2"],
@[ @3, @2, @"Forty-Thirty"],
@[ @2, @3, @"Thirty-Forty"],
@[ @4, @2, @"Win for player1"],
@[ @2, @4, @"Win for player2"],
@[ @4, @3, @"Advantage player1"],
@[ @3, @4, @"Advantage player2"],
@[ @5, @4, @"Advantage player1"],
@[ @4, @5, @"Advantage player2"],
@[ @15, @14, @"Advantage player1"],
@[ @14, @15, @"Advantage player2"],
@[ @6, @4, @"Win for player1"],
@[ @4, @6, @"Win for player2"],
@[ @16, @14, @"Win for player1"],
@[ @14, @16, @"Win for player2"]
];
}
- (id)initWithInvocation:(NSInvocation *)invocation scores:(NSArray *)scores {
self = [super initWithInvocation:invocation];
if (self) {
player1Score = [scores[0] intValue];
player2Score = [scores[1] intValue];
expectedScore = scores[2];
}
return self;
}
- (NSString *)name {
NSString *parametersDescription = [NSString stringWithFormat:@" (%d,%d,%@)]", player1Score, player2Score, expectedScore];
return [[super name] stringByReplacingOccurrencesOfString:@"]" withString:parametersDescription];
}
- (void)checkAllScoresForGame:(TennisGame *)game {
int highestScore = MAX(player1Score, player2Score);
for (int i = 0; i < highestScore; i++) {
if (i < player1Score)
[game wonPoint:@"player1"];
if (i < player2Score)
[game wonPoint:@"player2"];
}
STAssertEqualObjects([game score], expectedScore, @"");
}
- (void)testAllScoresTennisGame1 {
TennisGame1 * game = [[TennisGame1 alloc] initWithPlayer1:@"player1" player2:@"player2"];
[self checkAllScoresForGame:game];
}
- (void)testAllScoresTennisGame2 {
TennisGame2 * game = [[TennisGame2 alloc] initWithPlayer1:@"player1" player2:@"player2"];
[self checkAllScoresForGame:game];
}
- (void)testAllScoresTennisGame3 {
TennisGame3 * game = [[TennisGame3 alloc] initWithPlayer1:@"player1" player2:@"player2"];
[self checkAllScoresForGame:game];
}
@end

View File

@ -1,175 +0,0 @@
# -*- coding: utf-8 -*-
class TennisGame1:
def __init__(self, player1Name, player2Name):
self.player1Name = player1Name
self.player2Name = player2Name
self.p1points = 0
self.p2points = 0
def won_point(self, playerName):
if playerName == self.player1Name:
self.p1points += 1
else:
self.p2points += 1
def score(self):
result = ""
tempScore=0
if (self.p1points==self.p2points):
result = {
0 : "Love-All",
1 : "Fifteen-All",
2 : "Thirty-All",
3 : "Forty-All",
}.get(self.p1points, "Deuce")
elif (self.p1points>=4 or self.p2points>=4):
minusResult = self.p1points-self.p2points
if (minusResult==1):
result ="Advantage " + self.player1Name
elif (minusResult ==-1):
result ="Advantage " + self.player2Name
elif (minusResult>=2):
result = "Win for " + self.player1Name
else:
result ="Win for " + self.player2Name
else:
for i in range(1,3):
if (i==1):
tempScore = self.p1points
else:
result+="-"
tempScore = self.p2points
result += {
0 : "Love",
1 : "Fifteen",
2 : "Thirty",
3 : "Forty",
}[tempScore]
return result
class TennisGame2:
def __init__(self, player1Name, player2Name):
self.player1Name = player1Name
self.player2Name = player2Name
self.p1points = 0
self.p2points = 0
def won_point(self, playerName):
if playerName == self.player1Name:
self.P1Score()
else:
self.P2Score()
def score(self):
result = ""
if (self.p1points == self.p2points and self.p1points < 4):
if (self.p1points==0):
result = "Love"
if (self.p1points==1):
result = "Fifteen"
if (self.p1points==2):
result = "Thirty"
if (self.p1points==3):
result = "Forty"
result += "-All"
if (self.p1points==self.p2points and self.p1points>3):
result = "Deuce"
P1res = ""
P2res = ""
if (self.p1points > 0 and self.p2points==0):
if (self.p1points==1):
P1res = "Fifteen"
if (self.p1points==2):
P1res = "Thirty"
if (self.p1points==3):
P1res = "Forty"
P2res = "Love"
result = P1res + "-" + P2res
if (self.p2points > 0 and self.p1points==0):
if (self.p2points==1):
P2res = "Fifteen"
if (self.p2points==2):
P2res = "Thirty"
if (self.p2points==3):
P2res = "Forty"
P1res = "Love"
result = P1res + "-" + P2res
if (self.p1points>self.p2points and self.p1points < 4):
if (self.p1points==2):
P1res="Thirty"
if (self.p1points==3):
P1res="Forty"
if (self.p2points==1):
P2res="Fifteen"
if (self.p2points==2):
P2res="Thirty"
result = P1res + "-" + P2res
if (self.p2points>self.p1points and self.p2points < 4):
if (self.p2points==2):
P2res="Thirty"
if (self.p2points==3):
P2res="Forty"
if (self.p1points==1):
P1res="Fifteen"
if (self.p1points==2):
P1res="Thirty"
result = P1res + "-" + P2res
if (self.p1points > self.p2points and self.p2points >= 3):
result = "Advantage " + self.player1Name
if (self.p2points > self.p1points and self.p1points >= 3):
result = "Advantage " + self.player2Name
if (self.p1points>=4 and self.p2points>=0 and (self.p1points-self.p2points)>=2):
result = "Win for " + self.player1Name
if (self.p2points>=4 and self.p1points>=0 and (self.p2points-self.p1points)>=2):
result = "Win for " + self.player2Name
return result
def SetP1Score(self, number):
for i in range(number):
self.P1Score()
def SetP2Score(self, number):
for i in range(number):
self.P2Score()
def P1Score(self):
self.p1points +=1
def P2Score(self):
self.p2points +=1
class TennisGame3:
def __init__(self, player1Name, player2Name):
self.p1N = player1Name
self.p2N = player2Name
self.p1 = 0
self.p2 = 0
def won_point(self, n):
if n == self.p1N:
self.p1 += 1
else:
self.p2 += 1
def score(self):
if (self.p1 < 4 and self.p2 < 4):
p = ["Love", "Fifteen", "Thirty", "Forty"]
s = p[self.p1]
return s + "-All" if (self.p1 == self.p2) else s + "-" + p[self.p2]
else:
if (self.p1 == self.p2):
return "Deuce"
s = self.p1N if self.p1 > self.p2 else self.p2N
return "Advantage " + s if ((self.p1-self.p2)*(self.p1-self.p2) == 1) else "Win for " + s

View File

@ -1,23 +0,0 @@
# -*- coding: utf-8 -*-
import pytest
from tennis import TennisGame1, TennisGame2, TennisGame3
from tennis_unittest import test_cases, play_game
class TestTennis:
@pytest.mark.parametrize('p1Points p2Points score p1Name p2Name'.split(), test_cases)
def test_get_score_game1(self, p1Points, p2Points, score, p1Name, p2Name):
game = play_game(TennisGame1, p1Points, p2Points, p1Name, p2Name)
assert score == game.score()
@pytest.mark.parametrize('p1Points p2Points score p1Name p2Name'.split(), test_cases)
def test_get_score_game2(self, p1Points, p2Points, score, p1Name, p2Name):
game = play_game(TennisGame2, p1Points, p2Points, p1Name, p2Name)
assert score == game.score()
@pytest.mark.parametrize('p1Points p2Points score p1Name p2Name'.split(), test_cases)
def test_get_score_game3(self, p1Points, p2Points, score, p1Name, p2Name):
game = play_game(TennisGame3, p1Points, p2Points, p1Name, p2Name)
assert score == game.score()

View File

@ -1,85 +0,0 @@
# -*- coding: utf-8 -*-
import unittest
from tennis import TennisGame1, TennisGame2, TennisGame3
test_cases = [
(0, 0, "Love-All", 'player1', 'player2'),
(1, 1, "Fifteen-All", 'player1', 'player2'),
(2, 2, "Thirty-All", 'player1', 'player2'),
(3, 3, "Forty-All", 'player1', 'player2'),
(4, 4, "Deuce", 'player1', 'player2'),
(1, 0, "Fifteen-Love", 'player1', 'player2'),
(0, 1, "Love-Fifteen", 'player1', 'player2'),
(2, 0, "Thirty-Love", 'player1', 'player2'),
(0, 2, "Love-Thirty", 'player1', 'player2'),
(3, 0, "Forty-Love", 'player1', 'player2'),
(0, 3, "Love-Forty", 'player1', 'player2'),
(4, 0, "Win for player1", 'player1', 'player2'),
(0, 4, "Win for player2", 'player1', 'player2'),
(2, 1, "Thirty-Fifteen", 'player1', 'player2'),
(1, 2, "Fifteen-Thirty", 'player1', 'player2'),
(3, 1, "Forty-Fifteen", 'player1', 'player2'),
(1, 3, "Fifteen-Forty", 'player1', 'player2'),
(4, 1, "Win for player1", 'player1', 'player2'),
(1, 4, "Win for player2", 'player1', 'player2'),
(3, 2, "Forty-Thirty", 'player1', 'player2'),
(2, 3, "Thirty-Forty", 'player1', 'player2'),
(4, 2, "Win for player1", 'player1', 'player2'),
(2, 4, "Win for player2", 'player1', 'player2'),
(4, 3, "Advantage player1", 'player1', 'player2'),
(3, 4, "Advantage player2", 'player1', 'player2'),
(5, 4, "Advantage player1", 'player1', 'player2'),
(4, 5, "Advantage player2", 'player1', 'player2'),
(15, 14, "Advantage player1", 'player1', 'player2'),
(14, 15, "Advantage player2", 'player1', 'player2'),
(6, 4, 'Win for player1', 'player1', 'player2'),
(4, 6, 'Win for player2', 'player1', 'player2'),
(16, 14, 'Win for player1', 'player1', 'player2'),
(14, 16, 'Win for player2', 'player1', 'player2'),
(6, 4, 'Win for One', 'One', 'player2'),
(4, 6, 'Win for Two', 'player1', 'Two'),
(6, 5, 'Advantage One', 'One', 'player2'),
(5, 6, 'Advantage Two', 'player1', 'Two'),
]
def play_game(TennisGame, p1Points, p2Points, p1Name, p2Name):
game = TennisGame(p1Name, p2Name)
for i in range(max(p1Points, p2Points)):
if i < p1Points:
game.won_point(p1Name)
if i < p2Points:
game.won_point(p2Name)
return game
class TestTennis(unittest.TestCase):
def test_Score_Game1(self):
for testcase in test_cases:
(p1Points, p2Points, score, p1Name, p2Name) = testcase
game = play_game(TennisGame1, p1Points, p2Points, p1Name, p2Name)
self.assertEquals(score, game.score())
def test_Score_Game2(self):
for testcase in test_cases:
(p1Points, p2Points, score, p1Name, p2Name) = testcase
game = play_game(TennisGame2, p1Points, p2Points, p1Name, p2Name)
self.assertEquals(score, game.score())
def test_Score_Game3(self):
for testcase in test_cases:
(p1Points, p2Points, score, p1Name, p2Name) = testcase
game = play_game(TennisGame3, p1Points, p2Points, p1Name, p2Name)
self.assertEquals(score, game.score())
if __name__ == "__main__":
unittest.main()

View File

@ -1,223 +0,0 @@
class TennisGame1
def initialize(player1Name, player2Name)
@player1Name = player1Name
@player2Name = player2Name
@p1points = 0
@p2points = 0
end
def won_point(playerName)
if playerName == @player1Name
@p1points += 1
else
@p2points += 1
end
end
def score
result = ""
tempScore=0
if (@p1points==@p2points)
result = {
0 => "Love-All",
1 => "Fifteen-All",
2 => "Thirty-All",
3 => "Forty-All",
}.fetch(@p1points, "Deuce")
elsif (@p1points>=4 or @p2points>=4)
minusResult = @p1points-@p2points
if (minusResult==1)
result ="Advantage " + @player1Name
elsif (minusResult ==-1)
result ="Advantage " + @player2Name
elsif (minusResult>=2)
result = "Win for " + @player1Name
else
result ="Win for " + @player2Name
end
else
(1...3).each do |i|
if (i==1)
tempScore = @p1points
else
result+="-"
tempScore = @p2points
end
result += {
0 => "Love",
1 => "Fifteen",
2 => "Thirty",
3 => "Forty",
}[tempScore]
end
end
result
end
end
class TennisGame2
def initialize(player1Name, player2Name)
@player1Name = player1Name
@player2Name = player2Name
@p1points = 0
@p2points = 0
end
def won_point(playerName)
if playerName == @player1Name
p1Score()
else
p2Score()
end
end
def score
result = ""
if (@p1points == @p2points and @p1points < 4)
if (@p1points==0)
result = "Love"
end
if (@p1points==1)
result = "Fifteen"
end
if (@p1points==2)
result = "Thirty"
end
if (@p1points==3)
result = "Forty"
end
result += "-All"
end
if (@p1points==@p2points and @p1points>3)
result = "Deuce"
end
p1res = ""
p2res = ""
if (@p1points > 0 and @p2points==0)
if (@p1points==1)
p1res = "Fifteen"
end
if (@p1points==2)
p1res = "Thirty"
end
if (@p1points==3)
p1res = "Forty"
end
p2res = "Love"
result = p1res + "-" + p2res
end
if (@p2points > 0 and @p1points==0)
if (@p2points==1)
p2res = "Fifteen"
end
if (@p2points==2)
p2res = "Thirty"
end
if (@p2points==3)
p2res = "Forty"
end
p1res = "Love"
result = p1res + "-" + p2res
end
if (@p1points>@p2points and @p1points < 4)
if (@p1points==2)
p1res="Thirty"
end
if (@p1points==3)
p1res="Forty"
end
if (@p2points==1)
p2res="Fifteen"
end
if (@p2points==2)
p2res="Thirty"
end
result = p1res + "-" + p2res
end
if (@p2points>@p1points and @p2points < 4)
if (@p2points==2)
p2res="Thirty"
end
if (@p2points==3)
p2res="Forty"
end
if (@p1points==1)
p1res="Fifteen"
end
if (@p1points==2)
p1res="Thirty"
end
result = p1res + "-" + p2res
end
if (@p1points > @p2points and @p2points >= 3)
result = "Advantage " + @player1Name
end
if (@p2points > @p1points and @p1points >= 3)
result = "Advantage " + @player2Name
end
if (@p1points>=4 and @p2points>=0 and (@p1points-@p2points)>=2)
result = "Win for " + @player1Name
end
if (@p2points>=4 and @p1points>=0 and (@p2points-@p1points)>=2)
result = "Win for " + @player2Name
end
result
end
def setp1Score(number)
(0..number).each do |i|
p1Score()
end
end
def setp2Score(number)
(0..number).each do |i|
p2Score()
end
end
def p1Score
@p1points +=1
end
def p2Score
@p2points +=1
end
end
class TennisGame3
def initialize(player1Name, player2Name)
@p1N = player1Name
@p2N = player2Name
@p1 = 0
@p2 = 0
end
def won_point(n)
if n == @p1N
@p1 += 1
else
@p2 += 1
end
end
def score
if (@p1 < 4 and @p2 < 4)
p = ["Love", "Fifteen", "Thirty", "Forty"]
s = p[@p1]
@p1 == @p2 ? s + "-All" : s + "-" + p[@p2]
else
if (@p1 == @p2)
"Deuce"
else
s = @p1 > @p2 ? @p1N : @p2N
(@p1-@p2)*(@p1-@p2) == 1 ? "Advantage " + s : "Win for " + s
end
end
end
end

View File

@ -1,86 +0,0 @@
require File.join(File.dirname(__FILE__), 'tennis')
#require_relative("tennis")
require 'test/unit'
TEST_CASES = [
[0, 0, "Love-All", 'player1', 'player2'],
[1, 1, "Fifteen-All", 'player1', 'player2'],
[2, 2, "Thirty-All", 'player1', 'player2'],
[3, 3, "Forty-All", 'player1', 'player2'],
[4, 4, "Deuce", 'player1', 'player2'],
[1, 0, "Fifteen-Love", 'player1', 'player2'],
[0, 1, "Love-Fifteen", 'player1', 'player2'],
[2, 0, "Thirty-Love", 'player1', 'player2'],
[0, 2, "Love-Thirty", 'player1', 'player2'],
[3, 0, "Forty-Love", 'player1', 'player2'],
[0, 3, "Love-Forty", 'player1', 'player2'],
[4, 0, "Win for player1", 'player1', 'player2'],
[0, 4, "Win for player2", 'player1', 'player2'],
[2, 1, "Thirty-Fifteen", 'player1', 'player2'],
[1, 2, "Fifteen-Thirty", 'player1', 'player2'],
[3, 1, "Forty-Fifteen", 'player1', 'player2'],
[1, 3, "Fifteen-Forty", 'player1', 'player2'],
[4, 1, "Win for player1", 'player1', 'player2'],
[1, 4, "Win for player2", 'player1', 'player2'],
[3, 2, "Forty-Thirty", 'player1', 'player2'],
[2, 3, "Thirty-Forty", 'player1', 'player2'],
[4, 2, "Win for player1", 'player1', 'player2'],
[2, 4, "Win for player2", 'player1', 'player2'],
[4, 3, "Advantage player1", 'player1', 'player2'],
[3, 4, "Advantage player2", 'player1', 'player2'],
[5, 4, "Advantage player1", 'player1', 'player2'],
[4, 5, "Advantage player2", 'player1', 'player2'],
[15, 14, "Advantage player1", 'player1', 'player2'],
[14, 15, "Advantage player2", 'player1', 'player2'],
[6, 4, 'Win for player1', 'player1', 'player2'],
[4, 6, 'Win for player2', 'player1', 'player2'],
[16, 14, 'Win for player1', 'player1', 'player2'],
[14, 16, 'Win for player2', 'player1', 'player2'],
[6, 4, 'Win for One', 'One', 'player2'],
[4, 6, 'Win for Two', 'player1', 'Two'],
[6, 5, 'Advantage One', 'One', 'player2'],
[5, 6, 'Advantage Two', 'player1', 'Two']
]
class TestTennis < Test::Unit::TestCase
def play_game(tennisGameClass, p1Points, p2Points, p1Name, p2Name)
game = tennisGameClass.new(p1Name, p2Name)
(0..[p1Points, p2Points].max).each do |i|
if i < p1Points
game.won_point(p1Name)
end
if i < p2Points
game.won_point(p2Name)
end
end
game
end
def test_Score_Game1
TEST_CASES.each do |testcase|
(p1Points, p2Points, score, p1Name, p2Name) = testcase
game = play_game(TennisGame1, p1Points, p2Points, p1Name, p2Name)
assert_equal(score, game.score())
end
end
def test_Score_Game2
TEST_CASES.each do |testcase|
(p1Points, p2Points, score, p1Name, p2Name) = testcase
game = play_game(TennisGame2, p1Points, p2Points, p1Name, p2Name)
assert_equal(score, game.score())
end
end
def test_Score_Game3
TEST_CASES.each do |testcase|
(p1Points, p2Points, score, p1Name, p2Name) = testcase
game = play_game(TennisGame3, p1Points, p2Points, p1Name, p2Name)
assert_equal(score, game.score())
end
end
end

View File

@ -1,116 +0,0 @@
# Yahtzee Refactoring Kata
This Refactoring Kata was designed by Jon Jagger and is available in his Cyber-Dojo. See [his blog post](http://jonjagger.blogspot.co.uk/2012/05/yahtzee-cyber-dojo-refactoring-in-java.html)
The other language translations have been contributed by:
Python: Emily Bache
Ruby: Kim Persson and Lennart Fridén
## Kata: Yahtzee rules
The game of yahtzee is a simple dice game. Each player
rolls five six-sided dice. They can re-roll some or all
of the dice up to three times (including the original roll).
For example, suppose a players rolls
3,4,5,5,2
They hold (-,-,5,5,-) and re-roll (3,4,-,-,2)
5,1,5,5,3
They hold (5,-,5,5,-) and re-roll (-,1,-,-,3)
5,6,5,5,2
The player then places the roll in a category, such as ones,
twos, fives, pair, two pairs etc (see below). If the roll is
compatible with the category, the player gets a score for the
roll according to the rules. If the roll is not compatible
with the category, the player scores zero for the roll.
For example, suppose a player scores 5,6,5,5,2 in the fives
category they would score 15 (three fives). The score for
that go is then added to their total and the category cannot
be used again in the remaining goes for that game.
A full game consists of one go for each category. Thus, for
their last go in a game, a player must choose their only
remaining category.
Your task is to score a GIVEN roll in a GIVEN category.
You do NOT have to program the random dice rolling.
The game is NOT played by letting the computer choose the
highest scoring category for a given roll.
## Kata: Yahzee Categories and Scoring Rules
_Note these rules differ from the original (copyrighted) rules_
Chance:
The player scores the sum of all dice,
no matter what they read.
For example,
1,1,3,3,6 placed on "chance" scores 14 (1+1+3+3+6)
4,5,5,6,1 placed on "chance" scores 21 (4+5+5+6+1)
Yahtzee:
If all dice have the same number,
the player scores 50 points.
For example,
1,1,1,1,1 placed on "yahtzee" scores 50
1,1,1,2,1 placed on "yahtzee" scores 0
Ones, Twos, Threes, Fours, Fives, Sixes:
The player scores the sum of the dice that reads one,
two, three, four, five or six, respectively.
For example,
1,1,2,4,4 placed on "fours" scores 8 (4+4)
2,3,2,5,1 placed on "twos" scores 4 (2+2)
3,3,3,4,5 placed on "ones" scores 0
Pair:
The player scores the sum of the two highest matching dice.
For example, when placed on "pair"
3,3,3,4,4 scores 8 (4+4)
1,1,6,2,6 scores 12 (6+6)
3,3,3,4,1 scores 0
3,3,3,3,1 scores 0
Two pairs:
If there are two pairs of dice with the same number, the
player scores the sum of these dice.
For example, when placed on "two pairs"
1,1,2,3,3 scores 8 (1+1+3+3)
1,1,2,3,4 scores 0
1,1,2,2,2 scores 0
Three of a kind:
If there are three dice with the same number, the player
scores the sum of these dice.
For example, when placed on "three of a kind"
3,3,3,4,5 scores 9 (3+3+3)
3,3,4,5,6 scores 0
3,3,3,3,1 scores 0
Four of a kind:
If there are four dice with the same number, the player
scores the sum of these dice.
For example, when placed on "four of a kind"
2,2,2,2,5 scores 8 (2+2+2+2)
2,2,2,5,5 scores 0
2,2,2,2,2 scores 0
Small straight:
When placed on "small straight", if the dice read
1,2,3,4,5, the player scores 15 (the sum of all the dice.
Large straight:
When placed on "large straight", if the dice read
2,3,4,5,6, the player scores 20 (the sum of all the dice).
Full house:
If the dice are two of a kind and three of a kind, the
player scores the sum of all the dice.
For example, when placed on "full house"
1,1,2,2,2 scores 8 (1+1+2+2+2)
2,2,3,3,4 scores 0
4,4,4,4,4 scores 0

View File

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

Binary file not shown.

View File

@ -1,251 +0,0 @@
#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;
}

View File

@ -1,33 +0,0 @@
#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

@ -1,153 +0,0 @@
#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;
}

View File

@ -1,238 +0,0 @@
public class Yahtzee {
public static int 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;
}
public static int yahtzee(params int[] dice)
{
int[] counts = new int[6];
foreach (int die in dice)
counts[die-1]++;
for (int i = 0; i != 6; i++)
if (counts[i] == 5)
return 50;
return 0;
}
public static int 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;
}
public static int 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;
}
public static int 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;
}
protected int[] dice;
public 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;
}
public int Fours()
{
int sum;
sum = 0;
for (int at = 0; at != 5; at++) {
if (dice[at] == 4) {
sum += 4;
}
}
return sum;
}
public int Fives()
{
int s = 0;
int i;
for (i = 0; i < dice.Length; i++)
if (dice[i] == 5)
s = s + 5;
return s;
}
public int sixes()
{
int sum = 0;
for (int at = 0; at < dice.Length; at++)
if (dice[at] == 6)
sum = sum + 6;
return sum;
}
public static int ScorePair(int d1, int d2, int d3, int d4, int d5)
{
int[] counts = new int[6];
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;
}
public static int TwoPair(int d1, int d2, int d3, int d4, int d5)
{
int[] counts = new int[6];
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;
}
public static int FourOfAKind(int _1, int _2, int d3, int d4, int d5)
{
int[] tallies;
tallies = new int[6];
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;
}
public static int ThreeOfAKind(int d1, int d2, int d3, int d4, int d5)
{
int[] t;
t = new int[6];
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;
}
public static int SmallStraight(int d1, int d2, int d3, int d4, int d5)
{
int[] tallies;
tallies = new 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;
}
public static int LargeStraight(int d1, int d2, int d3, int d4, int d5)
{
int[] tallies;
tallies = new int[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;
}
public static int 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];
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;
}
}

View File

@ -1,124 +0,0 @@
using NUnit.Framework;
[TestFixture]
public class UntitledTest
{
[Test]
public void Chance_scores_sum_of_all_dice()
{
int expected = 15;
int actual = Yahtzee.Chance(2,3,4,5,1);
Assert.AreEqual(expected, actual);
Assert.AreEqual(16, Yahtzee.Chance(3,3,4,5,1));
}
[Test]
public void Yahtzee_scores_50()
{
int expected = 50;
int actual = Yahtzee.yahtzee(4,4,4,4,4);
Assert.AreEqual(expected, actual);
Assert.AreEqual(50, Yahtzee.yahtzee(6,6,6,6,6));
Assert.AreEqual(0, Yahtzee.yahtzee(6,6,6,6,3));
}
[Test]
public void Test_1s() {
Assert.IsTrue(Yahtzee.Ones(1,2,3,4,5) == 1);
Assert.AreEqual(2, Yahtzee.Ones(1,2,1,4,5));
Assert.AreEqual(0, Yahtzee.Ones(6,2,2,4,5));
Assert.AreEqual(4, Yahtzee.Ones(1,2,1,1,1));
}
[Test]
public void test_2s()
{
Assert.AreEqual(4, Yahtzee.Twos(1,2,3,2,6));
Assert.AreEqual(10, Yahtzee.Twos(2,2,2,2,2));
}
[Test]
public void test_threes()
{
Assert.AreEqual(6, Yahtzee.Threes(1,2,3,2,3));
Assert.AreEqual(12, Yahtzee.Threes(2,3,3,3,3));
}
[Test]
public void fours_test()
{
Assert.AreEqual(12, new Yahtzee(4,4,4,5,5).Fours());
Assert.AreEqual(8, new Yahtzee(4,4,5,5,5).Fours());
Assert.AreEqual(4, new Yahtzee(4,5,5,5,5).Fours());
}
[Test]
public void fives() {
Assert.AreEqual(10, new Yahtzee(4,4,4,5,5).Fives());
Assert.AreEqual(15, new Yahtzee(4,4,5,5,5).Fives());
Assert.AreEqual(20, new Yahtzee(4,5,5,5,5).Fives());
}
[Test]
public void sixes_test()
{
Assert.AreEqual(0, new Yahtzee(4,4,4,5,5).sixes());
Assert.AreEqual(6, new Yahtzee(4,4,6,5,5).sixes());
Assert.AreEqual(18, new Yahtzee(6,5,6,6,5).sixes());
}
[Test]
public void one_pair()
{
Assert.AreEqual(6, Yahtzee.ScorePair(3,4,3,5,6));
Assert.AreEqual(10, Yahtzee.ScorePair(5,3,3,3,5));
Assert.AreEqual(12, Yahtzee.ScorePair(5,3,6,6,5));
}
[Test]
public void two_Pair()
{
Assert.AreEqual(16, Yahtzee.TwoPair(3,3,5,4,5));
Assert.AreEqual(0, Yahtzee.TwoPair(3,3,5,5,5));
}
[Test]
public void three_of_a_kind()
{
Assert.AreEqual(9, Yahtzee.ThreeOfAKind(3,3,3,4,5));
Assert.AreEqual(15, Yahtzee.ThreeOfAKind(5,3,5,4,5));
Assert.AreEqual(0, Yahtzee.ThreeOfAKind(3,3,3,3,5));
}
[Test]
public void four_of_a_knd()
{
Assert.AreEqual(12, Yahtzee.FourOfAKind(3,3,3,3,5));
Assert.AreEqual(20, Yahtzee.FourOfAKind(5,5,5,4,5));
Assert.AreEqual(0, Yahtzee.FourOfAKind(3,3,3,3,3));
}
[Test]
public void smallStraight()
{
Assert.AreEqual(15, Yahtzee.SmallStraight(1,2,3,4,5));
Assert.AreEqual(15, Yahtzee.SmallStraight(2,3,4,5,1));
Assert.AreEqual(0, Yahtzee.SmallStraight(1,2,2,4,5));
}
[Test]
public void largeStraight()
{
Assert.AreEqual(20, Yahtzee.LargeStraight(6,2,3,4,5));
Assert.AreEqual(20, Yahtzee.LargeStraight(2,3,4,5,6));
Assert.AreEqual(0, Yahtzee.LargeStraight(1,2,2,4,5));
}
[Test]
public void fullHouse()
{
Assert.AreEqual(18, Yahtzee.FullHouse(6,2,2,2,6));
Assert.AreEqual(0, Yahtzee.FullHouse(2,3,4,5,6));
}
}

View File

@ -1,241 +0,0 @@
public class Yahtzee {
public static int 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;
}
public static int yahtzee(int... dice)
{
int[] counts = new int[6];
for (int die : dice)
counts[die-1]++;
for (int i = 0; i != 6; i++)
if (counts[i] == 5)
return 50;
return 0;
}
public static int 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;
}
public static int 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;
}
public static int 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;
}
protected int[] dice;
public 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;
}
public int fours()
{
int sum;
sum = 0;
for (int at = 0; at != 5; at++) {
if (dice[at] == 4) {
sum += 4;
}
}
return sum;
}
public int fives()
{
int s = 0;
int i;
for (i = 0; i < dice.length; i++)
if (dice[i] == 5)
s = s + 5;
return s;
}
public int sixes()
{
int sum = 0;
for (int at = 0; at < dice.length; at++)
if (dice[at] == 6)
sum = sum + 6;
return sum;
}
public static int score_pair(int d1, int d2, int d3, int d4, int d5)
{
int[] counts = new int[6];
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;
}
public static int two_pair(int d1, int d2, int d3, int d4, int d5)
{
int[] counts = new int[6];
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;
}
public static int four_of_a_kind(int _1, int _2, int d3, int d4, int d5)
{
int[] tallies;
tallies = new int[6];
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;
}
public static int three_of_a_kind(int d1, int d2, int d3, int d4, int d5)
{
int[] t;
t = new int[6];
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;
}
public static int smallStraight(int d1, int d2, int d3, int d4, int d5)
{
int[] tallies;
tallies = new 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;
}
public static int largeStraight(int d1, int d2, int d3, int d4, int d5)
{
int[] tallies;
tallies = new int[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;
}
public static int fullHouse(int d1, int d2, int d3, int d4, int d5)
{
int[] tallies;
boolean _2 = false;
int i;
int _2_at = 0;
boolean _3 = false;
int _3_at = 0;
tallies = new 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;
}
}

View File

@ -1,110 +0,0 @@
import org.junit.*;
import static org.junit.Assert.*;
public class YahtzeeTest {
@Test
public void chance_scores_sum_of_all_dice() {
int expected = 15;
int actual = Yahtzee.chance(2,3,4,5,1);
assertEquals(expected, actual);
assertEquals(16, Yahtzee.chance(3,3,4,5,1));
}
@Test public void yahtzee_scores_50() {
int expected = 50;
int actual = Yahtzee.yahtzee(4,4,4,4,4);
assertEquals(expected, actual);
assertEquals(50, Yahtzee.yahtzee(6,6,6,6,6));
assertEquals(0, Yahtzee.yahtzee(6,6,6,6,3));
}
@Test public void test_1s() {
assertTrue(Yahtzee.ones(1,2,3,4,5) == 1);
assertEquals(2, Yahtzee.ones(1,2,1,4,5));
assertEquals(0, Yahtzee.ones(6,2,2,4,5));
assertEquals(4, Yahtzee.ones(1,2,1,1,1));
}
@Test
public void test_2s() {
assertEquals(4, Yahtzee.twos(1,2,3,2,6));
assertEquals(10, Yahtzee.twos(2,2,2,2,2));
}
@Test
public void test_threes() {
assertEquals(6, Yahtzee.threes(1,2,3,2,3));
assertEquals(12, Yahtzee.threes(2,3,3,3,3));
}
@Test
public void fours_test()
{
assertEquals(12, new Yahtzee(4,4,4,5,5).fours());
assertEquals(8, new Yahtzee(4,4,5,5,5).fours());
assertEquals(4, new Yahtzee(4,5,5,5,5).fours());
}
@Test
public void fives() {
assertEquals(10, new Yahtzee(4,4,4,5,5).fives());
assertEquals(15, new Yahtzee(4,4,5,5,5).fives());
assertEquals(20, new Yahtzee(4,5,5,5,5).fives());
}
@Test
public void sixes_test() {
assertEquals(0, new Yahtzee(4,4,4,5,5).sixes());
assertEquals(6, new Yahtzee(4,4,6,5,5).sixes());
assertEquals(18, new Yahtzee(6,5,6,6,5).sixes());
}
@Test
public void one_pair() {
assertEquals(6, Yahtzee.score_pair(3,4,3,5,6));
assertEquals(10, Yahtzee.score_pair(5,3,3,3,5));
assertEquals(12, Yahtzee.score_pair(5,3,6,6,5));
}
@Test
public void two_Pair() {
assertEquals(16, Yahtzee.two_pair(3,3,5,4,5));
assertEquals(0, Yahtzee.two_pair(3,3,5,5,5));
}
@Test
public void three_of_a_kind()
{
assertEquals(9, Yahtzee.three_of_a_kind(3,3,3,4,5));
assertEquals(15, Yahtzee.three_of_a_kind(5,3,5,4,5));
assertEquals(0, Yahtzee.three_of_a_kind(3,3,3,3,5));
}
@Test
public void four_of_a_knd() {
assertEquals(12, Yahtzee.four_of_a_kind(3,3,3,3,5));
assertEquals(20, Yahtzee.four_of_a_kind(5,5,5,4,5));
assertEquals(0, Yahtzee.three_of_a_kind(3,3,3,3,3));
}
@Test
public void smallStraight() {
assertEquals(15, Yahtzee.smallStraight(1,2,3,4,5));
assertEquals(15, Yahtzee.smallStraight(2,3,4,5,1));
assertEquals(0, Yahtzee.smallStraight(1,2,2,4,5));
}
@Test
public void largeStraight() {
assertEquals(20, Yahtzee.largeStraight(6,2,3,4,5));
assertEquals(20, Yahtzee.largeStraight(2,3,4,5,6));
assertEquals(0, Yahtzee.largeStraight(1,2,2,4,5));
}
@Test
public void fullHouse() {
assertEquals(18, Yahtzee.fullHouse(6,2,2,2,6));
assertEquals(0, Yahtzee.fullHouse(2,3,4,5,6));
}
}

View File

@ -1,94 +0,0 @@
from yahtzee import Yahtzee
# These unit tests can be run using the py.test framework
# available from http://pytest.org/
def test_chance_scores_sum_of_all_dice():
expected = 15
actual = Yahtzee.chance(2,3,4,5,1)
assert expected == actual
assert 16 == Yahtzee.chance(3,3,4,5,1)
def test_yahtzee_scores_50():
expected = 50
actual = Yahtzee.yahtzee([4,4,4,4,4])
assert expected == actual
assert 50 == Yahtzee.yahtzee([6,6,6,6,6])
assert 0 == Yahtzee.yahtzee([6,6,6,6,3])
def 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)
def test_2s():
assert 4 == Yahtzee.twos(1,2,3,2,6)
assert 10 == Yahtzee.twos(2,2,2,2,2)
def test_threes():
assert 6 == Yahtzee.threes(1,2,3,2,3)
assert 12 == Yahtzee.threes(2,3,3,3,3)
def test_fours_test():
assert 12 == Yahtzee(4,4,4,5,5).fours()
assert 8 == Yahtzee(4,4,5,5,5).fours()
assert 4 == Yahtzee(4,5,5,5,5).fours()
def test_fives():
assert 10 == 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()
def test_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()
def test_one_pair():
assert 6 == Yahtzee.score_pair(3,4,3,5,6)
assert 10 == Yahtzee.score_pair(5,3,3,3,5)
assert 12 == Yahtzee.score_pair(5,3,6,6,5)
def test_two_Pair():
assert 16 == Yahtzee.two_pair(3,3,5,4,5)
assert 0 == Yahtzee.two_pair(3,3,5,5,5)
def test_three_of_a_kind():
assert 9 == Yahtzee.three_of_a_kind(3,3,3,4,5)
assert 15 == Yahtzee.three_of_a_kind(5,3,5,4,5)
assert 0 == Yahtzee.three_of_a_kind(3,3,3,3,5)
def test_four_of_a_knd():
assert 12 == Yahtzee.four_of_a_kind(3,3,3,3,5)
assert 20 == Yahtzee.four_of_a_kind(5,5,5,4,5)
assert 0 == Yahtzee.three_of_a_kind(3,3,3,3,3)
def test_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)
def test_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)
def test_fullHouse():
assert 18 == Yahtzee.fullHouse(6,2,2,2,6)
assert 0 == Yahtzee.fullHouse(2,3,4,5,6)

View File

@ -1,230 +0,0 @@
class Yahtzee:
@staticmethod
def chance(d1, d2, d3, d4, d5):
total = 0
total += d1
total += d2
total += d3
total += d4
total += d5
return total
@staticmethod
def yahtzee(dice):
counts = [0]*(len(dice)+1)
for die in dice:
counts[die-1] += 1
for i in range(len(counts)):
if counts[i] == 5:
return 50
return 0
@staticmethod
def ones( d1, d2, d3, d4, d5):
sum = 0
if (d1 == 1):
sum += 1
if (d2 == 1):
sum += 1
if (d3 == 1):
sum += 1
if (d4 == 1):
sum += 1
if (d5 == 1):
sum += 1
return sum
@staticmethod
def twos( d1, d2, d3, d4, d5):
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
@staticmethod
def threes( d1, d2, d3, d4, d5):
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
def __init__(self, d1, d2, d3, d4, _5):
self.dice = [0]*5
self.dice[0] = d1
self.dice[1] = d2
self.dice[2] = d3
self.dice[3] = d4
self.dice[4] = _5
def fours(self):
sum = 0
for at in range(5):
if (self.dice[at] == 4):
sum += 4
return sum
def fives(self):
s = 0
i = 0
for i in range(len(self.dice)):
if (self.dice[i] == 5):
s = s + 5
return s
def sixes(self):
sum = 0
for at in range(len(self.dice)):
if (self.dice[at] == 6):
sum = sum + 6
return sum
@staticmethod
def score_pair( d1, d2, d3, d4, d5):
counts = [0]*6
counts[d1-1] += 1
counts[d2-1] += 1
counts[d3-1] += 1
counts[d4-1] += 1
counts[d5-1] += 1
at = 0
for at in range(6):
if (counts[6-at-1] == 2):
return (6-at)*2
return 0
@staticmethod
def two_pair( d1, d2, d3, d4, d5):
counts = [0]*6
counts[d1-1] += 1
counts[d2-1] += 1
counts[d3-1] += 1
counts[d4-1] += 1
counts[d5-1] += 1
n = 0
score = 0
for i in range(6):
if (counts[6-i-1] == 2):
n = n+1
score += (6-i)
if (n == 2):
return score * 2
else:
return 0
@staticmethod
def four_of_a_kind( _1, _2, d3, d4, d5):
tallies = [0]*6
tallies[_1-1] += 1
tallies[_2-1] += 1
tallies[d3-1] += 1
tallies[d4-1] += 1
tallies[d5-1] += 1
for i in range(6):
if (tallies[i] == 4):
return (i+1) * 4
return 0
@staticmethod
def three_of_a_kind( d1, d2, d3, d4, d5):
t = [0]*6
t[d1-1] += 1
t[d2-1] += 1
t[d3-1] += 1
t[d4-1] += 1
t[d5-1] += 1
for i in range(6):
if (t[i] == 3):
return (i+1) * 3
return 0
@staticmethod
def smallStraight( d1, d2, d3, d4, d5):
tallies = [0]*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 and
tallies[1] == 1 and
tallies[2] == 1 and
tallies[3] == 1 and
tallies[4] == 1):
return 15
return 0
@staticmethod
def largeStraight( d1, d2, d3, d4, d5):
tallies = [0]*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 and
tallies[2] == 1 and
tallies[3] == 1 and
tallies[4] == 1
and tallies[5] == 1):
return 20
return 0
@staticmethod
def fullHouse( d1, d2, d3, d4, d5):
tallies = []
_2 = False
i = 0
_2_at = 0
_3 = False
_3_at = 0
tallies = [0]*6
tallies[d1-1] += 1
tallies[d2-1] += 1
tallies[d3-1] += 1
tallies[d4-1] += 1
tallies[d5-1] += 1
for i in range(6):
if (tallies[i] == 2):
_2 = True
_2_at = i+1
for i in range(6):
if (tallies[i] == 3):
_3 = True
_3_at = i+1
if (_2 and _3):
return _2_at * 2 + _3_at * 3
else:
return 0

View File

@ -1,94 +0,0 @@
require_relative 'yahtzee'
require 'test/unit'
class YahtzeeTest < Test::Unit::TestCase
def test_chance_scores_sum_of_all_dice
expected = 15
actual = Yahtzee.chance(2,3,4,5,1)
assert expected == actual
assert 16 == Yahtzee.chance(3,3,4,5,1)
end
def test_yahtzee_scores_50
expected = 50
actual = Yahtzee.yahtzee([4,4,4,4,4])
assert expected == actual
assert 50 == Yahtzee.yahtzee([6,6,6,6,6])
assert 0 == Yahtzee.yahtzee([6,6,6,6,3])
end
def 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)
end
def test_2s
assert Yahtzee.twos(1,2,3,2,6) == 4
assert Yahtzee.twos(2,2,2,2,2) == 10
end
def test_threes
assert 6 == Yahtzee.threes(1,2,3,2,3)
assert 12 == Yahtzee.threes(2,3,3,3,3)
end
def test_fours_test
assert 12 == Yahtzee.new(4,4,4,5,5).fours
assert 8 == Yahtzee.new(4,4,5,5,5).fours
assert 4 == Yahtzee.new(4,5,5,5,5).fours
end
def test_fives()
assert 10 == Yahtzee.new(4,4,4,5,5).fives()
assert 15 == Yahtzee.new(4,4,5,5,5).fives()
assert 20 == Yahtzee.new(4,5,5,5,5).fives()
end
def test_sixes_test
assert 0 == Yahtzee.new(4,4,4,5,5).sixes()
assert 6 == Yahtzee.new(4,4,6,5,5).sixes()
assert 18 == Yahtzee.new(6,5,6,6,5).sixes()
end
def test_one_pair
assert 6 == Yahtzee.score_pair(3,4,3,5,6)
assert 10 == Yahtzee.score_pair(5,3,3,3,5)
assert 12 == Yahtzee.score_pair(5,3,6,6,5)
end
def test_two_Pair
assert_equal 16, Yahtzee.two_pair(3,3,5,4,5)
assert_equal 0, Yahtzee.two_pair(3,3,5,5,5)
end
def test_three_of_a_kind()
assert 9 == Yahtzee.three_of_a_kind(3,3,3,4,5)
assert 15 == Yahtzee.three_of_a_kind(5,3,5,4,5)
assert 0 == Yahtzee.three_of_a_kind(3,3,3,3,5)
end
def test_four_of_a_knd
assert 12 == Yahtzee.four_of_a_kind(3,3,3,3,5)
assert 20 == Yahtzee.four_of_a_kind(5,5,5,4,5)
assert 0 == Yahtzee.three_of_a_kind(3,3,3,3,3)
end
def test_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)
end
def test_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)
end
def test_fullHouse()
assert 18 == Yahtzee.fullHouse(6,2,2,2,6)
assert 0 == Yahtzee.fullHouse(2,3,4,5,6)
end
end

View File

@ -1,256 +0,0 @@
class Yahtzee
def self.chance(d1, d2, d3, d4, d5)
total = 0
total += d1
total += d2
total += d3
total += d4
total += d5
return total
end
def self.yahtzee(dice)
counts = [0]*(dice.length+1)
for die in dice do
counts[die-1] += 1
end
for i in 0..counts.size do
if counts[i] == 5
return 50
end
end
return 0
end
def self.ones( d1, d2, d3, d4, d5)
sum = 0
if (d1 == 1)
sum += 1
end
if (d2 == 1)
sum += 1
end
if (d3 == 1)
sum += 1
end
if (d4 == 1)
sum += 1
end
if (d5 == 1)
sum += 1
end
sum
end
def self.twos( d1, d2, d3, d4, d5)
sum = 0
if (d1 == 2)
sum += 2
end
if (d2 == 2)
sum += 2
end
if (d3 == 2)
sum += 2
end
if (d4 == 2)
sum += 2
end
if (d5 == 2)
sum += 2
end
return sum
end
def self.threes( d1, d2, d3, d4, d5)
s = 0
if (d1 == 3)
s += 3
end
if (d2 == 3)
s += 3
end
if (d3 == 3)
s += 3
end
if (d4 == 3)
s += 3
end
if (d5 == 3)
s += 3
end
return s
end
def initialize(d1, d2, d3, d4, _5)
@dice = [0]*5
@dice[0] = d1
@dice[1] = d2
@dice[2] = d3
@dice[3] = d4
@dice[4] = _5
end
def fours
sum = 0
for at in Array 0..4
if (@dice[at] == 4)
sum += 4
end
end
return sum
end
def fives()
s = 0
i = 0
for i in (Range.new(0, @dice.size))
if (@dice[i] == 5)
s = s + 5
end
end
s
end
def sixes
sum = 0
for at in 0..@dice.length
if (@dice[at] == 6)
sum = sum + 6
end
end
return sum
end
def self.score_pair( d1, d2, d3, d4, d5)
counts = [0]*6
counts[d1-1] += 1
counts[d2-1] += 1
counts[d3-1] += 1
counts[d4-1] += 1
counts[d5-1] += 1
at = 0
(0...6).each do |at|
if (counts[6-at-1] == 2)
return (6-at)*2
end
end
return 0
end
def self.two_pair( d1, d2, d3, d4, d5)
counts = [0]*6
counts[d1-1] += 1
counts[d2-1] += 1
counts[d3-1] += 1
counts[d4-1] += 1
counts[d5-1] += 1
n = 0
score = 0
for i in Array 0..5
if (counts[6-i-1] == 2)
n = n+1
score += (6-i)
end
end
if (n == 2)
return score * 2
else
return 0
end
end
def self.four_of_a_kind( _1, _2, d3, d4, d5)
tallies = [0]*6
tallies[_1-1] += 1
tallies[_2-1] += 1
tallies[d3-1] += 1
tallies[d4-1] += 1
tallies[d5-1] += 1
for i in (0..6)
if (tallies[i] == 4)
return (i+1) * 4
end
end
return 0
end
def self.three_of_a_kind( d1, d2, d3, d4, d5)
t = [0]*6
t[d1-1] += 1
t[d2-1] += 1
t[d3-1] += 1
t[d4-1] += 1
t[d5-1] += 1
for i in [0,1,2,3,4,5]
if (t[i] == 3)
return (i+1) * 3
end
end
0
end
def self.smallStraight( d1, d2, d3, d4, d5)
tallies = [0]*6
tallies[d1-1] += 1
tallies[d2-1] += 1
tallies[d3-1] += 1
tallies[d4-1] += 1
tallies[d5-1] += 1
(tallies[0] == 1 and
tallies[1] == 1 and
tallies[2] == 1 and
tallies[3] == 1 and
tallies[4] == 1) ? 15 : 0
end
def self.largeStraight( d1, d2, d3, d4, d5)
tallies = [0]*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 and tallies[2] == 1 and tallies[3] == 1 and tallies[4] == 1 and tallies[5] == 1)
return 20
end
return 0
end
def self.fullHouse( d1, d2, d3, d4, d5)
tallies = []
_2 = false
i = 0
_2_at = 0
_3 = false
_3_at = 0
tallies = [0]*6
tallies[d1-1] += 1
tallies[d2-1] += 1
tallies[d3-1] += 1
tallies[d4-1] += 1
tallies[d5-1] += 1
for i in Array 0..5
if (tallies[i] == 2)
_2 = true
_2_at = i+1
end
end
for i in Array 0..5
if (tallies[i] == 3)
_3 = true
_3_at = i+1
end
end
if (_2 and _3)
return _2_at * 2 + _3_at * 3
else
return 0
end
end
end

View File

Before

Width:  |  Height:  |  Size: 905 B

After

Width:  |  Height:  |  Size: 905 B

Some files were not shown because too many files have changed in this diff Show More