Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class EnglishCheckers {
- // Global constants
- public static final int RED = 1;
- public static final int BLUE = -1;
- public static final int EMPTY = 0;
- public static final int SIZE = 8;
- // You can ignore these constants
- public static final int MARK = 3;
- public static EnglishCheckersGUI grid;
- public static Scanner getPlayerFullMoveScanner = null;
- public static Scanner getStrategyScanner = null;
- public static final int RANDOM = 1;
- public static final int DEFENSIVE = 2;
- public static final int SIDES = 3;
- public static final int CUSTOM = 4;
- public static void main(String[] args) {
- // ******** Don't delete *********
- // CREATE THE GRAPHICAL GRID
- grid = new EnglishCheckersGUI(SIZE);
- // *******************************
- //showBoard(createBoard());
- //printMatrix(createBoard());
- interactivePlay();
- //twoPlayers();
- /* ******** Don't delete ********* */
- if (getPlayerFullMoveScanner != null){
- getPlayerFullMoveScanner.close();
- }
- if (getStrategyScanner != null){
- getStrategyScanner.close();
- }
- /* ******************************* */
- }
- public static int[][] createBoard(){
- int [][] board=new int[8][8];
- for (int i=0; i<board.length; i=i+1){
- for (int j=0; j<board[i].length; j=j+1){
- if ((((i==0)||(i==2))&&(j%2==0))||((i==1)&&(j%2==1)))
- board [i][j]=1;
- else if ((((i==5)||(i==7))&&(j%2==1))||((i==6)&&(j%2==0)))
- board [i][j]=-1;
- }
- }
- return board;
- }
- public static int howManyPins (int[][] board, int player)//function that counts how many Pins the player has
- { //counts how many pins the certain player has
- int sum=0;
- for (int i=0;i<8;i=i+1)
- for(int j=0;j<8;j=j+1)
- if ((board[i][j]*player)>0)
- sum=sum+1;
- return sum;
- }
- public static int[][] playerDiscs(int[][] board, int player) {
- int[][] positions = new int [howManyPins(board,player)][2];
- int count=0;//counts the entered pins into the array
- for (int i=0;i<8;i=i+1)
- for(int j=0;j<8;j=j+1)
- if ((board[i][j]*player)>0)
- {
- positions[count][0]=i; //enters the pins Row into box 0
- positions[count][1]=j; //enters the pins Col into box 1
- count=count+1; //adds 1 to pin counters
- }
- return positions;
- }
- public static int [][] getRestrictedBasicMoves(int [][] board, int player, int row, int col){
- int [][] basicMoves;
- int n=0;
- for (int i=0; i<8; i=i+1){
- for (int j=0; j<8; j=j+1){
- if (isBasicMoveValid(board,player,row,col,i,j))//counts all the possible basic moves a disc has available
- n=n+1;
- }
- }
- basicMoves=new int[n][4]; //creates an array according to needed length
- n=0;
- for (int i=0; i<8; i=i+1){
- for (int j=0; j<8; j=j+1){
- if (isBasicMoveValid(board,player,row,col,i,j)){
- basicMoves[n][0]=row;
- basicMoves[n][1]=col;
- basicMoves[n][2]=i;
- basicMoves[n][3]=j;
- n=n+1;//enters the Moves information into the array
- }
- }
- }
- return basicMoves;
- }
- public static boolean isBasicMoveValid(int[][] board, int player, int fromRow, int fromCol, int toRow, int toCol) {
- boolean ans=false;
- if ((board[fromRow][fromCol]*player)>0) //checks that player has a disk on the 'from' spot
- if (((board[fromRow][fromCol]==1)&&(toRow>fromRow))||((board[fromRow][fromCol]==-1)&&(toRow<fromRow))||(Math.abs(board[fromRow][fromCol])==2))
- //checks that the disk is heading in the right direction
- if (board[toRow][toCol]==0)
- if ((Math.abs(toRow-fromRow)==1)&&(Math.abs(toCol-fromCol)==1))
- ans=true;
- return ans;
- }
- public static int[][] getAllBasicMoves(int[][] board, int player) {
- int n=0;
- int[][] allBasicMoves;
- int[][] discs=playerDiscs(board,player);//gets all the players discs
- for (int i=0; i<discs.length; i=i+1){//goes through each disk player has
- int [][] diskMoves=getRestrictedBasicMoves(board, player,discs[i][0],discs[i][1]);//checks all the possible basic moves the certain disk has
- n=n+diskMoves.length;//counts the possible basic moves for all the discs
- }
- allBasicMoves=new int[n][4];//makes an array according to number of possible basic moves
- n=0;
- for (int i=0; i<discs.length; i=i+1){//'for' for each disk
- int [][] diskMoves=getRestrictedBasicJumps(board, player, discs[i][0], discs[i][1]);//get the number of possible moves for each disk
- for (int j=0; j<diskMoves.length; j=j+1){//'for' for each move the disk has
- for (int s=0; s<4; s=s+1)
- allBasicMoves[n][s]=diskMoves[j][s];//inputs the 4 coordination into the array with each possible move
- n=n+1;
- }
- }
- return allBasicMoves;
- }
- public static boolean isBasicJumpValid(int [][] board, int player, int fromRow, int fromCol, int toRow, int toCol){
- boolean ans=false;
- if ((fromRow>=0)&&(fromRow<8)&&(fromCol>=0)&&(fromCol<8)&&(toRow>=0)&&(toRow<8)&&(toCol>=0)&&(toCol<8))
- if ((board[fromRow][fromCol]*player)>0) //checks that player has a disk on the 'from' spot
- if (((board[fromRow][fromCol]==1)&&(toRow>fromRow))||((board[fromRow][fromCol]==-1)&&(toRow<fromRow))||(Math.abs(board[fromRow][fromCol])==2))
- //checks that the disk is heading in the right direction
- if ((board[(fromRow+toRow)/2][(fromCol+toCol)/2]*player)<0)// checks that the disk jumps over an opponent disk
- if (board[toRow][toCol]==0)//checks that 'to' location is empty
- if ((Math.abs(toRow-fromRow)==2)&&(Math.abs(toCol-fromCol)==2))//checks that 'to' location is at the right distance from the 'from' location
- ans=true;
- return ans;
- }
- public static int [][] getRestrictedBasicJumps(int [][] board, int player, int row, int col){
- int [][] basicJumps;
- int n=0;
- for (int i=0; i<8; i=i+1){
- for (int j=0; j<8; j=j+1){
- if (isBasicJumpValid(board,player,row,col,i,j))//counts all the possible jumps a disk has available
- n=n+1;
- }
- }
- basicJumps=new int[n][4]; //creates an array according to needed length
- n=0;
- for (int i=0; i<8; i=i+1){
- for (int j=0; j<8; j=j+1){
- if (isBasicJumpValid(board,player,row,col,i,j)){
- basicJumps[n][0]=row;
- basicJumps[n][1]=col;
- basicJumps[n][2]=i;
- basicJumps[n][3]=j;
- n=n+1;//enters the jump information into the array
- }
- }
- }
- return basicJumps;
- }
- public static int [][] getAllBasicJumps(int [][] board, int player){
- int n=0;
- int[][] allBasicJumps;
- int[][] p2Moves=playerDiscs(board,player);//gets all the players discs
- for (int i=0; i<p2Moves.length; i=i+1){//goes through each disk player has
- int [][] diskJumps=getRestrictedBasicJumps(board, player,p2Moves[i][0],p2Moves[i][1]);//checks all the possible jumps the certain disk has
- n=n+diskJumps.length;//counts the possible jumps for all the discs
- }
- allBasicJumps=new int[n][4];//makes an array according to number of possible jumps
- n=0;
- for (int i=0; i<p2Moves.length; i=i+1){//'for' for each disk
- int [][] diskJumps=getRestrictedBasicJumps(board, player, p2Moves[i][0], p2Moves[i][1]);//get the number of possible jumps for each disk
- for (int j=0; j<diskJumps.length; j=j+1){//'for' for each move the disk has
- for (int s=0; s<4; s=s+1)
- allBasicJumps[n][s]=diskJumps[j][s];//inputs the 4 coordination into the array with each possible move
- n=n+1;
- }
- }
- return allBasicJumps;
- }
- public static boolean canJump(int [][] board, int player){
- boolean ans=false;
- int[][] availableJumps=getAllBasicJumps(board, player); //get an array for all the basic jumps
- if (availableJumps.length>0)//and return false if the player has no options to jump, else returns true
- ans=true;
- return ans;
- }
- public static boolean isMoveValid(int [][] board, int player, int fromRow, int fromCol, int toRow, int toCol){
- boolean ans=false;
- if ((isBasicJumpValid(board,player,fromRow,fromCol,toRow,toCol))||((isBasicMoveValid(board,player,fromRow,fromCol,toRow,toCol))&&!(canJump(board,player))))
- ans=true;//checks if the discs movement is legitimate
- return ans;
- }
- public static boolean hasValidMoves(int [][] board, int player){
- boolean ans=false;
- int[][]basicMoves=getAllBasicMoves(board,player);//gets all the possible moves in an array
- if ((basicMoves.length>0)||(canJump(board,player)))//checks if any player has any possible jump or moves
- ans=true;
- return ans;
- }
- public static int [][] playMove(int [][] board, int player, int fromRow, int fromCol, int toRow, int toCol){
- if (isBasicMoveValid(board,player,fromRow,fromCol,toRow,toCol)){//checks if move is legitimate move
- board[toRow][toCol]=board[fromRow][fromCol]; //and if so moves the disk
- board[fromRow][fromCol]=0;
- }
- else if (isBasicJumpValid(board,player,fromRow,fromCol,toRow,toCol)){//checks if the jump is legitimate jump
- board[toRow][toCol]=board[fromRow][fromCol]; //and if so makes the jump and removes the opponents disk
- board[fromRow][fromCol]=0;
- board[((fromRow+toRow)/2)][((fromCol+toCol)/2)]=0;
- }
- if ((board[toRow][toCol]==1)&&(toRow==7))//if the disk is RED and reached the last line,it will turn into a King
- board[toRow][toCol]=2;
- 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
- board[toRow][toCol]=-2;
- return board;
- }
- public static boolean gameOver(int[][] board, int player) {
- boolean ans = false;
- if(hasValidMoves(board,player)==false)//if the certain player does'nt have any possible moves its game over
- ans=true; //because no moves = either stuck or no discs either way it's game over
- return ans;
- }
- public static int findTheLeader(int[][] board) {
- int ans = 0;
- int sum=0;
- int[][] discsRED=playerDiscs(board,1);//gets all the discs for the RED player
- for(int i=0;i<discsRED.length;i=i+1)
- sum=sum+board[discsRED[i][0]][discsRED[i][1]];//adds all the Red player into sum when Kings are worth 2 players
- int [][] discsBLUE=playerDiscs(board,-1);//gets all the discs for the BLUE player
- for(int i=0;i<discsBLUE.length;i=i+1)
- sum=sum+board[discsBLUE[i][0]][discsBLUE[i][1]];//reduces all the Blue players from sum when Kings reduce 2 players
- if(sum>0)//if sums turns out to be a positive number answer will be 1 which means Red is the leader
- ans=1;
- else if (sum<0)//if sums turns out to be a negative number answer will be -1 which means Blue is the leader
- ans=-1;
- //otherwise they are equal and in that case answer will be 0 which means a draw
- return ans;
- }
- public static int[][] randomPlayer(int[][] board, int player) {
- int [][] PM=getAllBasicMoves(board,player); //PM=possible moves
- int [][] PJ=getAllBasicJumps(board,player); //PJ=possible jumps
- if((PM.length!=0)&&(PJ.length==0))//if exists a possible move,and no jumps are available, choose a move randomly
- {
- int RndM=(int)(Math.random()*PM.length);//RndM=Random move
- board=playMove(board,player,PM[RndM][0],PM[RndM][1],PM[RndM][2],PM[RndM][3]);//makes the move
- }
- else while (PJ.length!=0) //if exists a possible jump, make one of them randomly
- {
- int RndJ=(int)(Math.random()*PJ.length);//RndJ= random Jump
- board=playMove(board,player,PJ[RndJ][0],PJ[RndJ][1],PJ[RndJ][2],PJ[RndJ][3]); //makes the move
- int row=PJ[RndJ][2],col=PJ[RndJ][3];
- PJ =getRestrictedBasicJumps(board,player,row,col); //updates PJ into the next possible jump
- }
- return board;
- }
- public static int[][] defensivePlayer(int[][] board, int player) {
- int [][] PJ=getAllBasicJumps(board,player); //PJ=possible jumps
- int [][] PM=getAllBasicMoves(board,player); //PM=possible moves
- 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
- if((!canJump(board,player))&&(PM.length!=0))//checks that no jumps are possible and moves are available
- {
- for(int i=0;i<PM.length;i=i+1)//compares between the movements which is the most defensive
- {
- if(player==1){
- if(PM[i][2]<minRed)
- {
- minRed=PM[i][2];
- mark=i;
- sum=1;
- }
- else if (PM[i][2]==minRed)
- sum=sum+1;
- }
- else {
- if(PM[i][2]>maxBlue)
- {
- maxBlue=PM[i][2];
- mark=i;
- sum=1;
- }
- else if (PM[i][2]==maxBlue)
- sum=sum+1;
- }
- }
- if(sum==1)//if only one defensive choice play the move
- board=playMove(board,player,PM[mark][0],PM[mark][1],PM[mark][2],PM[mark][3]);//makes the most defensive move
- else //else if there is a few defensive plays randomly pick one
- {
- choosen=(int)((Math.random()*sum) + 1);
- int i=-1,s=0;
- while(s!=choosen)
- {
- i=i+1;
- if(PM[i][2]==PM[mark][2])
- s=s+1;
- }
- board=playMove(board,player,PM[i][0],PM[i][1],PM[i][2],PM[i][3]);//makes the most defensive move
- }
- }
- while (PJ.length!=0) //if exists a possible jump, make one of them randomly
- {
- int RndJ=(int)(Math.random()*PJ.length);//RndJ= random Jump
- board=playMove(board,player,PJ[RndJ][0],PJ[RndJ][1],PJ[RndJ][2],PJ[RndJ][3]); //makes the move
- int row=PJ[RndJ][2],col=PJ[RndJ][3];
- PJ =getRestrictedBasicJumps(board,player,row,col); //updates PJ into the next possible jump
- }
- return board;
- }
- public static int[][] sidesPlayer(int[][] board, int player) {
- int [][] PJ=getAllBasicJumps(board,player); //PJ=possible jumps
- int [][] PM=getAllBasicMoves(board,player); //PM=possible moves
- int side=8,mark=0;//to check which move is the most side,Side(to compare) mark(to mark the move)
- if((PJ.length==0)&&(PM.length!=0))//checks that no jumps are possible and only moves are available
- {
- for(int i=0;i<PM.length;i=i+1)//compares between the movements and checks which takes the disk into the side
- {
- if ((PM[i][4])<side)//if it is the most right side move it will remember it
- {
- side=PM[i][4];
- mark=i;
- }
- if (PM[i][4]>(7-side))//or if it is the most left side move it will remember it
- {
- side=7-PM[i][4];
- mark=i;
- }
- if (((PM[i][4]==side)||(PM[i][4]==(7-side)))&&(Math.random()>0.5))//if equally sideway randomly decides if to pick it
- mark=i;
- }
- board=playMove(board,player,PM[mark][0],PM[mark][1],PM[mark][2],PM[mark][3]);//makes the most sideway move
- }
- while (PJ.length!=0) //if exists a possible jump, make one of them randomly
- {
- int RndJ=(int)(Math.random()*PJ.length);//RndJ= random Jump
- board=playMove(board,player,PJ[RndJ][0],PJ[RndJ][1],PJ[RndJ][2],PJ[RndJ][3]); //makes the move
- int row=PJ[RndJ][2],col=PJ[RndJ][3];
- PJ =getRestrictedBasicJumps(board,player,row,col); //updates PJ into the next possible jump
- }
- return board;
- }
- //******************************************************************************//
- /* ---------------------------------------------------------- *
- * Play an interactive game between the computer and you *
- * ---------------------------------------------------------- */
- public static void interactivePlay() {
- int[][] board = createBoard();
- showBoard(board);
- System.out.println("Welcome to the interactive Checkers Game !");
- int strategy = getStrategyChoice();
- System.out.println("You are the first player (RED discs)");
- boolean oppGameOver = false;
- while (!gameOver(board, RED) && !oppGameOver) {
- board = getPlayerFullMove(board, RED);
- oppGameOver = gameOver(board, BLUE);
- if (!oppGameOver) {
- EnglishCheckersGUI.sleep(200);
- board = getStrategyFullMove(board, BLUE, strategy);
- }
- }
- int winner = 0;
- if (playerDiscs(board, RED).length == 0 | playerDiscs(board, BLUE).length == 0){
- winner = findTheLeader(board);
- }
- if (winner == RED) {
- System.out.println();
- System.out.println("\t *************************");
- System.out.println("\t * You are the winner !! *");
- System.out.println("\t *************************");
- }
- else if (winner == BLUE) {
- System.out.println("\n======= You lost :( =======");
- }
- else
- System.out.println("\n======= DRAW =======");
- }
- /* --------------------------------------------------------- *
- * A game between two players *
- * --------------------------------------------------------- */
- public static void twoPlayers() {
- int[][] board = createBoard();
- showBoard(board);
- System.out.println("Welcome to the 2-player Checkers Game !");
- boolean oppGameOver = false;
- while (!gameOver(board, RED) & !oppGameOver) {
- System.out.println("\nRED's turn");
- board = getPlayerFullMove(board, RED);
- oppGameOver = gameOver(board, BLUE);
- if (!oppGameOver) {
- System.out.println("\nBLUE's turn");
- board = getPlayerFullMove(board, BLUE);
- }
- }
- int winner = 0;
- if (playerDiscs(board, RED).length == 0 | playerDiscs(board, BLUE).length == 0)
- winner = findTheLeader(board);
- System.out.println();
- System.out.println("\t ************************************");
- if (winner == RED)
- System.out.println("\t * The red player is the winner !! *");
- else if (winner == BLUE)
- System.out.println("\t * The blue player is the winner !! *");
- else
- System.out.println("\t * DRAW !! *");
- System.out.println("\t ************************************");
- }
- /* --------------------------------------------------------- *
- * Get a complete (possibly a sequence of jumps) move *
- * from a human player. *
- * --------------------------------------------------------- */
- public static int[][] getPlayerFullMove(int[][] board, int player) {
- // Get first move/jump
- int fromRow = -1, fromCol = -1, toRow = -1, toCol = -1;
- boolean jumpingMove = canJump(board, player);
- boolean badMove = true;
- getPlayerFullMoveScanner = new Scanner(System.in);//I've modified it
- while (badMove) {
- if (player == 1){
- System.out.println("Red, Please play:");
- } else {
- System.out.println("Blue, Please play:");
- }
- fromRow = getPlayerFullMoveScanner.nextInt();
- fromCol = getPlayerFullMoveScanner.nextInt();
- int[][] moves = jumpingMove ? getAllBasicJumps(board, player) : getAllBasicMoves(board, player);
- markPossibleMoves(board, moves, fromRow, fromCol, MARK);
- toRow = getPlayerFullMoveScanner.nextInt();
- toCol = getPlayerFullMoveScanner.nextInt();
- markPossibleMoves(board, moves, fromRow, fromCol, EMPTY);
- badMove = !isMoveValid(board, player, fromRow, fromCol, toRow, toCol);
- if (badMove)
- System.out.println("\nThis is an illegal move");
- }
- // Apply move/jump
- board = playMove(board, player, fromRow, fromCol, toRow, toCol);
- showBoard(board);
- // Get extra jumps
- if (jumpingMove) {
- boolean longMove = (getRestrictedBasicJumps(board, player, toRow, toCol).length > 0);
- while (longMove) {
- fromRow = toRow;
- fromCol = toCol;
- int[][] moves = getRestrictedBasicJumps(board, player, fromRow, fromCol);
- boolean badExtraMove = true;
- while (badExtraMove) {
- markPossibleMoves(board, moves, fromRow, fromCol, MARK);
- System.out.println("Continue jump:");
- toRow = getPlayerFullMoveScanner.nextInt();
- toCol = getPlayerFullMoveScanner.nextInt();
- markPossibleMoves(board, moves, fromRow, fromCol, EMPTY);
- badExtraMove = !isMoveValid(board, player, fromRow, fromCol, toRow, toCol);
- if (badExtraMove)
- System.out.println("\nThis is an illegal jump destination :(");
- }
- // Apply extra jump
- board = playMove(board, player, fromRow, fromCol, toRow, toCol);
- showBoard(board);
- longMove = (getRestrictedBasicJumps(board, player, toRow, toCol).length > 0);
- }
- }
- return board;
- }
- /* --------------------------------------------------------- *
- * Get a complete (possibly a sequence of jumps) move *
- * from a strategy. *
- * --------------------------------------------------------- */
- public static int[][] getStrategyFullMove(int[][] board, int player, int strategy) {
- if (strategy == RANDOM)
- board = randomPlayer(board, player);
- else if (strategy == DEFENSIVE)
- board = defensivePlayer(board, player);
- else if (strategy == SIDES)
- board = sidesPlayer(board, player);
- showBoard(board);
- return board;
- }
- /* --------------------------------------------------------- *
- * Get a strategy choice before the game. *
- * --------------------------------------------------------- */
- public static int getStrategyChoice() {
- int strategy = -1;
- getStrategyScanner = new Scanner(System.in);
- System.out.println("Choose the strategy of your opponent:" +
- "\n\t(" + RANDOM + ") - Random player" +
- "\n\t(" + DEFENSIVE + ") - Defensive player" +
- "\n\t(" + SIDES + ") - To-the-Sides player player");
- while (strategy != RANDOM & strategy != DEFENSIVE
- & strategy != SIDES) {
- strategy=getStrategyScanner.nextInt();
- }
- return strategy;
- }
- /* --------------------------------------- *
- * Print the possible moves *
- * --------------------------------------- */
- public static void printMoves(int[][] possibleMoves) {
- for (int i = 0; i < 4; i = i+1) {
- for (int j = 0; j < possibleMoves.length; j = j+1)
- System.out.print(" " + possibleMoves[j][i]);
- System.out.println();
- }
- }
- /* --------------------------------------- *
- * Mark/unmark the possible moves *
- * --------------------------------------- */
- public static void markPossibleMoves(int[][] board, int[][] moves, int fromRow, int fromColumn, int value) {
- for (int i = 0; i < moves.length; i = i+1)
- if (moves[i][0] == fromRow & moves[i][1] == fromColumn)
- board[moves[i][2]][moves[i][3]] = value;
- showBoard(board);
- }
- /* --------------------------------------------------------------------------- *
- * Shows the board in a graphic window *
- * you can use it without understanding how it works. *
- * --------------------------------------------------------------------------- */
- public static void showBoard(int[][] board) {
- grid.showBoard(board);
- }
- /* --------------------------------------------------------------------------- *
- * Print the board *
- * you can use it without understanding how it works. *
- * --------------------------------------------------------------------------- */
- public static void printMatrix(int[][] matrix){
- for (int i = matrix.length-1; i >= 0; i = i-1){
- for (int j = 0; j < matrix.length; j = j+1){
- System.out.format("%4d", matrix[i][j]);
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement