/** Remove bids */ private void removeBids() { Connection con = null; try { con = L2DatabaseFactory.getInstance().getConnection(con); PreparedStatement statement; statement = con.prepareStatement("DELETE FROM auction_bid WHERE auctionId=?"); statement.setInt(1, getId()); statement.execute(); statement.close(); } catch (Exception e) { _log.fatal("Exception: Auction.deleteFromDB(): " + e.getMessage(), e); } finally { L2DatabaseFactory.close(con); } for (Bidder b : _bidders.values()) { if (ClanTable.getInstance().getClanByName(b.getClanName()).getHasHideout() == 0) returnItem(b.getClanName(), PcInventory.ADENA_ID, b.getBid(), true); // 10 % tax else { L2Player bidder = L2World.getInstance().getPlayer(b.getName()); if (bidder != null) bidder.sendMessage("Congratulations! You have won a ClanHall!"); } ClanTable.getInstance().getClanByName(b.getClanName()).setAuctionBiddedAt(0, true); } _bidders.clear(); }
static void displayRound( int playerOneMoney, int playerTwoMoney, int playerOneBid, int playerTwoBid, byte roundWinner, int currentPosition, Bidder playerOne, Bidder playerTwo) { System.out.println(playerOne.getName() + " Balance: " + playerOneMoney); System.out.println(playerTwo.getName() + " Balance: " + playerTwoMoney); switch (roundWinner) { case 0: break; case 1: System.out.println(playerOne.getName() + " bid: " + playerOneBid + " *Wins round"); System.out.println(playerTwo.getName() + " bid: " + playerTwoBid); break; case 2: System.out.println(playerOne.getName() + " bid: " + playerOneBid); System.out.println(playerTwo.getName() + " bid: " + playerTwoBid + " *Wins round"); break; default: // This case should never happen } for (int i = 0; i < NUMBER_OF_POSITIONS; i++) { if (i == currentPosition - 1) System.out.print(" X "); else System.out.print(" _ "); } System.out.println(); System.out.println(); }
public static void main(String[] args) { // keep track of how much money each player has int playerOneMoney; int playerTwoMoney; // keep track of the position int currentPos; // variable for the players Bidder playerOne; Bidder playerTwo; // keep track of moves ArrayList<Integer> playerOneMoves = new ArrayList<Integer>(); ArrayList<Integer> playerTwoMoves = new ArrayList<Integer>(); // flag for illegal move boolean illegalMove = false; // Change this based on the Class they choice playerOne = new ExampleBidderOne(); playerTwo = new ExampleBidderTwo(); // Initialize the money playerOneMoney = STARTING_AMOUNT; playerTwoMoney = STARTING_AMOUNT; // Set the position currentPos = STARTING_POSITION; // Display the beginning displayRound(playerOneMoney, playerTwoMoney, 0, 0, (byte) 0, currentPos, playerOne, playerTwo); // while you are not at the ends and both players have money left keep playing while (currentPos != 1 && currentPos != NUMBER_OF_POSITIONS && playerOneMoney > 0 && playerTwoMoney > 0) { byte roundWinner; // get the players bids int playerOneBid = playerOne.calculateBid( playerOneMoves, playerTwoMoves, currentPos - 1, playerOneMoney, playerTwoMoney); int playerTwoBid = playerTwo.calculateBid( playerTwoMoves, playerOneMoves, NUMBER_OF_POSITIONS - currentPos, playerTwoMoney, playerOneMoney); // if someone makes an illegal bet other player wins if (playerOneBid <= 0 || playerOneBid > playerOneMoney) { illegalMove = true; System.out.println("Illegal bet. " + playerTwo.getName() + " wins."); break; } if (playerTwoBid <= 0 || playerTwoBid > playerTwoMoney) { illegalMove = true; System.out.println("Illegal bet. " + playerOne.getName() + " wins."); break; } // add the bets to the lists playerOneMoves.add(playerOneBid); playerTwoMoves.add(playerTwoBid); // in the event of a tie randomly select a winner if (playerOneBid == playerTwoBid) { // New Random to select the winner Random random = new Random(); if (random.nextBoolean()) { playerOneMoney -= playerOneBid; roundWinner = 1; currentPos--; } else { playerTwoMoney -= playerTwoBid; roundWinner = 2; currentPos++; } } else if (playerOneBid > playerTwoBid) { playerOneMoney -= playerOneBid; roundWinner = 1; currentPos--; } else { playerTwoMoney -= playerTwoBid; roundWinner = 2; currentPos++; } displayRound( playerOneMoney, playerTwoMoney, playerOneBid, playerTwoBid, roundWinner, currentPos, playerOne, playerTwo); } if (illegalMove) return; // display who wins if (currentPos == 1) { System.out.println(playerOne.getName() + " Wins!"); } // display who wins else if (currentPos == NUMBER_OF_POSITIONS) { System.out.println(playerTwo.getName() + " Wins!"); } // if both people are out of money it is a tie else if (playerOneMoney == 0 && playerTwoMoney == 0) { System.out.println("Tie game."); } // if player one spent all their money and player two has enough money left to win than player // two wins else if (playerOneMoney == 0 && playerTwoMoney >= NUMBER_OF_POSITIONS - currentPos) { System.out.println( playerOne.getName() + " is out of money, " + playerTwo.getName() + " Wins!"); } // if player two spent all their money and player one has enough money left to win than player // one wins else if (playerTwoMoney == 0 && playerOneMoney >= currentPos) { System.out.println( playerTwo.getName() + " is out of money, " + playerOne.getName() + " Wins!"); } // it is a tie else { System.out.println("Tie game."); } }