/*
* Match.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 Match extends Applet {
// Declaration of the differents graphics components
Panel mainPanel;
BorderLayout mainLayout;
Panel choicePanel;
GridLayout choiceLayout;
Panel line1Panel;
FlowLayout line1Layout;
Panel line2Panel;
FlowLayout line2Layout;
Panel line3Panel;
FlowLayout line3Layout;
Panel line4Panel;
FlowLayout line4Layout;
Label title;
Choice choice_of_A;
Label text1;
Choice choice_of_B;
Label text2;
TextField length_of_match;
Label text3;
Button button_play;
Label game_of_A;
Label game_of_B;
// 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 (int i = 0; i < strats.size(); i++) {
choice_of_A.addItem(strats.playerAt(i).toString());
choice_of_B.addItem(strats.playerAt(i).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("MatchMessages", 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 GridLayout(4,1);
choicePanel.setLayout(choiceLayout);
line1Panel = new Panel();
line1Layout = new FlowLayout();
line1Panel.setLayout(line1Layout);
choice_of_A = new Choice();
text1 = new Label(messages.getString("meets"));
text1.setAlignment(Label.CENTER);
choice_of_B = new Choice();
line1Panel.add(choice_of_A);
line1Panel.add(text1);
line1Panel.add(choice_of_B);
choicePanel.add(line1Panel);
line2Panel = new Panel();
line2Layout = new FlowLayout();
line2Panel.setLayout(line2Layout);
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 ButtonPlay());
line2Panel.add(text2);
line2Panel.add(length_of_match);
line2Panel.add(text3);
line2Panel.add(button_play);
choicePanel.add(line2Panel);
line3Panel = new Panel();
line3Layout = new FlowLayout();
line3Layout.setAlignment(FlowLayout.LEFT);
line3Panel.setLayout(line3Layout);
game_of_A = new Label();
line3Panel.add(game_of_A);
choicePanel.add(line3Panel);
line4Panel = new Panel();
line4Layout = new FlowLayout();
line4Layout.setAlignment(FlowLayout.LEFT);
line4Panel.setLayout(line4Layout);
game_of_B = new Label();
line4Panel.add(game_of_B);
choicePanel.add(line4Panel);
mainPanel.add(choicePanel);
}
// The Button listener
public class ButtonPlay implements ActionListener {
// Method called when the button is pressed
public void actionPerformed(ActionEvent e) {
// Create and compute players, paramaters and a game
Player a =(Player)
strats.playerAt(choice_of_A.getSelectedIndex()).clone();
Player b =(Player)
strats.playerAt(choice_of_B.getSelectedIndex()).clone();
Parameters p = new Parameters(getLength());
Game g = new Game(a, b, p);
g.compute();
// Update the display for player A
game_of_A.setText(g.getGameOf(a) + " : " +
choice_of_A.getSelectedItem() + " = " +
g.getScoreOf(a));
game_of_A.invalidate();
// Update the display for player B
game_of_B.setText(g.getGameOf(b) + " : " +
choice_of_B.getSelectedItem() + " = " +
g.getScoreOf(b));
game_of_B.invalidate();
// 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 Match.java. */