Problem 42674. Cody meets Xiangqi: foresee the unseen (Part 1)

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

Xiangqi, also known as Chinese Chess (and 象棋 in Chinese characters), is one of the most popular board games in China. The modern Xiangqi board contains a middle section which divides two players' sides and is marked the "Chu River–Han border", in reference to the Chu–Han Contention between Xiang Yu and Liu Bang, two prominent warlords and opponents who fought thousands of battles against each other for supremacy over China in the late Qin dynasty (206–202 BC). Those interested in the story of the Chu–Han Contention and its relation to Xiangqi are referred to here.

Fresh to Xiangqi, Cody becomes interested in Xiangqi by raising a question: Who is the stronger player of Xiangqi between Xiang Yu and Liu Bang? To answer this question, Cody designs a match for Xiang Yu and Liu Bang, in which Cody serves as the referee. The smart Cody referee also sets an intelligent rule to determine the winner:

In a succession of Xiangqi games, once Xiang Yu wins Na games consecutively, whereas Liu Bang has not won Nb games consecutively, Cody immediately announces Xiang Yu as the winner. Contrarily, once Liu Bang defeats Xiang Yu Nb times consecutively, whereas Xiang Yu has not won Na times consecutively, Liu Bang becomes the winner.

Cody suggests that Na > 1 and Nb > 1, in order to enhance, to some extent, the confidence of the result of the match. Suppose in each individual game, the probability Xiang Yu would win is p, and the probability Liu Bang would win is 1 - p, which implicitly assumes that the probability of a tie is 0 (because they both refuse to draw and will fight to death). Unfortunately, this well-designed match has never taken place. Regretfully, Cody requests us --- active Cody players --- to foresee the outcome of this unseen match using Monte Carlo simulations. Our task is to write a function

                                sol = Xiangqi(p, Na, Nb)

with input: 0 <= p <= 1, Na > 1, Nb > 1, and output: sol --- the probability that Xiang Yu wins. Your solution will be tested against its true value Q (which is computed but hided in the P-file EvaluateSolution.p) according to a hybrid absolute and relative error tolerance criterion:

                      abs(sol - Q) <= max(AbsTol, RelTol*abs(sol))

where AbsTol and RelTol are absolute and relative error tolerances, respectively, which will be specified in the test suite. You are encouraged to optimize the performance (rather than the usual Cody size) of your code as much as possible, as the score of your solution will be measured based on the speed of your code.

Have fun!

Solution Stats

55.36% Correct | 44.64% Incorrect
Last Solution submitted on May 05, 2021

Problem Comments

Solution Comments

Show comments

Problem Recent Solvers8

Suggested Problems

More from this Author29

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!