forked from jbrains/trivia
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request jbrains#22 from marcpmichel/master
D (v2.0) version added
- Loading branch information
Showing
4 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
compile with dub | ||
================ | ||
|
||
1. install dub : [from here](http://code.dlang.org/download) | ||
2. compile & run with : | ||
dub run | ||
|
||
|
||
compile with dmd | ||
================ | ||
|
||
dmd source/trivia.d source/game.d | ||
|
||
and run : | ||
./trivia | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "trivia", | ||
"description": "Legacy Code Retreat - Trivia Game - D2 version", | ||
"copyright": "none", | ||
"authors": ["marcpmichel"], | ||
"dependencies": { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
|
||
module game; | ||
|
||
import std.stdio; | ||
import std.conv; | ||
|
||
class Game { | ||
string[] players; | ||
int[6] places; | ||
int[6] purses; | ||
bool[6] inPenaltyBox; | ||
|
||
string[] popQuestions; | ||
string[] scienceQuestions; | ||
string[] sportsQuestions; | ||
string[] rockQuestions; | ||
|
||
int currentPlayer = 0; | ||
bool isGettingOutOfPenaltyBox; | ||
|
||
this() { | ||
for (int i = 0; i < 50; i++) { | ||
popQuestions ~= "Pop Question " ~ to!string(i); | ||
scienceQuestions ~= "Science Question " ~ to!string(i); | ||
sportsQuestions ~= "Sports Question " ~ to!string(i); | ||
rockQuestions ~= createRockQuestion(i); | ||
} | ||
} | ||
|
||
string createRockQuestion(int index){ | ||
return "Rock Question " ~ to!string(index); | ||
} | ||
|
||
bool isPlayable() { | ||
return (howManyPlayers() >= 2); | ||
} | ||
|
||
bool add(string playerName) { | ||
|
||
players ~= playerName; | ||
places[howManyPlayers()] = 0; | ||
purses[howManyPlayers()] = 0; | ||
inPenaltyBox[howManyPlayers()] = false; | ||
|
||
writeln(playerName, " was added"); | ||
writeln("They are player number ", players.length); | ||
return true; | ||
} | ||
|
||
int howManyPlayers() { | ||
return cast(int)players.length; | ||
} | ||
|
||
void roll(int roll) { | ||
writeln(players[currentPlayer], " is the current player"); | ||
writefln("They have rolled a %d", roll); | ||
|
||
if (inPenaltyBox[currentPlayer]) { | ||
if (roll % 2 != 0) { | ||
isGettingOutOfPenaltyBox = true; | ||
|
||
writeln(players[currentPlayer], " is getting out of the penalty box"); | ||
places[currentPlayer] = places[currentPlayer] + roll; | ||
if (places[currentPlayer] > 11) places[currentPlayer] = places[currentPlayer] - 12; | ||
|
||
writeln(players[currentPlayer], | ||
"'s new location is ", | ||
places[currentPlayer]); | ||
writeln("The category is ", currentCategory()); | ||
askQuestion(); | ||
} else { | ||
writeln(players[currentPlayer], " is not getting out of the penalty box"); | ||
isGettingOutOfPenaltyBox = false; | ||
} | ||
|
||
} else { | ||
|
||
places[currentPlayer] = places[currentPlayer] + roll; | ||
if (places[currentPlayer] > 11) places[currentPlayer] = places[currentPlayer] - 12; | ||
|
||
writeln(players[currentPlayer], | ||
"'s new location is ", | ||
places[currentPlayer]); | ||
writeln("The category is " ~ currentCategory()); | ||
askQuestion(); | ||
} | ||
|
||
} | ||
|
||
private void askQuestion() { | ||
if (currentCategory() == "Pop") { | ||
writeln(popQuestions[0]); | ||
popQuestions = popQuestions[1..$]; | ||
} | ||
if (currentCategory() == "Science" ) { | ||
writeln( "*** ", currentCategory," has ", scienceQuestions.length, "questions left"); | ||
writeln(scienceQuestions[0]); | ||
scienceQuestions = scienceQuestions[1..$]; | ||
} | ||
if (currentCategory() == "Sports") { | ||
writeln(sportsQuestions[0]); | ||
sportsQuestions = sportsQuestions[1..$]; | ||
} | ||
if (currentCategory() == "Rock") { | ||
writeln(rockQuestions[0]); | ||
rockQuestions = rockQuestions[1..$]; | ||
} | ||
} | ||
|
||
|
||
private string currentCategory() { | ||
if (places[currentPlayer] == 0) return "Pop"; | ||
if (places[currentPlayer] == 4) return "Pop"; | ||
if (places[currentPlayer] == 8) return "Pop"; | ||
if (places[currentPlayer] == 1) return "Science"; | ||
if (places[currentPlayer] == 5) return "Science"; | ||
if (places[currentPlayer] == 9) return "Science"; | ||
if (places[currentPlayer] == 2) return "Sports"; | ||
if (places[currentPlayer] == 6) return "Sports"; | ||
if (places[currentPlayer] == 10) return "Sports"; | ||
return "Rock"; | ||
} | ||
|
||
bool wasCorrectlyAnswered() { | ||
if (inPenaltyBox[currentPlayer]){ | ||
if (isGettingOutOfPenaltyBox) { | ||
writeln("Answer was correct!!!!"); | ||
purses[currentPlayer]++; | ||
writeln(players[currentPlayer], | ||
" now has ", | ||
purses[currentPlayer], | ||
" Gold Coins."); | ||
|
||
bool winner = didPlayerWin(); | ||
currentPlayer++; | ||
if (currentPlayer == players.length) currentPlayer = 0; | ||
|
||
return winner; | ||
} else { | ||
currentPlayer++; | ||
if (currentPlayer == players.length) currentPlayer = 0; | ||
return true; | ||
} | ||
|
||
|
||
|
||
} else { | ||
|
||
writeln("Answer was corrent!!!!"); | ||
purses[currentPlayer] += 1; | ||
writeln(players[currentPlayer], | ||
" now has ", | ||
purses[currentPlayer], | ||
" Gold Coins."); | ||
|
||
bool winner = didPlayerWin(); | ||
currentPlayer++; | ||
if (currentPlayer == players.length) currentPlayer = 0; | ||
|
||
return winner; | ||
} | ||
} | ||
|
||
bool wrongAnswer(){ | ||
writeln("Question was incorrectly answered"); | ||
writeln(players[currentPlayer], " was sent to the penalty box"); | ||
inPenaltyBox[currentPlayer] = true; | ||
|
||
currentPlayer++; | ||
if (currentPlayer == players.length) currentPlayer = 0; | ||
return true; | ||
} | ||
|
||
|
||
private bool didPlayerWin() { | ||
return !(purses[currentPlayer] == 6); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
import game; | ||
import std.random; | ||
|
||
bool notAWinner; | ||
|
||
void main() { | ||
auto game = new Game(); | ||
|
||
game.add("Chet"); | ||
game.add("Pat"); | ||
game.add("Sue"); | ||
|
||
auto rand = new Random(); | ||
rand.seed(unpredictableSeed); | ||
|
||
do { | ||
|
||
game.roll(rand.front() % 5 + 1); | ||
rand.popFront(); | ||
|
||
if (rand.front() % 9 == 7) { | ||
notAWinner = game.wrongAnswer(); | ||
} else { | ||
notAWinner = game.wasCorrectlyAnswered(); | ||
} | ||
rand.popFront(); | ||
|
||
} while (notAWinner); | ||
|
||
} |