Started restructering the project.

This commit is contained in:
Jonathan Bernard
2010-07-04 08:01:51 -05:00
parent fc4084fb6e
commit 2ea56d05c5
8 changed files with 215 additions and 89 deletions

View File

@ -0,0 +1,14 @@
package com.jdbernard.teammaker
public class Player implements Comparable<Player> {
String name
int gamesSat
public int compareTo(Player that) {
int r = this.gamesSat - that.gamesSat
if (r == 0) r = this.name.compareTo(that.name)
return r
}
public String toString() { return name }
}

View File

@ -0,0 +1,14 @@
package com.jdbernard.teammaker
public abstract class PlayerChooser {
public abstract Player choose (def players)
public abstract float calculateOdds(def players, def player)
public final static Random = new Random(System.currentTimeMillis())
public static final Player chooseRandomly(def choices) {
choices.sort { rand.nextInt() }
return choices[0]
}
}

View File

@ -0,0 +1,68 @@
package com.jdbernard.teammaker
import java.awt.Color
import java.awt.Component
import javax.swing.EmptyBorder
import javax.swing.JLabel
import javax.swing.JList
import javax.swing.JPanel
import javax.swing.LineBorder
import javax.swing.ListCellRenderer
public class PlayerListCellRenderer extends JPanel implements ListCellRenderer {
boolean showStats
boolean colored
private PlayerChooser chooser
private JLabel nameLabel
private JLabel statsLabel
private static def linedBorder = new LineBorder(Color.BLACK)
private static def emptyBorder = new EmptyBorder(1)
private def colors = [Color.getHSBColor(0.32f, 1f, 1f),
Color.getHSBColor(0.24f,1f,1f), Color.getHSBColor(0.16f, 1f, 1f),
Color.getHSBColor(0.08f, 1f, 1f), Color.getHSBColor(0.0f, 1f, 1f)]
public PlayerListCellRenderer(Map params) {
showStats = params.showStats ?: false
colored = params.colored ?: false
chooser = params.chooser ?: WeightedChooser.getInstance()
setLayout(new BorderLayout())
setOpaque(colored)
nameLabel = new JLabel()
nameLabel.opaque = false
nameLabel.horizontalAlignment = JLabel.LEADING
add(nameLabel, BorderLayout.WEST)
if (showStats) {
statsLabel = new JLabel()
statsLabel.horizontalAlignment = JLabel.TRAILING,
statsLabel.opaque = false
add(statsLabel, BorderLayout.EAST)
}
}
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
assert value instanceof Player
nameLabel.setText(value.name)
if (isSelected) setBorder(linedBorder)
else setBorder(emptyBorder)
if (showStats)
statsLabel.text = value.gamesSat
if (colored) {
def c = colors[Math.min(4, value.gamesSat)]
setBackground(c)
}
return this
}
}

View File

@ -0,0 +1,14 @@
package com.jdbernard.teammaker
public class RandomChooser extends PlayerChooser {
public Player choose(def players) {
def choices = []
players.each { choices << it }
chooseRandomly(choices)
}
public float calculateOdds(def players, def player) {
return 1f / (float) players
}
}

View File

@ -0,0 +1,28 @@
package com.jdbernard.teammaker
import javax.swing.AbstractListModel
public class SortedPlayerModel extends AbstractListModel {
private TreeSet<Player> set = new TreeSet<Player>()
public int getSize() { return set.size() }
public Object getElementAt(int index) {
int i = 0
for (player in set) {
if (i == index) return player
i++
}
throw new ArrayIndexOutOfBoundsException()
}
public void addElement(Player p) {
set.add(p)
fireIntervalAdded(this, 0, set.size())
}
public void removeElement(Player p) {
set.remove(p)
fireIntervalRemoved(this, 0, set.size())
}
}

View File

