I am doing a small project, it involves coding the card game pontoon.
I have most things sorted for the player 1 and player 2 hands however when it is the computers turn to play things don't quite go as planned.
It always shows the computer as the winner.
I am not sure whether the problem is occurring from the IF statement in the computergo state, or whether it is with the scoring system in the player 1 and player 2 sections.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
score1 = 0;
int numberCards = player1->getNumberCardsInHand();
for(int n = 0; n<numberCards;n++)
score1 = score1 + GetFaceValue(player1->getCardAtPosition(n)->getRank());
// IF SCORE = 21 - SHOW PONTOON MESSAGE + STATE = player2reveal
if (score1 == 21)
{
player1score=21;
state = player2reveal;
}
// IF SCORE > 21 - SHOW BUST MESSAGE + STATE = player2reveal
if (score1 > 21)
{
state=player2reveal;
player1score=0;
}
// IF SCORE IS NEITHER - PLAYER 1 GO
if (score1 < 21)
{
state = player1go;
}
}
if (s == 's' && state == player1go)
{
// ADD SCORE TO PLAYER SCORE
player1score=player1score+score1;
// CHANGE STATE
state = player2reveal;
}
|
As you can see I have a function to work out the value of the cards (score1) however for the end of the game game where I decide who wins I needed an additional score which is 0 if the player went bust, 21 if the player got pontoon and so on.
I need this because if set the (value of the cards) score1; to be 0 on a bust, the game gets stuck between states and won't move onto to the player 2 state.
So I added an int called player1score; I want this to contain the value of the cards (unless bust) the player has in his hand, so it can be compared to the computers score later on.
I am having trouble determining whether the code I have here is wrong, or whether it is within the other section.
These if statements at the end are to work out who the winner is, at the moment it always comes up saying "Computer Wins!" which most of the time isn't true.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
if (state == computergo)
{
// GET COMPUTER SCORE
score3 = 0;
int numberCards = dealer->getNumberCardsInHand();
for(int n = 0; n < numberCards;n++)
score3 = score3 + GetFaceValue(dealer->getCardAtPosition(n)->getRank());
// IF LESS THAN 21 AND LOWER THAN PLAYER1 + PLAYER2
if (score3 < 21 && score3 < player1score)
{
deck.deal_n_cards_to_hand(*dealer, 1, true);
}
// IF SCORE IS HIGHER THAN PLAYER ONE + PLAYER TWO AND LOWER 21
if (score3 > player1score && score3 > player2score && score3 < 21)
{
// SHOW MESSAGE BANKER WINS
board.add(&computerwinMsg[PLAY_MESSAGE_INDEX],CWINX,CWINY);
// CHANGE STATE
state = endgame;
}
// IF SCORE IS HIGHER THAN PLAYER 1 +NOT PLAYER 2 + UNDER 21
if (score3 > player1score && score3 < player2score && score3 < 21)
{
// DEAL ANOTHER CARD
deck.deal_n_cards_to_hand(*dealer, 1, true);
// CHANGE STATE
state = computergo;
}
// IF SCORE IS LOWER THAN PLAYER 1 +NOT PLAYER 2 + UNDER 21
if (score3 < player1score && score3 > player2score && score3 < 21)
{
// DEAL ANOTHER CARD
deck.deal_n_cards_to_hand(*dealer, 1, true);
// CHANGE STATE
state = computergo;
}
// IF SCORE IS LOWER THAN PLAYER 1 + PLAYER 2 + UNDER 21
if (score3 < player1score && score3 < player2score && score3 < 21)
{
// DEAL ANOTHER CARD
deck.deal_n_cards_to_hand(*dealer, 1, true);
// CHANGE STATE
state = computergo;
}
// IF SCORE IS OVER 21 + PLAYER 1 WINS
if (score3 > 21 && player1score > player2score)
{
// MESSAGE PLAYER1 WINS
board.add(&player1winMsg[PLAY_MESSAGE_INDEX],ONEWINX,ONEWINY);
// CHANGE STATE
state=endgame;
}
// IF SCORE IS OVER 21 + PLAYER 2 WINS
if (score3 > 21 && player1score < player2score)
{
// MESSAGE PLAYER2 WINS
board.add(&player2winMsg[PLAY_MESSAGE_INDEX],TWOWINX,TWOWINY);
// CHANGE STATE
state=endgame;
}
|
As you can see I use the player1score to compare with the computer score, to show the appropriate message.
I was wondering if anyone could help shed light on my situation! Perhaps where exactly the problem is happening and some steps I can take!
Thanks for your time!