- 分享
- 0
- 人气
- 0
- 主题
- 10
- 帖子
- 551
- UID
- 36793
- 积分
- 862
- 阅读权限
- 17
- 注册时间
- 2006-6-8
- 最后登录
- 2015-5-7
- 在线时间
- 2568 小时
|
package snakeandladder;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
import javax.swing.border.*;
import javax.swing.Timer;
public class GameFrame extends JFrame implements ActionListener {
/**************************************************************/
/* Set up flexible variable for further developement or */
/* improvement convenient */
private int MAX_PLAYERS = 4;
private int MAX_RADIO_COLOR = MAX_PLAYERS * 4;
private int MAX_CELLS = 40;
private int MAX_CELLS_PER_ROW = 10;
/**************************************************************/
private JLabel[] chessBoard = new JLabel[MAX_CELLS];
private JRadioButton[] colorRadioButton = new JRadioButton[MAX_RADIO_COLOR];
private JTextField[] playerName = new JTextField[MAX_PLAYERS];
private JTextArea gameTextArea = new JTextArea();
private JLabel lblDiceNum, gameTitle, lblSnakes1, lblSnakes2, lblSnakes3, lblLadders1, lblLadders2, lblLadders3, lblNoofplayer = new JLabel();
private JLabel[] playerlbl = new JLabel[MAX_PLAYERS];
private JLabel[] colorlbl = new JLabel[MAX_PLAYERS];
private JLabel[] playerToken = new JLabel[MAX_PLAYERS];
private JButton jbtnStart, jbtnExitGame, jbtnRollDice = new JButton();
private JComboBox numPlayer = new JComboBox();
private ButtonGroup[] colorGroup = new ButtonGroup[MAX_PLAYERS];
private JPanel jpnlChoosenoofPlayer, jpnlBigSouth, jpnlBigCenter, jpnlBigNorth, jpnlBigNorthUp, jpnlBigNorthDown, jpnlNumbers, jpnlLaddersLabel, jpnlSnakesLabel, jpnlSnakeandLadder = new JPanel();
private JPanel[] jpnlRadio = new JPanel[MAX_PLAYERS];
private String[] textToPass;
private String[] cmbNum = {"1", "2", "3", "4"};
private int tempCount;
private String[] userToCheck;
private String[] colorSelected;
private String defaultText = "Instructions:\n1. Enter the players name \n2. Select a color for each player \n3. Click start game button";
private boolean endOfGame = false;
ImageIcon tokenImage[] = new ImageIcon[MAX_PLAYERS];
ImageIcon diceImage[] = new ImageIcon[6];
private int startXcoor = 3, startYcoor = 455;
private int newPosX, newPosY;
private int[] playerMove = new int[numPlayer.getSelectedIndex() + 1];
int geng = 0;
int gameCount = 0, turnNow = 0;
Game game = new Game();
Events event = new Events();
public GameFrame() {
jpnlBigNorth = new JPanel(new BorderLayout());
jpnlBigCenter = new JPanel(new BorderLayout());
jpnlBigSouth = new JPanel();
snakeandladderlabel();
initBigNorthPanel();
initBigCenterPanel();
initBigSouthPanel();
setDefault();
initTokenLocation();
initDiceImage();
/*********************************************************************************/
jpnlBigNorth.add(jpnlBigNorthUp, BorderLayout.NORTH);
jpnlBigNorth.add(jpnlBigNorthDown, "South");
jpnlBigCenter.add(jpnlNumbers, "Center");
jpnlBigCenter.add(jpnlSnakeandLadder, "East");
/*********************************************************************************/
add(jpnlBigNorth, "North");
add(jpnlBigCenter, "Center");
add(jpnlBigSouth, "South");
}
private void initBigNorthPanel() {
//a. upper north panel for titles and combobox
jpnlBigNorthDown = new JPanel(new GridLayout(4, 4, 15, 4));
jpnlBigNorthUp = new JPanel(new GridLayout(1, 2));
jpnlChoosenoofPlayer = new JPanel(new GridLayout(2, 2));
gameTitle = new JLabel("Snakes and Ladder");
Font font1 = new Font("SansSerif", Font.BOLD, 35);
gameTitle.setFont(font1);
gameTitle.setForeground(Color.GREEN);
numPlayer = new JComboBox(cmbNum);
lblNoofplayer = new JLabel("No of Players");
lblDiceNum = new JLabel();
/******************************************************/
jpnlChoosenoofPlayer.add(lblNoofplayer);
jpnlChoosenoofPlayer.add(lblDiceNum);
jpnlChoosenoofPlayer.add(numPlayer);
jpnlBigNorthUp.add(gameTitle);
jpnlBigNorthUp.add(jpnlChoosenoofPlayer);
//b. lower north panel for players names and colors
for (int i = 0; i < 4; i++) {
playerlbl = new JLabel("Name of player : " + (i + 1));
}
for (int l = 0; l < colorlbl.length; l++) {
colorlbl[l] = new JLabel("Select Color :");
}
for (int k = 0; k < playerName.length; k++) {
playerName[k] = new JTextField(10);
}
for (int m = 0; m < colorGroup.length; m++) {
colorGroup[m] = new ButtonGroup();
}
for (int i = 0; i < colorRadioButton.length; i += 4) {
colorRadioButton = new JRadioButton("R");
}
for (int i = 1; i < colorRadioButton.length; i += 4) {
colorRadioButton = new JRadioButton("B");
}
for (int i = 2; i < colorRadioButton.length; i += 4) {
colorRadioButton = new JRadioButton("Y");
}
for (int i = 3; i < colorRadioButton.length; i += 4) {
colorRadioButton = new JRadioButton("G");
}
for (int i = 0; i < 4; i++) {
jpnlRadio = new JPanel(new GridLayout(1, 4));
}
/********* Wrong Logic Here *************/
/********* Wrong Logic Here *************/
/********* Wrong Logic Here *************/
int m = 0;
for (int k = 0; k < 4; k++) {
jpnlRadio[k].add(colorRadioButton[m]);
colorGroup[k].add(colorRadioButton[m]);
jpnlRadio[k].add(colorRadioButton[m += 1]);
colorGroup[k].add(colorRadioButton[m]);
jpnlRadio[k].add(colorRadioButton[m += 1]);
colorGroup[k].add(colorRadioButton[m]);
jpnlRadio[k].add(colorRadioButton[m += 1]);
colorGroup[k].add(colorRadioButton[m]);
m += 1;
}
/********* Wrong Logic Here *************/
/********* Wrong Logic Here *************/
/********* Wrong Logic Here *************/
for (int n = 0; n < 4; n++) {
jpnlBigNorthDown.add(playerlbl[n]);
jpnlBigNorthDown.add(playerName[n]);
jpnlBigNorthDown.add(colorlbl[n]);
jpnlBigNorthDown.add(jpnlRadio[n]);
}
numPlayer.addActionListener(this);
}//initBigNorthPanel
|
|