@ -0,0 +1,218 @@
package com.jdbernard.teammaker
import groovy.beans.Bindable
import groovy.swing.SwingBuilder
import java.awt.GridBagConstraints as GBC
import java.awt.GridBagLayout
import java.awt.BorderLayout
import javax.swing.DefaultListModel
import javax.swing.JFrame
import javax.swing.JOptionPane
public class TeamMaker {
/* ======== MODEL ======== */
public static def swing = new SwingBuilder()
public static final String version = "0.2"
def frame
def team1List
def team2List
def team1WinsButton
def team2WinsButton
def newGameButton
def sittingList
PlayerChooser chooser = new RandomChooser()
@Bindable boolean inGame = false
@Bindable int teamSize = 4
/* ======== VIEW ======== */
/*
+--------------------------------------------------+
| JDB TeamMaker v0.2 _[]X |
+--------------------------------------------------+
| |
| +-Current-Game--------------+ +-Sitting-Players+ |
| | | | | |
| | Team 1 Team 2 | | Jeptha 3 | |
| | Derrick Eric | | Jack 2 | |
| | Shane Brandon | | Sam 2 | |
| | Shane Darren | | Greg 2 | |
| | Christian Brett | | Frank 1 | |
| +---------------------------+ | Chris D 1 | |
| | Joshua 1 | |
| | | |
| | | |
| | | |
| | | |
| | | |
| +----------------+ |
+--------------------------------------------------+
*/
private void init() {
frame = swing.frame(title: "JDB TeamMaker v$version",
size: [600, 400], locationRelativeTo: null,
defaultCloseOperation: JFrame.EXIT_ON_CLOSE) {
panel() {
gridBagLayout()
panel(constraints: gbc(gridx: 0, gridy: 0, fill: GBC.BOTH,
insets: [5, 5, 5, 0], weightx: 3, weighty: 1),
border: titledBorder(title: 'Current Game')) {
gridBagLayout()
label('Team A', constraints: gbc(gridx: 0, gridy: 0,
fill: GBC.BOTH, insets: [5, 5, 0, 0]))
label('Team B', constraints: gbc(gridx: 1, gridy: 0,
fill: GBC.BOTH, insets: [5, 5, 0, 5]))
def teamListRenderer = new PlayerListCellRenderer(showStats: false,
colored: false)
team1List = list(cellRenderer: teamListRenderer,
constraints: gbc(gridx: 0, gridy: 1, fill: GBC.BOTH,
insets: [5, 5, 0, 0], weightx: 2, weighty: 2),
model: new DefaultListModel())
team1WinsButton = button('Team A Wins',
constraints: gbc(gridx: 0, gridy: 2, anchor:
GBC.CENTER, insets: [5, 5, 5, 0]),
enabled: false,
actionPerformed: {
sittingList.model.each { it.gamesSat++ }
team1List.model.each { player ->
player.gamesSat = 0
sittingList.model.addElement(player)
}
team1List.model.clear()
team1List.repaint()
sittingList.repaint()
inGame = false
team1WinsButton.enabled = false
team2WinsButton.enabled = false
})
team2List = list(cellRenderer: teamListRenderer,
constraints: gbc(gridx: 1, gridy: 1, fill: GBC.BOTH,
insets: [5, 5, 0, 5], weightx: 2, weighty: 2),
model: new DefaultListModel())
team2WinsButton = button('Team B Wins',
constraints: gbc(gridx: 1, gridy: 2,
anchor: GBC.CENTER, insets: [5, 5, 5, 5]),
enabled: false,
actionPerformed: {
sittingList.model.each { it.gamesSat++ }
team2List.model.each { player ->
player.gamesSat = 0
sittingList.model.addElement(player)
}
team2List.model.clear()
team2List.repaint()
sittingList.repaint()
inGame = false
team1WinsButton.enabled = false
team2WinsButton.enabled = false
})
}
scrollPane(constraints: gbc(gridx: 1, gridy: 0, gridwidth: 2,
fill: GBC.BOTH, insets: [5, 5, 0, 5], weighty: 1),
border: titledBorder(title: 'Sitting Players')) {
sittingList = list(cellRenderer: new PlayerListCellRenderer(
showStats: true, colored: true),
model: new SortedPlayerModel())
}
button('Next Game', constraints: gbc(gridx: 0, gridy: 1,
anchor: GBC.CENTER, insets: [5, 5, 5, 0]),
actionPerformed: { newGame() })
button('Add Player', constraints: gbc(gridx: 1, gridy: 1,
anchor: GBC.CENTER, insets: [5, 5, 5, 0]),
actionPerformed: {
def name = JOptionPane.showInputDialog(frame,
"Enter the new player's name: ", "New Player...",
JOptionPane.QUESTION_MESSAGE)
def player = new Player()
player.name = name
player.gamesSat = 0
sittingList.model.addElement(player)
})
newGameButton = button('Delete Player',
constraints: gbc(gridx: 2, gridy: 1, anchor: GBC.CENTER,
insets: [5, 5, 5, 5]),
actionPerformed: {
sittingList.model.removeElement(
sittingList.selectedValue) })
}
}
}
/* ======== CONTROLLER ======== */
public static void main(String[] args) {
def inst = new TeamMaker()
inst.frame.show()
}
TeamMaker() {
init()
}
private void newGame() {
if (team1List.model.size() < teamSize)
populate(team1List, sittingList, teamSize)
if (team2List.model.size() < teamSize)
populate(team2List, sittingList, teamSize)
team1WinsButton.enabled = true
team2WinsButton.enabled = true
newGameButton.enabled = false
}
private static void populate(def teamList, def sittingList, int teamSize) {
while (teamList.model.size() < teamSize) {
def player = choosePlayer(sittingList)
teamList.model.addElement(player)
sittingList.model.removeElement(player)
}
}
private static Player choosePlayer(def list) {
def choices = []
list.model.each { player ->
player.gamesSat.times { choices << player }
}
// if no players have sat at least one game, add all once
if (choices.size() == 0) list.model.each { player -> choices << player }
choices.sort { rand.nextInt() }
println choices
return choices[0]
}
private float calculatePercentage(Player p, def sittingList) {
}
}