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
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#[derive(Debug)]
pub struct Score {
    pub han: u8,
    pub fu: u8,
    oya: bool,
    tsumo: bool,
}

impl Score {
    pub fn new(han: u8, fu: u8, oya: bool, tsumo: bool) -> Score {
        let mut new_fu = fu;
        if fu != 25 {
            new_fu = ((fu as f32 / 10f32).ceil() * 10f32) as u8;
        }

        Score {
            han,
            fu: new_fu,
            oya,
            tsumo,
        }
    }

    /// Finds the first han + fu combination that reaches at least the value of points given.
    pub fn from_points(points: u32, oya: bool, tsumo: bool, fu_limit: u8) -> Option<Vec<Score>> {
        let base_points: f32 = if oya {
            points as f32 / 6f32
        } else {
            points as f32 / 4f32
        };

        //        println!("base points {}", base_points);

        // 8000 is base for yakuman - if it's more, this score can't be reached by a hand
        // TODO double yakumans?
        if base_points > 8000f32 {
            return None;
        }

        let mut scores = vec![];

        if base_points > 6000f32 {
            scores.push(Score::new(13, 0, oya, tsumo));
            return Some(scores);
        }

        if base_points > 4000f32 {
            scores.push(Score::new(11, 0, oya, tsumo));
            scores.push(Score::new(12, 0, oya, tsumo));
            return Some(scores);
        }

        if base_points > 3000f32 {
            scores.push(Score::new(8, 0, oya, tsumo));
            scores.push(Score::new(9, 0, oya, tsumo));
            scores.push(Score::new(10, 0, oya, tsumo));
            return Some(scores);
        }

        if base_points > 2000f32 {
            scores.push(Score::new(6, 0, oya, tsumo));
            scores.push(Score::new(7, 0, oya, tsumo));
            return Some(scores);
        }

        if base_points > 1920f32 {
            if fu_limit > 60 {
                scores.push(Score::new(3, 70, oya, tsumo));
            }
            scores.push(Score::new(4, 40, oya, tsumo));
            scores.push(Score::new(5, 0, oya, tsumo));
            return Some(scores);
        }

        let mut done = false;
        for mut han in 1..4 {
            let mut fu = 20;

            // 1 han hands and rons can't have 20 fu
            if han == 1 || !tsumo {
                fu = 30;
            }

            while fu <= fu_limit {
                let s = Score::new(han, fu, oya, tsumo);
                if s.total_points() >= points {
                    // found!
                    scores.push(s);
                    done = true;

                    loop {
                        han += 1;
                        fu /= 2;
                        if fu >= 20 && (fu == 25 || fu as f32 % 10f32 == 0f32) {
                            // fu 20 can only be achieved with a tsumo
                            if fu == 20 && !tsumo {
                                break;
                            }

                            scores.push(Score::new(han, fu, oya, tsumo));

                            if fu == 25 {
                                break;
                            }
                        } else {
                            break;
                        }
                    }

                    break;
                }

                fu += 10;
            }

            if done {
                break;
            }
        }

        if !scores.is_empty() {
            return Some(scores);
        }

        None
    }

    /// Returns the base points of this Score. See http://arcturus.su/wiki/Japanese_mahjong_scoring_rules#Scoring_procedure
    fn base_points(&self) -> u32 {
        if self.han == 5 || (self.han == 3 && self.fu >= 70) || (self.han == 4 && self.fu >= 40) {
            // mangan
            return 2000;
        } else if self.han >= 6 && self.han <= 7 {
            // haneman
            return 3000;
        } else if self.han >= 8 && self.han <= 10 {
            // baiman
            return 4000;
        } else if self.han >= 11 && self.han <= 12 {
            // sanbaiman
            return 6000;
        } else if self.han >= 13 {
            // yakuman
            // TODO double yakuman?
            return 8000;
        }

        self.fu as u32 * (2u32.pow(2u32 + self.han as u32))
    }

    /// Returns total points that will be distributed from this Score
    pub fn total_points(&self) -> u32 {
        let base_points = self.base_points();

        if self.han == 5 || (self.han == 3 && self.fu >= 70) || (self.han == 4 && self.fu >= 40) {
            // mangan
            if self.oya {
                12000
            } else {
                8000
            }
        } else if self.han >= 6 && self.han <= 7 {
            // haneman
            if self.oya {
                18000
            } else {
                12000
            }
        } else if self.han >= 8 && self.han <= 10 {
            // baiman
            if self.oya {
                24000
            } else {
                16000
            }
        } else if self.han >= 11 && self.han <= 12 {
            // sanbaiman
            if self.oya {
                36000
            } else {
                24000
            }
        } else if self.han >= 13 {
            // yakuman
            if self.oya {
                48000
            } else {
                32000
            }
        } else if self.oya {
            if self.tsumo {
                ((((base_points * 2) as f32 / 100f32).ceil() * 100f32) * 3f32) as u32
            } else {
                (((base_points * 6) as f32 / 100f32).ceil() * 100f32) as u32
            }
        } else if self.tsumo {
            ((((base_points as f32 / 100f32).ceil() * 100f32) * 2f32)
                + (((base_points * 2) as f32 / 100f32).ceil() * 100f32)) as u32
        } else {
            (((base_points * 4) as f32 / 100f32).ceil() * 100f32) as u32
        }
    }

    /// How many points will oya pay from this Score?
    pub fn points_from_oya(&self) -> u32 {
        let base_points = self.base_points();

        if self.tsumo {
            return (((2 * base_points) as f32 / 100f32).ceil() * 100f32) as u32;
        }

        (((4 * base_points) as f32 / 100f32).ceil() * 100f32) as u32
    }

