KeeganT

Ch10Ex15

Apr 2nd, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.89 KB | None | 0 0
  1. package mancala;
  2. import java.util.Scanner;
  3. import static java.lang.Character.toLowerCase;
  4.  
  5. public class Mancala
  6. {
  7.     //Globals
  8.     static int[] board=new int[]{3,3,3,3,3,3,0,3,3,3,3,3,3,0};
  9.     //Board:
  10.     //    b13, b12,b11,b10,b9,b8
  11.     //b14                        b7
  12.     //    b1, b2, b3, b4, b5, b6
  13.     static int playerTurn=1; //Bottom Player = Player 1
  14.     static boolean gameOver=false, repeatTurn=false;
  15.     static String bottomChars="abcdef";
  16.     static char[] bottomPits=bottomChars.toCharArray();
  17.     static String topChars="fedcba";
  18.     static char[] topPits=topChars.toCharArray();
  19.    
  20.     public static void displayBoard()
  21.     {
  22.         System.out.print("   ");
  23.         for (int c=12;c>=7;c--)
  24.         {
  25.             if(board[c]<10)System.out.print(board[c]+"  ");
  26.             else System.out.print(board[c]+" ");
  27.         }
  28.         System.out.print("\n"+board[13]);
  29.         if(board[6]<10&&board[13]<10)for(int c=0;c<20;c++)System.out.print(" ");
  30.         else if(board[6]>9&&board[13]>9)for(int c=0;c<18;c++)System.out.print(" ");
  31.         else if((board[6]>9&&board[13]<10)||(board[13]>9&&board[6]<10))for(int c=0;c<19;c++)System.out.print(" ");
  32.         System.out.println(board[6]);
  33.         System.out.print("   ");
  34.         for (int c=0;c<6;c++)
  35.         {
  36.             if(board[c]<10)System.out.print(board[c]+"  ");
  37.             else System.out.print(board[c]+" ");
  38.         }
  39.         System.out.println("\n   A  B  C  D  E  F   ");
  40.     }
  41.    
  42.     public static void playerTurn(char selectedPit)
  43.     {
  44.         int currentSeedCount=0,arrayPosition=0,nextPit=0;
  45.         if(playerTurn==1)
  46.         {
  47.             for(int c=0;c<6;c++)
  48.             {
  49.                 if(selectedPit==bottomPits[c])
  50.                 {
  51.                     currentSeedCount=board[c];
  52.                     board[c]=0;
  53.                     arrayPosition=c;
  54.                 }
  55.             }
  56.         }
  57.        
  58.         else if(playerTurn==2)
  59.         {
  60.             for(int c=7;c<13;c++)
  61.             {
  62.                 if(selectedPit==topPits[c-7])
  63.                 {
  64.                     currentSeedCount=board[c];
  65.                     board[c]=0;
  66.                     arrayPosition=c;
  67.                 }
  68.             }
  69.         }
  70.        
  71.         arrayPosition+=1;
  72.         repeatTurn=false;
  73.        
  74.         for(int c=0;currentSeedCount!=0;c++)
  75.         {
  76.             if(arrayPosition+c+1==13)nextPit=0;
  77.             else nextPit=arrayPosition+c+1;
  78.             if(nextPit==14)nextPit=0;
  79.             if(currentSeedCount==2)
  80.             {
  81.                 if(playerTurn==1)
  82.                 {
  83.                     for(int x=0,i=12;x<=5;x++,i--)
  84.                     {
  85.                         if(nextPit==x&&board[x]==0)
  86.                         {
  87.                             board[6]+=board[i];
  88.                             board[i]=0;
  89.                         }
  90.                     }
  91.                 }
  92.                
  93.                 else if(playerTurn==2)
  94.                 {
  95.                     for(int x=7,i=5;x<=12;x++,i--)
  96.                     {
  97.                         if(nextPit==x&&board[x]==0)
  98.                         {
  99.                             board[13]+=board[i];
  100.                             board[i]=0;
  101.                         }
  102.                     }
  103.                 }
  104.             }
  105.             board[arrayPosition+c]+=1;
  106.             if(arrayPosition+c==13)
  107.             {
  108.                 arrayPosition=-1;
  109.                 c=0;
  110.             }
  111.             if(currentSeedCount>1)
  112.             {
  113.                 if(playerTurn==1&&nextPit==0)
  114.                 {
  115.                     arrayPosition=-1;
  116.                     c=0;
  117.                 }
  118.                 else if(playerTurn==2&&nextPit==6)c++;
  119.             }
  120.             currentSeedCount--;
  121.             if((playerTurn==1&&arrayPosition+c==6)||(playerTurn==2&&arrayPosition+c==-1))repeatTurn=true;
  122.             else repeatTurn=false;
  123.         }    
  124.         if(repeatTurn==true)System.out.println("Landed in home bin! Player "+playerTurn+" gets another turn.");
  125.         if(repeatTurn==false&&playerTurn==1)playerTurn=2;
  126.         else if(repeatTurn==false&&playerTurn==2)playerTurn=1;
  127.     }
  128.    
  129.     public static void main(String[] args)
  130.     {
  131.         Scanner sc=new Scanner(System.in);
  132.         System.out.println("Welcome to Mancala!");
  133.         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.");
  134.         for(int c=0;true;c++)
  135.         {
  136.             displayBoard();
  137.             int topSum=board[7]+board[8]+board[9]+board[10]+board[11]+board[12];
  138.             int bottomSum=board[0]+board[1]+board[2]+board[3]+board[4]+board[5];
  139.             if(board[0]==0&&board[1]==0&&board[2]==0&&board[3]==0&&board[4]==0&&board[5]==0&&topSum>0)
  140.             {
  141.                 board[13]+=topSum;
  142.                 gameOver=true;
  143.             }
  144.             if(board[7]==0&&board[8]==0&&board[9]==0&&board[10]==0&&board[11]==0&&board[12]==0&&bottomSum>0)
  145.             {
  146.                 board[6]+=bottomSum;
  147.                 gameOver=true;
  148.             }
  149.             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;
  150.             if(gameOver==true)break;
  151.             System.out.print("Player "+playerTurn+" Select a Pit: ");
  152.             char selectedPit=sc.next().charAt(0);
  153.             selectedPit=toLowerCase(selectedPit);
  154.             if(selectedPit=='0')break; //Exit sequence (temp)
  155.             playerTurn(selectedPit);
  156.         }
  157.         System.out.println("Game Over!");
  158.         System.out.println("Player 1 Score: "+board[6]);
  159.         System.out.println("Player 2 Score: "+board[13]);
  160.         if(board[6]>board[13])System.out.println("Player 1 Wins!");
  161.         else if(board[6]==board[13])System.out.println("Tie!");
  162.         else System.out.println("Player 2 Wins!");
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment