Skip to content

Commit

Permalink
Merge pull request #1 from sdenier/master
Browse files Browse the repository at this point in the history
Add Smalltalk code for Trivia game
  • Loading branch information
Sébastien Nicouleaud committed Nov 10, 2011
2 parents 91d89ed + a73a028 commit 2357ea6
Showing 1 changed file with 1 addition and 0 deletions.
1 change: 1 addition & 0 deletions smalltalk/Game.cs
@@ -0,0 +1 @@
'From Pharo1.3 of 16 June 2011 [Latest update: #13315] on 4 November 2011 at 2:19:20 pm'!Object subclass: #Game instanceVariableNames: 'players places purses inPenaltyBox currentPlayer popQuestions scienceQuestions sportsQuestions rockQuestions isGettingOutOfPenaltyBox' classVariableNames: 'notAWinner' poolDictionaries: '' category: 'Trivia'!!Game methodsFor: 'as yet unclassified' stamp: 'simondenier 11/4/2011 12:25'!add: playerName players add: playerName. places at: self howManyPlayers put: 0. purses at: self howManyPlayers put: 0. inPenaltyBox at: self howManyPlayers put: false. Transcript show: playerName , ' was added' ; cr. Transcript show: 'They are player number ' , players size asString ; cr.^ true! !!Game methodsFor: 'as yet unclassified' stamp: 'simondenier 11/4/2011 13:54'!askQuestion self currentCategory = 'Pop' ifTrue: [ Transcript show: popQuestions removeFirst ; cr ]. self currentCategory = 'Science' ifTrue: [ Transcript show: scienceQuestions removeFirst ; cr ]. self currentCategory = 'Sports' ifTrue: [ Transcript show: sportsQuestions removeFirst ; cr ]. self currentCategory = 'Rock' ifTrue: [ Transcript show: rockQuestions removeFirst ; cr ].! !!Game methodsFor: 'as yet unclassified' stamp: 'simondenier 11/4/2011 12:27'!createRockQuestion: index ^ 'Rock Question ' , index asString! !!Game methodsFor: 'as yet unclassified' stamp: 'simondenier 11/4/2011 13:53'!currentCategory (places at: currentPlayer) = 0 ifTrue: [ ^ 'Pop' ]. (places at: currentPlayer) = 4 ifTrue: [ ^ 'Pop' ]. (places at: currentPlayer) = 8 ifTrue: [ ^ 'Pop' ]. (places at: currentPlayer) = 1 ifTrue: [ ^ 'Science' ]. (places at: currentPlayer) = 5 ifTrue: [ ^ 'Science' ]. (places at: currentPlayer) = 9 ifTrue: [ ^ 'Science' ]. (places at: currentPlayer) = 2 ifTrue: [ ^ 'Sports' ]. (places at: currentPlayer) = 6 ifTrue: [ ^ 'Sports' ]. (places at: currentPlayer) = 10 ifTrue: [ ^ 'Sports' ]. ^ 'Rock'! !!Game methodsFor: 'as yet unclassified' stamp: 'simondenier 11/4/2011 14:09'!didPlayerWin ^ ((purses at: currentPlayer) = 6) not ! !!Game methodsFor: 'as yet unclassified' stamp: 'simondenier 11/3/2011 23:45'!howManyPlayers ^ players size! !!Game methodsFor: 'as yet unclassified' stamp: 'simondenier 11/4/2011 14:13'!initialize players := OrderedCollection new. places := Array new: 6 withAll: 0. purses := Array new: 6 withAll: 0. inPenaltyBox := Array new: 6 withAll: false. currentPlayer := 1. popQuestions := LinkedList new. scienceQuestions := LinkedList new. sportsQuestions := LinkedList new. rockQuestions := LinkedList new. 1 to: 50 do: [ :i | popQuestions addLast: 'Pop Question ' , i asString. scienceQuestions addLast: 'Science Question ' , i asString. sportsQuestions addLast: 'Sports Question ' , i asString. rockQuestions addLast: (self createRockQuestion: i) ]! !!Game methodsFor: 'as yet unclassified' stamp: 'simondenier 11/4/2011 12:24'!isPlayable ^ (self howManyPlayers >= 2)! !!Game methodsFor: 'as yet unclassified' stamp: 'simondenier 11/4/2011 14:14'!roll: roll Transcript show: (players at: currentPlayer) , ' is the current player' ; cr. Transcript show: 'They have rolled a ' , roll asString ; cr. ( inPenaltyBox at: currentPlayer ) ifTrue: [ roll \\ 2 ~= 0 ifTrue: [ isGettingOutOfPenaltyBox := true. Transcript show: (players at: currentPlayer) , ' is getting out of the penalty box' ; cr. places at: currentPlayer put: (places at: currentPlayer) + roll. (places at: currentPlayer) > 11 ifTrue: [ places at: currentPlayer put: (places at: currentPlayer) - 12 ]. Transcript show: (players at: currentPlayer) , '''s new location is ' , (places at: currentPlayer) asString ; cr. Transcript show: 'The category is ' , self currentCategory ; cr. self askQuestion ] ifFalse: [ Transcript show: (players at: currentPlayer) , ' is not getting out of the penalty box' ; cr. isGettingOutOfPenaltyBox := false. ] ] ifFalse: [ places at: currentPlayer put: (places at: currentPlayer) + roll. (places at: currentPlayer) > 11 ifTrue: [ places at: currentPlayer put: (places at: currentPlayer) - 12 ]. Transcript show: (players at: currentPlayer) , '''s new location is ' , (places at: currentPlayer) asString ; cr. Transcript show: 'The category is ' , self currentCategory ; cr. self askQuestion ]! !!Game methodsFor: 'as yet unclassified' stamp: 'simondenier 11/4/2011 14:11'!wasCorrectlyAnswered | winner | (inPenaltyBox at: currentPlayer) ifTrue: [ isGettingOutOfPenaltyBox ifTrue: [ Transcript show: 'Answer was correct!!!!!!!!' ; cr. purses at: currentPlayer put: (purses at: currentPlayer) + 1. Transcript show: (players at: currentPlayer) , ' now has ' , (purses at: currentPlayer) asString , ' Gold Coins.' ; cr. winner := self didPlayerWin. currentPlayer := currentPlayer + 1. currentPlayer = players size ifTrue: [ currentPlayer := 1 ]. ^ winner ] ifFalse: [ currentPlayer := currentPlayer + 1. currentPlayer = players size ifTrue: [ currentPlayer := 1 ]. ^ true ] ] ifFalse: [ Transcript show: 'Answer was correct!!!!!!!!' ; cr. purses at: currentPlayer put: (purses at: currentPlayer) + 1. Transcript show: (players at: currentPlayer) , ' now has ' , (purses at: currentPlayer) asString , ' Gold Coins.' ; cr. winner := self didPlayerWin. currentPlayer := currentPlayer + 1. currentPlayer = players size ifTrue: [ currentPlayer := 1 ]. ^ winner ]! !!Game methodsFor: 'as yet unclassified' stamp: 'simondenier 11/4/2011 14:12'!wrongAnswer Transcript show: 'Question was incorrectly answered' ; cr. Transcript show: (players at: currentPlayer), ' was sent to the penalty box' ; cr. inPenaltyBox at: currentPlayer put: true. currentPlayer := currentPlayer + 1. currentPlayer = players size ifTrue: [ currentPlayer := 1 ]. ^ true! !!Game class methodsFor: 'as yet unclassified' stamp: 'simondenier 11/4/2011 14:14'!run "Game run" | game rand | Transcript clear. notAWinner := false. game := Game new. game add: 'Chet'. game add: 'Pat'. game add: 'Sue'. rand := Random new. [ game roll: ((rand nextInt: 5) + 1). ((rand nextInt: 9) = 7) ifTrue: [ notAWinner := game wrongAnswer. ] ifFalse: [ notAWinner := game wasCorrectlyAnswered. ] ] doWhileTrue: [ notAWinner ]! !Game removeSelector: #popo!!Game reorganize!('as yet unclassified' add: askQuestion createRockQuestion: currentCategory didPlayerWin howManyPlayers initialize isPlayable roll: wasCorrectlyAnswered wrongAnswer)!
Expand Down

0 comments on commit 2357ea6

Please sign in to comment.