I am making a poker application in JavaFX. When I try to display the cards on the screen it is working perfectly fine. Yet I have a method with a lot of duplicated code in it.
I have tried to compress the code into 1 function with a for loop but I couldn't get it to work. Anyone has an idea how to get rid of the duplication?
public class GameController implements Initializable {
@FXML
private Canvas canvasBoard;
@FXML
private Canvas canvasPlayer1;
@FXML
private Canvas canvasPlayer2;
@FXML
private Canvas canvasPlayer3;
@FXML
private Canvas canvasPlayer4;
private int playerCardsWidth1 = 0;
private int playerCardsWidth2 = 0;
private int playerCardsWidth3 = 0;
private int playerCardsWidth4 = 0;
private int dealerCardsWidth = 0;
private TexasHoldem game;
private IPlayer board;
public GameController() {
try {
game = new TexasHoldem();
} catch (Exception e) {
e.printStackTrace();
}
}
private void showCardsBoard() {
board = game.getBoard();
dealerCardsWidth += 50;
for (ICard c : board.getCards()) {
String foto = c.getFilename();
Image image = new Image(foto);
GraphicsContext gcBoard = canvasBoard.getGraphicsContext2D();
int dealerCardsHeight = 0;
gcBoard.drawImage(image,dealerCardsWidth, dealerCardsHeight,60,80);
dealerCardsWidth += 75;
}
}
private void showCardsPlayers() {
List<IPlayer> spelers = game.getPlayers();
for (int i = 0; i < spelers.size(); i++) {
if (i == 0) {
IPlayer player1 = spelers.get(i);
for (ICard c : player1.getCards()) {
Image image = new Image(c.getFilename());
GraphicsContext gc1 = canvasPlayer1.getGraphicsContext2D();
int playerCardsHeight1 = 0;
gc1.drawImage(image, playerCardsWidth1, playerCardsHeight1, 60, 80);
playerCardsWidth1 += 75;
}
}
if (i == 1) {
IPlayer player2 = spelers.get(i);
for (ICard c : player2.getCards()) {
Image image = new Image(c.getFilename());
GraphicsContext gc2 = canvasPlayer2.getGraphicsContext2D();
int playerCardsHeight2 = 0;
gc2.drawImage(image, playerCardsWidth2, playerCardsHeight2, 60, 80);
playerCardsWidth2 += 75;
}
}
if (i == 2) {
IPlayer player3 = spelers.get(i);
for (ICard c : player3.getCards()) {
Image image = new Image(c.getFilename());
GraphicsContext gc3 = canvasPlayer3.getGraphicsContext2D();
int playerCardsHeight3 = 0;
gc3.drawImage(image, playerCardsWidth3, playerCardsHeight3, 60, 80);
playerCardsWidth3 += 75;
}
}
if (i == 3) {
IPlayer player4 = spelers.get(i);
for (ICard c : player4.getCards()) {
Image image = new Image(c.getFilename());
GraphicsContext gc4 = canvasPlayer4.getGraphicsContext2D();
int playerCardsHeight4 = 0;
gc4.drawImage(image, playerCardsWidth4, playerCardsHeight4, 60, 80);
playerCardsWidth4 += 75;
}
}
}
}