JBTALKS.CC

标题: [分享] 我的Singapore Poly 的 Java 的功课 [打印本页]

作者: duo8668    时间: 2009-5-4 01:24 PM
标题: [分享] 我的Singapore Poly 的 Java 的功课
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);

     }
}

作者: duo8668    时间: 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


作者: duo8668    时间: 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;
    }


作者: duo8668    时间: 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

作者: duo8668    时间: 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;

    }
}

作者: duo8668    时间: 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;
    }
}

作者: duo8668    时间: 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="";
        }
}

作者: duo8668    时间: 2009-5-4 01:39 PM
谢谢各位英雄的指点。 这个游戏是snake and ladder,还有些在结束game后的东西没 repaint。 一些 coordinating的 algo 也还没解决。

希望这些code能够帮忙入门新手学些简单的东西。 em0009
作者: goodday    时间: 2009-5-4 10:08 PM
我没时间哦
帮不到你咯

作者: goodhermit95    时间: 2009-5-4 11:37 PM
你应该把那里不会的地方放出来
而不是放整个code让我们慢慢看
作者: yclaw1015    时间: 2009-5-5 06:33 PM
em0018 em0018
java真吓人em0019
我都还没学呢
作者: duo8668    时间: 2009-5-6 05:11 AM
各位误会了 em0026
我是放上来让大家参考互相学习学习的。这个成品是可以Run的。只不过比较fine的地方我没去改。。。因为是之前的。em0026

最近被一个帮了很多次的同学叫 " Selfish Creature",感觉很不愉快。很像生下来就注定一定要帮她似的。。。
作者: 毛毛小子    时间: 2009-5-6 05:18 PM
标题: 回复 #12 duo8668 的帖子
谢谢参考哦,我也是有类似的JAVA程序
不过没你的那么够力
谢谢分享
作者: Super-Tomato    时间: 2009-5-8 05:18 PM
要看完真的有些累, 不過大致上是眇過所有 Class 的 Function name了, 蛇棋中主架構是一開始設定介面 InitBigNorth/Center/South 等部分, 然後對 Panel 進行監聽并通過 actionPerformed 來執行. Event class中的爬上和跌落是固定好的哦, 要是可以 random 會比較好玩

對初學者來說雖然是不錯的例子, 但是 coding 太多要一開始下手研究會抓不著重點
作者: duo8668    时间: 2009-5-8 07:51 PM
题目不能放random不然没分
看得出来你的programming不错,可以给我你的msn吗?想跟你请教关于 drag and drop 的设定。还有 java 2D animation
作者: goodhermit95    时间: 2009-5-8 10:41 PM
标题: 回复 #15 duo8668 的帖子
不是不错,是很强
作者: duo8668    时间: 2009-5-8 11:45 PM
标题: 回复 #16 goodhermit95 的帖子
还要跟各位大大学习呢。。。
谁可以教我 java的 drag and drop。。。 看Tutorial 看到gong gong
作者: goodhermit95    时间: 2009-5-8 11:51 PM
标题: 回复 #17 duo8668 的帖子
drop什么东西?file?文字?
作者: duo8668    时间: 2009-5-9 12:27 AM
picture file.
想做个方便user drag and drop picture 的 功能,此功能会导入file 的 URL。imageicon就是应用这个URL来显示图档。基本上用file browser可以解决不过觉得麻烦。我想做的这个是其中之一的部分,它会将image录进txt file来储存。老师不给用SQL,只能用txt file来当作SQL那样。

不过我在找有没有其他display image/picture的东西。
作者: goodhermit95    时间: 2009-5-9 10:50 AM
我做过一个这样的,不过是VB
你要看吗
作者: duo8668    时间: 2009-5-9 11:18 AM
要要~
可以参考

不过什么是 protected access ? mean 要自己extends 还是 implements TransferHandler ?
作者: Super-Tomato    时间: 2009-5-9 12:33 PM
原帖由 duo8668 于 2009-5-8 07:51 PM 发表
题目不能放random不然没分
看得出来你的programming不错,可以给我你的msn吗?想跟你请教关于 drag and drop 的设定。还有 java 2D animation


我只是初學者, 甚麼都不太會哦, 而且也沒用 msn 很久了, D&D的教學網上很多教程和例子, 可以搜索到處看看, sun 網站的雖然說得很清楚但太長了.



原帖由 duo8668 于 2009-5-9 11:18 AM 发表
要要~
可以参考

不过什么是 protected access ? mean 要自己extends 还是 implements TransferHandler ?


private 或 final
作者: duo8668    时间: 2009-5-9 06:40 PM
原帖由 Super-Tomato 于 2009-5-9 12:33 PM 发表


我只是初學者, 甚麼都不太會哦, 而且也沒用 msn 很久了, D&D的教學網上很多教程和例子, 可以搜索到處看看, sun 網站的雖然說得很清楚但太長了.


最近在做学校Assignment,改我的filereader的function改到头痛。readline只读一行,要自己做multiline的。因为不能用SQL的关系还要做delimiterdetect,不可以单一用逗号之类的。听说regex可以帮到忙可是我又不太会那种可以征查escape characters的formulae。。。

private 或 final

这个是说什么???
作者: Super-Tomato    时间: 2009-5-9 07:33 PM
原帖由 duo8668 于 2009-5-9 06:40 PM 发表


最近在做学校Assignment,改我的filereader的function改到头痛。readline只读一行,要自己做multiline的。因为不能用SQL的关系还要做delimiterdetect,不可以单一用逗号之类的。听说regex可以帮到忙可是我又 ...



regular expression 其實不難, 只是一開始用起來比較蹩腳, 但是用慣之後就非常容易而且可以簡化很多編寫步驟

你不是問 protected access 嗎? Private 不是就只有本身可以被訪問, 而 Final 則是只限讀取不被更變.
作者: duo8668    时间: 2009-5-9 07:52 PM
标题: 回复 #24 Super-Tomato 的帖子
哦哦哦~~~
原来如此。

那么你了解regex怎么用了吗?可以教我吗???em0025

[ 本帖最后由 duo8668 于 2009-5-9 07:54 PM 编辑 ]
作者: Super-Tomato    时间: 2009-5-9 08:02 PM
原帖由 duo8668 于 2009-5-9 07:52 PM 发表
哦哦哦~~~
原来如此。

那么你了解regex怎么用了吗?可以教我吗???em0025



多瀏覽 sun 的網站就可以學到很多基礎
http://java.sun.com/docs/books/tutorial/essential/regex/
作者: duo8668    时间: 2009-5-9 08:35 PM
标题: 回复 #26 Super-Tomato 的帖子
本来一知半解。。。
看了这个。。。。
就完全不了解。。。
作者: ~叫我宝哥~    时间: 2009-5-11 09:12 AM
标题: 回复 #27 duo8668 的帖子
请问一下computing loan payments 程序方式怎样写啊?/
我做这个做到疯掉了em0028 em0016
作者: duo8668    时间: 2009-5-11 10:28 AM
标题: 回复 #28 ~叫我宝哥~ 的帖子
开个帖 放上 source code 这里就会有大大帮忙的了
作者: goodhermit95    时间: 2009-5-11 09:25 PM
我只看得懂怎么vilate 某个pattern
作者: duo8668    时间: 2009-5-11 11:19 PM
标题: 回复 #30 goodhermit95 的帖子
是playername 之类的???
不过目前在找方法replace certain string value in text file 。。。

希望这次的功课不会像这个snake and ladder 那么惨。。。很多 rubbish logic。




欢迎光临 JBTALKS.CC (https://jbtalks.my/) Powered by Discuz! X2.5