Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 24.58 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class EnglishCheckers {
  5.  
  6.     // Global constants
  7.     public static final int RED   = 1;
  8.     public static final int BLUE  = -1;
  9.     public static final int EMPTY = 0;
  10.  
  11.     public static final int SIZE  = 8;
  12.  
  13.     // You can ignore these constants
  14.     public static final int MARK  = 3;
  15.     public static EnglishCheckersGUI grid;
  16.  
  17.     public static Scanner getPlayerFullMoveScanner = null;
  18.     public static Scanner getStrategyScanner = null;
  19.  
  20.     public static final int RANDOM          = 1;
  21.     public static final int DEFENSIVE       = 2;
  22.     public static final int SIDES               = 3;
  23.     public static final int CUSTOM          = 4;
  24.  
  25.  
  26.     public static void main(String[] args) {
  27.  
  28.         // ******** Don't delete *********
  29.         // CREATE THE GRAPHICAL GRID
  30.         grid = new EnglishCheckersGUI(SIZE);
  31.         // *******************************
  32.  
  33.  
  34.         //showBoard(createBoard());
  35.         //printMatrix(createBoard());
  36.  
  37.         interactivePlay();
  38.         //twoPlayers();
  39.        
  40.  
  41.         /* ******** Don't delete ********* */    
  42.         if (getPlayerFullMoveScanner != null){
  43.             getPlayerFullMoveScanner.close();
  44.         }
  45.         if (getStrategyScanner != null){
  46.             getStrategyScanner.close();
  47.         }
  48.         /* ******************************* */
  49.  
  50.     }
  51.  
  52.  
  53.     public static int[][] createBoard(){
  54.         int [][] board=new int[8][8];
  55.         for (int i=0; i<board.length; i=i+1){
  56.             for (int j=0; j<board[i].length; j=j+1){
  57.                 if ((((i==0)||(i==2))&&(j%2==0))||((i==1)&&(j%2==1)))
  58.                         board [i][j]=1;
  59.                 else if ((((i==5)||(i==7))&&(j%2==1))||((i==6)&&(j%2==0)))
  60.                     board [i][j]=-1;
  61.             }
  62.         }
  63.         return board;
  64.     }
  65.  
  66.  
  67.     public static int howManyPins (int[][] board, int player)//function that counts how many Pins the player has
  68.     { //counts how many pins the certain player has
  69.         int sum=0;
  70.     for (int i=0;i<8;i=i+1)
  71.         for(int j=0;j<8;j=j+1)
  72.             if ((board[i][j]*player)>0)
  73.                 sum=sum+1;
  74.     return sum;
  75.     }
  76.     public static int[][] playerDiscs(int[][] board, int player) {
  77.         int[][] positions = new int [howManyPins(board,player)][2];
  78.         int count=0;//counts the entered pins into the array
  79.         for (int i=0;i<8;i=i+1)
  80.             for(int j=0;j<8;j=j+1)
  81.                 if ((board[i][j]*player)>0)
  82.                 {
  83.                     positions[count][0]=i;  //enters the pins Row into box 0
  84.                     positions[count][1]=j;  //enters the pins Col into box 1
  85.                     count=count+1;          //adds 1 to pin counters
  86.                 }
  87.  
  88.         return positions;
  89.     }
  90.  
  91.  
  92.     public static int [][] getRestrictedBasicMoves(int [][] board, int player, int row, int col){
  93.         int [][] basicMoves;
  94.         int n=0;
  95.         for (int i=0; i<8; i=i+1){
  96.                 for (int j=0; j<8; j=j+1){
  97.                         if (isBasicMoveValid(board,player,row,col,i,j))//counts all the possible basic moves a disc has available
  98.                                 n=n+1;
  99.                 }
  100.         }
  101.         basicMoves=new int[n][4]; //creates an array according to needed length
  102.         n=0;
  103.         for (int i=0; i<8; i=i+1){
  104.                 for (int j=0; j<8; j=j+1){
  105.                         if (isBasicMoveValid(board,player,row,col,i,j)){
  106.                                         basicMoves[n][0]=row;
  107.                                         basicMoves[n][1]=col;
  108.                                         basicMoves[n][2]=i;
  109.                                         basicMoves[n][3]=j;
  110.                                         n=n+1;//enters the Moves information into the array
  111.                         }
  112.                 }                              
  113.         }
  114. return basicMoves;
  115. }
  116.    
  117.    
  118.     public static boolean isBasicMoveValid(int[][] board, int player, int fromRow, int fromCol, int toRow, int toCol) {
  119.         boolean ans=false;
  120.         if ((board[fromRow][fromCol]*player)>0) //checks that player has a disk on the 'from' spot
  121.             if (((board[fromRow][fromCol]==1)&&(toRow>fromRow))||((board[fromRow][fromCol]==-1)&&(toRow<fromRow))||(Math.abs(board[fromRow][fromCol])==2))
  122.                 //checks that the disk is heading in the right direction
  123.                 if (board[toRow][toCol]==0)
  124.                     if ((Math.abs(toRow-fromRow)==1)&&(Math.abs(toCol-fromCol)==1))
  125.                         ans=true;
  126.         return ans;
  127.     }
  128.     public static int[][] getAllBasicMoves(int[][] board, int player) {
  129.          int n=0;
  130.             int[][] allBasicMoves;
  131.             int[][] discs=playerDiscs(board,player);//gets all the players discs
  132.             for (int i=0; i<discs.length; i=i+1){//goes through each disk player has
  133.                             int [][] diskMoves=getRestrictedBasicMoves(board, player,discs[i][0],discs[i][1]);//checks all the possible basic moves the certain disk has
  134.                             n=n+diskMoves.length;//counts the possible basic moves for all the discs
  135.                     }
  136.             allBasicMoves=new int[n][4];//makes an array according to number of possible basic moves
  137.             n=0;
  138.             for (int i=0; i<discs.length; i=i+1){//'for' for each disk
  139.                             int [][] diskMoves=getRestrictedBasicJumps(board, player, discs[i][0], discs[i][1]);//get the number of possible moves for each disk
  140.                             for (int j=0; j<diskMoves.length; j=j+1){//'for' for each move the disk has
  141.                                     for (int s=0; s<4; s=s+1)
  142.                                         allBasicMoves[n][s]=diskMoves[j][s];//inputs the 4 coordination into the array with each possible move
  143.                                     n=n+1;      
  144.                                     }
  145.                             }
  146.             return allBasicMoves;
  147.     }
  148.  
  149.     public static boolean isBasicJumpValid(int [][] board, int player, int fromRow, int fromCol, int toRow, int toCol){
  150.                     boolean ans=false;
  151.                     if ((fromRow>=0)&&(fromRow<8)&&(fromCol>=0)&&(fromCol<8)&&(toRow>=0)&&(toRow<8)&&(toCol>=0)&&(toCol<8))
  152.                             if ((board[fromRow][fromCol]*player)>0) //checks that player has a disk on the 'from' spot
  153.                             if (((board[fromRow][fromCol]==1)&&(toRow>fromRow))||((board[fromRow][fromCol]==-1)&&(toRow<fromRow))||(Math.abs(board[fromRow][fromCol])==2))
  154.                                 //checks that the disk is heading in the right direction
  155.                             if ((board[(fromRow+toRow)/2][(fromCol+toCol)/2]*player)<0)// checks that the disk jumps over an opponent disk
  156.                             if (board[toRow][toCol]==0)//checks that 'to' location is empty
  157.                             if ((Math.abs(toRow-fromRow)==2)&&(Math.abs(toCol-fromCol)==2))//checks that 'to' location is at the right distance from the 'from' location
  158.                                     ans=true;
  159.                     return ans;    
  160.             }
  161.  
  162.  
  163.  
  164.     public static int [][] getRestrictedBasicJumps(int [][] board, int player, int row, int col){
  165.                     int [][] basicJumps;
  166.                     int n=0;
  167.                     for (int i=0; i<8; i=i+1){
  168.                             for (int j=0; j<8; j=j+1){
  169.                                     if (isBasicJumpValid(board,player,row,col,i,j))//counts all the possible jumps a disk has available
  170.                                             n=n+1;
  171.                             }
  172.                     }
  173.                     basicJumps=new int[n][4]; //creates an array according to needed length
  174.                     n=0;
  175.                     for (int i=0; i<8; i=i+1){
  176.                             for (int j=0; j<8; j=j+1){
  177.                                     if (isBasicJumpValid(board,player,row,col,i,j)){
  178.                                                     basicJumps[n][0]=row;
  179.                                                     basicJumps[n][1]=col;
  180.                                                     basicJumps[n][2]=i;
  181.                                                     basicJumps[n][3]=j;
  182.                                                     n=n+1;//enters the jump information into the array
  183.                                     }
  184.                             }                              
  185.                     }
  186.             return basicJumps;
  187.             }
  188.              
  189.  
  190.  
  191.     public static int [][] getAllBasicJumps(int [][] board, int player){
  192.         int n=0;
  193.         int[][] allBasicJumps;
  194.         int[][] p2Moves=playerDiscs(board,player);//gets all the players discs
  195.         for (int i=0; i<p2Moves.length; i=i+1){//goes through each disk player has
  196.                         int [][] diskJumps=getRestrictedBasicJumps(board, player,p2Moves[i][0],p2Moves[i][1]);//checks all the possible jumps the certain disk has
  197.                         n=n+diskJumps.length;//counts the possible jumps for all the discs
  198.                 }
  199.         allBasicJumps=new int[n][4];//makes an array according to number of possible jumps
  200.         n=0;
  201.         for (int i=0; i<p2Moves.length; i=i+1){//'for' for each disk
  202.                         int [][] diskJumps=getRestrictedBasicJumps(board, player, p2Moves[i][0], p2Moves[i][1]);//get the number of possible jumps for each disk
  203.                         for (int j=0; j<diskJumps.length; j=j+1){//'for' for each move the disk has
  204.                                 for (int s=0; s<4; s=s+1)
  205.                                     allBasicJumps[n][s]=diskJumps[j][s];//inputs the 4 coordination into the array with each possible move
  206.                                 n=n+1;      
  207.                                 }
  208.                         }
  209.         return allBasicJumps;
  210. }
  211.  
  212.  
  213.     public static boolean canJump(int [][] board, int player){
  214.         boolean ans=false;
  215.         int[][] availableJumps=getAllBasicJumps(board, player); //get an array for all the basic jumps
  216.         if (availableJumps.length>0)//and return false if the player has no options to jump, else returns true
  217.                 ans=true;
  218.         return ans;
  219. }
  220.  
  221.  
  222.     public static boolean isMoveValid(int [][] board, int player, int fromRow, int fromCol, int toRow, int toCol){
  223.                     boolean ans=false;      
  224.                     if ((isBasicJumpValid(board,player,fromRow,fromCol,toRow,toCol))||((isBasicMoveValid(board,player,fromRow,fromCol,toRow,toCol))&&!(canJump(board,player))))
  225.                             ans=true;//checks if the discs movement is legitimate
  226.                     return ans;
  227.             }
  228.  
  229.     public static boolean hasValidMoves(int [][] board, int player){
  230.         boolean ans=false;
  231.         int[][]basicMoves=getAllBasicMoves(board,player);//gets all the possible moves in an array
  232.         if ((basicMoves.length>0)||(canJump(board,player)))//checks if any player has any possible jump or moves
  233.                 ans=true;
  234.         return ans;
  235. }
  236.  
  237.     public static int [][] playMove(int [][] board, int player, int fromRow, int fromCol, int toRow, int toCol){
  238.                     if (isBasicMoveValid(board,player,fromRow,fromCol,toRow,toCol)){//checks if move is legitimate move
  239.                             board[toRow][toCol]=board[fromRow][fromCol];            //and if so moves the disk
  240.                             board[fromRow][fromCol]=0;
  241.                     }
  242.                     else if (isBasicJumpValid(board,player,fromRow,fromCol,toRow,toCol)){//checks if the jump is legitimate jump
  243.                             board[toRow][toCol]=board[fromRow][fromCol];                //and if so makes the jump and removes the opponents disk
  244.                             board[fromRow][fromCol]=0;
  245.                             board[((fromRow+toRow)/2)][((fromCol+toCol)/2)]=0;
  246.                     }
  247.                     if ((board[toRow][toCol]==1)&&(toRow==7))//if the disk is RED and reached the last line,it will turn into a King
  248.                             board[toRow][toCol]=2;
  249.                     else if  ((board[toRow][toCol]==-1)&&(toRow==0))//if the disk is Blue and it reached the last line, it will turn into a king
  250.                             board[toRow][toCol]=-2;
  251.                     return board;
  252.             }
  253.  
  254.     public static boolean gameOver(int[][] board, int player) {
  255.         boolean ans = false;
  256.         if(hasValidMoves(board,player)==false)//if the certain player does'nt have any possible moves its game over
  257.             ans=true;                   //because no moves = either stuck or no discs either way it's game over
  258.         return ans;
  259.     }
  260.  
  261.  
  262.     public static int findTheLeader(int[][] board) {
  263.         int ans = 0;
  264.         int sum=0;
  265.         int[][] discsRED=playerDiscs(board,1);//gets all the discs for the RED player
  266.         for(int i=0;i<discsRED.length;i=i+1)
  267.         sum=sum+board[discsRED[i][0]][discsRED[i][1]];//adds all the Red player into sum when Kings are worth 2 players
  268.         int [][] discsBLUE=playerDiscs(board,-1);//gets all the discs for the BLUE player
  269.         for(int i=0;i<discsBLUE.length;i=i+1)
  270.         sum=sum+board[discsBLUE[i][0]][discsBLUE[i][1]];//reduces all the Blue players from sum when Kings reduce 2 players
  271.         if(sum>0)//if sums turns out to be a positive number answer will be 1 which means Red is the leader
  272.             ans=1;
  273.         else if (sum<0)//if sums turns out to be a negative number answer will be -1 which means Blue is the leader
  274.             ans=-1;
  275.                         //otherwise they are equal and in that case answer will be 0 which means a draw
  276.         return ans;
  277.     }
  278.  
  279.  
  280.     public static int[][] randomPlayer(int[][] board, int player) {
  281.             int [][] PM=getAllBasicMoves(board,player); //PM=possible moves
  282.             int [][] PJ=getAllBasicJumps(board,player); //PJ=possible jumps
  283.              if((PM.length!=0)&&(PJ.length==0))//if exists a possible move,and no jumps are available, choose a move randomly
  284.                 {
  285.                     int RndM=(int)(Math.random()*PM.length);//RndM=Random move
  286.                     board=playMove(board,player,PM[RndM][0],PM[RndM][1],PM[RndM][2],PM[RndM][3]);//makes the move
  287.                 }
  288.              else while (PJ.length!=0) //if exists a possible jump, make one of them randomly
  289.             {
  290.                 int RndJ=(int)(Math.random()*PJ.length);//RndJ= random Jump
  291.                 board=playMove(board,player,PJ[RndJ][0],PJ[RndJ][1],PJ[RndJ][2],PJ[RndJ][3]);  //makes the move
  292.                 int row=PJ[RndJ][2],col=PJ[RndJ][3];
  293.                 PJ =getRestrictedBasicJumps(board,player,row,col); //updates PJ into the next possible jump
  294.             }
  295.         return board;
  296.     }
  297.  
  298.  
  299.     public static int[][] defensivePlayer(int[][] board, int player) {
  300.         int [][] PJ=getAllBasicJumps(board,player); //PJ=possible jumps
  301.         int [][] PM=getAllBasicMoves(board,player); //PM=possible moves
  302.         int minRed=8,maxBlue=-1,mark=0,sum=1,choosen;//minRed is to compare which movement is most defensive, and mark is going to be the marked movement we are will use
  303.         if((!canJump(board,player))&&(PM.length!=0))//checks that no jumps are possible and  moves are available
  304.         {
  305.             for(int i=0;i<PM.length;i=i+1)//compares between the movements which is the most defensive
  306.             {
  307.                 if(player==1){
  308.                     if(PM[i][2]<minRed)
  309.                     {
  310.                         minRed=PM[i][2];
  311.                         mark=i;
  312.                         sum=1;
  313.                     }
  314.                     else if (PM[i][2]==minRed)
  315.                         sum=sum+1;
  316.                 }
  317.                 else {
  318.                     if(PM[i][2]>maxBlue)
  319.                 {
  320.                     maxBlue=PM[i][2];
  321.                     mark=i;
  322.                     sum=1;
  323.                 }
  324.                 else if (PM[i][2]==maxBlue)
  325.                     sum=sum+1;
  326.                 }
  327.             }
  328.                 if(sum==1)//if only one defensive choice play the move
  329.             board=playMove(board,player,PM[mark][0],PM[mark][1],PM[mark][2],PM[mark][3]);//makes the most defensive move
  330.                
  331.                 else //else if there is a few defensive plays randomly pick one
  332.                 {
  333.                     choosen=(int)((Math.random()*sum) + 1);
  334.                     int i=-1,s=0;
  335.                     while(s!=choosen)
  336.                     {
  337.                         i=i+1;
  338.                         if(PM[i][2]==PM[mark][2])
  339.                             s=s+1;
  340.                     }
  341.                     board=playMove(board,player,PM[i][0],PM[i][1],PM[i][2],PM[i][3]);//makes the most defensive move
  342.                 }
  343.         }
  344.         while (PJ.length!=0) //if exists a possible jump, make one of them randomly
  345.         {
  346.             int RndJ=(int)(Math.random()*PJ.length);//RndJ= random Jump
  347.             board=playMove(board,player,PJ[RndJ][0],PJ[RndJ][1],PJ[RndJ][2],PJ[RndJ][3]);  //makes the move
  348.             int row=PJ[RndJ][2],col=PJ[RndJ][3];
  349.             PJ =getRestrictedBasicJumps(board,player,row,col); //updates PJ into the next possible jump
  350.         }
  351.         return board;
  352.     }
  353.  
  354.     public static int[][] sidesPlayer(int[][] board, int player) {
  355.         int [][] PJ=getAllBasicJumps(board,player); //PJ=possible jumps
  356.         int [][] PM=getAllBasicMoves(board,player); //PM=possible moves
  357.         int side=8,mark=0;//to check which move is the most side,Side(to compare) mark(to mark the move)
  358.         if((PJ.length==0)&&(PM.length!=0))//checks that no jumps are possible and only moves are available
  359.         {
  360.             for(int i=0;i<PM.length;i=i+1)//compares between the movements and checks which takes the disk into the side
  361.             {
  362.                 if ((PM[i][4])<side)//if it is the most right side move it will remember it
  363.                 {
  364.                     side=PM[i][4];
  365.                     mark=i;
  366.                 }
  367.                 if (PM[i][4]>(7-side))//or if it is the most left side move it will remember it
  368.                 {
  369.                     side=7-PM[i][4];
  370.                     mark=i;
  371.                 }
  372.                 if (((PM[i][4]==side)||(PM[i][4]==(7-side)))&&(Math.random()>0.5))//if equally sideway randomly decides if to pick it
  373.                     mark=i;    
  374.             }
  375.             board=playMove(board,player,PM[mark][0],PM[mark][1],PM[mark][2],PM[mark][3]);//makes the most sideway move
  376.                
  377.         }  
  378.         while (PJ.length!=0) //if exists a possible jump, make one of them randomly
  379.         {
  380.             int RndJ=(int)(Math.random()*PJ.length);//RndJ= random Jump
  381.             board=playMove(board,player,PJ[RndJ][0],PJ[RndJ][1],PJ[RndJ][2],PJ[RndJ][3]);  //makes the move
  382.             int row=PJ[RndJ][2],col=PJ[RndJ][3];
  383.             PJ =getRestrictedBasicJumps(board,player,row,col); //updates PJ into the next possible jump
  384.         }
  385.  
  386.         return board;
  387.     }
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.    
  396.    
  397.    
  398.    
  399.    
  400.     //******************************************************************************//
  401.  
  402.     /* ---------------------------------------------------------- *
  403.      * Play an interactive game between the computer and you      *
  404.      * ---------------------------------------------------------- */
  405.     public static void interactivePlay() {
  406.         int[][] board = createBoard();
  407.         showBoard(board);
  408.  
  409.         System.out.println("Welcome to the interactive Checkers Game !");
  410.  
  411.         int strategy = getStrategyChoice();
  412.         System.out.println("You are the first player (RED discs)");
  413.  
  414.         boolean oppGameOver = false;
  415.         while (!gameOver(board, RED) && !oppGameOver) {
  416.             board = getPlayerFullMove(board, RED);
  417.  
  418.             oppGameOver = gameOver(board, BLUE);
  419.             if (!oppGameOver) {
  420.                 EnglishCheckersGUI.sleep(200);
  421.  
  422.                 board = getStrategyFullMove(board, BLUE, strategy);
  423.             }
  424.         }
  425.  
  426.         int winner = 0;
  427.         if (playerDiscs(board, RED).length == 0  |  playerDiscs(board, BLUE).length == 0){
  428.             winner = findTheLeader(board);
  429.         }
  430.  
  431.         if (winner == RED) {
  432.             System.out.println();
  433.             System.out.println("\t *************************");
  434.             System.out.println("\t * You are the winner !! *");
  435.             System.out.println("\t *************************");
  436.         }
  437.         else if (winner == BLUE) {
  438.             System.out.println("\n======= You lost :( =======");
  439.         }
  440.         else
  441.             System.out.println("\n======= DRAW =======");
  442.     }
  443.  
  444.  
  445.     /* --------------------------------------------------------- *
  446.      * A game between two players                                *
  447.      * --------------------------------------------------------- */
  448.     public static void twoPlayers() {
  449.         int[][] board = createBoard();
  450.         showBoard(board);
  451.  
  452.         System.out.println("Welcome to the 2-player Checkers Game !");
  453.  
  454.         boolean oppGameOver = false;
  455.         while (!gameOver(board, RED)  &  !oppGameOver) {
  456.             System.out.println("\nRED's turn");
  457.             board = getPlayerFullMove(board, RED);
  458.  
  459.             oppGameOver = gameOver(board, BLUE);
  460.             if (!oppGameOver) {
  461.                 System.out.println("\nBLUE's turn");
  462.                 board = getPlayerFullMove(board, BLUE);
  463.             }
  464.         }
  465.  
  466.         int winner = 0;
  467.         if (playerDiscs(board, RED).length == 0  |  playerDiscs(board, BLUE).length == 0)
  468.             winner = findTheLeader(board);
  469.  
  470.         System.out.println();
  471.         System.out.println("\t ************************************");
  472.         if (winner == RED)
  473.             System.out.println("\t * The red player is the winner !!  *");
  474.         else if (winner == BLUE)
  475.             System.out.println("\t * The blue player is the winner !! *");
  476.         else
  477.             System.out.println("\t * DRAW !! *");
  478.         System.out.println("\t ************************************");
  479.     }
  480.  
  481.  
  482.     /* --------------------------------------------------------- *
  483.      * Get a complete (possibly a sequence of jumps) move        *
  484.      * from a human player.                                      *
  485.      * --------------------------------------------------------- */
  486.     public static int[][] getPlayerFullMove(int[][] board, int player) {
  487.         // Get first move/jump
  488.         int fromRow = -1, fromCol = -1, toRow = -1, toCol = -1;
  489.         boolean jumpingMove = canJump(board, player);
  490.         boolean badMove   = true;
  491.         getPlayerFullMoveScanner = new Scanner(System.in);//I've modified it
  492.         while (badMove) {
  493.             if (player == 1){
  494.                 System.out.println("Red, Please play:");
  495.             } else {
  496.                 System.out.println("Blue, Please play:");
  497.             }
  498.  
  499.             fromRow = getPlayerFullMoveScanner.nextInt();
  500.             fromCol = getPlayerFullMoveScanner.nextInt();
  501.  
  502.             int[][] moves = jumpingMove ? getAllBasicJumps(board, player) : getAllBasicMoves(board, player);
  503.             markPossibleMoves(board, moves, fromRow, fromCol, MARK);
  504.             toRow   = getPlayerFullMoveScanner.nextInt();
  505.             toCol   = getPlayerFullMoveScanner.nextInt();
  506.             markPossibleMoves(board, moves, fromRow, fromCol, EMPTY);
  507.  
  508.             badMove = !isMoveValid(board, player, fromRow, fromCol, toRow, toCol);
  509.             if (badMove)
  510.                 System.out.println("\nThis is an illegal move");
  511.         }
  512.  
  513.         // Apply move/jump
  514.         board = playMove(board, player, fromRow, fromCol, toRow, toCol);
  515.         showBoard(board);
  516.  
  517.         // Get extra jumps
  518.         if (jumpingMove) {
  519.             boolean longMove = (getRestrictedBasicJumps(board, player, toRow, toCol).length > 0);
  520.             while (longMove) {
  521.                 fromRow = toRow;
  522.                 fromCol = toCol;
  523.  
  524.                 int[][] moves = getRestrictedBasicJumps(board, player, fromRow, fromCol);
  525.  
  526.                 boolean badExtraMove = true;
  527.                 while (badExtraMove) {
  528.                     markPossibleMoves(board, moves, fromRow, fromCol, MARK);
  529.                     System.out.println("Continue jump:");
  530.                     toRow = getPlayerFullMoveScanner.nextInt();
  531.                     toCol = getPlayerFullMoveScanner.nextInt();
  532.                     markPossibleMoves(board, moves, fromRow, fromCol, EMPTY);
  533.  
  534.                     badExtraMove = !isMoveValid(board, player, fromRow, fromCol, toRow, toCol);
  535.                     if (badExtraMove)
  536.                         System.out.println("\nThis is an illegal jump destination :(");
  537.                 }
  538.  
  539.                 // Apply extra jump
  540.                 board = playMove(board, player, fromRow, fromCol, toRow, toCol);
  541.                 showBoard(board);
  542.  
  543.                 longMove = (getRestrictedBasicJumps(board, player, toRow, toCol).length > 0);
  544.             }
  545.         }
  546.         return board;
  547.     }
  548.  
  549.  
  550.     /* --------------------------------------------------------- *
  551.      * Get a complete (possibly a sequence of jumps) move        *
  552.      * from a strategy.                                          *
  553.      * --------------------------------------------------------- */
  554.     public static int[][] getStrategyFullMove(int[][] board, int player, int strategy) {
  555.         if (strategy == RANDOM)
  556.             board = randomPlayer(board, player);
  557.         else if (strategy == DEFENSIVE)
  558.             board = defensivePlayer(board, player);
  559.         else if (strategy == SIDES)
  560.             board = sidesPlayer(board, player);
  561.  
  562.         showBoard(board);
  563.         return board;
  564.     }
  565.  
  566.  
  567.     /* --------------------------------------------------------- *
  568.      * Get a strategy choice before the game.                    *
  569.      * --------------------------------------------------------- */
  570.     public static int getStrategyChoice() {
  571.         int strategy = -1;
  572.         getStrategyScanner = new Scanner(System.in);
  573.         System.out.println("Choose the strategy of your opponent:" +
  574.                 "\n\t(" + RANDOM + ") - Random player" +
  575.                 "\n\t(" + DEFENSIVE + ") - Defensive player" +
  576.                 "\n\t(" + SIDES + ") - To-the-Sides player player");
  577.         while (strategy != RANDOM  &  strategy != DEFENSIVE
  578.                 &  strategy != SIDES) {
  579.             strategy=getStrategyScanner.nextInt();
  580.         }
  581.         return strategy;
  582.     }
  583.  
  584.  
  585.     /* --------------------------------------- *
  586.      * Print the possible moves                *
  587.      * --------------------------------------- */
  588.     public static void printMoves(int[][] possibleMoves) {
  589.         for (int i = 0;  i < 4;  i = i+1) {
  590.             for (int j = 0;  j < possibleMoves.length;  j = j+1)
  591.                 System.out.print(" " + possibleMoves[j][i]);
  592.             System.out.println();
  593.         }
  594.     }
  595.  
  596.  
  597.     /* --------------------------------------- *
  598.      * Mark/unmark the possible moves          *
  599.      * --------------------------------------- */
  600.     public static void markPossibleMoves(int[][] board, int[][] moves, int fromRow, int fromColumn, int value) {
  601.         for (int i = 0;  i < moves.length;  i = i+1)
  602.             if (moves[i][0] == fromRow  &  moves[i][1] == fromColumn)
  603.                 board[moves[i][2]][moves[i][3]] = value;
  604.  
  605.         showBoard(board);
  606.     }
  607.  
  608.  
  609.     /* --------------------------------------------------------------------------- *
  610.      * Shows the board in a graphic window                                         *
  611.      * you can use it without understanding how it works.                          *                                                    
  612.      * --------------------------------------------------------------------------- */
  613.     public static void showBoard(int[][] board) {
  614.         grid.showBoard(board);
  615.     }
  616.  
  617.  
  618.     /* --------------------------------------------------------------------------- *
  619.      * Print the board                                                             *
  620.      * you can use it without understanding how it works.                          *                                                    
  621.      * --------------------------------------------------------------------------- */
  622.     public static void printMatrix(int[][] matrix){
  623.         for (int i = matrix.length-1; i >= 0; i = i-1){
  624.             for (int j = 0; j < matrix.length; j = j+1){
  625.                 System.out.format("%4d", matrix[i][j]);
  626.             }
  627.             System.out.println();
  628.         }
  629.     }
  630.  
  631. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement