| | |
| | | package net.jacekk.bridge; |
| | | |
| | | import java.util.Arrays; |
| | | |
| | | public class BridgeCompute { |
| | | // Points for tricks |
| | | protected int tricks[][] = { |
| | | {0, 20, 40, 60, 80, 100, 120, 140}, // minor suits |
| | | {0, 30, 60, 90, 120, 150, 180, 210}, // major suits |
| | | {0, 40, 70, 100, 130, 160, 190, 220} // no trump |
| | | }; |
| | | |
| | | // Points for tricks (minor, major, no trump) |
| | | protected int tricks[] = {20, 30, 30}; |
| | | |
| | | // Points for [i] undertricks |
| | | protected int undertricks[][][] = { |
| | | { // non vulnerable |
| | | {0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650}, // normal |
| | | {0, 100, 300, 500, 700, 900, 1100, 1300, 1500, 1700, 1900, 2100, 2300, 2500}, // double |
| | | {0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650},// normal |
| | | {0, 100, 300, 500, 700, 900, 1100, 1300, 1500, 1700, 1900, 2100, 2300, 2500},// double |
| | | {0, 200, 600, 1000, 1400, 1800, 2200, 2600, 3000, 3400, 3800, 4200, 4600, 5000} // redouble |
| | | }, |
| | | { // vulnerable |
| | | {0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300}, // normal |
| | | {0, 200, 500, 800, 1100, 1400, 1700, 2000, 2300, 2600, 2900, 3200, 3500, 3800}, // double |
| | | {0, 400, 1000, 1600, 2200, 2800, 3400, 4000, 4600, 5200, 5800, 6400, 7000, 7600}// redouble |
| | | {0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300},// normal |
| | | {0, 200, 500, 800, 1100, 1400, 1700, 2000, 2300, 2600, 2900, 3200, 3500, 3800},// double |
| | | {0, 400, 1000, 1600, 2200, 2800, 3400, 4000, 4600, 5200, 5800, 6400, 7000, 7600} // redouble |
| | | } |
| | | }; |
| | | |
| | | // Points required per Milton Work Point Count |
| | | protected int PC[][][] = { |
| | | { // We: non vulnerable, they: non vulnerable |
| | | {-1400, -1400, -1400, -1400, -1200, -1100, -1000, -900, -700, -600, -490, |
| | | -460, -430, -400, -350, -300, -200, -110, -70, -50, 0, |
| | | 50, 70, 110, 200, 300, 350, 400, 430, 460, 490, |
| | | 600, 700, 900, 1000, 1100, 1200, 1400, 1400, 1400, 1400}, |
| | | // We: non vulnerable, they: vulnerable |
| | | {-2100, -2100, -2100, -2100, -1800, -1650, -1500, -1350, -1050, -800, -690, |
| | | -660, -630, -600, -520, -440, -290, -110, -70, -50, 0, |
| | | 50, 70, 110, 200, 300, 350, 400, 430, 460, 490, |
| | | 600, 700, 900, 1000, 1100, 1200, 1400, 1400, 1400, 1400} |
| | | // Points required per Milton Work Point Count (from 20 up) |
| | | protected int PC[][] = { |
| | | {0, 50, 70, 110, 200, 300, 350, 400, 430, 460, 490, 600, 700, 900, |
| | | 1000, 1100, 1200, 1400, 1400, 1400, 1400 |
| | | }, |
| | | { // We: vulnerable, they: non vulnerable |
| | | {-1400, -1400, -1400, -1400, -1200, -1100, -1000, -900, -700, -600, -490, |
| | | -460, -430, -400, -350, -300, -200, -110, -70, -50, 0, |
| | | 50, 70, 110, 290, 440, 520, 600, 630, 660, 690, |
| | | 800, 1050, 1350, 1500, 1650, 1800, 2100, 2100, 2100, 2100}, |
| | | // We: vulnerable, they: vulnerable |
| | | {-2100, -2100, -2100, -2100, -1800, -1650, -1500, -1350, -1050, -800, -690, |
| | | -660, -630, -600, -520, -440, -290, -110, -70, -50, 0, |
| | | 50, 70, 110, 290, 440, 520, 600, 630, 660, 690, |
| | | 800, 1050, 1350, 1500, 1650, 1800, 2100, 2100, 2100, 2100} |
| | | {0, 50, 70, 110, 290, 440, 520, 600, 630, 660, 690, 800, 1050, 1350, |
| | | 1500, 1650, 1800, 2100, 2100, 2100, 2100 |
| | | } |
| | | }; |
| | | |
| | |
| | | // See: http://www.gplewniak.republika.pl/strony/zapis.html |
| | | |
| | | // Points received - [i] used to access row from percent variable |
| | | protected int percent_points[][] = { |
| | | protected int percentPoints[][] = { |
| | | {0, 50, 90, 120, 150, 180, 210, 300, 400, 430, 460, 490, 520, 800, 920, 940, 980, 990, |
| | | 1020, 1400, 1440, 1520, 1530, 9999999}, // non vulnerable |
| | | {0, 50, 90, 120, 150, 180, 210, 500, 600, 360, 660, 690, 720, 810, 1370, 1390, 1430, |
| | | {0, 50, 90, 120, 150, 180, 210, 500, 600, 630, 660, 690, 720, 810, 1370, 1390, 1430, |
| | | 1440, 1470, 1700, 2000, 2220, 2230, 9999999} // vulnerable |
| | | }; |
| | | |
| | | // Work Point Count - [i] used to access column from property percent |
| | | protected int percent_PC[] = {0, 6, 10, 16, 21, 25, 31, 35, 9999999}; |
| | | protected int percentPCs[] = {0, 6, 10, 16, 21, 25, 31, 35, 9999999}; |
| | | |
| | | // Percent score per [i] - points scored, [j] - PC had |
| | | // Percent score per [i] - points scored, [j] - PCs had |
| | | protected int percent[][] = { |
| | | {-1, -1, -1, 50, 44, 26, 8, 0}, |
| | | {83, 74, 65, 56, 47, 29, 11, 0}, |
| | |
| | | }; |
| | | |
| | | // Finds IMP score for points given (using IMPs property) |
| | | protected int IMP(int points) { |
| | | int i = 0; |
| | | while (IMPs[i + 1] <= points) { |
| | | i++; |
| | | protected int getIMPs(int points) { |
| | | int position = Arrays.binarySearch(this.IMPs, points); |
| | | if (position < 0) { |
| | | return -position - 2; |
| | | } |
| | | return i; |
| | | return position; |
| | | } |
| | | |
| | | // Finds percent score for data given (using percent* properties) |
| | | protected int percent(int PC, int points, int vulnerability) { |
| | | int points_i = 0; |
| | | int PC_i = 0; |
| | | protected int getPercent(int PC, int points, int vulnerability) { |
| | | int positionPoints = Arrays.binarySearch(this.percentPoints[vulnerability], points); |
| | | int positionPCs = Arrays.binarySearch(this.percentPCs, PC); |
| | | if (positionPoints < 0) positionPoints = -positionPoints - 2; |
| | | if (positionPCs < 0) positionPCs = -positionPCs - 2; |
| | | |
| | | while (this.percent_points[vulnerability][points_i + 1] <= points) { |
| | | points_i++; |
| | | } |
| | | |
| | | while (this.percent_PC[PC_i + 1] <= PC) { |
| | | PC_i++; |
| | | } |
| | | |
| | | return this.percent[points_i][PC_i]; |
| | | return this.percent[positionPoints][positionPCs]; |
| | | } |
| | | |
| | | public BridgeResult compute(int bid, int color, int dbl, int vulnerability, int PC, int tricks) { |
| | | public void validateInput(BridgeInput input) throws BridgeInputException { |
| | | if (input.bid < 0 || input.bid > 7) { |
| | | throw new BridgeInputException(BridgeInputException.Error.CONTRACT_LEVEL_INVALID); |
| | | } |
| | | |
| | | if (input.suit == null) { |
| | | throw new BridgeInputException(BridgeInputException.Error.CONTRACT_SUIT_INVALID); |
| | | } |
| | | |
| | | if (input.contract == null) { |
| | | throw new BridgeInputException(BridgeInputException.Error.CONTRACT_DOUBLE_INVALID); |
| | | } |
| | | |
| | | if (input.PC < 0 || input.PC > 40) { |
| | | throw new BridgeInputException(BridgeInputException.Error.PC_INVALID); |
| | | } |
| | | |
| | | if (input.tricks < 0 || input.tricks > 13) { |
| | | throw new BridgeInputException(BridgeInputException.Error.TRICKS_INVALID); |
| | | } |
| | | |
| | | if (input.bid == 0 && input.PC < 20) { |
| | | throw new BridgeInputException(BridgeInputException.Error.PASSES_INVALID); |
| | | } |
| | | } |
| | | |
| | | public BridgeResult getResultForInput(BridgeInput input) throws BridgeInputException { |
| | | validateInput(input); |
| | | |
| | | int weVulnerable = (input.weVulnerable) ? 1 : 0; |
| | | int theyVulnerable = (input.theyVulnerable) ? 1 : 0; |
| | | int bid = input.bid; |
| | | |
| | | BridgeResult result = new BridgeResult(); |
| | | int weVulnerable = vulnerability / 2; |
| | | int theyVulnerable = vulnerability % 2; |
| | | |
| | | // 4 passes |
| | | if (bid == 0) { |
| | | result.pointsFor = BridgeResult.PointsFor.Them; |
| | | result.points = this.PC[weVulnerable][theyVulnerable][PC]; |
| | | result.IMPs = this.IMP(result.points); |
| | | result.percent = 100 - this.percent(PC, 0, weVulnerable); |
| | | |
| | | if (input.bid == 0) { |
| | | result.winners = BridgeResult.Side.They; |
| | | result.points = this.PC[weVulnerable][input.PC - 20]; |
| | | result.IMPs = this.getIMPs(result.points); |
| | | result.percent = 100 - this.getPercent(input.PC, 0, weVulnerable); |
| | | return result; |
| | | } |
| | | |
| | | |
| | | int multiplier = input.contract.multiplier; |
| | | int dbl = input.contract.tableIndex; |
| | | int tricks = input.tricks; |
| | | |
| | | // Result in points |
| | | int resultPoints; |
| | | // Points for tricks |
| | | int pointsForTricks; |
| | | int points; |
| | | // Points for tricks bid |
| | | int pointsForTricksBid; |
| | | // Result minus points required, according to PC property |
| | | int pointsForScoring; |
| | | |
| | | // Number of overtricks (or undertricks if negative) |
| | | int additionalTricks = tricks - 6 - bid; |
| | | |
| | | // Double/redouble multiplier |
| | | int multiplier = dbl + 1; |
| | | if (multiplier == 3) { |
| | | multiplier = 4; |
| | | } |
| | | |
| | | // Contract made |
| | | if (additionalTricks >= 0) { |
| | | pointsForTricksBid = this.tricks[input.suit.tableIndex] * bid * multiplier; |
| | | if (input.suit.equals(BridgeInput.Suit.NOTRUMP)) { |
| | | // First trick in no trump game is worth 10 points more |
| | | pointsForTricksBid += 10 * multiplier; |
| | | } |
| | | points = pointsForTricksBid; |
| | | |
| | | // Add points for overtricks |
| | | |
| | | // Ordinary contract |
| | | if (multiplier == 1) { |
| | | pointsForTricks = this.tricks[color][bid]; |
| | | resultPoints = this.tricks[color][tricks - 6]; |
| | | if (input.contract.equals(BridgeInput.Contract.NORMAL)) { |
| | | points += this.tricks[input.suit.tableIndex] * additionalTricks; |
| | | } |
| | | // Contract doubled/redoubled |
| | | else { |
| | | pointsForTricks = this.tricks[color][bid] * multiplier; |
| | | resultPoints = pointsForTricks; |
| | | points += (!input.weVulnerable ? 50 : 100) * multiplier * additionalTricks; |
| | | |
| | | // Bonus for winning doubled/redoubled contract |
| | | resultPoints += 25 * multiplier; |
| | | // For overtricks |
| | | resultPoints += additionalTricks * (weVulnerable == 0 ? 50 : 100) * multiplier; |
| | | points += 25 * multiplier; |
| | | } |
| | | |
| | | if (bid == 6) { |
| | | // Bonus for small slam |
| | | resultPoints += (weVulnerable == 0 ? 500 : 750); |
| | | points += (!input.weVulnerable ? 500 : 750); |
| | | } else if (bid == 7) { |
| | | // Bonus for grand slam |
| | | resultPoints += (weVulnerable == 0 ? 1000 : 1500); |
| | | points += (!input.weVulnerable ? 1000 : 1500); |
| | | } |
| | | |
| | | if (pointsForTricks >= 100) { |
| | | if (pointsForTricksBid >= 100) { |
| | | // Bonus for game |
| | | resultPoints += (weVulnerable == 0 ? 300 : 500); |
| | | points += (!input.weVulnerable ? 300 : 500); |
| | | } else { |
| | | // Bonus for part-game |
| | | resultPoints += 50; |
| | | points += 50; |
| | | } |
| | | } |
| | | // Contract defeated |
| | | else { |
| | | // Points for undertricks (for defending side) |
| | | resultPoints = -1 * this.undertricks[weVulnerable][dbl][-1 * additionalTricks]; |
| | | points = -1 * this.undertricks[weVulnerable][dbl][-1 * additionalTricks]; |
| | | } |
| | | |
| | | // Deduct points required |
| | | pointsForScoring = resultPoints - this.PC[weVulnerable][theyVulnerable][PC]; |
| | | if (input.PC >= 20) { |
| | | pointsForScoring = points - this.PC[weVulnerable][input.PC - 20]; |
| | | } else { |
| | | pointsForScoring = points + this.PC[theyVulnerable][20 - input.PC]; |
| | | } |
| | | |
| | | // Score for defeaters |
| | | if (pointsForScoring < 0) { |
| | | result.pointsFor = BridgeResult.PointsFor.Them; |
| | | result.pointsBefore = -1 * resultPoints; |
| | | result.winners = BridgeResult.Side.They; |
| | | result.pointsBefore = -1 * points; |
| | | result.points = -1 * pointsForScoring; |
| | | result.IMPs = IMP(result.points); |
| | | if (resultPoints < 0) { |
| | | result.percent = percent(40 - PC, -1 * resultPoints, theyVulnerable); |
| | | |
| | | if (points < 0) { |
| | | result.percent = getPercent(40 - input.PC, -points, theyVulnerable); |
| | | } else { |
| | | result.percent = 100 - percent(PC, resultPoints, weVulnerable); |
| | | result.percent = 100 - getPercent(input.PC, points, weVulnerable); |
| | | } |
| | | } |
| | | // Score for declarers |
| | | else { |
| | | result.pointsFor = BridgeResult.PointsFor.Us; |
| | | result.pointsBefore = resultPoints; |
| | | result.winners = BridgeResult.Side.We; |
| | | result.pointsBefore = points; |
| | | result.points = pointsForScoring; |
| | | result.IMPs = IMP(result.points); |
| | | if (resultPoints > 0) { |
| | | result.percent = percent(PC, resultPoints, weVulnerable); |
| | | |
| | | if (points > 0) { |
| | | result.percent = getPercent(input.PC, points, weVulnerable); |
| | | } else { |
| | | result.percent = 100 - percent(40 - PC, -1 * resultPoints, theyVulnerable); |
| | | result.percent = 100 - getPercent(40 - input.PC, -points, theyVulnerable); |
| | | } |
| | | } |
| | | |
| | | result.IMPs = getIMPs(result.points); |
| | | |
| | | |
| | | return result; |
| | | } |
| | | } |