Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mancala;
- import java.util.Scanner;
- import static java.lang.Character.toLowerCase;
- public class Mancala
- {
- //Globals
- static int[] board=new int[]{3,3,3,3,3,3,0,3,3,3,3,3,3,0};
- //Board:
- // b13, b12,b11,b10,b9,b8
- //b14 b7
- // b1, b2, b3, b4, b5, b6
- static int playerTurn=1; //Bottom Player = Player 1
- static boolean gameOver=false, repeatTurn=false;
- static String bottomChars="abcdef";
- static char[] bottomPits=bottomChars.toCharArray();
- static String topChars="fedcba";
- static char[] topPits=topChars.toCharArray();
- public static void displayBoard()
- {
- System.out.print(" ");
- for (int c=12;c>=7;c--)
- {
- if(board[c]<10)System.out.print(board[c]+" ");
- else System.out.print(board[c]+" ");
- }
- System.out.print("\n"+board[13]);
- if(board[6]<10&&board[13]<10)for(int c=0;c<20;c++)System.out.print(" ");
- else if(board[6]>9&&board[13]>9)for(int c=0;c<18;c++)System.out.print(" ");
- else if((board[6]>9&&board[13]<10)||(board[13]>9&&board[6]<10))for(int c=0;c<19;c++)System.out.print(" ");
- System.out.println(board[6]);
- System.out.print(" ");
- for (int c=0;c<6;c++)
- {
- if(board[c]<10)System.out.print(board[c]+" ");
- else System.out.print(board[c]+" ");
- }
- System.out.println("\n A B C D E F ");
- }
- public static void playerTurn(char selectedPit)
- {
- int currentSeedCount=0,arrayPosition=0,nextPit=0;
- if(playerTurn==1)
- {
- for(int c=0;c<6;c++)
- {
- if(selectedPit==bottomPits[c])
- {
- currentSeedCount=board[c];
- board[c]=0;
- arrayPosition=c;
- }
- }
- }
- else if(playerTurn==2)
- {
- for(int c=7;c<13;c++)
- {
- if(selectedPit==topPits[c-7])
- {
- currentSeedCount=board[c];
- board[c]=0;
- arrayPosition=c;
- }
- }
- }
- arrayPosition+=1;
- repeatTurn=false;
- for(int c=0;currentSeedCount!=0;c++)
- {
- if(arrayPosition+c+1==13)nextPit=0;
- else nextPit=arrayPosition+c+1;
- if(nextPit==14)nextPit=0;
- if(currentSeedCount==2)
- {
- if(playerTurn==1)
- {
- for(int x=0,i=12;x<=5;x++,i--)
- {
- if(nextPit==x&&board[x]==0)
- {
- board[6]+=board[i];
- board[i]=0;
- }
- }
- }
- else if(playerTurn==2)
- {
- for(int x=7,i=5;x<=12;x++,i--)
- {
- if(nextPit==x&&board[x]==0)
- {
- board[13]+=board[i];
- board[i]=0;
- }
- }
- }
- }
- board[arrayPosition+c]+=1;
- if(arrayPosition+c==13)
- {
- arrayPosition=-1;
- c=0;
- }
- if(currentSeedCount>1)
- {
- if(playerTurn==1&&nextPit==0)
- {
- arrayPosition=-1;
- c=0;
- }
- else if(playerTurn==2&&nextPit==6)c++;
- }
- currentSeedCount--;
- if((playerTurn==1&&arrayPosition+c==6)||(playerTurn==2&&arrayPosition+c==-1))repeatTurn=true;
- else repeatTurn=false;
- }
- if(repeatTurn==true)System.out.println("Landed in home bin! Player "+playerTurn+" gets another turn.");
- if(repeatTurn==false&&playerTurn==1)playerTurn=2;
- else if(repeatTurn==false&&playerTurn==2)playerTurn=1;
- }
- public static void main(String[] args)
- {
- Scanner sc=new Scanner(System.in);
- System.out.println("Welcome to Mancala!");
- System.out.println("Player 1 is on the bottom, with their bin on the right. Player 2 is on top, with their bin on the left.");
- for(int c=0;true;c++)
- {
- displayBoard();
- int topSum=board[7]+board[8]+board[9]+board[10]+board[11]+board[12];
- int bottomSum=board[0]+board[1]+board[2]+board[3]+board[4]+board[5];
- if(board[0]==0&&board[1]==0&&board[2]==0&&board[3]==0&&board[4]==0&&board[5]==0&&topSum>0)
- {
- board[13]+=topSum;
- gameOver=true;
- }
- if(board[7]==0&&board[8]==0&&board[9]==0&&board[10]==0&&board[11]==0&&board[12]==0&&bottomSum>0)
- {
- board[6]+=bottomSum;
- gameOver=true;
- }
- if(board[0]==0&&board[1]==0&&board[2]==0&&board[3]==0&&board[4]==0&&board[5]==0&&board[7]==0&&board[8]==0&&board[9]==0&&board[10]==0&&board[11]==0&&board[12]==0)gameOver=true;
- if(gameOver==true)break;
- System.out.print("Player "+playerTurn+" Select a Pit: ");
- char selectedPit=sc.next().charAt(0);
- selectedPit=toLowerCase(selectedPit);
- if(selectedPit=='0')break; //Exit sequence (temp)
- playerTurn(selectedPit);
- }
- System.out.println("Game Over!");
- System.out.println("Player 1 Score: "+board[6]);
- System.out.println("Player 2 Score: "+board[13]);
- if(board[6]>board[13])System.out.println("Player 1 Wins!");
- else if(board[6]==board[13])System.out.println("Tie!");
- else System.out.println("Player 2 Wins!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment