Facebook Sharer
选择您要替换的背景颜色:
【农历新年】背景图片:
个性化设定
 注册  找回密码
查看: 5353|回复: 30
打印 上一主题 下一主题

[分享] 我的Singapore Poly 的 Java 的功课

[复制链接]

10

主题

0

好友

862

积分

青铜长老

Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7

跳转到指定楼层
1#
发表于 2009-5-4 01:24 PM |只看该作者 |倒序浏览
package snakeandladder;
import javax.swing.*;

public class GameFrameUser {
     public static void main(String[] args) {
    GameFrame f = new GameFrame();


        f.setVisible(true);
        f.setTitle("Snake and Ladder");
        f.setSize(800, 600);
        f.setResizable(false);
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     }
}




收藏收藏0

10

主题

0

好友

862

积分

青铜长老

Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7

2#
发表于 2009-5-4 01:28 PM |只看该作者
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



回复

使用道具 举报

10

主题

0

好友

862

积分

青铜长老

Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7

3#
发表于 2009-5-4 01:29 PM |只看该作者

    private void initBigCenterPanel() {
        jpnlNumbers = new JPanel(new GridLayout(4, 10));


        int count1 = 0, count2 = 0;
        int i = 0, tempk;

        for (int j = 0; j < 40; j++) {
            chessBoard[j] = new JLabel();
            jpnlNumbers.add(chessBoard[j]);
            chessBoard[j].setBorder(BorderFactory.createLineBorder(Color.BLUE));
        }

        if (i < 40) {
            for (int l = MAX_CELLS; l > 0; l--) {
                chessBoard.setText("" + l);
                //  System.out.print(l+" ");  /***** Checking Purpose  *****/
                count1++;
                i++;
                if (count1 > 9) {
                    for (int k = 10; k > 0; k--) {
                        tempk = l - k;
                        chessBoard.setText("" + tempk);
                        //System.out.print(""+tempk+" ");  /***** Checking Purpose  *****/
                        count2++;
                        i++;
                        if (count2 > 9) {
                            count1 = 0;
                        }//end reset count1
                    }//end for k loop
                    l -= MAX_CELLS_PER_ROW;
                }//end reset count2
            }
        }//end if



    }//initbigcenterpanel

    private void snakeandladderlabel() {

        //c. declare for labels inside jpnlsanekslabel and jpnlladderslabel
        lblSnakes1 = new JLabel("11===>4");
        lblSnakes2 = new JLabel("27===>9");
        lblSnakes3 = new JLabel("38===>19");
        lblLadders1 = new JLabel("2===>17");
        lblLadders2 = new JLabel("13===>29");
        lblLadders3 = new JLabel("25===>33");


        //a. panel for ladders
        jpnlLaddersLabel = new JPanel(new GridLayout(3, 1));
        jpnlLaddersLabel.setBorder(BorderFactory.createTitledBorder("Ladder"));
        //b. panel for snakes
        jpnlSnakesLabel = new JPanel(new GridLayout(3, 1));
        jpnlSnakesLabel.setBorder(BorderFactory.createTitledBorder("Snakes"));
        jpnlSnakeandLadder = new JPanel(new GridLayout(2, 1));



        //e. add snakes label and ladders label to panels
        jpnlLaddersLabel.add(lblLadders1);
        jpnlLaddersLabel.add(lblLadders2);
        jpnlLaddersLabel.add(lblLadders3);
        jpnlSnakesLabel.add(lblSnakes1);
        jpnlSnakesLabel.add(lblSnakes2);
        jpnlSnakesLabel.add(lblSnakes3);
        /*******************************************************/
        jpnlSnakeandLadder.add(jpnlSnakesLabel);
        jpnlSnakeandLadder.add(jpnlLaddersLabel);
    }

    private void initBigSouthPanel() {
        jpnlBigSouth = new JPanel();
        jbtnRollDice = new JButton("Roll the dice");
        jbtnStart = new JButton("Start Game");
        jbtnExitGame = new JButton("Exit Game");
        gameTextArea = new JTextArea(defaultText);
        jpnlBigSouth.setLayout(new GridLayout(1, 4, 3, 3));
        jpnlBigSouth.add(jbtnRollDice);
        jpnlBigSouth.add(jbtnStart);
        jpnlBigSouth.add(jbtnExitGame);
        jpnlBigSouth.add(gameTextArea);
        jbtnStart.addActionListener(this);
        jbtnRollDice.addActionListener(this);
        jbtnExitGame.addActionListener(this);

    }//bigsouthpanel

    private void setDefault() {

        playerName[0].setEditable(true);
        playerName[0].setText("");
        colorRadioButton[0].setSelected(true);


        for (int i = 1; i < MAX_PLAYERS; i++) {
            playerName.setEditable(false);
            playerName.setText("");
        }

        colorRadioButton[0].setSelected(true);
        colorRadioButton[5].setSelected(true);
        colorRadioButton[10].setSelected(true);
        colorRadioButton[15].setSelected(true);

        jbtnStart.setEnabled(true);
        jbtnRollDice.setEnabled(false);
        jbtnExitGame.setEnabled(true);

        numPlayer.setSelectedIndex(0);
        gameTextArea.setText(defaultText);


    }//setdefault

    private void playersNameToPass() {
        textToPass = new String[numPlayer.getSelectedIndex()];
        for (int i = 0; i <= numPlayer.getSelectedIndex(); i++) {
            if (playerName.isEnabled() == true) {
                textToPass = playerName.getText().trim();
            }
        }
    }

    private String[][] radioTextToPass() {

        String[][] radioColorText = new String[numPlayer.getSelectedIndex()][];

        for (int i = 0; i < numPlayer.getSelectedIndex(); i++) {
            for (int j = 0; j < 16; j++) {
                if (colorRadioButton[j].isEnabled() && colorRadioButton[j].isSelected()) {
                    radioColorText[j] = colorRadioButton[j].getText();
                }
            }
        }

        return radioColorText;
    }



回复

使用道具 举报

10

主题

0

好友

862

积分

青铜长老

Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7

4#
发表于 2009-5-4 01:34 PM |只看该作者

    public void actionPerformed(ActionEvent e) {
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
         /*Universe variable in this method will be use in comboBox setup and color compare*/

        if (e.getSource() == numPlayer) {
            //System.out.println(numPlayer.getSelectedIndex()); /*Testing Purpose */
            game.setTotalPlayer(numPlayer.getSelectedIndex());

            for (int i = 0; i <= numPlayer.getSelectedIndex(); i++) {
                playerName.setText("");
                playerName.setEditable(true);
                for (int k = numPlayer.getSelectedIndex() + 1; k < MAX_PLAYERS; k++) {
                    playerName[k].setEditable(false);

                }
                /*This calculation is to set the radiobutton to be enabled according to number players selected*/
                tempCount = (1 + numPlayer.getSelectedIndex()) * 4;

                for (int l = 0; l < tempCount; l++) {
                    colorRadioButton[l].setEnabled(true);
                }
                for (int m = tempCount; m < MAX_RADIO_COLOR; m++) {
                    colorRadioButton[m].setEnabled(false);
                }
            }// end big foor loop
        }
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
        if (e.getSource() == jbtnStart) {
            String t="",text="";
            for (int i = 0; i < 40; i++) {
            System.out.println(chessBoard.getLocation()/*f.lblChessBoard[5].getSize()*/);
            t =chessBoard.getSize().toString();
            text= chessBoard.getText();

        }
            game.clearAllData();
            game.setTotalPlayer(numPlayer.getSelectedIndex());
            game.setTotalRadioButton(tempCount);
            userToCheck = new String[numPlayer.getSelectedIndex() + 1];

            /*****************************Disabling All editable data field ************************************/
            int startPoint = 0;


            System.out.println("Index is ::" + numPlayer.getSelectedIndex());
            System.out.println("StartButton OK");  /* Show Start Button working*/
            /* Loop a String to pass value to Game Class for checking*/
            for (int i = 0; i <= numPlayer.getSelectedIndex(); i++) {
                userToCheck = playerName.getText().trim();
            }
            /* Now passing an array of String to Game Class for check empty String*/
            game.checkEmptyTextBox(userToCheck);

            /* Now getting boolean value to make sure whether need show message for empty string*/
            if (game.getEmptyTextBox() == true) {
                JOptionPane.showMessageDialog(null, game.getEmptyTextString(),"Message",JOptionPane.ERROR_MESSAGE);/* Tracking purpose due to java is phail*/
            } else {
                /*Now checking playername to make sure it not duplicated*/
                if (numPlayer.getSelectedIndex() > 0) {
//                        System.out.println(userToCheck[0]);
//                        System.out.println(userToCheck[1]);
//                        System.out.println(userToCheck[2]);
                    game.checkUserName(userToCheck);
                    System.out.println("Entered checkUserName loop");
                    /* Part for display msg if there is duplicated username*/
                    if (game.getBooleanUserName()) {
                        JOptionPane.showMessageDialog(null, game.getMessageCompareResult(),"Error",JOptionPane.ERROR_MESSAGE);
                    }
                }
                /*This part track down the success run loop message */
                System.out.println("Successfully check all names");
            }
            /* This part will be the checking part for color button */
            // basic concept is array. which is flexible to amount of player joined without checking enabled again
            // e.g : if 3 players joined then this methods will auto extract it out as String [] arr = {"R","B","G"}
            //       so any chges does to codes dun hav re-code
            colorSelected = new String[numPlayer.getSelectedIndex() + 1];
            System.out.println("tempCount value is " + tempCount); /* Checking tempCount value*/
            for (int mother = 0; mother <= numPlayer.getSelectedIndex(); mother++) {
                //this inner for loop is to extract out wanted value for radioButton ranged from 0-3,4-7,8-11,12-15
                //break is use to make sure come out from the loop after getting values to reduce retard looping.
                //temp count will keep chging according to players enabled
                for (int i = startPoint; i < tempCount; i++) {
                    if (colorRadioButton.isSelected()) {
                        colorSelected[mother] = colorRadioButton.getText();
                        startPoint += 4;
                        break;
                    }
                }
                System.out.println(colorSelected[mother]);
            }
            /* Assuming colorSelected at index 0 is 1st player and so on*/
            if (numPlayer.getSelectedIndex() > 0) {
                /* Now is the time to pass in value in to Game Class*/
                game.checkColorRadioBtn(colorSelected);
                if (game.getBooleanSameRadionButton()) {
                    JOptionPane.showMessageDialog(null, game.getSameRadioButton(),"Error",JOptionPane.ERROR_MESSAGE);
                }
                System.out.println("Successfully show compare result.");
            }

            if(game.getBooleanSameRadionButton()==false&&game.getBooleanUserName()==false&&game.getEmptyTextBox()==false){
            playerMove=new int[numPlayer.getSelectedIndex()+1];
            setPlayerFirstToken();
            jbtnRollDice.setEnabled(true);
            jbtnStart.setEnabled(false);
            /*****************************Disabling All editable data field ************************************/
            for (int i = 0; i < MAX_PLAYERS; i++) {
                playerName.setEditable(false);
            }

            for (int j = 0; j < MAX_RADIO_COLOR; j++) {
                colorRadioButton[j].setEnabled(false);
            }
            gameTextArea.setText("");
            gameTextArea.setText(game.getWhoseTurn(playerName[0].getText().trim()));
            jbtnStart.setText("");

            }
      
        }// end jbtnStart
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        if (e.getSource() == jbtnRollDice) {
        event.clearLadder();event.clearSnake();
        
            if (gameCount == turnNow&&endOfGame==false) {
                gameTextArea.setText(game.getWhoseTurn(playerName[gameCount].getText().trim()));
                game.rollDice();
                playerMove[gameCount] += game.getDiceNum();

                System.out.println(playerMove[gameCount]);
                jbtnStart.setIcon(diceImage[game.getDiceNum()-1]);
                JOptionPane.showMessageDialog(null,"Player "+playerName[gameCount].getText().trim()+" rolls "+game.getDiceNum());
                getPosition(playerMove[gameCount]);
                System.out.println(newPosX);
                System.out.println(newPosY);
                playerToken[gameCount].setLocation(newPosX+(gameCount*6),newPosY+(gameCount*(-6)));
                event.snakeAndLadder(playerMove[gameCount]);
                event.showSnakeandLadder();
                getPosition(event.getLastPos());
               
                playerToken[gameCount].setLocation(newPosX+(gameCount*6), newPosY+(gameCount*(-6)));


                    if(playerMove[gameCount]>=40){
                        endOfGame=true;
                        setDefault();
                        jbtnStart.setText("Start");

                        for(int i=0;i<numPlayer.getSelectedIndex();i++){
                            playerToken.setIcon(null);

                        }
                        JOptionPane.showMessageDialog(null, "Player "+(gameCount+1)+" win !!! ");                        
                    }

                gameCount++;
                turnNow++;
               
                if (gameCount > numPlayer.getSelectedIndex()) {
                    gameCount = 0;
                    turnNow = 0;
                }
            }else if(endOfGame){
                JOptionPane.showMessageDialog(null, "The Game End !!!");
                setDefault();
                for(int i=0;i<numPlayer.getSelectedIndex();i++){
                            playerToken.setIcon(null);

                        }

            }
            gameTextArea.setText(game.getWhoseTurn(playerName[gameCount].getText().trim()));
        }
        if(e.getSource()==jbtnExitGame){
            System.exit(0);
        }
    ////////////////////////////////////////////////////
    }//end actionlistener


