Programs Experts Community Question Online Exam ResourceLink Gallery Members Search
Login
Username:
Password:
New user? Sign up
Forgot Password?
Notice
New Category Started

Question, Resource &
Rich Text Editor
for content text input
Invite Friends
Email *
Firstname *
Lastname
 

 Topic Name: Can anyone make my code simplier and neater? *Domino Game Proper*
 
View Profile of Mark
Mark
Domino Game Proper
[code]
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author vista
 */

import java.util.*;
import javax.swing.JOptionPane;
class Variables {

   static Dice dice=new Dice (49);
   static tiles tile=new tiles();
   static String [] character=new String[3];
   static int charcount=0;
   static int player1[][]=new int[2][49];
   static int player2[][]=new int[2][49];
   static int dicevalue;
   static boolean check;
   static boolean checkroll;
   static boolean checkroll1;
   static boolean checkgame=true;
   static int highhead1=0;
   static int highhead2=0;
   static int hightail1=0;
   static int hightail2=0;
   static int map[][]=new int[2][98];
   static int mapx2=49;
   static int mapy2=49;
   static int mapx22=49;
   static int mapy22=49;
   static int toplace;
   static int counter1;
   static int counterplayer1;
   static int counterplayer2;
   static int temp;
}


 class Dice {

    Random r =new Random();
    int sides;

 //constructor
    public Dice(int s){
       sides = s;
    }
 //roll method depending on sides sa taas
    public int roll(){

    return r.nextInt(sides);

    }
}

 class tiles {
  static int headcount=0;
  static int tailcount=0;
  static int a=0;
  static int b=0;
  int arraydomino[][]=new int [2][49];
    public void dominotiles(){

        for(int x=0;x<49;x++){
            for(int y=0; y<2; y++){
                if(y==0){
                    if(headcount==7){
                        headcount=0;
                        a=a+1;
                    }
                    if(headcount<=6){
                    arraydomino[y][x]=a;
                    headcount=headcount+1;
                    }
                }
                else {
                    if(tailcount==7){
                        tailcount=0;
                    }
                    if(tailcount<=6){
                    arraydomino[y][x]=tailcount;
                    tailcount=tailcount+1;
                    }
                }
            }
        }
    }
}


public class Main extends Variables {
    public static void main(String[]args){
      tile.dominotiles();
      for(int x=0;x<49;x++){
        for(int y=0; y<2; y++){
                    player1[y][x]=7;
                    player2[y][x]=7;
        }
      }
     for(int x=0;x<98;x++)
        for(int y=0;y<2;y++){
                map[y][x]=7;
        }
      newCharacter();
      newCharacter(); 
      JOptionPane.showMessageDialog(null,character[0]+" will draw 7 random tiles");
      for(int x=0;x<7;x++){
          check=true;
        while(check){
           dicevalue=dice.roll();
            if(tile.arraydomino[0][dicevalue]<7 && tile.arraydomino[1][dicevalue]<7){
                player1[0][dicevalue]=tile.arraydomino[0][dicevalue];
                player1[1][dicevalue]=tile.arraydomino[1][dicevalue];
                tile.arraydomino[0][dicevalue]=7;
                tile.arraydomino[1][dicevalue]=7;
                if(highhead1<player1[0][dicevalue]){
                    highhead1=player1[0][dicevalue];
                    hightail1=player1[1][dicevalue];
                }
                check=false;
            }
        }
      }
            System.out.println(character[0]+"'s tiles");
            for(int x=0;x<49;x++){
                    if(player1[0][x]<7&&player1[1][x]<7){
                        System.out.print(player1[0][x]+"\t"+player1[1][x]);
                        System.out.println();
                    }
            }
      JOptionPane.showMessageDialog(null,character[1]+" will draw 7 random tiles");
      for(int x=0;x<7;x++){
          check=true;
        while(check){
           dicevalue=dice.roll();
            if(tile.arraydomino[0][dicevalue]<7 && tile.arraydomino[1][dicevalue]<7){
                player2[0][dicevalue]=tile.arraydomino[0][dicevalue];
                player2[1][dicevalue]=tile.arraydomino[1][dicevalue];
                tile.arraydomino[0][dicevalue]=7;
                tile.arraydomino[1][dicevalue]=7;
                if(highhead2<player2[0][dicevalue]){
                    highhead2=player2[0][dicevalue];
                    hightail2=player2[1][dicevalue];
                }
                check=false;
            }
        }
      }
            System.out.println(character[1]+"'s tiles");
            for(int x=0;x<49;x++){
                    if(player2[0][x]<7&&player2[1][x]<7){
                        System.out.print(player2[0][x]+"\t"+player2[1][x]);
                        System.out.println();
                    }
            }
      if(highhead1>highhead2){
          JOptionPane.showMessageDialog(null,character[0]+" will go first having the highest value of tile");
          game();
      }
      else if(highhead1<highhead2){
          JOptionPane.showMessageDialog(null,character[1]+" will go first having the highest value of tile");
          character[2]=character[0];
          character[0]=character[1];
          character[1]=character[2];
          for(int x=0;x<49;x++){
              temp=player1[0][x];
              player1[0][x]=player2[0][x];
              player2[0][x]=temp;
              temp=player1[1][x];
              player1[1][x]=player2[1][x];
              player2[1][x]=temp;
          }
          game();
      }
      else{
          if(hightail1>hightail2){
              JOptionPane.showMessageDialog(null,character[0]+" will go first having the highest value of tile");
              game();
          }
          else{
              JOptionPane.showMessageDialog(null,character[1]+" will go first having the highest value of tile");
                character[2]=character[0];
                character[0]=character[1];
                character[1]=character[2];
                for(int x=0;x<49;x++){
                    temp=player1[0][x];
                    player1[0][x]=player2[0][x];
                    player2[0][x]=temp;
                    temp=player1[1][x];
                    player1[1][x]=player2[1][x];
                    player2[1][x]=temp;
                }
              game();
          }
      }
           
     
    }
    public static void newCharacter(){
        if (charcount<2){
            character[charcount]=JOptionPane.showInputDialog("Enter a name:");
            charcount+=1;
        }
    }
    public static void printmap(){
        System.out.println("MAP");
        for(int x=0;x<98;x++){
                if(map[0][x]<7&&map[0][x]<7)
                System.out.println(map[0][x]+"\t"+map[1][x]); 
        }
    }
   
