/******************************************************************* * Board.java * Mancala Project #1 * CMSY 331 3-27-01 * ************************************************************* * Objectives: * This program will implement the basic rules of game called * "MANCALA". you are to write a text version of this game. * Follow the rules and play against computer or second player. * ****************************************************************/ import javax.swing.JOptionPane; /**************************************************************** * CLASS Board: board represent the game board and their 12 boxes * and two khala, score boxes. ****************************************************************/ public class Board { //mancala Box 12+2 score box=14 public static int box[]=new int[14]; //static byte public static int count; //stone # public static int boxNum, boxNum1; // box number user chose /**************************************************************** * constructor:Board(): initialize the boxes to three stones * and score boxes to zero. * ****************************************************************/ Board() { for (int n=0;n<14;++n) { if(n==6 || n==13){ //box[n]=new byte[]; box[n]=0; //score box } else{ //box[n]= new byte[]; box[n]=3; //stone # } } } /***************************************************************** * printBoard()-print board * ****************************************************************/ public void printBoard() { System.out.println("\n\n <<< Mancala Board >>>\n"); System.out.println("opp score | "+box[12]+" | "+box[11]+" | " +box[10]+" | "+box[9]+" | "+box[8]+" | "+box[7] +" | your score "); System.out.println(" "+box[13]+" -----------------------" +"--- "+box[6]); System.out.println(" | "+box[0]+" | "+box[1]+" | " +box[2]+" | "+box[3]+" | "+box[4]+" | "+box[5] +" | "); } /***************************************************************** * printPrompt()- print as example * ****************************************************************/ public void printPrompt() { System.out.println("\n\n <<< Mancala Board >>>\n"); System.out.println("opp score | box13 | box12 | box11 | box10 | " +"box 9 | box 8 | your score "); System.out.println(" XXX -------------------------------------------" +"------- XXX"); System.out.println(" | box 1 | box 2 | box 3 | box 4 | " +"box 5 | box 6 | \n"); System.out.println("player 1 - Bottom Boxes - select between 1 to 6\n" +"player 2/computer - top Boxes - select between 8 to 13\n"); System.out.println("Player 1 always goes first and add Counterclockwise\n"); } /***************************************************************** * printMenu()- ask user for player or computer/player2 * ****************************************************************/ public char printMenu()throws Exception { System.out.println("\n\nGAME MANCALA:"); System.out.println("play against A) computer or \n" +" B) another player\n" +"select ==> (A or B) "); char choice=(char)System.in.read(); System.in.read(); System.in.read(); System.out.println("your selection was "+choice); return choice; } /***************************************************************** * getPosition(); ask for box position and change to integer number * ****************************************************************/ public static int getPosition() { String pickBox1; //int num1; pickBox1=JOptionPane.showInputDialog("Enter your move:(1-6)"); boxNum = Integer.parseInt(pickBox1); //num1 is box number //JOptionPane.showMessageDialog( null, " The numbers you entered are "+boxNum, // "Output", JOptionPane.PLAIN_MESSAGE); System.out.println("** YOU PICK Box =>>" + boxNum ); return boxNum; } /***************************************************************** * getPosition2(); ask for box position and change to integer number * from player 2 ****************************************************************/ public static int getPosition2() { String pickBox2; //int num1; pickBox2=JOptionPane.showInputDialog("Enter your move:(8-13)"); boxNum1 = Integer.parseInt(pickBox2); //num1 is box number //JOptionPane.showMessageDialog( null, " The numbers you entered are "+boxNum1, // "Output", JOptionPane.PLAIN_MESSAGE); System.out.println("** YOU PICK Box =>>" + boxNum1 ); return boxNum1; } /***************************************************************** * moveStone(); do calculations and move stones to next boxes * and add scores ****************************************************************/ public void moveStone() { int a; count=box[boxNum-1];//stone # box[boxNum-1]=0; //set to zero for( a=0; a0) { count=box[12]; box[12]=0; for( a=0; a6) { if (box[(13+a-1)%14]==1) { box[13]=box[13]+box[(13+a-1)%14]; box[(13+a-1)%14]=0; box[13]=box[13]+box[12-((13+a-1)%14)]; box[12-((13+a-1)%14)]=0; } } printBoard(); if(((13+a-1)%14)==13) { System.out.println("\n** Computer move again ** \n"); computer(); } else { mancala(); } } if (box[11]>0) { count=box[11]; box[11]=0; for( a=0; a6) { if (box[(12+a-1)%14]==1) { box[13]=box[13]+box[(12+a-1)%14]; box[(12+a-1)%14]=0; box[13]=box[13]+box[12-((12+a-1)%14)]; box[12-((12+a-1)%14)]=0; } } printBoard(); if(((12+a-1)%14)==13) { System.out.println("\n** Computer move again ** \n"); computer(); } else { mancala(); } } if (box[10]>0) { count=box[10]; box[10]=0; for( a=0; a6) { if (box[(11+a-1)%14]==1) { box[13]=box[13]+box[(11+a-1)%14]; box[(12+a-1)%14]=0; box[13]=box[13]+box[12-((11+a-1)%14)]; box[12-((11+a-1)%14)]=0; } } printBoard(); if(((11+a-1)%14)==13) { System.out.println("\n** Computer move again ** \n"); computer(); } else { mancala(); } } if (box[9]>0) { count=box[9]; box[9]=0; for( a=0; a6) { if (box[(10+a-1)%14]==1) { box[13]=box[13]+box[(10+a-1)%14]; box[(10+a-1)%14]=0; box[13]=box[13]+box[12-((10+a-1)%14)]; box[12-((10+a-1)%14)]=0; } } printBoard(); if(((10+a-1)%14)==13) { System.out.println("\n** Computer move again ** \n"); computer(); } else { mancala(); } } if (box[8]>0) { count=box[8]; box[8]=0; for( a=0; a6) { if (box[(9+a-1)%14]==1) { box[13]=box[13]+box[(9+a-1)%14]; box[(9+a-1)%14]=0; box[13]=box[13]+box[12-((9+a-1)%14)]; box[12-((9+a-1)%14)]=0; } } printBoard(); if(((9+a-1)%14)==13) { System.out.println("\n** Computer move again ** \n"); computer(); } else { mancala(); } } if (box[7]>0) { count=box[7]; box[7]=0; for( a=0; a6) { if (box[(8+a-1)%14]==1) { box[13]=box[13]+box[(8+a-1)%14]; box[(8+a-1)%14]=0; box[13]=box[13]+box[12-((8+a-1)%14)]; box[12-((8+a-1)%14)]=0; } } printBoard(); if(((8+a-1)%14)==13) { System.out.println("\n** Computer move again ** \n"); computer(); } else { mancala(); } } //check for wins= if all boxes are empty if(box[12]==0&&box[11]==0&&box[10]==0&&box[9]==0&&box[8]==0&&box[7]==0) { box[13]=box[13]+box[0]+box[1]+box[2]+box[3]+box[4]+box[5]; box[0]=0;box[1]=0;box[2]=0;box[3]=0;box[4]=0;box[5]=0; printBoard(); System.out.println("\n ** THE Computer WON!!!! **\n"); System.exit(0); } } /***************************************************************** * moveStonePlayer1(); ask for moves from player 1 and move s stones * of player 1 boxes. * ****************************************************************/ public void moveStonePlayer1() { int a; count=box[boxNum-1];//stone # box[boxNum-1]=0; //set to zero for( a=0; a6) { if (box[(boxNum1+t-1)%14]==1) { box[13]=box[13]+box[(boxNum1+t-1)%14]; box[(boxNum1+t-1)%14]=0; box[13]=box[13]+box[12-((boxNum1+t-1)%14)]; box[12-((boxNum1+t-1)%14)]=0; } } printBoard(); //check for winner here if(box[12]==0&&box[11]==0&&box[10]==0&&box[9]==0&&box[8]==0&&box[7]==0) { box[13]=box[13]+box[0]+box[1]+box[2]+box[3]+box[4]+box[5]; box[0]=0;box[1]=0;box[2]=0;box[3]=0;box[4]=0;box[5]=0; printBoard(); System.out.println("\n** PLAYER2 WON!!!! ** \n"); System.exit(0); } if(((boxNum1+t-1)%14)==13) { System.out.println("\n ** PLAYER2 Move again **"); player(); } else { System.out.println("\n ** PLAYER1 Move again **"); manual(); } } /***************************************************************** * main (): call for objects and ask for select for option. * ****************************************************************/ public static void main(String[] args) throws Exception { Board b=new Board(); b.printPrompt(); char choice1=b.printMenu(); if( choice1 =='a'|| choice1 =='A') { System.out.println("Playing against Computer");//call computer b.printBoard(); b.mancala(); } if(choice1=='b'||choice1=='B') { System.out.println("Playing againt another player"); b.printBoard(); b.manual(); } else { System.out.println("\n\n ***You entered Invalid choice!!!***"); System.out.println(" ***Program exiting!!!!!!***"); } System.exit(1); } } /***************************************************************** * The end of program!! * ****************************************************************/