回复

使用道具 举报

10

主题

0

好友

862

积分

青铜长老

Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7

5#
发表于 2009-5-4 01:35 PM |只看该作者

    public void initTokenLocation() {
        for (int i = 0; i < MAX_PLAYERS; i++) {
            playerToken = new JLabel();
            playerToken.setSize(30, 30);
            add(playerToken);
            playerToken.setLocation(startXcoor, startYcoor);
            startXcoor += 6;
            startYcoor -= 6;
        }

    }

    public void initDiceImage() {
        for (int i = 0; i < 6; i++) {
            diceImage = new ImageIcon("image/no" + i + ".png");
        }
    }

    private void setPlayerFirstToken() {

        for (int i = 0; i <= numPlayer.getSelectedIndex(); i++) {

                    tokenImage = new ImageIcon("image/" +  colorSelected + ".png");
                    playerToken.setIcon(tokenImage);
                    playerMove=1;
      
            
        }
    }

    public void getPosition(int currentMove) {
        int firstDigit, secondDigit, stimulatePosX, stimulatePosY, stimulatePos,temp,check=1;
        String jk;
        stimulatePos = MAX_CELLS - currentMove;

        firstDigit = (Math.abs(stimulatePos / 10));
        secondDigit = (Math.abs(stimulatePos % 10));
        
        stimulatePosY = (firstDigit * 85) + 210;
        System.out.println("First Digit is "+firstDigit);
        System.out.println("Modulus of first digit :"+(firstDigit%2));
        temp=(firstDigit%2);

         if (temp==check) {
             System.out.println(" Come ");
               stimulatePosX = ((9 - secondDigit) * 71) + 4;
        }
            else{
                stimulatePosX = (secondDigit * 71) + 4;
            }
        

        newPosX = stimulatePosX;
        newPosY = stimulatePosY;

    }
}