    public static void game(){
        check=true;
        while(check){
            while(check){
                try{
                highhead1=Integer.parseInt(JOptionPane.showInputDialog(character[0]+"'s turn\nEnter a your head tile number to use:"));
                hightail1=Integer.parseInt(JOptionPane.showInputDialog(character[0]+"'s turn\nEnter a your tail tile number to use:"));
                check=false;
                }
                catch(NumberFormatException e){
                JOptionPane.showMessageDialog(null,"WRONG INPUT","ERROR",JOptionPane.ERROR_MESSAGE);
                }
            }
           
            if(highhead1<=6&&highhead1>=0&&hightail1<=6&&hightail1>=0){
                   for(int x=0;x<49;x++){
                      if(player1[0][x]==highhead1&&player1[1][x]==hightail1){
                         map[0][mapx2]=player1[0][x];
                         map[1][mapy2]=player1[1][x];
                         player1[0][x]=7;
                         player1[1][x]=7;
                      }
                  }
                check=false;
            printmap();
            }
           
      }
       
       
while(checkgame){
    counter1=0;
    for(int x=0;x<49;x++){
        if(tile.arraydomino[0][x]==7&&tile.arraydomino[1][x]==7)
            counter1+=1;
    }
    if(counter1==49){
        for(int x=0;x<49;x++){
            if(player1[0][x]<7&&player1[1][x]<7)
                counterplayer1+=1;
            if(player2[0][x]<7&&player2[1][x]<7)
                counterplayer2+=1;
        }
        if(counterplayer1<counterplayer2){
            JOptionPane.showMessageDialog(null,character[0]+"Wins");
            checkgame=false;
            check=false;
            checkroll=false;
            checkroll1=false;
        }
        else if(counterplayer1>counterplayer2){
            JOptionPane.showMessageDialog(null,character[1]+"Wins");
            checkgame=false;
            check=false;
            checkroll=false;
            checkroll1=false;
        }
        else if(counterplayer1==counterplayer2){
            JOptionPane.showMessageDialog(null,"DRAW!!!");
            checkgame=false;
            check=false;
            checkroll=false;
            checkroll1=false;
        }
    }
       
        while(true){
            check=false;
            checkroll1=true;
            while(checkroll1){
                checkroll=true;
                for(int x=0;x<49;x++){
                    if(highhead1==player2[0][x]||highhead1==player2[1][x]||hightail1==player2[0][x]||hightail1==player2[1][x]){
                        check=true;
                        checkroll=false;
                        checkroll1=false;
                    }
                    else if(counter1==49){
                        check=false;
                        checkroll=false;
                        checkroll1=false;
                    }
                   
                }  
                while(checkroll){
                    dicevalue=dice.roll();
                        if(tile.arraydomino[0][dicevalue]<7 && tile.arraydomino[1][dicevalue]<7){
                            JOptionPane.showMessageDialog(null,character[1]+" chose to pass, required to get new domino from boneyard");
                            player2[0][dicevalue]=tile.arraydomino[0][dicevalue];
                            player2[1][dicevalue]=tile.arraydomino[1][dicevalue];
                            tile.arraydomino[0][dicevalue]=7;
                            tile.arraydomino[1][dicevalue]=7;
                            highhead2=highhead1;
                            hightail2=hightail1;
                            check=false;
                            checkroll=false;
                            checkroll1=false;
                        }
                        counter1=0;
                        for(int x=0;x<49;x++){
                        if(tile.arraydomino[0][x]==7&&tile.arraydomino[1][x]==7)
                        counter1+=1;
                        }
                        if(counter1==49){
                            check=false;
                            checkroll=false;
                            checkroll1=false;
                        } 
                 }     
            }
            System.out.println(character[1]+"'s tiles");
            for(int x=0;x<49;x++){
                if(player2[0][x]<7&&player2[1][x]<7)
                System.out.println(player2[0][x]+"\t"+player2[1][x]);
            }
            while(check){
                while(check){
                try{
                highhead2=Integer.parseInt(JOptionPane.showInputDialog(character[1]+"'s turn\nEnter a your head tile number to use:"));
                hightail2=Integer.parseInt(JOptionPane.showInputDialog(character[1]+"'s turn\nEnter a your tail tile number to use:"));
                    while(check){
                    toplace=Integer.parseInt(JOptionPane.showInputDialog("Where to place the tiles:\n[0]head portion\n[1]tail portion"));
                    if(toplace==0||toplace==1){
                        check=false;
                    }
                    else
                    JOptionPane.showMessageDialog(null,"WRONG INPUT","Warning",JOptionPane.WARNING_MESSAGE);
                    }
                check=false;
                }
                catch(NumberFormatException e){
                JOptionPane.showMessageDialog(null,"WRONG INPUT","ERROR",JOptionPane.ERROR_MESSAGE);
                }
                }
                check=true;
            if(toplace==0){  
                if(highhead2==highhead1){
                   for(int x=0;x<49;x++){
                      if(player2[0][x]==highhead2&&player2[1][x]==hightail2){
                         mapx2-=1;
                         mapy2-=1;
                         map[0][mapx2]=player2[1][x];
                         map[1][mapy2]=player2[0][x];
                         highhead2=player2[1][x];
                         hightail2=hightail1;
                         player2[0][x]=7;
                         player2[1][x]=7;
                         check=false;
                        
                      }
                  }
                }
                else if(hightail2==highhead1){
                     for(int x=0;x<49;x++){
                      if(player2[0][x]==highhead2&&player2[1][x]==hightail2){
                         mapx2-=1;
                         mapy2-=1;
                         map[0][mapx2]=player2[0][x];
                         map[1][mapy2]=player2[1][x];
                         hightail2=hightail1;
                         player2[0][x]=7;
                         player2[1][x]=7;
                         check=false;
                      }
                  }
                }
            }
           
            else if(toplace==1){
                if(highhead2==hightail1){
                   for(int x=0;x<49;x++){
                      if(player2[0][x]==highhead2&&player2[1][x]==hightail2){
                         mapx22+=1;
                         mapy22+=1;
                         map[0][mapx22]=player2[0][x];
                         map[1][mapy22]=player2[1][x];
                         highhead2=highhead1;
                         player2[0][x]=7;
                         player2[1][x]=7;
                         check=false;
                      }
                  }
                }
                else if(hightail2==hightail1){
                    for(int x=0;x<49;x++){
                      if(player2[0][x]==highhead2&&player2[1][x]==hightail2){
                         mapx22+=1;
                         mapy22+=1;
                         map[0][mapx22]=player2[1][x];
                         map[1][mapy22]=player2[0][x];
                         highhead2=highhead1;
                         hightail2=player2[0][x];
                         player2[0][x]=7;
                         player2[1][x]=7;
                         check=false;
                      }
                  }
                }
            }

            }
            printmap();
                counterplayer2=0;
                for(int x=0;x<49;x++){
                if(player2[0][x]<7&&player2[1][x]<7)
                counterplayer2+=1;
                }
                if(counterplayer2==0){
                JOptionPane.showMessageDialog(null,character[1]+" Wins!!!");
                checkgame=false;
                check=false;
                checkroll=false;
                checkroll1=false;
                }
                else{
                check=false;
                checkroll1=true; 
                }
        break;
    }
               
        
        while(true){  
            while(checkroll1){
                checkroll=true;
                for(int x=0;x<49;x++){
                    if(highhead2==player1[0][x]||highhead2==player1[1][x]||hightail2==player1[0][x]||hightail2==player1[1][x]){
                        check=true;
                        checkroll=false;
                        checkroll1=false;
                    }
                    else if(counter1==49){
                        check=false;
                        checkroll=false;
                        checkroll1=false;
                    }
                }
                while(checkroll){
                    dicevalue=dice.roll();
                        if(tile.arraydomino[0][dicevalue]<7 && tile.arraydomino[1][dicevalue]<7){
                            JOptionPane.showMessageDialog(null,character[0]+" chose to pass, required to get new domino from boneyard");
                            player1[0][dicevalue]=tile.arraydomino[0][dicevalue];
                            player1[1][dicevalue]=tile.arraydomino[1][dicevalue];
                            tile.arraydomino[0][dicevalue]=7;
                            tile.arraydomino[1][dicevalue]=7;
                            highhead1=highhead2;
                            hightail1=hightail2;
                            check=false;
                            checkroll=false;
                            checkroll1=false;
                        }
                        counter1=0;
                        for(int x=0;x<49;x++){
                        if(tile.arraydomino[0][x]==7&&tile.arraydomino[1][x]==7)
                        counter1+=1;
                        }
                        if(counter1==49){
                            check=false;
                            checkroll=false;
                            checkroll1=false;
                        }
                }
            }
            System.out.println(character[0]+"'s tiles");
            for(int x=0;x<49;x++){
                if(player1[0][x]<7&&player1[1][x]<7)
                System.out.println(player1[0][x]+"\t"+player1[1][x]);
            }
            while(check){
                while(check){
                try{
                highhead1=Integer.parseInt(JOptionPane.showInputDialog(character[0]+"'s turn\nEnter a your head tile number to use:"));
                hightail1=Integer.parseInt(JOptionPane.showInputDialog(character[0]+"'s turn\nEnter a your tail tile number to use:"));
                while(check){
                    toplace=Integer.parseInt(JOptionPane.showInputDialog("Where to place the tiles:\n[0]head portion\n[1]tail portion"));
                    if(toplace==0||toplace==1){
                    check=false;
                    }
                    else
                    JOptionPane.showMessageDialog(null,"WRONG INPUT","Warning",JOptionPane.WARNING_MESSAGE);
                    }
                check=false;
                }
                catch(NumberFormatException e){
                JOptionPane.showMessageDialog(null,"WRONG INPUT","ERROR",JOptionPane.ERROR_MESSAGE);
                }
                }
                check=true;
            if(toplace==0){  
                if(highhead1==highhead2){
                   for(int x=0;x<49;x++){
                      if(player1[0][x]==highhead1&&player1[1][x]==hightail1){
                         mapx2-=1;
                         mapy2-=1;
                         map[0][mapx2]=player1[1][x];
                         map[1][mapy2]=player1[0][x];
                         highhead1=player1[1][x];
                         hightail1=hightail2;
                         player1[0][x]=7;
                         player1[1][x]=7;
                         check=false;
                        
                      }
                  }
                }
                else if(hightail1==highhead2){
                     for(int x=0;x<49;x++){
                      if(player1[0][x]==highhead1&&player1[1][x]==hightail1){
                         mapx2-=1;
                         mapy2-=1;
                         map[0][mapx2]=player1[0][x];
                         map[1][mapy2]=player1[1][x];
                         hightail1=hightail2;
                         player1[0][x]=7;
                         player1[1][x]=7;
                         check=false;
                      }
                  }
                }
            }
           
            else if(toplace==1){
                if(highhead1==hightail2){
                   for(int x=0;x<49;x++){
                      if(player1[0][x]==highhead1&&player1[1][x]==hightail1){
                         mapx22+=1;
                         mapy22+=1;
                         map[0][mapx22]=player1[0][x];
                         map[1][mapy22]=player1[1][x];
                         highhead1=highhead2;
                         player1[0][x]=7;
                         player1[1][x]=7;
                         check=false;
                      }
                  }
                }
                else if(hightail1==hightail2){
                    for(int x=0;x<49;x++){
                      if(player1[0][x]==highhead1&&player1[1][x]==hightail1){
                         mapx22+=1;
                         mapy22+=1;
                         map[0][mapx22]=player1[1][x];
                         map[1][mapy22]=player1[0][x];
                         highhead1=highhead2;
                         hightail1=player1[0][x];
                         player1[0][x]=7;
                         player1[1][x]=7;
                         check=false;
                      }
                  }
                }
            }

            }
            printmap();
                counterplayer1=0;
                for(int x=0;x<49;x++){
            
24th Mar 10
08:17:23 AM

Post Reply

 
© Scodz, 2006 All Rights Reserved. Guest Book | FAQ | Privacy Policy | Terms & Conditions | Contact Us  
Contact Us for Advertising in this website