reset password
Author Message
Eric Liao
Posts: 158
Posted 22:34 Mar 06, 2014 |

I just write an email to one of the students to clarify on what you have to do for the AI_Template, I think it would be helpful for others to know as well.

Here is the information I wrote to the student, if you found yourself struggling writing the AI_Template.java. Please use the hint wisely.

In the psudocode, I write that you can create a helper method to help you to accomplish the calculateMoney method. [check below for pseudocode]

You will notice that you have to write another method called calculateBid that takes 4 parameters
Each parameter represents:
1. first_move > one move/bid from the first player
2. second_move > one move/bid from the second player
3. draw > who is going to win the bidding if tie happens
4. first_money > how much money first player has currently
5. second_money > how much money second player has currently
 
What you have to do in this method is similar to the doMoves method you implemented earlier in the part 1 of the project. You need to compare the first_move and second_move to see who win the bid and use it to deduct money from first_money or second_money accordingly.
I would highly recommend you implementing a test case for this calculateBid method so that you know you implement this method correctly.
 
After finishing the calculateBid method as above, what you have to do in the calculateMoney is:
int first_money = 100;
int second_money = 100;
int draw = 1;
for (int i = 0; i < first_moves.length; i ++) {
    calculateMoney( first_moves[i], second_moves[i], draw, first_money, second_money );
}
// after the line above, you still need to assign the result to hold the correct number
// for instance, if you are playing as player1 result[0] is equals to first_money and result[1] is equals to second_money
// on the other hand, if you are playing as player2, result[0] should hold second_money, result[1] should hold first_money
// and result[2] should hold draw
Eric Liao
Posts: 158
Posted 22:39 Mar 06, 2014 |

If you are still confusing on what to write after reading the description in the post, feel free to ask question. I will stay online until 12. I will respond as quick as I can if you have question and post on the forum.

lakerfan94
Posts: 143
Posted 17:16 Mar 08, 2014 |

The pseudocode that you used, isn't that for the calculateMoney method? I'm still confused about what we have to do for calculateBid and how it differs from calculateMoney.

lakerfan94
Posts: 143
Posted 17:29 Mar 08, 2014 |
"After finishing the calculateBid method as above, what you have to do in the calculateMoney is:
int first_money = 100;
int second_money = 100;
int draw = 1;
for (int i = 0; i < first_moves.length; i ++) {
    calculateMoney( first_moves[i], second_moves[i], draw, first_money, second_money );"
 
In calculateMoney, you mean call the calculateBid method right?
(i.e. calculateBid(first_moves[i], second_moves[i],draw, first_money, second_money)
 
lakerfan94
Posts: 143
Posted 18:41 Mar 08, 2014 |

From calculateBid, what do we return?

Eric Liao
Posts: 158
Posted 20:52 Mar 08, 2014 |

Yeah, you are right that you call caclculateBid in the calculateMoney method. In calculate Bid you will nothing, it's a mutator method.

What calculateBid will do is to update first_money and second_money according to first_move, second_move and draw.

So that in the calculateMoney method, you will call calculateBid to evaluate each move one by one and update the each player's money accordingly. Then, after the loop that calls calculateBid method, you will have the first_money and second_money correctly.

Last thing you have to do in this calculateMoney method is to assign first_money and second_money to the correct variable and return it. (e.g. result)

Eric Liao
Posts: 158
Posted 00:23 Mar 09, 2014 |

Please check this post for further hints!

Last edited by Eric Liao at 00:31 Mar 09, 2014.
lakerfan94
Posts: 143
Posted 14:04 Mar 09, 2014 |

For the test case for testCalculateBid1, the expected value for first_money is 80. I already coded the calculateBid method and I'm pretty sure I did it according to how it was supposed to be structured but the message window said it was expecting 100. I know the assertEquals compares 80 to first_money but I was thinking, would we have to compare 80 to calculateMoney instead because first_money wasn't changed? I said that if first_money is equal to second_money and if draw is an odd number, then first_money -= first_move and draw++ and similar logic if draw was an even number except money would be deducted for the second player.

Eric Liao
Posts: 158
Posted 14:10 Mar 09, 2014 |

first_money is 80 because draw was given as 1 in the testCalculateBid1(), which means this is the first tie happens.

Scenario is like the following:

This is the first round of the bidding game

player 1 bids 20

player 2 bids 20

player 1 wins the bid because this is the first time draw happens.

player 1's money becomes 80 now

player 2's money stays as 100

after updating the players' money, we also need to update the draw count so that next time if draw happens, we would count player to be winner.

lakerfan94
Posts: 143
Posted 14:15 Mar 09, 2014 |

That's exactly how I did it but somehow first_money still stays at 100. Here's the code I wrote:

 static void calculateBid(int first_move, int second_move, int draw,
    int first_money, int second_money)
    {
        if(first_move > second_move)
            first_money -= first_move;
        else if(second_move > first_move)
            second_money -= second_move;
        else if(first_move == second_move)
        {
            if(draw %2 != 0)
            {
                first_money -= first_move;
                draw++;
            }
            else
            {
                second_money -= second_move;
                draw++;
            }
        }
    }

Eric Liao
Posts: 158
Posted 14:31 Mar 09, 2014 |

I just tested and found answer that

Java passed primitive types by value, which means that passing the int to the calculateBid method and update the first_money will not work.

I will fix my template code a bit in a moment so that it will work.

Eric Liao
Posts: 158
Posted 14:47 Mar 09, 2014 |

Many thanks to Andrew!


I updated the AI_Template.java and AI_TemplateTest.java again for the situation Andrew brought up now. Please check the template code again to see the difference.

Main change is calculateBid method that returns a array of integer so that we don't need to touch this pass by reference or pass by value problem.

lakerfan94
Posts: 143
Posted 14:52 Mar 09, 2014 |

Thank you Eric :)

Last edited by lakerfan94 at 14:54 Mar 09, 2014.
lakerfan94
Posts: 143
Posted 15:12 Mar 09, 2014 |

Do we do anything for the calculate_Bid method for the AI_Template or do we just leave it as you designed it?

Eric Liao
Posts: 158
Posted 15:13 Mar 09, 2014 |

Just leave calculate_bid method as it is, it should be fine as long as all other methods are done.

lakerfan94
Posts: 143
Posted 15:22 Mar 09, 2014 |

Ok thank you. For the aistrategy method in the AI_Template, when do we return theirMoney+1 or theirMoney? Is it when we have the tiebreaker or when we don't have the tiebreaker?

Eric Liao
Posts: 158
Posted 15:24 Mar 09, 2014 |

When you have the tie breaker, which means if tie happens you win the bid, you should return their money.

When you do not have the tie breaker, return their money + 1.