Cody

# Problem 42685. Cody meets Xiangqi: foresee the unseen (Part 2)

This is the second part of the Xiangqi series. The first part in this series is: Cody meets Xiangqi: foresee the unseen (Part 1)

Being increasingly interested in Xiangqi (a.k.a., Chinese Chess), Mr. Cody has designed a new Xiangqi match for Xiang Yu and Liu Bang by taking into account the likelihood of tie games. The new rule is described as follows:

Once

```   1) Xiang Yu wins Na games consecutively,
2) Liu Bang wins Nb games consecutively,
3) No ties occur consecutively, ```

whichever comes first, Mr. Cody announces the outcome accordingly as follows:

```   1) Xiang Yu is the final winner,
2) Liu Bang is the final winner,
3) They end up with a final draw.```

Again, Cody asks us --- active Cody players --- to foresee the outcome of this unseen match using Monte Carlo simulations. Our task is to write the following function

`                         [Pa, Pb, Pc] = Xiangqi2(a, b, Na, Nb, Nc)`

where

• a: the probability that Xiang Yu wins one individual game
• b: the probability that Liu Bang wins one individual game
• Na: # of consecutive wins required for Xiang Yu to become the final winner
• Nb: # of consecutive wins required for Liu Bang to become the final winner
• Nc: # of consecutive ties required to result in a final draw
• Pa: the probability that Xiang Yu wins the match
• Pb: the probability that Liu Bang wins the match
• Pc: the probability of a final draw

The main focus of this problem is on Monte Carlo simulations, rather than analytical approaches. Your provided solution Xiangqi2.m will be checked by a P-file EvaluateSolution.p, which mainly does 3 things as follows:

1) Call your function [Pa, Pb, Pc] = Xiangqi2(a, b, Na, Nb, Nc) and then Check if the result P = [Pa, Pb, Pc] is within tolerance of its expected value Q. That is, If norm(P - Q) < tol holds, it means that your solution is accurate enough. If this does not hold, your solution will be rejected.

2) Check if your solution is based on pure Monte Carlo simulations or analytical approaches. If it is based on analytical approaches (i.e., using analytical expressions to directly compute the probabilities), then your solution will be rejected. EvaluateSolution.p accomplishes this goal by exploiting a combination of distinct features possessed by analytical solutions, but are generally not shared by Monte Carlo simulations.

3) If your solution passes the above two checks, then the score of your solution will be determined based on the speed of your code. The faster your solution is, the smaller score you get.

If you have any concerns or suggestions on this problem, please feel free to leave me a comment. Thanks.

### Solution Stats

13.85% Correct | 86.15% Incorrect
Last Solution submitted on Nov 17, 2016