回复

使用道具 举报

10

主题

0

好友

862

积分

青铜长老

Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7

6#
发表于 2009-5-4 01:36 PM |只看该作者

package snakeandladder;
import javax.swing.*;

public class Game {

    private int totalPlayer,totalRadioButton;
    private String[]playerName=new String[totalPlayer];
    private boolean emptyPlayerName=false,sameUserName=false,samePlayerColor=false, endGame=false;
    private String emptyTextString="",userNameComparison="",sameRadioButton;
    private int turnNow=0;
    private int diceRolls;

    public void setTotalPlayer(int amountJoined){
        totalPlayer=amountJoined;
    }// end of setTotalPlayer

    public void setTotalRadioButton(int totalRadioBtn){
        totalRadioButton=totalRadioBtn;

    }//end of setTotalRadioButton

    public void setAllPlayerName(String [] allPlayerName){
        playerName=allPlayerName;
    }

    public void checkEmptyTextBox(String[] textbox){
        System.out.println("Run into checkEmptyTextBox now");
        System.out.println(textbox+"empty");
        int i;
            for(i=0;i<=totalPlayer;i++){
            if(textbox.equals("")){
                System.out.println("Entered Loop");
                emptyTextString+="Please insert player"+(i+1)+"'s name, thanks!\n";
                emptyPlayerName=true;
                }
            else{
                System.out.println("This prove cant enter null circuit !");
                emptyPlayerName=false;
            }
         }      
    }

    public boolean getEmptyTextBox(){

    return emptyPlayerName;
}
   
    public String getEmptyTextString(){
        
        return emptyTextString;
    }

    public String getMessageCompareResult(){
        return userNameComparison;
    }

    public boolean getBooleanUserName(){

        return sameUserName;
    }

    public String getSameRadioButton(){

        return sameRadioButton;
    }

    public boolean getBooleanSameRadionButton(){
        return samePlayerColor;
    }

    public void checkUserName(String[] playersName){
   
         int i,k,count=0,startPos=1;
         String temp;
         System.out.println(playersName[0]);
         /*count must < totalPlayer which indicates the last index of string that dun hav to check*/
     while(count<totalPlayer){
            temp=playersName[count];
         for(k=startPos;k<=totalPlayer;k++){
             //System.out.println("Check totalPlayer at Game line 67"+ totalPlayer);
            if(temp.equals(playersName[k])){
            userNameComparison+=" The player"+(k+1)+"'s name "+playersName[k]+" is same with "+"player"+(count+1)+"'s name.\n";
            sameUserName=true;
            System.out.println("Got name same :"+playersName[k]);
             }
             else{
            System.out.println("Name Checked , no name is the same");
            
            }
         }
         count++;
         startPos++;
        }//
    }//end of checkusername

