All pastes #2105545 Raw Edit

Untitled

public text v1 · immutable
#2105545 ·published 2012-01-24 00:59 UTC
rendered paste body
port java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;




public class workingnim extends JPanel implements ActionListener{//add actionlistner to get information from player
  private static final String JButton = null;
  GridBagConstraints constraints = new GridBagConstraints();//set new gridbagconstraints
  JButton [][]counters= new JButton[9][9];//use 9 by 9 2d array of JButton
  static int binary [][]= new int[9][9];
  static int savectr=99;

  public static  workingnim sc;
  
  public workingnim(){ //costructors
   setLayout(new GridBagLayout());//set gridbaglayout
   constraints.weightx = 0.5;
   constraints.weighty = 0.5;
   constraints.fill = GridBagConstraints.BOTH;//make sure the layout is the same size of the window

   constraints.gridheight = 1;
   constraints.gridwidth = 1;// span two rows
  }
 
  public  void idiotlevel() {//the easier level of the game
   
 
   int line=1;
   int rows=5;//have 6 rows
   int counter=0;
   int istar=0;

   ImageIcon pic = new ImageIcon("h.jpg");//insert the picture
                
   while (counter<=rows){//use double loop to draw a triangle shape (buttons)
       istar=0;
       while (istar<line){
               counters[counter][istar]=new JButton(pic);// set the button into the 2d array + add picture
               addGB(counters[counter][istar],istar,line);//insert a button on the 2d array
               counters[counter][istar].addActionListener(this);//add action to this button
               istar=istar+1;
               
               }
       counter++;
       line=line+1;
       }
}
 
  public void humanlevel() {//the harder level of the game

   int line=1;
   int rows=8;//have 8 rows
   int counter=0;
   int istar=0;
   
   ImageIcon pic = new ImageIcon("s.jpg");
  
   while (counter<=rows){
    istar=0;//set to 0

    while (istar<line){
    	counters[counter][istar]=new JButton(pic);// set the button into the 2d array
    	addGB(counters[counter][istar],istar,line);//insert a button on the 2d array
    	counters[counter][istar].addActionListener(this);//add action to this button
    istar=istar+1;
    
    }
  
    counter++;
    line=line+1;
    
   
   }


 }


  void addGB(Component component, int x, int y) { 
   constraints.gridx = x;
   constraints.gridy = y;
   add(component, constraints);
   
 }
  
  


  public static void main(String[] args) {//main class
    JFrame frame = new JFrame("workingnim");//set frame
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//able to exit frame
    frame.setSize(700, 700);
    JOptionPane.showMessageDialog(null, "Game rules: 1)player who takes the last picture loses                                 2)every time play can only choose pictures from the same rows");
    //use joptionpane to show player the game rules
    String levelchoice= JOptionPane.showInputDialog("choose level 1 or 2");//get the input(level) from player
    int numberlevel= Integer.parseInt(levelchoice);//change the input into integer

    sc=new workingnim();
    frame.setContentPane(sc);
    
  
    if (numberlevel== 1){
    	sc.idiotlevel();//call idiotlevel method
    }
    else if (numberlevel== 2){
    	sc.humanlevel();//call humanlevel method
     }
    frame.setVisible(true);//make the game visible
    
  }
  
 public void removebutton(int ctr,int ctr2){
	 counters[ctr][ctr2].add(new JLabel(new ImageIcon("images/redcircle.png")));//if so, change the image into gray
     sc.remove(counters[ctr][ctr2]);//meanwhile remove the button
     counters[ctr][ctr2]=null;
 }

  
  public void actionPerformed(ActionEvent e){ //Execute when button is pressed
   boolean fcomputerturn=false;
 for (int ctr=0;ctr<9;ctr++){//use the double loop to go through the game board
          for (int ctr2=0;ctr2<9;ctr2++){ 
                  if (e.getSource()==counters[ctr][ctr2]){//check if player click the button
                      if (savectr==99) savectr=ctr;
                      if (ctr==savectr) {
                    	  removebutton(ctr,ctr2);
                      	}
                      else	  fcomputerturn=true;                                          
                      this.repaint();//update the screen
                  	  }
          		}
 			}
 if (fcomputerturn==true)  {
	 int starnumber=0;
	 for (int r=0; r<9; r++){//use double loop to go through the screen again to count how many buttons are in the row
		 for (int c=0; c<9; c++){// go through every column
			 if ((counters[r][c]!=null))//check if has buttons		
		      starnumber++;// if so, count how many\		  	 
	       	}
	  System.out.println ("r "+ r+ " how many star "+starnumber);
	  bitaconvertrow(starnumber, r);
	  starnumber=0;// reset starnumber once the computer check over all the columns in the same rows
	  int total;
	  savectr=99;
 	  }
 	computerturn();
 	}
 	
  	} 
  
  
  
  public void computerturn(){
	  guess();
	  while (coladder()==false){
		  guess();
	  }
	  for (int i=0;i<9;++i){
		  for (int j=0;j<9;++j){
			  if (binary[i][j]==0) removebutton(i,j);
		  	  }
	  	  }
  }
  
  public void guess(){
  String sbin="";
  int binrowtotal=0;
    boolean winner=false;
	for (int i=8; ((i>-1)&& (winner==false));--i){
		for (int j=0;j<9;++j){
				if (binary[i][j]==1) sbin=sbin+"1";
				if (binary[i][j]==0) sbin=sbin+"0";				
						//binrowtotal+=binary[i][j];
			}
			binrowtotal=Integer.parseInt(sbin,2);
			
			sbin="";
			int ctr=binrowtotal+1;
			while ((--ctr>0) && (coladder()==false)){
				bitaconvertrow (ctr, i);
				}
			if (coladder()==false) {
				bitaconvertrow (binrowtotal, i);
			}
			else	winner=true;
			
		}
	
	}
	  
	  
  
  static void bitaconvertrow (int starnumber, int r){
	  System.out.println("starnumber: " + starnumber + "r:" + r); 
		String binaryNumber; 

		binaryNumber = ""; 
			while (starnumber != 0) { 
					binaryNumber = (starnumber % 2) + binaryNumber; 
					starnumber /= 2; 
				} 
			System.out.println("Binary: " + binaryNumber);
			int row=-1, bin, col=8;
			int lenstr=binaryNumber.length();
			while (--lenstr >= 0){
				if (binaryNumber.charAt(lenstr)=='1'){
					binary [r] [col]= 1;
					}
				--col;
				}	
			}
 // } 
  
  static boolean coladder (){
  
  //int binary [][]= new int[9][9];
	  boolean bal=true;
	int tot=0;
	int col=9;
	int row=-1;
	while (--col>0){
		 tot=0;
		 while (++row<9){	
		         tot=binary[row][col]+tot;
//		         j = i + j ;
		 		}
		 if (tot%2!=0) bal=false;
		 System.out.println("tot" + tot);
		 row=0;
		}
	
	return bal;
//  displaybinary();
  }


static void displaybinary(){
	int i = 0, j = 0;
	//System.out.print( "add");
	while (i<9){
		
		while (j<9){
			System.out.print( "" + binary [i][j] );
			++j;			
			}
		System.out.println("");
		j=0;
		++i;
		
		}
	}
}