Skip to content

Commit

Permalink
Add dart version
Browse files Browse the repository at this point in the history
  • Loading branch information
Muldec committed Sep 5, 2019
1 parent 72f6a8f commit 69ec44f
Show file tree
Hide file tree
Showing 7 changed files with 369 additions and 0 deletions.
10 changes: 10 additions & 0 deletions dart/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
bin/
.packages
pubspec.lock

# IntelliJ files
*.iml
.idea/

# VS Code files
.vscode/
4 changes: 4 additions & 0 deletions dart/lib/TennisGame.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
abstract class TennisGame {
void wonPoint(String playerName);
String getScore();
}
80 changes: 80 additions & 0 deletions dart/lib/TennisGame1.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

import 'dart:core';

import 'TennisGame.dart';

class TennisGame1 implements TennisGame {

int _m_score1 = 0;
int _m_score2 = 0;
String _player1Name;
String _player2Name;

TennisGame1(String player1Name, String player2Name) {
this._player1Name = player1Name;
this._player2Name = player2Name;
}

void wonPoint(String playerName) {
if (playerName == "player1")
_m_score1 += 1;
else
_m_score2 += 1;
}

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;
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;
}
}
139 changes: 139 additions & 0 deletions dart/lib/TennisGame2.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@

import 'dart:core';

import 'TennisGame.dart';

class TennisGame2 implements TennisGame
{
int P1point = 0;
int P2point = 0;

String P1res = "";
String P2res = "";
String _player1Name;
String _player2Name;

TennisGame2(String player1Name, String player2Name) {
this._player1Name = player1Name;
this._player2Name = player2Name;
}

String getScore(){
String score = "";
if (P1point == P2point && P1point < 4)
{
if (P1point==0)
score = "Love";
if (P1point==1)
score = "Fifteen";
if (P1point==2)
score = "Thirty";
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;
}

void SetP1Score(int number){

for (int i = 0; i < number; i++)
{
P1Score();
}

}

void SetP2Score(int number){

for (int i = 0; i < number; i++)
{
P2Score();
}

}

void P1Score(){
P1point++;
}

void P2Score(){
P2point++;
}

void wonPoint(String player) {
if (player == "player1")
P1Score();
else
P2Score();
}
}
40 changes: 40 additions & 0 deletions dart/lib/TennisGame3.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

import 'dart:core';

import 'TennisGame.dart';

class TennisGame3 implements TennisGame {

int _p2 = 0;
int _p1 = 0;
String _p1N;
String _p2N;

TennisGame3(String p1N, String p2N) {
this._p1N = p1N;
this._p2N = p2N;
}

String getScore() {
String s;
if (_p1 < 4 && _p2 < 4 && !(_p1 + _p2 == 6)) {
List<String> p = ["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;
}
}

void wonPoint(String playerName) {
if (playerName == "player1")
this._p1 += 1;
else
this._p2 += 1;

}

}
6 changes: 6 additions & 0 deletions dart/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: tennis_game
version: 0.0.1
description: A simple console application.

dev_dependencies:
test: any
90 changes: 90 additions & 0 deletions dart/test/tennis_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import 'dart:math';

import 'package:test/test.dart';

import '../lib/TennisGame.dart';
import '../lib/TennisGame1.dart';
import '../lib/TennisGame2.dart';
import '../lib/TennisGame3.dart';

main() {
var allScores = [
TennisTest(0, 0, "Love-All"),
TennisTest(1, 1, "Fifteen-All"),
TennisTest(2, 2, "Thirty-All"),
TennisTest(3, 3, "Deuce"),
TennisTest(4, 4, "Deuce"),
TennisTest(1, 0, "Fifteen-Love"),
TennisTest(0, 1, "Love-Fifteen"),
TennisTest(2, 0, "Thirty-Love"),
TennisTest(0, 2, "Love-Thirty"),
TennisTest(3, 0, "Forty-Love"),
TennisTest(0, 3, "Love-Forty"),
TennisTest(4, 0, "Win for player1"),
TennisTest(0, 4, "Win for player2"),
TennisTest(2, 1, "Thirty-Fifteen"),
TennisTest(1, 2, "Fifteen-Thirty"),
TennisTest(3, 1, "Forty-Fifteen"),
TennisTest(1, 3, "Fifteen-Forty"),
TennisTest(4, 1, "Win for player1"),
TennisTest(1, 4, "Win for player2"),
TennisTest(3, 2, "Forty-Thirty"),
TennisTest(2, 3, "Thirty-Forty"),
TennisTest(4, 2, "Win for player1"),
TennisTest(2, 4, "Win for player2"),
TennisTest(4, 3, "Advantage player1"),
TennisTest(3, 4, "Advantage player2"),
TennisTest(5, 4, "Advantage player1"),
TennisTest(4, 5, "Advantage player2"),
TennisTest(15, 14, "Advantage player1"),
TennisTest(14, 15, "Advantage player2"),
TennisTest(6, 4, "Win for player1"),
TennisTest(4, 6, "Win for player2"),
TennisTest(16, 14, "Win for player1"),
TennisTest(14, 16, "Win for player2"),
];

void checkAllScores(TennisGame game, TennisTest test) {
int highestScore = max(test.player1Score, test.player2Score);
for (int i = 0; i < highestScore; i++) {
if (i < test.player1Score) game.wonPoint("player1");
if (i < test.player2Score) game.wonPoint("player2");
}
expect(game.getScore(), test.expectedScore);
}

group('checkAllScoresTennisGame1', () {
allScores.forEach((tested) {
test('${tested.player1Score} - ${tested.player2Score} - ${tested.expectedScore}', () {
TennisGame1 game = new TennisGame1("player1", "player2");
checkAllScores(game, tested);
});
});
});

group('checkAllScoresTennisGame2', () {
allScores.forEach((tested) {
test('${tested.player1Score} - ${tested.player2Score} - ${tested.expectedScore}', () {
TennisGame2 game = new TennisGame2("player1", "player2");
checkAllScores(game, tested);
});
});
});

group('checkAllScoresTennisGame3', () {
allScores.forEach((tested) {
test('${tested.player1Score} - ${tested.player2Score} - ${tested.expectedScore}', () {
TennisGame3 game = new TennisGame3("player1", "player2");
checkAllScores(game, tested);
});
});
});
}

class TennisTest {
int player1Score;
int player2Score;
String expectedScore;

TennisTest(this.player1Score, this.player2Score, this.expectedScore) {}
}

0 comments on commit 69ec44f

Please sign in to comment.