    public void checkColorRadioBtn( String [] colorSelected){
        
    int i,count=0,startPos=1;
    String temp;

    //colorSelected= new String[totalPlayer+1];
   
        System.out.println("totalPlayer at Game Class line 98 "+totalPlayer);

    while(count<totalPlayer){
        temp=colorSelected[count];
        for(i=startPos;i<=totalPlayer;i++){
                    if(temp.equals(colorSelected)){
                    //System.out.println("The if loop for color checking ~");
                    sameRadioButton+=" The player "+(i+1)+"'s color is same as player "+(count+1+".\n");
                    samePlayerColor=true;
                }
                else
                {
                    System.out.println("Color Checked , no same color selected");                                      
                }               
        }
       count++;
       startPos++;
    }//end while

//    for( i=0;i<=totalPlayer;i++){
//        for(j=0;j<=totalRadioBtnToCheck;j++){
//            if(i!=j){
//                if(colorSelected[j].equals(colorSelected[j])){
//                    player[j]++;
//                    sameRadioButton+=" The player "+playerName+"'s color is same with "+player[j]+".\n";
//                    }
//                }
//            }
//        }


    }

    public String getWhoseTurn(String playerNow){
        
        String returnTurns="";
        returnTurns="\n"+playerNow+"'s turn.\nPlease roll the dice." +"\n";
        return returnTurns;
    }

    public int getTurnNow(){

        return turnNow;
    }

    public void setEndGame(boolean endgame){

        this.endGame=endgame;

    }

    public void rollDice(){
        
         this.diceRolls=(1 + (int) (Math.random() * 6));
    }

    public void clearAllData(){
        emptyTextString="";userNameComparison="";emptyPlayerName=false;sameUserName=false;
        samePlayerColor=false; endGame=false;sameRadioButton="";
    }

    public int getDiceNum(){
        
        return diceRolls;
    }
}


回复

使用道具 举报

10

主题

0

好友

862

积分

青铜长老

Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7

7#
发表于 2009-5-4 01:36 PM |只看该作者

package snakeandladder;


import javax.swing.*;

public class Events {

private int start,end;
private String type="";
String s="snakes",l="ladders";
boolean showMsg=false;

public void snakeAndLadder(int position){

     end = position;
     start = position;

        switch (start) {
            case 11:
                end = 4;
                type = s;

                break;
            case 27:
                end = 9;

                type = s;

                break;
            case 38:
                end = 19;

                type = s;

                break;
            case 2:
                end = 17;

                type = l;
              ;
                break;
            case 13:
                end = 29;

                type = l;

                break;
            case 25:
                end = 33;
                type = l;

                break;

        }//switch
}
        public int getLastPos(){
            return end;
        }

        public void showSnakeandLadder(){
            if(type.equals(s)){
                JOptionPane.showMessageDialog(null, " You land on snake at "+start+" , your new position will be "+end);
            }
            else if(type.equals(l)){
                JOptionPane.showMessageDialog(null, "You land on ladder at position "+ start+", your new position will be "+end );
            }

        }

        public void clearSnake(){
            this.type="";
        }

        public void clearLadder(){
            this.type="";
        }
}


回复

使用道具 举报

10

主题

0

好友

862

积分

青铜长老

Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7

8#
发表于 2009-5-4 01:39 PM |只看该作者
谢谢各位英雄的指点。 这个游戏是snake and ladder,还有些在结束game后的东西没 repaint。 一些 coordinating的 algo 也还没解决。

希望这些code能够帮忙入门新手学些简单的东西。 em0009


回复

使用道具 举报

13

主题

0

好友

2113

积分

白金长老

Rank: 10

9#
发表于 2009-5-4 10:08 PM |只看该作者
我没时间哦
帮不到你咯


回复

使用道具 举报

62

主题

5

好友

3715

积分

本站名嘴

Rank: 11Rank: 11

10#
发表于 2009-5-4 11:37 PM |只看该作者
你应该把那里不会的地方放出来
而不是放整个code让我们慢慢看


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

JBTALKS.CC |联系我们 |隐私政策 |Share

GMT+8, 2024-5-24 01:17 AM , Processed in 0.104427 second(s), 26 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

Ultra High-performance Dedicated Server powered by iCore Technology Sdn. Bhd.
Domain Registration | Web Hosting | Email Hosting | Forum Hosting | ECShop Hosting | Dedicated Server | Colocation Services
本论坛言论纯属发表者个人意见,与本论坛立场无关
Copyright © 2003-2012 JBTALKS.CC All Rights Reserved
合作联盟网站:
JBTALKS 马来西亚中文论坛 | JBTALKS我的空间 | ICORE TECHNOLOGY SDN. BHD.
回顶部