/*
* Championship.java
*
* $Id: jprison.el,v 1.1 1999/03/03 15:12:17 beaufils Exp $
*
JPRISON_COPYRIGHT
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import fr.lifl.prison.*;
import fr.lifl.prison.util.*;
import fr.lifl.prison.strategies.*;
/**
* This applet may take two parameters for localization :
*
*
* At that time the default language is english, and the following ones are
* set up in MatchMessages Properties files :
*
*/
public class Championship extends Applet {
// Declaration of the differents graphics components
Panel mainPanel;
BorderLayout mainLayout;
Panel choicePanel;
BorderLayout choiceLayout;
Panel line1Panel;
FlowLayout line1Layout;
Panel line2Panel;
FlowLayout line2Layout;
ScrollPane line2ScrollPane;
Label title;
List list_of_strategies;
Label text1;
Choice choice_of_B;
Label text2;
TextField length_of_match;
Label text3;
Button button_play;
Panel matrixPanel;
// Declaration of localised needed object
ResourceBundle messages;
// Game objects
BasicStrategies strats;
// Initialisation of the applet
public void init() {
setBackground(Color.white);
// Set the locale string
set_locale();
// Create all the needed components
create_components();
// Create the basic strategies
strats = new BasicStrategies(getLocale());
// Add all the strategies in the two choices
for (Enumeration e = strats.players(); e.hasMoreElements();) {
list_of_strategies.addItem(e.nextElement().toString());
}
// add the mainPanel to the applet
add(mainPanel);
}
// Set the locale informations
private void set_locale() {
String language;
String country;
// Changed the locale only if asked
if (getParameter("language") != null
&& getParameter("country") != null) {
setLocale(new Locale(getParameter("language"),
getParameter("country")));
}
// Creating the ressources
messages = ResourceBundle.getBundle("ChampionshipMessages",
getLocale());
}
// Creation of the differents components
private void create_components() {
mainPanel = new Panel();
mainLayout = new BorderLayout();
mainPanel.setLayout(mainLayout);
title = new Label(messages.getString("title"));
title.setAlignment(Label.CENTER);
mainPanel.add(title, "North");
choicePanel = new Panel();
choiceLayout = new BorderLayout();
choicePanel.setLayout(choiceLayout);
line1Panel = new Panel();
line1Layout = new FlowLayout();
line1Panel.setLayout(line1Layout);
list_of_strategies = new List(5, true);
text2 = new Label(messages.getString("during"));
text2.setAlignment(Label.CENTER);
length_of_match = new TextField("20", 2);
text3 = new Label(messages.getString("moves"));
text3.setAlignment(Label.CENTER);
button_play = new Button(messages.getString("play"));
button_play.setBackground(Color.orange);
button_play.addActionListener(new Championship.ButtonPlay());
line1Panel.add(list_of_strategies);
line1Panel.add(text2);
line1Panel.add(length_of_match);
line1Panel.add(text3);
line1Panel.add(button_play);
choicePanel.add("North", line1Panel);
line2ScrollPane = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
line2ScrollPane.setSize(370,170);
line2Panel = new Panel();
line2Layout = new FlowLayout();
line2Layout.setAlignment(FlowLayout.CENTER);
line2Panel.setLayout(line2Layout);
line2ScrollPane.add(line2Panel);
choicePanel.add("Center", line2ScrollPane);
mainPanel.add(choicePanel);
}
// The Button listener
public class ButtonPlay implements ActionListener {
// Method called when the button is pressed
public void actionPerformed(ActionEvent e) {
// Just in case it will be long
line2Panel.removeAll();
Label waitLabel = new Label(messages.getString("wait"));
waitLabel.setAlignment(Label.CENTER);
line2Panel.add(waitLabel);
validate();
// Create and compute players list, paramaters and a tournament
int choosenStrategies[] = list_of_strategies.getSelectedIndexes();
PlayerList listOfPlayer = new PlayerList();
Parameters p = new Parameters(getLength());
for (int i = 0; i < choosenStrategies.length; i += 1) {
listOfPlayer.addPlayer(strats.playerAt(choosenStrategies[i]));
}
Tournament tournament = new Tournament(listOfPlayer, p);
int winner = listOfPlayer.indexOf(tournament.compute());
// Update the display
Matrix matrix = tournament.getPayoff();
Matrix results = new Matrix(listOfPlayer.size(), 1);
results.setColumnAt(0, tournament.getFinalScores());
results.setColumnHeaderAt(0, messages.getString("score"));
matrix.setRowBackground(winner, Color.orange);
results.setRowBackground(winner, Color.orange);
line2Panel.removeAll();
line2Panel.add(matrix.getPanel(true, true));
line2Panel.add(results.getPanel(false,true));
// Give the focus back to the button
button_play.requestFocus();
// Validate all changes
validate();
}
// Get the length of the match with some constraints
private int getLength() {
// Get the value in the text field
int n = Integer.valueOf(length_of_match.getText()).intValue();
// If not in the range 1..20 set it to 20
if (n < 1 || n > 20) {
length_of_match.setText("20");
return 20;
}
else
return n;
}
}
}
/* End of Championship.java. */