Results 1 to 8 of 8
  1. #1
    dstars5 is offline Member
    Join Date
    Mar 2013
    Posts
    19
    Rep Power
    0

    Question Bracket Generator (Need a better way)

    Ok, so with March Madness in full swing, and me looking for something to do, I made this bracket generator. It takes only 16 teams right now and chooses random winners, then creates an Excel spreadsheet with the bracket. The code
    is below, but I really need a better way to do this. The way I am going about this now is using a switch, then a case for each game. The entire thing is almost 350 lines. There has to be a better way than this. Any suggestions?

    Java Code:
    import java.util.Random;
    import java.util.Scanner;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.util.WorkbookUtil;
    import java.io.FileOutputStream;
    
    public class BracketGeneration {
    	static Bracket bracket = new Bracket();
    	
    	int lastBracket = 0;
    	static int lastGame = 0;
    	static int bracketnum = 0;
    	
    	public static void createBracket(boolean done) {
    		Workbook workbook = new HSSFWorkbook();
    		String team = "";
    		int row = 0;
    		int column = 1;
    		
    		Sheet sheet = workbook.createSheet("Brackets");
    		
    		Cell cell = sheet.createRow(bracketnum * 33).createCell(0);
    
    		for (int i = 1; i<=31; i++) {
    			//System.out.println(i);
    			switch (i) {
    			case 1:
    				team = "Louisville";
    				column = 0;
    				row = bracketnum * 34 + 1;
    				break;
    			case 2:
    				team = "NC A&T";
    				column = 0;
    				row = bracketnum * 34 + 3;
    				break;
    			case 3:
    				team = "Colo St";
    				column = 0;
    				row = bracketnum * 34 + 5;
    				break;
    			case 4:
    				team = "Missouri";
    				column = 0;
    				row = bracketnum * 34 + 7;
    				break;
    			case 5:
    				team = "Okla St";
    				column = 0;
    				row = bracketnum * 34 + 9;
    				break;
    			case 6:
    				team = "Oregon";
    				column = 0;
    				row = bracketnum * 34 + 11;
    				break;
    			case 7:
    				team = "Saint Louis";
    				column = 0;
    				row = bracketnum * 34 + 13;
    				break;
    			case 8:
    				team = "New Mex St";
    				column = 0;
    				row = bracketnum * 34 + 15;
    				break;
    			case 9:
    				team = "Memphis";
    				column = 0;
    				row = bracketnum * 34 + 17;
    				break;
    			case 10:
    				team = "Saint Mary's";
    				column = 0;
    				row = bracketnum * 34 + 19;
    				break;
    			case 11:
    				team = "Mich St";
    				column = 0;
    				row = bracketnum * 34 + 21;
    				break;
    			case 12:
    				team = "Valpraiso";
    				column = 0;
    				row = bracketnum * 34 + 23;
    				break;
    			case 13:
    				team = "Creighton";
    				column = 0;
    				row = bracketnum * 34 + 25;
    				break;
    			case 14:
    				team = "Cincinnati";
    				column = 0;
    				row = bracketnum * 34 + 27;
    				break;
    			case 15:
    				team = "Duke";
    				column = 0;
    				row = bracketnum * 34 + 29;
    				break;
    			case 16:
    				team = "Albany";
    				column = 0;
    				row = bracketnum * 34 + 31;
    				break;
    			//Round Of 32
    			case 17:
    				team = bracket.G9.get(0);
    				column = 1;
    				row = bracketnum * 34 + 2;
    				break;
    			case 18:
    				team = bracket.G9.get(1);
    				column = 1;
    				row = bracketnum * 34 + 6;
    				break;
    			case 19:
    				team = bracket.G10.get(0);
    				column = 1;
    				row = bracketnum * 34 + 10;
    				break;
    			case 20:
    				team = bracket.G10.get(1);
    				column = 1;
    				row = bracketnum * 34 + 14;
    				break;
    			case 21:
    				team = bracket.G11.get(0);
    				column = 1;
    				row = bracketnum * 34 + 18;
    				break;
    			case 22:
    				team = bracket.G11.get(1);
    				column = 1;
    				row = bracketnum * 34 + 22;
    				break;
    			case 23:
    				team = bracket.G12.get(0);
    				column = 1;
    				row = bracketnum * 34 + 26;
    				break;
    			case 24:
    				team = bracket.G12.get(1);
    				column = 1;
    				row = bracketnum * 34 + 30;
    				break;
    			//Sweet Sixteen
    			case 25:
    				team = bracket.G13.get(0);
    				column = 2;
    				row = bracketnum * 34 + 4;
    				break;
    			case 26:
    				team = bracket.G13.get(1);
    				column = 2;
    				row = bracketnum * 34 + 12;
    				break;
    			case 27:
    				team = bracket.G14.get(0);
    				column = 2;
    				row = bracketnum * 34 + 20;
    				break;
    			case 28:
    				team = bracket.G14.get(1);
    				column = 2;
    				row = bracketnum * 34 + 28;
    				break;
    			//Elite Eight
    			case 29:
    				team = bracket.G15.get(0);
    				column = 3;
    				row = bracketnum * 34 + 8;
    				break;
    			case 30:
    				team = bracket.G15.get(1);
    				column = 3;
    				row = bracketnum * 34 + 24;
    				break;
    			case 31:
    				team = bracket.champion;
    				column = 4;
    				row = bracketnum * 34 + 16;
    				break;
    			}
    			Cell game = sheet.createRow(row).createCell(column);
    			game.setCellValue(team);
    			//System.out.println("(" + column + "," + row + ") set to" + team);
    			
    			if (done) {
    				try {
    					FileOutputStream output = new FileOutputStream("Brackets.xls");
    					workbook.write(output);
    					output.close();
    				} catch(Exception e) {
    					e.printStackTrace();
    				}
    			}
    		}
    	}
    	
    	public static boolean Games() {
    	boolean correct = false;
    		for (int i = 1; i <= 15; i++) {
    			lastGame = i;
    			int nextGame = lastGame + 1;
    			String team1 = "";
    			String team2 = "";
    				
    			switch (lastGame) {
    				//Round Of 64
    				case 1:
    					team1 = "Louisville";
    					team2 = "N.C. A&T";
    					nextGame = 9;
    					break;
    				case 2:
    					team1 = "Colo St";
    					team2 = "Missouri";
    					nextGame = 9;
    					break;
    				case 3:
    					team1 = "Okla St";
    					team2 = "Oregon";
    					nextGame = 10;
    					break;
    				case 4:
    					team1 = "Saint Louis";
    					team2 = "New Mex St";
    					nextGame = 10;
    					break;
    				case 5:
    					team1 = "Memphis";
    					team2 = "St. Mary's";
    					nextGame = 11;
    					break;
    				case 6:
    					team1 = "Mich St";
    					team2 = "Valpraiso";
    					nextGame = 11;
    					break;
    				case 7:
    					team1 = "Creighton";
    					team2 = "Cincinnati";
    					nextGame = 12;
    					break;
    				case 8:
    					team1 = "Duke";
    					team2 = "Albany";
    					nextGame = 12;
    					break;
    				//Round Of 32
    				case 9:
    					System.out.println("ROUND OF 32");
    					team1 = bracket.G9.get(0);
    					team2 = bracket.G9.get(1);
    					nextGame = 13;
    					break;
    				case 10:
    					team1 = bracket.G10.get(0);
    					team2 = bracket.G10.get(1);
    					nextGame = 13;
    					break;
    				case 11:
    					team1 = bracket.G11.get(0);
    					team2 = bracket.G11.get(1);
    					nextGame = 14;
    					break;
    				case 12:
    					team1 = bracket.G12.get(0);
    					team2 = bracket.G12.get(1);
    					nextGame = 14;
    					break;
    				case 13:
    					System.out.println("SWEET SIXTEEN");
    					team1 = bracket.G13.get(0);
    					team2 = bracket.G13.get(1);
    					nextGame = 15;
    					break;
    				case 14:
    					team1 = bracket.G14.get(0);
    					team2 = bracket.G14.get(1);
    					nextGame = 15;
    					break;
    				case 15:
    					System.out.println("ELITE EIGHT");
    					team1 = bracket.G15.get(0);
    					team2 = bracket.G15.get(1);
    					nextGame = -1;
    					break;
    			}
    				
    			Random gen = new Random();
    			int winner = gen.nextInt(2);
    			System.out.println(winner);
    			
    			switch (winner) {
    				case 0:
    					bracket.addGame(nextGame, team1);
    					System.out.println(team1 + " defeated " + team2 + ".");
    					break;
    				case 1:
    					bracket.addGame(nextGame, team2);
    					System.out.println(team2 + " defeated " + team1 + ".");
    					break;
    			}
    		}
    		if (bracket.champion == "Louisville") {
    			correct = true;
    		}
    		return correct;
    		}
    	public static void main(String[] args) {
    		while (true) {
    			Scanner reader = new Scanner(System.in);
    			
    			int numbracket = 0;
    			int correctbrackets = 0;
    			bracketnum = 0;
    			
    			System.out.println("How many brackets should I generate?");
    			numbracket = reader.nextInt();
    			
    			for (int i = 1; i <= numbracket; i++) {
    				bracket.resetBracket();
    				System.out.println("--------------------------------BRACKET " + i +"--------------------------------");
    				boolean correct = Games();
    				if (correct) {
    					correctbrackets += 1;
    				}
    				if (i == numbracket) {
    					createBracket(true);
    				} else createBracket(false);
    				bracketnum++;
    			}
    			System.out.println(correctbrackets + " out of " + numbracket + " brackets were correct.");
    		}
    		
    	}
    
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,730
    Blog Entries
    7
    Rep Power
    21

    Default Re: Bracket Generator (Need a better way)

    This is just an English language thingie: but what's a 'bracket'? (besides ( or [ ...) I do speak quite some math-lingo if that may help.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    dstars5 is offline Member
    Join Date
    Mar 2013
    Posts
    19
    Rep Power
    0

    Default Re: Bracket Generator (Need a better way)

    Quote Originally Posted by JosAH View Post
    This is just an English language thingie: but what's a 'bracket'? (besides ( or [ ...) I do speak quite some math-lingo if that may help.

    kind regards,

    Jos
    By bracket I mean like a tournament. So something like this:
    Bracket Generator (Need a better way)-corhole-bracket.jpg

    Sorry about that!

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,730
    Blog Entries
    7
    Rep Power
    21

    Default Re: Bracket Generator (Need a better way)

    So the number of teams that start the tournament has to be a power of two, right?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    dstars5 is offline Member
    Join Date
    Mar 2013
    Posts
    19
    Rep Power
    0

    Default Re: Bracket Generator (Need a better way)

    Quote Originally Posted by JosAH View Post
    So the number of teams that start the tournament has to be a power of two, right?

    kind regards,

    Jos
    I think it's the number of games in the tournament. Because 1 team, the champion, won't lose. So a 64 team bracket would be 2 to the 63rd power (Like 9.2 quintillion or something like that).

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,730
    Blog Entries
    7
    Rep Power
    21

    Default Re: Bracket Generator (Need a better way)

    Quote Originally Posted by dstars5 View Post
    I think it's the number of games in the tournament. Because 1 team, the champion, won't lose. So a 64 team bracket would be 2 to the 63rd power (Like 9.2 quintillion or something like that).
    Not so; if the tournament starts with 16 teams (see your picture), there are 8 matches in the first round, 4 matches in the second round, 2 matches in the semi-final and 1 match in the finale; that makes 8+4+2+1 == 15 matches in total. In general when there are 2^n teams to start with, there are 2^n-1 matches to play. All you have to do is (conceptually) construct the tree and shuffle the 2^n teams at the start (leaves) of the tree.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    dstars5 is offline Member
    Join Date
    Mar 2013
    Posts
    19
    Rep Power
    0

    Default Re: Bracket Generator (Need a better way)

    Quote Originally Posted by JosAH View Post
    Not so; if the tournament starts with 16 teams (see your picture), there are 8 matches in the first round, 4 matches in the second round, 2 matches in the semi-final and 1 match in the finale; that makes 8+4+2+1 == 15 matches in total. In general when there are 2^n teams to start with, there are 2^n-1 matches to play. All you have to do is (conceptually) construct the tree and shuffle the 2^n teams at the start (leaves) of the tree.

    kind regards,

    Jos
    You kind of lost me with the last part there. Can you elaborate a little?

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,730
    Blog Entries
    7
    Rep Power
    21

    Default Re: Bracket Generator (Need a better way)

    Quote Originally Posted by dstars5 View Post
    You kind of lost me with the last part there. Can you elaborate a little?
    Sure: you can store a 'complete' binary tree in an array as follows: if you have a node at index position #i, its left and right childs are stored at position #2*i+1 and #2*i+2 (draw an example on paper and you'll see what I mean); the root is stored at position #0; for 16 teams, the leaf nodes are at positions #16-1 ... #31-1; (#15, #16) is a match, (#17, #18) is a match etc. Suppose team #w wins a match; the winner at a position #w is stored at a new position #(w/2) if w is odd, otherwise it is moved to position #(w-1)/2 i.e. it is moved towards the root (to its parent position). The winner of the finale ends up at position #0; I didn't have my espresso yet so I can't draw a beautiful ASCII-art illustration (yet); you have to do it yourself ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Tournament Pool and Bracket Tracker 0.6.1
    By java software in forum Java Software
    Replies: 0
    Last Post: 03-22-2013, 11:22 PM
  2. Class name generator
    By aianta in forum New To Java
    Replies: 3
    Last Post: 04-09-2011, 07:25 PM
  3. Bracket help - can't find the missing brackets.
    By Z E P H Y R in forum New To Java
    Replies: 0
    Last Post: 03-17-2009, 11:26 PM
  4. bracket confusion...?
    By gallimaufry in forum New To Java
    Replies: 4
    Last Post: 10-28-2008, 12:17 PM
  5. Extra bracket
    By CrazyShells Slam in forum New To Java
    Replies: 5
    Last Post: 05-16-2008, 07:12 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •