patternjavascriptMinor
Simple Poker Game
Viewed 0 times
pokergamesimple
Problem
I'm building a simple Poker game. It is almost complete, but I want to refactor it early.
My code is mainly about game-play, determine if ready to start next round, determine whose turn it is, etc. I want to make this code more readable, easy to maintain, and not too difficult to understand for other programmers. Any suggestions?
index.js (usage)
Results in console:
`Player A added to the game
Player B added to the game
Player C added to the game
Game reset
========== STARTING GAME ==========
Player A gets card : As & 4c
Player B gets card : Th & 2s
Player C gets card : 5c & 7s
Player A is the dealer
Player B pays small blind : 1000
Player C pays big blind : 2000
Now its player A's turn
========== Round DEAL ==========
Player A CALL : 2000
cannot begin next round
Player B CALL : 1000
cannot begin next round
Player C Raises : 2000
cannot begin next round
Player A Raises : 4000
cannot begin next round
Player
- To calculate hand strength / ranks, I use this library from NPM: poker-evaluator
- For deck shuffling, I use the Fisher-Yates algorithm
My code is mainly about game-play, determine if ready to start next round, determine whose turn it is, etc. I want to make this code more readable, easy to maintain, and not too difficult to understand for other programmers. Any suggestions?
index.js (usage)
var Game = require('./src/game');
var game = new Game();
game.addPlayer({
name: "A",
chips: 40000
});
game.addPlayer({
name: "B",
chips: 20000
});
game.addPlayer({
name: "C",
chips: 20000
});
game.start();
game.getCurrentPlayer().callOrCheck(); // A
game.getCurrentPlayer().callOrCheck(); // B
game.getCurrentPlayer().raise(2000); // C
game.getCurrentPlayer().raise(2000); // A
game.getCurrentPlayer().fold(); // B
game.getCurrentPlayer().callOrCheck(); // C
game.getCurrentPlayer().callOrCheck(); // A
game.getCurrentPlayer().raise(1000); // C
game.getCurrentPlayer().callOrCheck(); // A
game.getCurrentPlayer().callOrCheck(); // C
game.getCurrentPlayer().raise(3000); // A
game.getCurrentPlayer().callOrCheck(); // C
game.getCurrentPlayer().callOrCheck(); // A
game.getCurrentPlayer().callOrCheck(); // CResults in console:
`Player A added to the game
Player B added to the game
Player C added to the game
Game reset
========== STARTING GAME ==========
Player A gets card : As & 4c
Player B gets card : Th & 2s
Player C gets card : 5c & 7s
Player A is the dealer
Player B pays small blind : 1000
Player C pays big blind : 2000
Now its player A's turn
========== Round DEAL ==========
Player A CALL : 2000
cannot begin next round
Player B CALL : 1000
cannot begin next round
Player C Raises : 2000
cannot begin next round
Player A Raises : 4000
cannot begin next round
Player
Solution
From a once over:
-
In
-
You should use a common logger / output module instead of copy pasting in each module
-
This might be overkill, but read up on Model View Controller, you are mixing output and logic so much that you might regret it at some point
-
Consider using a library like lodash, it has a built-in function to shuffle arrays.
Other than that I like your code, I could not find much of any problem.
-
In
index.js you keep using game.getCurrentPlayer()., since all those activities can only be done by the current player I feel the caller should just be able to just call game.callOrCheck(); and callOrCheck then figures out who the current player is.-
You should use a common logger / output module instead of copy pasting in each module
-
This might be overkill, but read up on Model View Controller, you are mixing output and logic so much that you might regret it at some point
-
Consider using a library like lodash, it has a built-in function to shuffle arrays.
Other than that I like your code, I could not find much of any problem.
Context
StackExchange Code Review Q#71630, answer score: 5
Revisions (0)
No revisions yet.