Game class - lajkonikz/town-country-river GitHub Wiki

Game Class

In order to start a game:

let game = new Game();
game.addPlayer('Andersona');
game.addPlayer('Raquela');
game.addPlayer('Armanda');

You also can enchain the calls:

let game = new Game()
      .addPlayer('Andersona')
      .addPlayer('Raquela')
      .addPlayer('Armanda')
      .setCategories(['color', 'country', 'fruit'])
      .excludeLetters(['a', 'k']);

excludeLetters should receive an array and will exclude these letters from the game.

By default, one letter is chosen by the whole game. If you want to shuffle one different letter per round, use the setPickLetterBehaviour method, as below:

let game = new Game()
      .addPlayer('Anderson')
      .addPlayer('Raquela')
      .addPlayer('Armanda')
      .setCategories(['color', 'country', 'fruit'])
      .excludeLetters(['a', 'k'])
      .setPickLetterBehaviour('EVERY_ROUND')
      .start();

Once all settings are done. Use the start method().

At any moment, use game.currentState to check the currentState. You will receive the output below:

{ category: 'color', player: 'Andersona', letter: 'q', roundId: 2 }

If you need to check the round, use: game.getRound(2) and you will have it:

{                                                 
  round_id: 2,                                    
  category: 'color',                              
  letter: 'v',                                    
  round_players: [                                
    { name: 'Armanda', scored: false },           
    { name: 'Raquela', scored: false },           
    { name: 'Andersona', scored: false }          
  ]                                               
}                                                 

If you need to score a point to a player, use the score method. As below:

game.score(roundId, playerName, points, word);
// Examples:
// game.score(1, 'Armanda', 5, 'brazil');
// game.score(game.currentState.roundId, game.currentState.player, 10, 'brazil');

If for testing purposes you need to use repeated words in a row, disable the word check as:

game.notValidateRecordedWords();

In order to generate a json with the whole data use the method dumps:

game.dumps(); // and you will receive a string with the whole game

In order to load it, you should use the class constructor. As below:

let game = new Game(myJson);
// and you have your whole game again.

After several successful score calls you should reach the FINISHED state. Check using game.state. game.state can have three different states, as follows: NOT_STARTED, STARTED, FINISHED. In your to check the variables of the current state, use game.currentState. When the state is FINISHED, game.currentState is equal null and surely game.state is FINISHED.

At any moment, you can check the scoreboard using the method getScoreboard, as follows:

game.getScoreboard();

which you will receive an ordered list with names and points. As follows:

[                                                      
  { name: 'Andersona', points: 10 },                   
  { name: 'Raquela', points: 5 },                      
  { name: 'Armanda', points: 5 }                       
]                                                      

Check also the game unit test: https://github.com/lajkonikz/town-country-river/blob/miani-add-game/lambda/test/game_test.js