/***************************************************************************** This is a simplified game of checkers. In particular, a winner is only determined if one team eliminates ALL of the checkers of the opposing team. This means that if a team has checkers, but they are all stuck, this program will go into an infinite loop. (This is something we are going to fix as an exercise, in class.) Also, the game does not currently support kings. ******************************************************************************/ import java.io.*; import java.util.*; public class Checkers { private final static int SIZE = 8; private char[][] board; // Stores the checkerboard, with chars 'r','b','_' private int redcheckers; // Number of red checkers on the board private int blackcheckers; // Number of black checkers on the board private char whosemove; // Either 'r' or 'b', for who's move it currently is. // Constructs default Checkers object, initializing board to starting // playing position. public Checkers() { board = new char[SIZE][SIZE]; redcheckers = 12; blackcheckers = 12; whosemove = 'r'; // Initialize board with all the red and black checkers in starting // positions. int i,j; for (i=0;i 7 || yfrom < 0 || yfrom > 7 || xto < 0 || xto > 7 || yto < 0 || yto > 7) return false; // Check to see you are moving your piece to a blank square. else if (board[xfrom][yfrom]==whosemove && board[xto][yto]=='_') { // Checks case of simple move if (Math.abs(xfrom-xto)==1) { if ((whosemove == 'r') && (yto - yfrom == 1)) return true; else if ((whosemove == 'b') && (yto - yfrom == -1)) return true; } // Checks case of a jump else if (Math.abs(xfrom-xto)==2) { if (whosemove == 'r' && (yto - yfrom == 2) && board[(xfrom+xto)/2][(yfrom+yto)/2] == 'b') return true; else if (whosemove == 'b' && (yto - yfrom == -2) && board[(xfrom+xto)/2][(yfrom+yto)/2] == 'r') return true; } } // If move is neither a simple one or a jump, it is not legal. return false; } // Executes a move. public void executeMove(int movefrom, int moveto) { // Gets array indeces corresponding to the move, from parameters. int xfrom = movefrom/10 - 1; int yfrom = movefrom%10 - 1; int xto = moveto/10 - 1; int yto = moveto%10 - 1; // Change appropriate board elements and decrement redcheckers or // blackcheckers if necessary. board[xfrom][yfrom] = '_'; board[xto][yto] = whosemove; if (Math.abs(xto - xfrom) == 2) { board[(xfrom+xto)/2][(yfrom+yto)/2] = '_'; if (whosemove == 'r') redcheckers--; else blackcheckers--; } } // Checks to see if game is over based on number of checkers left. public boolean gameOver() { return (redcheckers == 0 || blackcheckers == 0); } // Returns color of the winner. public String winnerIs() { if (blackcheckers == 0) return "red"; else return "black"; } public static void main(String args[]) throws IOException { // Setup and print out checker board. Checkers game = new Checkers(); game.printBoard(); // Loop until game is over. while (!game.gameOver()) { //Execute a move and print the board out afterwards. game.getNextMove(); game.printBoard(); } // Announce winner. System.out.println("The winner is " + game.winnerIs()); } }