import 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 nim 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 binary [][]= new int[9][9]; //static int savectr=99; static int savebinary [][]= new int[9][9]; JButton jb; public static nim sc; public nim(){ //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 pic2 = new ImageIcon("evil-computer.jpg");//insert the picture jb=new JButton(pic2); addGB(jb,7,1);//insert a button on the 2d array jb.addActionListener(this); 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-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; } } }