    /// How many points will non-oya pay from this Score?
    pub fn points_from_ko(&self) -> u32 {
        let base_points = self.base_points();

        if self.tsumo {
            if self.oya {
                return (((2 * base_points) as f32 / 100f32).ceil() * 100f32) as u32;
            }
            return (((base_points) as f32 / 100f32).ceil() * 100f32) as u32;
        }

        if self.oya {
            return (((6 * base_points) as f32 / 100f32).ceil() * 100f32) as u32;
        }

        (((4 * base_points) as f32 / 100f32).ceil() * 100f32) as u32
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn score_1_30() {
        let score = Score::new(1, 30, false, false);
        assert_eq!(score.total_points(), 1000);
    }

    #[test]
    fn score_1_30_oya() {
        let score = Score::new(1, 30, true, false);
        assert_eq!(score.total_points(), 1500);
    }

    #[test]
    fn score_1_30_tsumo() {
        let score = Score::new(1, 30, false, true);
        assert_eq!(score.total_points(), 1100);
    }

    #[test]
    fn score_1_30_oya_tsumo() {
        let score = Score::new(1, 30, true, true);
        assert_eq!(score.total_points(), 1500);
    }

    #[test]
    fn score_1_40() {
        let score = Score::new(1, 40, false, false);
        assert_eq!(score.total_points(), 1300);
    }

    #[test]
    fn score_1_40_oya() {
        let score = Score::new(1, 40, true, false);
        assert_eq!(score.total_points(), 2000);
    }

    #[test]
    fn score_1_40_tsumo() {
        let score = Score::new(1, 40, false, true);
        assert_eq!(score.total_points(), 1500);
    }

    #[test]
    fn score_1_40_oya_tsumo() {
        let score = Score::new(1, 40, true, true);
        assert_eq!(score.total_points(), 2100);
    }

    #[test]
    fn score_4_30() {
        let score = Score::new(4, 30, false, false);
        assert_eq!(score.total_points(), 7700);
    }

    #[test]
    fn score_3_50() {
        let score = Score::new(3, 70, false, false);
        assert_eq!(score.total_points(), 8000);
    }

    #[test]
    fn score_4_25() {
        let score = Score::new(4, 25, false, false);
        assert_eq!(score.total_points(), 6400);
    }

    #[test]
    fn oya_points_1_30() {
        let score = Score::new(1, 30, false, false);
        assert_eq!(score.points_from_oya(), 1000);
    }

    #[test]
    fn oya_points_1_30_tsumo() {
        let score = Score::new(1, 30, false, true);
        assert_eq!(score.points_from_oya(), 500);
    }

    #[test]
    fn ko_points_1_30() {
        let score = Score::new(1, 30, false, false);
        assert_eq!(score.points_from_ko(), 1000);
    }

    #[test]
    fn ko_points_1_30_tsumo() {
        let score = Score::new(1, 30, false, true);
        assert_eq!(score.points_from_ko(), 300);
    }

    #[test]
    fn from_points_2000() {
        let scores = Score::from_points(2000, false, false, 60).unwrap();

        assert_eq!(scores.len(), 2);

        assert_eq!(scores[0].han, 1);
        assert_eq!(scores[0].fu, 60);

        assert_eq!(scores[1].han, 2);
        assert_eq!(scores[1].fu, 30);
    }

    #[test]
    fn from_points_3800() {
        let scores = Score::from_points(3800, false, false, 60).unwrap();

        assert_eq!(scores.len(), 2);

        assert_eq!(scores[0].han, 2);
        assert_eq!(scores[0].fu, 60);

        assert_eq!(scores[1].han, 3);
        assert_eq!(scores[1].fu, 30);
    }

    #[test]
    fn from_points_3800_oya() {
        let scores = Score::from_points(3800, true, false, 60).unwrap();

        //        println!("{:#?}", scores);

        assert_eq!(scores.len(), 1);

        assert_eq!(scores[0].han, 2);
        assert_eq!(scores[0].fu, 40);
    }

    #[test]
    fn from_points_3800_oya_tsumo() {
        let scores = Score::from_points(3800, true, true, 60).unwrap();

        //        println!("{:#?}", scores);

        assert_eq!(scores.len(), 2);

        assert_eq!(scores[0].han, 2);
        assert_eq!(scores[0].fu, 40);

        assert_eq!(scores[1].han, 3);
        assert_eq!(scores[1].fu, 20);
    }

    #[test]
    fn from_points_4000() {
        let scores = Score::from_points(4000, false, false, 60).unwrap();

        //        println!("{:#?}", scores);

        assert_eq!(scores.len(), 1);

        assert_eq!(scores[0].han, 3);
        assert_eq!(scores[0].fu, 40);
    }

    #[test]
    fn from_points_7600() {
        let scores = Score::from_points(7600, false, false, 60).unwrap();

        //        println!("{:#?}", scores);

        assert_eq!(scores.len(), 2);

        assert_eq!(scores[0].han, 3);
        assert_eq!(scores[0].fu, 60);

        assert_eq!(scores[1].han, 4);
        assert_eq!(scores[1].fu, 30);
    }

    #[test]
    fn from_points_8000() {
        let scores = Score::from_points(8000, false, false, 60).unwrap();

        println!("{:#?}", scores);

        assert_eq!(scores.len(), 2);

        assert_eq!(scores[0].han, 4);
        assert_eq!(scores[0].fu, 40);

        assert_eq!(scores[1].han, 5);
        assert_eq!(scores[1].fu, 0);
    }

    #[test]
    fn create_score_1_22() {
        let score = Score::new(1, 22, false, true);
        assert_eq!(score.han, 1);
        assert_eq!(score.fu, 30);
    }
}