/******************************************************************* * Project #2 - Mancala * Board.java * Kyu Bae 4-16-01 * CS331 * ***************************************************************** * Objectives: * This program will implement the basic rules of game called * "MANCALA". you are to write a graphic version of this game. * Follow the rules and play against computer or second player. * * 1)Create a game called "Mancala" in java. * 2)Create a graphic version of game. * 3)create a webpage and load your applet. * 4)run to use graphics and awt(swing) to manipulate graphics * 5)will be using an appletviewer to load the game * *******************************************************************/ import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.awt.Color; /**************************************************************** * CLASS Board: board represent the game board and their 12 boxes * and two khala, score boxes. ****************************************************************/ public class Board extends JFrame implements ActionListener, ItemListener { private Container container; private GridBagLayout gbLayout; private GridBagConstraints gbc; private JLabel label1, label2, label3; private JRadioButton radiob1, radiob2; private ButtonGroup radiogroup; private JTextField textF; static public JButton []b= new JButton[14]; //14 boxes static public int count, boxNum, boxNum1; static public int []boxCount= new int[14]; //number of stones static public boolean playingComputer=true; /**************************************************************** * constructor:Board(): initialize the boxes to three stones * and score boxes to zero. * ****************************************************************/ public Board() { super("GridBagLayout"); container =getContentPane(); gbLayout= new GridBagLayout(); container.setLayout(gbLayout); gbc= new GridBagConstraints(); getContentPane().setForeground(Color.cyan); gbc.fill=GridBagConstraints.BOTH; //label1 JLabel label1= new JLabel(" << Q's Mancala >>"); //container.setBackGround(Color.cyan); addComponent(label1,0,0,8,1); //button1=first bottom row box next to opp score box b[0]= new JButton("3"); addComponent(b[0], 1,2,1,1); b[0].addActionListener(this); //JButton2 b[1]= new JButton("3"); addComponent(b[1], 2,2,1,1); b[1].addActionListener(this); //JButton3 b[2]= new JButton("3"); addComponent(b[2], 3,2,1,1); b[2].addActionListener(this); //JButton4 b[3]= new JButton("3"); addComponent(b[3], 4,2,1,1); b[3].addActionListener(this); //JButton5 b[4]= new JButton("3"); addComponent(b[4], 5,2,1,1); b[4].addActionListener(this); //JButton6 b[5]= new JButton("3"); addComponent(b[5], 6,2,1,1); b[5].addActionListener(this); //JButton7==your score box b[6]= new JButton("0"); addComponent(b[6], 7,1,1,2); b[6].addActionListener(this); b[6].setEnabled(false); //score box disabled //JButton8 b[7]= new JButton("3"); addComponent(b[7], 6,1,1,1); b[7].addActionListener(this); //JButton9 b[8]= new JButton("3"); addComponent(b[8], 5,1,1,1); b[8].addActionListener(this); //JButton10 b[9]= new JButton("3"); addComponent(b[9], 4,1,1,1); b[9].addActionListener(this); //JButton11 b[10]= new JButton("3"); addComponent(b[10], 3,1,1,1); b[10].addActionListener(this); //JButton12 b[11]= new JButton("3"); addComponent(b[11], 2,1,1,1); b[11].addActionListener(this); //JButton13 b[12]= new JButton("3"); addComponent(b[12], 1,1,1,1); b[12].addActionListener(this); //JButton14 b[13]= new JButton("0"); addComponent(b[13], 0,1,1,2); b[13].addActionListener(this); b[13].setEnabled(false); //your score box //checkBox radiob1=new JRadioButton("Computer", false); addComponent(radiob1,1,3,3,1); radiob2=new JRadioButton("Player2", false); addComponent(radiob2,5,3,3,1); //create relationship of group radiogroup= new ButtonGroup(); radiogroup.add(radiob1); radiob1.addItemListener(this); radiogroup.add(radiob2); //register event //RadioButtonHandler handler= new RadioButtonHandler(); radiob2.addItemListener(this); //textfield textF=new JTextField("Select opponent ==>>"); addComponent(textF,0,4,8,1); setSize(550, 300); show(); } /***************************************************************** * actionPerformed(): handles button event and call for functions * ****************************************************************/ public void actionPerformed(ActionEvent evt) { Object source=evt.getSource(); topRowEnabled(false);//disabletoprow if(source==b[0]){ boxNum=1; if(playingComputer==true){ moveStone();} //playing against computer else{ moveStonePlayer1();//playing against player2 } } else if(source==b[1]){ boxNum=2; if(playingComputer==true){ moveStone();} else{ moveStonePlayer1(); } } else if(source==b[2]){ boxNum=3; if(playingComputer==true){ moveStone();} else{ moveStonePlayer1(); } } else if(source==b[3]){ boxNum=4; if(playingComputer==true){ moveStone();} else{ moveStonePlayer1(); } } else if(source==b[4]){ boxNum=5; if(playingComputer==true){ moveStone();} else{ moveStonePlayer1(); } } else if(source==b[5]){ boxNum=6; if(playingComputer==true){ moveStone();} else{ moveStonePlayer1(); } } else if(source==b[7]){ topRowEnabled(true); bottomRowEnabled(false); boxNum=8; moveStonePlayer2(); } else if(source==b[8]){ boxNum=9; topRowEnabled(true); bottomRowEnabled(false); moveStonePlayer2(); //player2 control } else if(source==b[9]){ boxNum=10; topRowEnabled(true); bottomRowEnabled(false); moveStonePlayer2(); //player2 control } else if(source==b[10]){ boxNum=11; topRowEnabled(true); bottomRowEnabled(false); moveStonePlayer2(); } else if(source==b[11]){ boxNum=12; topRowEnabled(true); bottomRowEnabled(false); moveStonePlayer2(); } else if(source==b[12]){ boxNum=13; topRowEnabled(true); bottomRowEnabled(false); moveStonePlayer2(); } else{ System.out.println("Don't press these buttons!!!"); } } /**************************************************************** * topRowEnabled():set top row buttons to immobile * passes argument as true or false * ***************************************************************/ public void topRowEnabled(boolean onOff) { for(int i=7;i<13;i++) { b[i].setEnabled(onOff); } } /**************************************************************** * bottomRowEnabled():set bottom row buttons to immobile * passes argument as true or false * ***************************************************************/ public void bottomRowEnabled(boolean onOff) { for(int i=0;i<6;i++) { b[i].setEnabled(onOff); } } /****************************************************************** * itemStateChanged(ItemEvent e):event handling for the radioButton * checks for computer or playrer2 and call for functions * *****************************************************************/ public void itemStateChanged(ItemEvent e) { if (e.getSource() == radiob1) { textF.setText("playing against computer"); playingComputer=true; topRowEnabled(false); } else { textF.setText("Playing against player2"); playingComputer=false; topRowEnabled(false); } } /************************************************************************ * addCOmponet():gridbagLayout initialzer. * add button and textfield to specific positions * ***********************************************************************/ void addComponent(Component c, int row, int column, int width, int height) { gbc.gridx=row; gbc.gridy=column; gbc.gridwidth=width; gbc.gridheight=height; gbLayout.setConstraints(c, gbc); container.add(c); } /***************************************************************** * main (): call for objects and ask for select for option. * ****************************************************************/ public static void main(String [] args) { Board game=new Board(); game.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0); } }); } /***************************************************************** * moveStone(); do calculations and move stones to next boxes * and add scores * when playing against computer ****************************************************************/ public void moveStone() { int a; for(int p=0;p<14; ++p) { boxCount[p]=Integer.parseInt(b[p].getText()); } for( a=0; a0) //always pick box 13 then 12,11,10, &etc. { count=boxCount[12]; boxCount[12]=0; for( a=0; a6) { if (boxCount[(13+a-1)%14]==1) { boxCount[13]=boxCount[13]+boxCount[(13+a-1)%14]; boxCount[(13+a-1)%14]=0; boxCount[13]=boxCount[13]+boxCount[12-((13+a-1)%14)]; boxCount[12-((13+a-1)%14)]=0; } } printBoard(); //checking ending at the score box if(((13+a-1)%14)==13) { textF.setText("** Computer moves again **"); computer(); } else { textF.setText("** Your Move again **"); } } else if (boxCount[11]>0) { count=boxCount[11]; boxCount[11]=0; for( a=0; a6) { if (boxCount[(12+a-1)%14]==1) { boxCount[13]=boxCount[13]+boxCount[(12+a-1)%14]; boxCount[(12+a-1)%14]=0; boxCount[13]=boxCount[13]+boxCount[12-((12+a-1)%14)]; boxCount[12-((12+a-1)%14)]=0; } } printBoard(); if(((12+a-1)%14)==13) { textF.setText("** Computer moves again **"); computer(); } else { textF.setText("** Your Move again **"); } } else if (boxCount[10]>0) { count=boxCount[10]; boxCount[10]=0; for( a=0; a6) { if (boxCount[(11+a-1)%14]==1) { boxCount[13]=boxCount[13]+boxCount[(11+a-1)%14]; boxCount[(11+a-1)%14]=0; boxCount[13]=boxCount[13]+boxCount[12-((11+a-1)%14)]; boxCount[12-((11+a-1)%14)]=0; } } printBoard(); if(((11+a-1)%14)==13) { textF.setText("** Computer moves again **"); computer(); } else { textF.setText("** Your Move again **"); } } else if (boxCount[9]>0) { count=boxCount[9]; boxCount[9]=0; for( a=0; a6) { if (boxCount[(10+a-1)%14]==1) { boxCount[13]=boxCount[13]+boxCount[(10+a-1)%14]; boxCount[(10+a-1)%14]=0; boxCount[13]=boxCount[13]+boxCount[12-((10+a-1)%14)]; boxCount[12-((10+a-1)%14)]=0; } } printBoard(); if(((10+a-1)%14)==13) { textF.setText("** Computer moves again **"); computer(); } else { textF.setText("** Your Move again **"); } } else if (boxCount[8]>0) { count=boxCount[8]; boxCount[8]=0; for( a=0; a6) { if (boxCount[(9+a-1)%14]==1) { boxCount[13]=boxCount[13]+boxCount[(9+a-1)%14]; boxCount[(9+a-1)%14]=0; boxCount[13]=boxCount[13]+boxCount[12-((9+a-1)%14)]; boxCount[12-((9+a-1)%14)]=0; } } printBoard(); if(((9+a-1)%14)==13) { textF.setText("** Computer moves again **"); computer(); } else { textF.setText("** Your Move again **"); } } else if(boxCount[7]>0) { count=boxCount[7]; boxCount[7]=0; for( a=0; a6) { if (boxCount[(8+a-1)%14]==1) { boxCount[13]=boxCount[13]+boxCount[(8+a-1)%14]; boxCount[(8+a-1)%14]=0; boxCount[13]=boxCount[13]+boxCount[12-((8+a-1)%14)]; boxCount[12-((8+a-1)%14)]=0; } } printBoard(); //check for landing at the score box if(((8+a-1)%14)==13) { textF.setText("** Computer moves again **"); computer(); } else { textF.setText("** Your Move again after computer **"); } } //check for wins= if all boxes are empty if(boxCount[12]==0&&boxCount[11]==0&&boxCount[10]==0 &&boxCount[9]==0&&boxCount[8]==0&&boxCount[7]==0) { boxCount[13]=boxCount[13]+boxCount[0]+boxCount[1] +boxCount[2]+boxCount[3]+boxCount[4]+boxCount[5]; boxCount[0]=0;boxCount[1]=0;boxCount[2]=0; boxCount[3]=0;boxCount[4]=0;boxCount[5]=0; printBoard(); textF.setText(" ** THE Computer WON!!!! **"); } } /***************************************************************** * moveStonePlayer1(); ask for moves from player 1 and move s stones * of player 1 boxes. * ****************************************************************/ public void moveStonePlayer1() { int a; for(int p=0;p<14; ++p) { boxCount[p]=Integer.parseInt(b[p].getText()); } for( a=0; a6) { if (boxCount[(boxNum+a-1)%14]==1) { boxCount[13]=boxCount[13]+boxCount[(boxNum+a-1)%14]; boxCount[(boxNum+a-1)%14]=0; boxCount[13]=boxCount[13]+boxCount[12-((boxNum+a-1)%14)]; boxCount[12-((boxNum+a-1)%14)]=0; } } printBoard(); //checking for the winner if(boxCount[7]==0&&boxCount[8]==0&&boxCount[9]==0&&boxCount[10]==0 &&boxCount[11]==0&&boxCount[12]==0) { boxCount[13]=boxCount[13]+boxCount[0]+boxCount[1]+boxCount[2] +boxCount[3]+boxCount[4]+boxCount[5]; boxCount[0]=0;boxCount[1]=0;boxCount[2]=0;boxCount[3]=0; boxCount[4]=0;boxCount[5]=0; printBoard(); textF.setText("** Player 2 WON!!!! ** "); endGame(); //holding screen to let player see score and exit out } //chekcing for landing at the score box if(((boxNum+a-1)%14)==13) { textF.setText("** Player 2 Move again **"); } else { textF.setText("** Player 1 moves **"); topRowEnabled(false); bottomRowEnabled(true); } } } /***************************************************************** * The end of program!! * ****************************************************************/