Results 1 to 9 of 9

Thread: Checkers

  1. #1
    evan42781 is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

    Default Checkers

    Hey guys so i am trying to write a checkers program that a user can play against the computer. However due to some certain requirements by the teacher it has to be done in visual studio which i believe does not allow drag and drop for moves therefore moves can only be made by user input due that this is done in a executable. So i would like some suggestions on where to start in writing this program to play checkers and just allow user typed input for moving the pieces and print statements to show moves by the user and comp. thanks.

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Visual Studio is an IDE for Microsoft languages (C#, VB.NET, etc.). Both of these languages (and most others) have drag-and-drop functionality.

    However, this has nothing to do with Java, so there's nothing we can do for you here.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    evan42781 is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

    Default

    it also includes J# which is java. Ive already written 2 other programs for this class in java using visual studio.

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    While the J# language has the same syntax as Java, it does not use any of the Java core libraries or API. You'll probably be using .NET libraries to construct your application, and this forum is not the place to ask about them.

    Again, there is noting to stop you using a GUI with drag-and-drop features.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  5. #5
    evan42781 is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

    Default

    ok so i have most of this checkers games solved now...but its not user interactive. its just computer vs computer. what I would like to do with this is make it to where a user can select a piece (by input of row and column) and then input another (row and column) to move the piece to that location. yall have any suggestions?

    checkers
    Java Code:
    import java.util.*;
    import java.awt.*;
    
    class Checkers
    {
    	KeyboardInputClass k1 = new KeyboardInputClass();
    	int jump;
    	int n;
    	Date date = null;
    	Random random = null;
    
    	public Checkers()
    	{
    		n = 8;
    		date = new Date();
    		random = new Random(date.getTime());
    	}
    
    	boolean blackWins(int[][] board)
    	{
    		for (int i = 0; i < n; i++)
    			for (int j = 0; j < n; j++)
    				if (board[i][j] == 3 || board[i][j] == 4)
    					return false;
    		return true;
    	}
    
    	boolean redWins(int[][] board)
    	{
    		for (int i = 0; i < n; i++)
    			for (int j = 0; j < n; j++)
    				if (board[i][j] == 1 || board[i][j] == 2)
    					return false;
    		return true;
    	}
    
    	boolean blackKingJump(int r, int c, int[][] board)
    	{
    		boolean found = true, flag = false;
    		int col = c, row = r;
    
    		for (; found && row >= 0; )
    		{
    			if ((row - 2 >= 0 && col - 2 >= 0) &&
    				(board[row - 1][col - 1] == 3 ||
    				 board[row - 1][col - 1] == 4) &&
    				 board[row - 2][col - 2] == 0)
    			{
    				board[row][col] = 0;
    				board[row - 1][col - 1] = 0;
    				board[row - 2][col - 2] = 2;
    				flag = true;
    				row = row - 2;
    				col = col - 2;
    			}
    			else if ((row - 2 >= 0 && col + 2 < 8) &&
    				(board[row - 1][col + 1] == 3 ||
    				 board[row - 1][col + 1] == 4) &&
    				 board[row - 2][col + 2] == 0)
    			{
    				board[row][col] = 0;
    				board[row - 1][col + 1] = 0;
    				board[row - 2][col + 2] = 2;
    				flag = true;
    				row = row - 2;
    				col = col + 2;
    			}
    			else if ((row + 2 < 8 && col - 2 >= 0) &&
    				(board[row + 1][col - 1] == 3 ||
    				 board[row + 1][col - 1] == 4) &&
    				 board[row + 2][col - 2] == 0)
    			{
    				board[row][col] = 0;
    				board[row + 1][col - 1] = 0;
    				board[row + 2][col - 2] = 2;
    				flag = true;
    				row = row + 2;
    				col = col - 2;
    			}
    			else if ((row + 2 < 8 && col + 2 < 8) &&
    				(board[row + 1][col + 1] == 3 ||
    				 board[row + 1][col + 1] == 4) &&
    				 board[row + 2][col + 2] == 0)
    			{
    				board[row][col] = 0;
    				board[row + 1][col + 1] = 0;
    				board[row + 2][col + 2] = 2;
    				flag = true;
    				row = row + 2;
    				col = col + 2;
    			}
    			else
    				found = false;
    		}
    		return flag;
    	}
    
    	boolean blackCheckerJump(int r, int c, int[][] board)
    	{
    		boolean found = true, flag = false;
    		int col = c, row = r;
    
    		for (; found && row >= 0; )
    		{
    			if ((row - 2 >= 0 && col - 2 >= 0) &&
    				(board[row - 1][col - 1] == 3 ||
    				 board[row - 1][col - 1] == 4) &&
    				 board[row - 2][col - 2] == 0)
    			{
    				board[row][col] = 0;
    				board[row - 1][col - 1] = 0;
    				board[row - 2][col - 2] = 1;
    				flag = true;
    				row = row - 2;
    				col = col - 2;
    			}
    			if ((row - 2 >= 0 && col + 2 < 8) &&
    				(board[row - 1][col + 1] == 3 ||
    				 board[row - 1][col + 1] == 4) &&
    				 board[row - 2][col + 2] == 0)
    			{
    				board[row][col] = 0;
    				board[row - 1][col + 1] = 0;
    				board[row - 2][col + 2] = 1;
    				flag = true;
    				row = row - 2;
    				col = col + 2;
    			}
    			else
    				found = false;
    		}
    		return flag;
    	}
    
    	boolean redKingJump(int row, int col, int[][] board)
    	{
    		boolean found = true, flag = false;
    
    		for (; found && row < 8; )
    		{
    			if ((row - 2 >= 0 && col - 2 >= 0) &&
    				(board[row - 1][col - 1] == 1 ||
    				 board[row - 1][col - 1] == 2) &&
    				 board[row - 2][col - 2] == 0)
    			{
    				board[row][col] = 0;
    				board[row - 1][col - 1] = 0;
    				board[row - 2][col - 2] = 4;
    				flag = true;
    				row = row - 2;
    				col = col - 2;
    			}
    			else if ((row - 2 >= 0 && col + 2 < 8) &&
    				(board[row - 1][col + 1] == 1 ||
    				 board[row - 1][col + 1] == 2) &&
    				 board[row - 2][col + 2] == 0)
    			{
    				board[row][col] = 0;
    				board[row - 1][col + 1] = 0;
    				board[row - 2][col + 2] = 4;
    				flag = true;
    				row = row - 2;
    				col = col + 2;
    			}
    			else if ((row + 2 < 8 && col - 2 >= 0) &&
    				(board[row + 1][col - 1] == 1 ||
    				 board[row + 1][col - 1] == 2) &&
    				 board[row + 2][col - 2] == 0)
    			{
    				board[row][col] = 0;
    				board[row + 1][col - 1] = 0;
    				board[row + 2][col - 2] = 4;
    				flag = true;
    				row = row + 2;
    				col = col - 2;
    			}
    			else if ((row + 2 < 8 && col + 2 < 8) &&
    				(board[row + 1][col + 1] == 1 ||
    				 board[row + 1][col + 1] == 2) &&
    				 board[row + 2][col + 2] == 0)
    			{
    				board[row][col] = 0;
    				board[row + 1][col + 1] = 0;
    				board[row + 2][col + 2] = 4;
    				flag = true;
    				row = row + 2;
    				col = col + 2;
    			}
    			else
    				found = false;
    		}
    		return flag;
    	}
    
    	boolean RCheckerJump(int row, int col, int[][] board)
    	{
    		boolean found = true, flag = false;
    
    		for (; found && row < 8; )
    		{
    			if ((row + 2 < 8 && col - 2 >= 0) &&
    				(board[row + 1][col - 1] == 1 ||
    				 board[row + 1][col - 1] == 2) &&
    				 board[row + 2][col - 2] == 0)
    			{
    				board[row][col] = 0;
    				board[row + 1][col - 1] = 0;
    				board[row + 2][col - 2] = 3;
    				flag = true;
    				row = row + 2;
    				col = col - 2;
    			}
    			else if ((row + 2 < 8) && (col + 2 < 8) &&
    				(board[row + 1][col + 1] == 1 ||
    				 board[row + 1][col + 1] == 2) &&
    				 board[row + 2][col + 2] == 0)
    			{
    				board[row][col] = 0;
    				board[row + 1][col + 1] = 0;
    				board[row + 2][col + 2] = 3;
    				flag = true;
    				row = row + 2;
    				col = col + 2;
    			}
    			else
    				found = false;
    		}
    		return flag;
    	}
    
    	boolean mandatoryJump(Color color, int[][] board)
    	{
    		//returns true if jump is made, 0 otherwise
    		int i, j;
    
    		jump = 0;
    		//check for black to move
    		if (color == Color.black)
    		{
    			//look for a mandatory jump among black checkers
    			for (i = 7; i >= 0; i--)
    				for (j = i & 1; j < 8; j += 2)
    					if (board[i][j] == 1)
    						if (blackCheckerJump(i, j, board))
    							return true;
    			// check for mandatory jump among black kings
    			for (i = 0; i < 8; i++)
    				for (j = i & 1; j < 8; j += 2)
    					if (board[i][j] == 2)
    						if (blackKingJump(i, j, board))
    							return true;
    		}
    		else
    		{
    			// look for a mandatory jump among red checkers
    			for (i = 0; i < 8; i++)
    				for (j = i & 1; j < 8; j += 2)
    					if (board[i][j] == 3)
    						if (RCheckerJump(i, j, board))
    							return true;
    			// check for mandatory jump among red kings
    			for (i = 0; i < 8; i++)
    				for (j = i & 1; j < 8; j += 2)
    					if (board[i][j] == 4)
    						if (redKingJump(i, j, board))
    							return true;
    		}
    		return false;
    	}
    
    	double target(double[] w, int[][] board)
    	{
    		double t = w[0];
    		int i, j;
    		int[] x = new int[8];
    		int[][] temp = new int[8][8];
    
    		x[0] = 1;
    		for (i = 1; i < 7; i++)
    			x[i] = 0;
    		//count the number of pieces on the board
    		for (i = 0; i < 8; i++)
    		{
    			for (j = i & 1; j < 8; j += 2)
    			{
    				if (board[i][j] == 1)
    					x[1]++;
    				else if (board[i][j] == 2)
    					x[3]++;
    				else if (board[i][j] == 3)
    					x[2]++;
    				else if (board[i][j] == 4)
    					x[4]++;
    			}
    		}
    		//black threatened by red
    		for (i = 7; i >= 0; i--)
    		{
    			for (j = i & 1; j < 8; j += 2)
    			{
    				copyBoard(temp, board);
    				if (board[i][j] == 1)
    					if (blackCheckerJump(i, j, temp))
    						x[5]++;
    					else if (board[i][j] == 2)
    						if (blackKingJump(i, j, temp))
    							x[5]++;
    			}
    		}
    		//red threatened by black
    		for (i = 0; i < 8; i++)
    		{
    			for (j = i & 1; j < 8; j += 2)
    			{
    				copyBoard(temp, board);
    				if (board[i][j] == 3)
    					if (RCheckerJump(i, j, temp))
    						x[6]++;
    					else if (board[i][j] == 4)
    						if (redKingJump(i, j, temp))
    							x[6]++;
    			}
    		}
    		for (i = 0; i < 7; i++)
    			t += w[i] * x[i];
    		return t;
    	}
    
    	int move(Color color, double[] w, int[][] board)
    	{
    		// returns 1 if black wins, 2 if red wins, 0 otherwise
    		double dTemp, t0;
    		double[] t = new double[48];
    		int count = 0, i, j;
    		int[][] bTemp = new int[8][8];
    		int[][][] bInp = new int[48][8][8];
    		int[][][] bOut = new int[48][8][8];
    
    		if (mandatoryJump(color, board))
    		{
    			// test for new kings
    			for (i = 0; i < 8; i++)
    			{
    				if (board[0][i] == 1)
    					board[0][i] = 2;
    				if (board[7][i] == 3)
    					board[7][i] = 4;
    			}
    			if (blackWins(board))
    				return 1;
    			if (redWins(board))
    				return 2;
    			return 0;
    		}
    		for (i = 0; i < 48; i++)
    		{
    			copyBoard(bInp[i], board);
    			copyBoard(bOut[i], board);
    		}
    		if (color == Color.black)
    		{
    			// find all black checker moves
    			for (i = 7; i >= 1; i--)
    			{
    				for (j = i & 1; j < 8; j += 2)
    				{
    					if (bInp[count][i][j] == 1)
    					{
    						if (j - 1 >= 0 && board[i - 1][j - 1] == 0)
    						{
    							bOut[count][i][j] = 0;
    							bOut[count][i - 1][j - 1] = 1;
    							count++;
    						}
    						if (j + 1 < 8 && board[i - 1][j + 1] == 0)
    						{
    							bOut[count][i][j] = 0;
    							bOut[count][i - 1][j + 1] = 1;
    							count++;
    						}
    					}
    				}
    			}
    			// find all black king moves
    			for (i = 0; i < 8; i++)
    			{
    				for (j = i & 1; j < 8; j += 2)
    				{
    					if (bInp[count][i][j] == 2)
    					{
    						if (i - 1 >= 0 && j - 1 >= 0 &&
    							bInp[count][i - 1][j - 1] == 0)
    						{
    							bOut[count][i][j] = 0;
    							bOut[count][i - 1][j - 1] = 2;
    							count++;
    						}
    						if (i - 1 >= 0 && j + 1 < 8 &&
    							bInp[count][i - 1][j + 1] == 0)
    						{
    							bOut[count][i][j] = 0;
    							bOut[count][i - 1][j + 1] = 2;
    							count++;
    						}
    						if (i + 1 < 8 && j - 1 >= 0 &&
    							bInp[count][i + 1][j - 1] == 0)
    						{
    							bOut[count][i][j] = 0;
    							bOut[count][i + 1][j - 1] = 2;
    							count++;
    						}
    						if (i + 1 < 8 && j + 1 < 8 &&
    							bInp[count][i + 1][j + 1] == 0)
    						{
    							bOut[count][i][j] = 0;
    							bOut[count][i + 1][j + 1] = 2;
    							count++;
    						}
    					}
    				}
    			}
    			if (count == 0)
    				return 2;
    		}
    		else
    		{
    			// find all red checker moves
    			for (i = 0; i < 7; i++)
    			{
    				for (j = i & 1; j < 8; j += 2)
    				{
    					if (bInp[count][i][j] == 3)
    					{
    						if (j - 1 >= 0 && bInp[count][i + 1][j - 1] == 0)
    						{
    							bOut[count][i][j] = 0;
    							bOut[count][i + 1][j - 1] = 3;
    							count++;
    						}
    						if (j + 1 < 8 && bInp[count][i + 1][j + 1] == 0)
    						{
    							bOut[count][i][j] = 0;
    							bOut[count][i + 1][j + 1] = 3;
    							count++;
    						}
    					}
    				}
    			}
    			// find all red king moves
    			for (i = 0; i < 8; i++)
    			{
    				for (j = i & 1; j < 8; j += 2)
    				{
    					if (bInp[count][i][j] == 4)
    					{
    						if (i - 1 >= 0 && j - 1 >= 0 &&
    							bInp[count][i - 1][j - 1] == 0)
    						{
    							bOut[count][i][j] = 0;
    							bOut[count][i - 1][j - 1] = 4;
    							count++;
    						}
    						if (i - 1 >= 0 && j + 1 < 8 &&
    							bInp[count][i - 1][j + 1] == 0)
    						{
    							bOut[count][i][j] = 0;
    							bOut[count][i - 1][j + 1] = 4;
    							count++;
    						}
    						if (i + 1 < 8 && j - 1 >= 0 &&
    							bInp[count][i + 1][j - 1] == 0)
    						{
    							bOut[count][i][j] = 0;
    							bOut[count][i + 1][j - 1] = 4;
    							count++;
    						}
    						if (i + 1 < 8 && j + 1 < 8 &&
    							bInp[count][i + 1][j + 1] == 0)
    						{
    							bOut[count][i][j] = 0;
    							bOut[count][i + 1][j + 1] = 4;
    							count++;
    						}
    					}
    				}
    			}
    			if (count == 0)
    				return 1;
    		}
    		for (i = 0; i < count; i++)
    			t[i] = target(w, bOut[i]);
    		// sort by target function value
    		for (i = 0; i < count - 1; i++)
    		{
    			for (j = i + 1; j < count; j++)
    			{
    				if (t[i] > t[j])
    				{
    					dTemp = t[i];
    					t[i] = t[j];
    					t[j] = dTemp;
    					bTemp = bOut[i];
    					bOut[i] = bOut[j];
    					bOut[j] = bTemp;
    				}
    			}
    		}
    		j = 0;
    		t0 = t[0];
    		for (i = 0; i < count; i++)
    			if (t0 == t[i])
    				j++;
    			else
    				break;
    		copyBoard(board, bOut[random.nextInt(j)]);
    		// test for new kings
    		for (i = 0; i < 8; i++)
    		{
    			if (board[0][i] == 1)
    				board[0][i] = 2;
    			if (board[7][i] == 3)
    				board[7][i] = 4;
    		}
    		if (blackWins(board))
    			return 1;
    		if (redWins(board))
    			return 2;
    		return 0;
    	}
    
    	void copyBoard(int[][] destin, int[][] source)
    	{
    		for (int i = 0; i < 8; i++)
    			for (int j = 0; j < 8; j++)
    				destin[i][j] = source[i][j];
    	}
    
    	void initBoard(int[][] board)
    	{
    		int i;
    
    		zeroBoard(board);
    		for (i = 0; i < 8; i += 2)
    		{
    			board[0][i] = 3;
    			board[2][i] = 3;
    			board[6][i] = 1;
    		}
    		for (i = 1; i < 8; i += 2)
    		{
    			board[1][i] = 3;
    			board[5][i] = 1;
    			board[7][i] = 1;
    		}
    	}
    
    	void zeroBoard(int[][] board)
    	{
    		for (int i = 0; i < 8; i++)
    			for (int j = 0; j < 8; j++)
    				board[i][j] = 0;
    	}
    
    	public static void PrintBoard(int[][] Board)
    	{
    		System.out.println("        col col col col col col col col");
    		System.out.println("         0   1   2   3   4   5   6   7");
    		System.out.println();
    		for (int row = 0; row < 8; row++)
    		{
    			System.out.print("row " + (row) + "  | ");
    			for (int col = 0; col < 8; col++)
    			{
    				if (Board[row][col] == 0)
    				{
    					System.out.print(' ');
    				}
    				if (Board[row][col] == 1)
    				{
    					System.out.print('b');
    				}
    				if (Board[row][col] == 2)
    				{
    					System.out.print('B');
    				}
    				if (Board[row][col] == 3)
    				{
    					System.out.print('r');
    				}
    				if (Board[row][col] == 4)
    				{
    					System.out.print('R');
    				}
    				else
    				{
    					System.out.print(" | ");
    				}
    			}
    			System.out.println();
    			System.out.println("       ---------------------------------");
    		}
    	}
    
    	public void run()
    	{
    		double[] w = new double[7];
    		int m;
    		int[][] board = new int[8][8];
    
    		w[0] = 2970821.387271;
    		w[1] = 3004848.086621;
    		w[2] = 3013328.744006;
    		w[3] = 3020611.008753;
    		w[4] = 3056966.210776;
    		w[5] = 3026257.028194;
    		w[6] = 3032073.551093;
    
    		initBoard(board);
    		PrintBoard(board);
    		System.out.println();
    		k1.getKeyboardInput("press enter to start game");
    
    		for (; ; )
    		{
    			
    			m = move(Color.black, w, board);
    			PrintBoard(board);
    			System.out.println();
    			k1.getKeyboardInput("press enter");
    		
    			if (m != 0)
    				break;
    	
    			m = move(Color.red, w, board);
    			PrintBoard(board);
    			System.out.println();
    			k1.getKeyboardInput("press enter");
    			
    			if (m != 0)
    				break;
    		}
    	}
    
    	public static void main(String[] args)
    	{
    		(new Checkers()).run();
    	}
    }
    keyboard input class (provided by teacher...works well)
    Java Code:
    //**************************************************************************************************************************
    //**************************************************************************************************************************
    //Class:		KeyboardInputClass
    //Description:	Provides multiple methods for entering information from the keyboard for console based programs.
    //Author:		Steve Donaldson
    //Date:			9/26/08
    import java.io.*;
    class KeyboardInputClass
    {
    	//**********************************************************************************************************************
    	//Method:		getKeyboardInput
    	//Description:	Permits keyboard input for strings
    	//Parameters:	prompt - descriptive text telling the user what to enter
    	//Returns:		inputString	- the entered text (i.e., the user's response). Note that even though this is a string,
    	//								it can be converted to an integer, double, etc. if necessary in the client routine.
    	//Throws:		Exception (but doesn't do anything with it!)
    	//Calls:		nothing
    	public String getKeyboardInput(String prompt)
    	{
    		String inputString = "";
    		System.out.println(prompt);
    		try
    		{
    			InputStreamReader reader = new InputStreamReader(System.in);
    			BufferedReader buffer = new BufferedReader(reader);
    			inputString = buffer.readLine();
    		}
    		catch (Exception e) { }
    		return inputString;
    	}
    	//**********************************************************************************************************************
    	//Method:		getCharacter
    	//Description:	Gets a character (char) from the keyboard. If validateInput=true, the routine loops until the user entry
    	//				matches defaultResult (which may be obtained just by pressing the ENTER key without entering anything)
    	//				or is one of the validEntries.
    	//Parameters:	validateInput		- true=make sure the entered character is in validEntries; false=accept any
    	//										character that is entered
    	//				defaultResult		- character to be returned if the user enters no character (i.e., just presses
    	//										ENTER). If validateInput=true, the method assumes that this is a valid entry
    	//										even if it is not explicitly included in validEntries (i.e., the method will
    	//										add it to validEntries).
    	//				validEntries		- acceptable characters if validateInput = true. Note: if validation is to be
    	//										performed, then unless one of the case conversion modes is specified, the user
    	//										entry must match one of the validEntries characters exactly in order to be
    	//										accepted.
    	//				caseConversionMode	- 0=no conversion occurs; 1=the entered character is converted to uppercase before
    	//										being checked against validEntries and before being returned; 2= the entered
    	//										character is converted to lowercase before being checked against validEntries
    	//										and before being returned. Note: both case conversion modes 1 and 2 also convert
    	//										validEntries to the specified case prior to checking the validity of the entry.
    	//										If validateInput=false, this parameter is ignored.
    	//				prompt				- descriptive text prompting the user for an entry
    	//Returns:		result				- the character entered by the user or defaultResult if no character was entered
    	//Calls:		getKeyboardInput
    	public char getCharacter(boolean validateInput, char defaultResult, String validEntries, int caseConversionMode, String prompt)
    	{
    		if (validateInput)
    		{
    			if (caseConversionMode == 1)
    			{
    				validEntries = validEntries.toUpperCase();
    				defaultResult = Character.toUpperCase(defaultResult);
    			}
    			else if (caseConversionMode == 2)
    			{
    				validEntries = validEntries.toLowerCase();
    				defaultResult = Character.toLowerCase(defaultResult);
    			}
    			if ((validEntries.indexOf(defaultResult) < 0))								//if default not in validEntries
    				validEntries = (new Character(defaultResult)).toString() + validEntries;//then add it
    		}
    		String inputString = "";
    		char result = defaultResult;
    		boolean entryAccepted = false;
    		while (!entryAccepted)
    		{
    			result = defaultResult;
    			entryAccepted = true;
    			inputString = getKeyboardInput(prompt);
    			if (inputString.length() > 0)
    			{
    				result = (inputString.charAt(0));
    				if (caseConversionMode == 1)
    					result = Character.toUpperCase(result);
    				else if (caseConversionMode == 2)
    					result = Character.toLowerCase(result);
    			}
    			if (validateInput)
    				if (validEntries.indexOf(result) < 0)
    				{
    					entryAccepted = false;
    					System.out.println("Invalid entry. Select an entry from the characters shown in brackets: [" + validEntries + "]");
    				}
    		}
    		return result;
    	}
    	//**********************************************************************************************************************
    	//Method:		getInteger
    	//Description:	Gets an integer (int) from the keyboard. If validateInput=true, the routine loops until the user entry
    	//				matches defaultResult (which may be obtained just by pressing the ENTER key without entering anything)
    	//				or falls within the range specified by minAllowableResult and maxAllowableResult.
    	//Parameters:	validateInput		- true=make sure the entered integer equals the default or is in in the allowable
    	//										range specified by minAllowableResult and maxAllowableResult; false=accept any
    	//										integer that is entered
    	//				defaultResult		- integer to be returned if the user enters nothing (i.e., just presses (ENTER).
    	//										If validateInput=true, the method assumes that this is a valid entry
    	//										even if it is not explicitly included in the specified range.
    	//				minAllowableResult	- the minimum allowable value for the user entry (if validateEntries=true)
    	//				maxAllowableResult	- the maximum allowable value for the user entry (if validateEntries=true)
    	//										Note: if validateInput=false,these values are ignored
    	//				prompt				- descriptive text prompting the user for an entry
    	//Returns:		result				- the integer entered by the user or defaultResult if no integer was entered
    	//Calls:		getKeyboardInput
    	public int getInteger(boolean validateInput, int defaultResult, int minAllowableResult, int maxAllowableResult, String prompt)
    	{
    		String inputString = "";
    		int result = defaultResult;
    		boolean entryAccepted = false;
    		while (!entryAccepted)
    		{
    			result = defaultResult;
    			entryAccepted = true;
    			inputString = getKeyboardInput(prompt);
    			if (inputString.length() > 0)
    			{
    				try
    				{
    					result = Integer.parseInt(inputString);
    				}
    				catch (Exception e)
    				{
    					entryAccepted = false;
    					System.out.println("Invalid entry...");
    				}
    			}
    			if (entryAccepted && validateInput)
    			{
    				if ((result != defaultResult) && ((result < minAllowableResult) || (result > maxAllowableResult)))
    				{
    					entryAccepted = false;
    					System.out.println("Invalid entry. Allowable range is " + minAllowableResult + "..." + maxAllowableResult + " (default = " + defaultResult + ").");
    				}
    			}
    		}
    		return result;
    	}
    	//**********************************************************************************************************************
    	//Method:		getDouble
    	//Description:	Gets a double (int) from the keyboard. If validateInput=true, the routine loops until the user entry
    	//				matches defaultResult (which may be obtained just by pressing the ENTER key without entering anything)
    	//				or falls within the range specified by minAllowableResult and maxAllowableResult.
    	//Parameters:	validateInput		- true=make sure the entered double equals the default or is in in the allowable
    	//										range specified by minAllowableResult and maxAllowableResult; false=accept any
    	//										double that is entered
    	//				defaultResult		- double to be returned if the user enters nothing (i.e., just presses (ENTER).
    	//										If validateInput=true, the method assumes that this is a valid entry
    	//										even if it is not explicitly included in the specified range.
    	//				minAllowableResult	- the minimum allowable value for the user entry (if validateEntries=true)
    	//				maxAllowableResult	- the maximum allowable value for the user entry (if validateEntries=true)
    	//										Note: if validateInput=false,these values are ignored
    	//				prompt				- descriptive text prompting the user for an entry
    	//Returns:		result				- the double entered by the user or defaultResult if no double was entered
    	//Calls:		getKeyboardInput
    	public double getDouble(boolean validateInput, double defaultResult, double minAllowableResult, double maxAllowableResult, String prompt)
    	{
    		String inputString = "";
    		double result = defaultResult;
    		boolean entryAccepted = false;
    		while (!entryAccepted)
    		{
    			result = defaultResult;
    			entryAccepted = true;
    			inputString = getKeyboardInput(prompt);
    			if (inputString.length() > 0)
    			{
    				try
    				{
    					result = Double.parseDouble(inputString);
    				}
    				catch (Exception e)
    				{
    					entryAccepted = false;
    					System.out.println("Invalid entry...");
    				}
    			}
    			if (entryAccepted && validateInput)
    			{
    				if ((result != defaultResult) && ((result < minAllowableResult) || (result > maxAllowableResult)))
    				{
    					entryAccepted = false;
    					System.out.println("Invalid entry. Allowable range is " + minAllowableResult + "..." + maxAllowableResult + " (default = " + defaultResult + ").");
    				}
    			}
    		}
    		return result;
    	}
    	//**********************************************************************************************************************
    	//Method:		
    	//Description:	
    	//Parameters:	
    	//Returns:		
    	//Calls:		
    	//**********************************************************************************************************************
    }
    //**************************************************************************************************************************
    //**************************************************************************************************************************
    
    //Here is how to use it... (remove the comments!)
    //KeyboardInputClass keyboardInput = new KeyboardInputClass();
    //String userInput="";
    //userInput=keyboardInput.getKeyboardInput("Specify the string to be processed");

  6. #6
    evan42781 is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

    Default

    ok so i have most of this checkers games solved now...but its not user interactive. its just computer vs computer. what I would like to do with this is make it to where a user can select a piece (by input of row and column) and then input another (row and column) to move the piece to that location. yall have any suggestions?

    Java Code:
    import java.util.*;
    import java.awt.*;
    
    class Checkers
    {
    	KeyboardInputClass k1 = new KeyboardInputClass();
    	int Jump;
    	int N;
    	Date Date = null;
    	Random Random = null;
    
    	public Checkers()
    	{
    		N = 8;
    		Date = new Date();
    		Random = new Random(Date.getTime());
    	}
    
    	boolean BWins(int[][] board)
    	{
    		for (int i = 0; i < N; i++)
    			for (int j = 0; j < N; j++)
    				if (board[i][j] == 3 || board[i][j] == 4)
    					return false;
    		return true;
    	}
    
    	boolean RWins(int[][] board)
    	{
    		for (int i = 0; i < N; i++)
    			for (int j = 0; j < N; j++)
    				if (board[i][j] == 1 || board[i][j] == 2)
    					return false;
    		return true;
    	}
    
    	boolean BKJump(int row, int col, int[][] board)
    	{
    		boolean found = true, flag = false;
    
    		for (; found && row >= 0; )
    		{
    			if ((row - 2 >= 0 && col - 2 >= 0) &&
    				(board[row - 1][col - 1] == 3 ||
    				 board[row - 1][col - 1] == 4) &&
    				 board[row - 2][col - 2] == 0)
    			{
    				board[row][col] = 0;
    				board[row - 1][col - 1] = 0;
    				board[row - 2][col - 2] = 2;
    				flag = true;
    				row = row - 2;
    				col = col - 2;
    			}
    			else if ((row - 2 >= 0 && col + 2 < 8) &&
    				(board[row - 1][col + 1] == 3 ||
    				 board[row - 1][col + 1] == 4) &&
    				 board[row - 2][col + 2] == 0)
    			{
    				board[row][col] = 0;
    				board[row - 1][col + 1] = 0;
    				board[row - 2][col + 2] = 2;
    				flag = true;
    				row = row - 2;
    				col = col + 2;
    			}
    			else if ((row + 2 < 8 && col - 2 >= 0) &&
    				(board[row + 1][col - 1] == 3 ||
    				 board[row + 1][col - 1] == 4) &&
    				 board[row + 2][col - 2] == 0)
    			{
    				board[row][col] = 0;
    				board[row + 1][col - 1] = 0;
    				board[row + 2][col - 2] = 2;
    				flag = true;
    				row = row + 2;
    				col = col - 2;
    			}
    			else if ((row + 2 < 8 && col + 2 < 8) &&
    				(board[row + 1][col + 1] == 3 ||
    				 board[row + 1][col + 1] == 4) &&
    				 board[row + 2][col + 2] == 0)
    			{
    				board[row][col] = 0;
    				board[row + 1][col + 1] = 0;
    				board[row + 2][col + 2] = 2;
    				flag = true;
    				row = row + 2;
    				col = col + 2;
    			}
    			else
    				found = false;
    		}
    		return flag;
    	}
    
    	boolean BCJump(int row, int col, int[][] board)
    	{
    		boolean found = true, flag = false;
    
    		for (; found && row >= 0; )
    		{
    			if ((row - 2 >= 0 && col - 2 >= 0) &&
    				(board[row - 1][col - 1] == 3 ||
    				 board[row - 1][col - 1] == 4) &&
    				 board[row - 2][col - 2] == 0)
    			{
    				board[row][col] = 0;
    				board[row - 1][col - 1] = 0;
    				board[row - 2][col - 2] = 1;
    				flag = true;
    				row = row - 2;
    				col = col - 2;
    			}
    			if ((row - 2 >= 0 && col + 2 < 8) &&
    				(board[row - 1][col + 1] == 3 ||
    				 board[row - 1][col + 1] == 4) &&
    				 board[row - 2][col + 2] == 0)
    			{
    				board[row][col] = 0;
    				board[row - 1][col + 1] = 0;
    				board[row - 2][col + 2] = 1;
    				flag = true;
    				row = row - 2;
    				col = col + 2;
    			}
    			else
    				found = false;
    		}
    		return flag;
    	}
    
    	boolean RKJump(int row, int col, int[][] board)
    	{
    		boolean found = true, flag = false;
    
    		for (; found && row < 8; )
    		{
    			if ((row - 2 >= 0 && col - 2 >= 0) &&
    				(board[row - 1][col - 1] == 1 ||
    				 board[row - 1][col - 1] == 2) &&
    				 board[row - 2][col - 2] == 0)
    			{
    				board[row][col] = 0;
    				board[row - 1][col - 1] = 0;
    				board[row - 2][col - 2] = 4;
    				flag = true;
    				row = row - 2;
    				col = col - 2;
    			}
    			else if ((row - 2 >= 0 && col + 2 < 8) &&
    				(board[row - 1][col + 1] == 1 ||
    				 board[row - 1][col + 1] == 2) &&
    				 board[row - 2][col + 2] == 0)
    			{
    				board[row][col] = 0;
    				board[row - 1][col + 1] = 0;
    				board[row - 2][col + 2] = 4;
    				flag = true;
    				row = row - 2;
    				col = col + 2;
    			}
    			else if ((row + 2 < 8 && col - 2 >= 0) &&
    				(board[row + 1][col - 1] == 1 ||
    				 board[row + 1][col - 1] == 2) &&
    				 board[row + 2][col - 2] == 0)
    			{
    				board[row][col] = 0;
    				board[row + 1][col - 1] = 0;
    				board[row + 2][col - 2] = 4;
    				flag = true;
    				row = row + 2;
    				col = col - 2;
    			}
    			else if ((row + 2 < 8 && col + 2 < 8) &&
    				(board[row + 1][col + 1] == 1 ||
    				 board[row + 1][col + 1] == 2) &&
    				 board[row + 2][col + 2] == 0)
    			{
    				board[row][col] = 0;
    				board[row + 1][col + 1] = 0;
    				board[row + 2][col + 2] = 4;
    				flag = true;
    				row = row + 2;
    				col = col + 2;
    			}
    			else
    				found = false;
    		}
    		return flag;
    	}
    
    	boolean RCJump(int row, int col, int[][] board)
    	{
    		boolean found = true, flag = false;
    
    		for (; found && row < 8; )
    		{
    			if ((row + 2 < 8 && col - 2 >= 0) &&
    				(board[row + 1][col - 1] == 1 ||
    				 board[row + 1][col - 1] == 2) &&
    				 board[row + 2][col - 2] == 0)
    			{
    				board[row][col] = 0;
    				board[row + 1][col - 1] = 0;
    				board[row + 2][col - 2] = 3;
    				flag = true;
    				row = row + 2;
    				col = col - 2;
    			}
    			else if ((row + 2 < 8) && (col + 2 < 8) &&
    				(board[row + 1][col + 1] == 1 ||
    				 board[row + 1][col + 1] == 2) &&
    				 board[row + 2][col + 2] == 0)
    			{
    				board[row][col] = 0;
    				board[row + 1][col + 1] = 0;
    				board[row + 2][col + 2] = 3;
    				flag = true;
    				row = row + 2;
    				col = col + 2;
    			}
    			else
    				found = false;
    		}
    		return flag;
    	}
    
    	boolean HaveToJump(Color color, int[][] board)
    	{
    		//returns true if jump is made, 0 otherwise
    		int i; 
    		int j;
    		Jump = 0;
    		//check for black to move
    		if (color == Color.black)
    		{
    			//look for a mandatory jump among black checkers
    			for (i = 7; i >= 0; i--)
    				for (j = i & 1; j < 8; j += 2)
    					if (board[i][j] == 1)
    						if (BCJump(i, j, board))
    							return true;
    			// check for mandatory jump among black kings
    			for (i = 0; i < 8; i++)
    				for (j = i & 1; j < 8; j += 2)
    					if (board[i][j] == 2)
    						if (BKJump(i, j, board))
    							return true;
    		}
    		else
    		{
    			// look for a mandatory jump among red checkers
    			for (i = 0; i < 8; i++)
    				for (j = i & 1; j < 8; j += 2)
    					if (board[i][j] == 3)
    						if (RCJump(i, j, board))
    							return true;
    			// check for mandatory jump among red kings
    			for (i = 0; i < 8; i++)
    				for (j = i & 1; j < 8; j += 2)
    					if (board[i][j] == 4)
    						if (RKJump(i, j, board))
    							return true;
    		}
    		return false;
    	}
    
    	double target(double[] w, int[][] board)
    	{
    		double t = w[0];
    		int i; 
    		int j;
    		int[] x = new int[8];
    		int[][] temp = new int[8][8];
    
    		x[0] = 1;
    		for (i = 1; i < 7; i++)
    			x[i] = 0;
    		//count the number of pieces on the board
    		for (i = 0; i < 8; i++)
    		{
    			for (j = i & 1; j < 8; j += 2)
    			{
    				if (board[i][j] == 1)
    					x[1]++;
    				else if (board[i][j] == 2)
    					x[3]++;
    				else if (board[i][j] == 3)
    					x[2]++;
    				else if (board[i][j] == 4)
    					x[4]++;
    			}
    		}
    		//black threatened by red
    		for (i = 7; i >= 0; i--)
    		{
    			for (j = i & 1; j < 8; j += 2)
    			{
    				CopyBoard(temp, board);
    				if (board[i][j] == 1)
    					if (BCJump(i, j, temp))
    						x[5]++;
    					else if (board[i][j] == 2)
    						if (BKJump(i, j, temp))
    							x[5]++;
    			}
    		}
    		//red threatened by black
    		for (i = 0; i < 8; i++)
    		{
    			for (j = i & 1; j < 8; j += 2)
    			{
    				CopyBoard(temp, board);
    				if (board[i][j] == 3)
    					if (RCJump(i, j, temp))
    						x[6]++;
    					else if (board[i][j] == 4)
    						if (RKJump(i, j, temp))
    							x[6]++;
    			}
    		}
    		for (i = 0; i < 7; i++)
    			t += w[i] * x[i];
    		return t;
    	}
    
    	int move(Color color, double[] w, int[][] board)
    	{
    		// returns 1 if black wins, 2 if red wins, 0 otherwise
    		double dTemp, t0;
    		double[] t = new double[48];
    		int count = 0, i, j;
    		int[][] bTemp = new int[8][8];
    		int[][][] bInp = new int[48][8][8];
    		int[][][] bOut = new int[48][8][8];
    
    		if (HaveToJump(color, board))
    		{
    			// test for new kings
    			for (i = 0; i < 8; i++)
    			{
    				if (board[0][i] == 1)
    					board[0][i] = 2;
    				if (board[7][i] == 3)
    					board[7][i] = 4;
    			}
    			if (BWins(board))
    				return 1;
    			if (RWins(board))
    				return 2;
    			return 0;
    		}
    		for (i = 0; i < 48; i++)
    		{
    			CopyBoard(bInp[i], board);
    			CopyBoard(bOut[i], board);
    		}
    		if (color == Color.black)
    		{
    			// find all black checker moves
    			for (i = 7; i >= 1; i--)
    			{
    				for (j = i & 1; j < 8; j += 2)
    				{
    					if (bInp[count][i][j] == 1)
    					{
    						if (j - 1 >= 0 && board[i - 1][j - 1] == 0)
    						{
    							bOut[count][i][j] = 0;
    							bOut[count][i - 1][j - 1] = 1;
    							count++;
    						}
    						if (j + 1 < 8 && board[i - 1][j + 1] == 0)
    						{
    							bOut[count][i][j] = 0;
    							bOut[count][i - 1][j + 1] = 1;
    							count++;
    						}
    					}
    				}
    			}
    			// find all black king moves
    			for (i = 0; i < 8; i++)
    			{
    				for (j = i & 1; j < 8; j += 2)
    				{
    					if (bInp[count][i][j] == 2)
    					{
    						if (i - 1 >= 0 && j - 1 >= 0 &&
    							bInp[count][i - 1][j - 1] == 0)
    						{
    							bOut[count][i][j] = 0;
    							bOut[count][i - 1][j - 1] = 2;
    							count++;
    						}
    						if (i - 1 >= 0 && j + 1 < 8 &&
    							bInp[count][i - 1][j + 1] == 0)
    						{
    							bOut[count][i][j] = 0;
    							bOut[count][i - 1][j + 1] = 2;
    							count++;
    						}
    						if (i + 1 < 8 && j - 1 >= 0 &&
    							bInp[count][i + 1][j - 1] == 0)
    						{
    							bOut[count][i][j] = 0;
    							bOut[count][i + 1][j - 1] = 2;
    							count++;
    						}
    						if (i + 1 < 8 && j + 1 < 8 &&
    							bInp[count][i + 1][j + 1] == 0)
    						{
    							bOut[count][i][j] = 0;
    							bOut[count][i + 1][j + 1] = 2;
    							count++;
    						}
    					}
    				}
    			}
    			if (count == 0)
    				return 2;
    		}
    		else
    		{
    			// find all red checker moves
    			for (i = 0; i < 7; i++)
    			{
    				for (j = i & 1; j < 8; j += 2)
    				{
    					if (bInp[count][i][j] == 3)
    					{
    						if (j - 1 >= 0 && bInp[count][i + 1][j - 1] == 0)
    						{
    							bOut[count][i][j] = 0;
    							bOut[count][i + 1][j - 1] = 3;
    							count++;
    						}
    						if (j + 1 < 8 && bInp[count][i + 1][j + 1] == 0)
    						{
    							bOut[count][i][j] = 0;
    							bOut[count][i + 1][j + 1] = 3;
    							count++;
    						}
    					}
    				}
    			}
    			// find all red king moves
    			for (i = 0; i < 8; i++)
    			{
    				for (j = i & 1; j < 8; j += 2)
    				{
    					if (bInp[count][i][j] == 4)
    					{
    						if (i - 1 >= 0 && j - 1 >= 0 &&
    							bInp[count][i - 1][j - 1] == 0)
    						{
    							bOut[count][i][j] = 0;
    							bOut[count][i - 1][j - 1] = 4;
    							count++;
    						}
    						if (i - 1 >= 0 && j + 1 < 8 &&
    							bInp[count][i - 1][j + 1] == 0)
    						{
    							bOut[count][i][j] = 0;
    							bOut[count][i - 1][j + 1] = 4;
    							count++;
    						}
    						if (i + 1 < 8 && j - 1 >= 0 &&
    							bInp[count][i + 1][j - 1] == 0)
    						{
    							bOut[count][i][j] = 0;
    							bOut[count][i + 1][j - 1] = 4;
    							count++;
    						}
    						if (i + 1 < 8 && j + 1 < 8 &&
    							bInp[count][i + 1][j + 1] == 0)
    						{
    							bOut[count][i][j] = 0;
    							bOut[count][i + 1][j + 1] = 4;
    							count++;
    						}
    					}
    				}
    			}
    			if (count == 0)
    				return 1;
    		}
    		for (i = 0; i < count; i++)
    			t[i] = target(w, bOut[i]);
    		// sort by target function value
    		for (i = 0; i < count - 1; i++)
    		{
    			for (j = i + 1; j < count; j++)
    			{
    				if (t[i] > t[j])
    				{
    					dTemp = t[i];
    					t[i] = t[j];
    					t[j] = dTemp;
    					bTemp = bOut[i];
    					bOut[i] = bOut[j];
    					bOut[j] = bTemp;
    				}
    			}
    		}
    		j = 0;
    		t0 = t[0];
    		for (i = 0; i < count; i++)
    			if (t0 == t[i])
    				j++;
    			else
    				break;
    		CopyBoard(board, bOut[Random.nextInt(j)]);
    		// test for new kings
    		for (i = 0; i < 8; i++)
    		{
    			if (board[0][i] == 1)
    				board[0][i] = 2;
    			if (board[7][i] == 3)
    				board[7][i] = 4;
    		}
    		if (BWins(board))
    			return 1;
    		if (RWins(board))
    			return 2;
    		return 0;
    	}
    
    	void CopyBoard(int[][] destin, int[][] source)
    	{
    		for (int i = 0; i < 8; i++)
    			for (int j = 0; j < 8; j++)
    				destin[i][j] = source[i][j];
    	}
    
    	void initBoard(int[][] board)
    	{
    		int i;
    
    		zeroBoard(board);
    		for (i = 0; i < 8; i += 2)
    		{
    			board[0][i] = 3;
    			board[2][i] = 3;
    			board[6][i] = 1;
    		}
    		for (i = 1; i < 8; i += 2)
    		{
    			board[1][i] = 3;
    			board[5][i] = 1;
    			board[7][i] = 1;
    		}
    	}
    
    	void zeroBoard(int[][] board)
    	{
    		for (int i = 0; i < 8; i++)
    			for (int j = 0; j < 8; j++)
    				board[i][j] = 0;
    	}
    
    	public static void PrintBoard(int[][] Board)
    	{
    		System.out.println("        col col col col col col col col");
    		System.out.println("         0   1   2   3   4   5   6   7");
    		System.out.println();
    		for (int row = 0; row < 8; row++)
    		{
    			System.out.print("row " + (row) + "  | ");
    			for (int col = 0; col < 8; col++)
    			{
    				if (Board[row][col] == 0)
    				{
    					System.out.print(' ');
    				}
    				if (Board[row][col] == 1)
    				{
    					System.out.print('b');
    				}
    				if (Board[row][col] == 2)
    				{
    					System.out.print('B');
    				}
    				if (Board[row][col] == 3)
    				{
    					System.out.print('r');
    				}
    				if (Board[row][col] == 4)
    				{
    					System.out.print('R');
    				}
    				else
    				{
    					System.out.print(" | ");
    				}
    			}
    			System.out.println();
    			System.out.println("       ---------------------------------");
    		}
    	}
    
    	public void run()
    	{
    		double[] w = new double[7];
    		int m;
    		int[][] board = new int[8][8];
    
    		w[0] = 2970821.387271;
    		w[1] = 3004848.086621;
    		w[2] = 3013328.744006;
    		w[3] = 3020611.008753;
    		w[4] = 3056966.210776;
    		w[5] = 3026257.028194;
    		w[6] = 3032073.551093;
    
    		initBoard(board);
    		PrintBoard(board);
    		System.out.println();
    		k1.getKeyboardInput("press enter to start game");
    
    		for (; ; )
    		{
    			
    			m = move(Color.black, w, board);
    			PrintBoard(board);
    			System.out.println();
    			k1.getKeyboardInput("press enter for RED's TURN");
    		
    			if (m != 0)
    				break;
    	
    			m = move(Color.red, w, board);
    			PrintBoard(board);
    			System.out.println();
    			k1.getKeyboardInput("press enter for BLACK's TURN");
    			
    			if (m != 0)
    				break;
    		}
    	}
    
    	public static void main(String[] args)
    	{
    		(new Checkers()).run();
    	}
    }

  7. #7
    evan42781 is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

    Default

    keyboard input class
    Java Code:
    //**************************************************************************************************************************
    //**************************************************************************************************************************
    //Class:		KeyboardInputClass
    //Description:	Provides multiple methods for entering information from the keyboard for console based programs.
    //Author:		Steve Donaldson
    //Date:			9/26/08
    import java.io.*;
    class KeyboardInputClass
    {
    	//**********************************************************************************************************************
    	//Method:		getKeyboardInput
    	//Description:	Permits keyboard input for strings
    	//Parameters:	prompt - descriptive text telling the user what to enter
    	//Returns:		inputString	- the entered text (i.e., the user's response). Note that even though this is a string,
    	//								it can be converted to an integer, double, etc. if necessary in the client routine.
    	//Throws:		Exception (but doesn't do anything with it!)
    	//Calls:		nothing
    	public String getKeyboardInput(String prompt)
    	{
    		String inputString = "";
    		System.out.println(prompt);
    		try
    		{
    			InputStreamReader reader = new InputStreamReader(System.in);
    			BufferedReader buffer = new BufferedReader(reader);
    			inputString = buffer.readLine();
    		}
    		catch (Exception e) { }
    		return inputString;
    	}
    	//**********************************************************************************************************************
    	//Method:		getCharacter
    	//Description:	Gets a character (char) from the keyboard. If validateInput=true, the routine loops until the user entry
    	//				matches defaultResult (which may be obtained just by pressing the ENTER key without entering anything)
    	//				or is one of the validEntries.
    	//Parameters:	validateInput		- true=make sure the entered character is in validEntries; false=accept any
    	//										character that is entered
    	//				defaultResult		- character to be returned if the user enters no character (i.e., just presses
    	//										ENTER). If validateInput=true, the method assumes that this is a valid entry
    	//										even if it is not explicitly included in validEntries (i.e., the method will
    	//										add it to validEntries).
    	//				validEntries		- acceptable characters if validateInput = true. Note: if validation is to be
    	//										performed, then unless one of the case conversion modes is specified, the user
    	//										entry must match one of the validEntries characters exactly in order to be
    	//										accepted.
    	//				caseConversionMode	- 0=no conversion occurs; 1=the entered character is converted to uppercase before
    	//										being checked against validEntries and before being returned; 2= the entered
    	//										character is converted to lowercase before being checked against validEntries
    	//										and before being returned. Note: both case conversion modes 1 and 2 also convert
    	//										validEntries to the specified case prior to checking the validity of the entry.
    	//										If validateInput=false, this parameter is ignored.
    	//				prompt				- descriptive text prompting the user for an entry
    	//Returns:		result				- the character entered by the user or defaultResult if no character was entered
    	//Calls:		getKeyboardInput
    	public char getCharacter(boolean validateInput, char defaultResult, String validEntries, int caseConversionMode, String prompt)
    	{
    		if (validateInput)
    		{
    			if (caseConversionMode == 1)
    			{
    				validEntries = validEntries.toUpperCase();
    				defaultResult = Character.toUpperCase(defaultResult);
    			}
    			else if (caseConversionMode == 2)
    			{
    				validEntries = validEntries.toLowerCase();
    				defaultResult = Character.toLowerCase(defaultResult);
    			}
    			if ((validEntries.indexOf(defaultResult) < 0))								//if default not in validEntries
    				validEntries = (new Character(defaultResult)).toString() + validEntries;//then add it
    		}
    		String inputString = "";
    		char result = defaultResult;
    		boolean entryAccepted = false;
    		while (!entryAccepted)
    		{
    			result = defaultResult;
    			entryAccepted = true;
    			inputString = getKeyboardInput(prompt);
    			if (inputString.length() > 0)
    			{
    				result = (inputString.charAt(0));
    				if (caseConversionMode == 1)
    					result = Character.toUpperCase(result);
    				else if (caseConversionMode == 2)
    					result = Character.toLowerCase(result);
    			}
    			if (validateInput)
    				if (validEntries.indexOf(result) < 0)
    				{
    					entryAccepted = false;
    					System.out.println("Invalid entry. Select an entry from the characters shown in brackets: [" + validEntries + "]");
    				}
    		}
    		return result;
    	}
    	//**********************************************************************************************************************
    	//Method:		getInteger
    	//Description:	Gets an integer (int) from the keyboard. If validateInput=true, the routine loops until the user entry
    	//				matches defaultResult (which may be obtained just by pressing the ENTER key without entering anything)
    	//				or falls within the range specified by minAllowableResult and maxAllowableResult.
    	//Parameters:	validateInput		- true=make sure the entered integer equals the default or is in in the allowable
    	//										range specified by minAllowableResult and maxAllowableResult; false=accept any
    	//										integer that is entered
    	//				defaultResult		- integer to be returned if the user enters nothing (i.e., just presses (ENTER).
    	//										If validateInput=true, the method assumes that this is a valid entry
    	//										even if it is not explicitly included in the specified range.
    	//				minAllowableResult	- the minimum allowable value for the user entry (if validateEntries=true)
    	//				maxAllowableResult	- the maximum allowable value for the user entry (if validateEntries=true)
    	//										Note: if validateInput=false,these values are ignored
    	//				prompt				- descriptive text prompting the user for an entry
    	//Returns:		result				- the integer entered by the user or defaultResult if no integer was entered
    	//Calls:		getKeyboardInput
    	public int getInteger(boolean validateInput, int defaultResult, int minAllowableResult, int maxAllowableResult, String prompt)
    	{
    		String inputString = "";
    		int result = defaultResult;
    		boolean entryAccepted = false;
    		while (!entryAccepted)
    		{
    			result = defaultResult;
    			entryAccepted = true;
    			inputString = getKeyboardInput(prompt);
    			if (inputString.length() > 0)
    			{
    				try
    				{
    					result = Integer.parseInt(inputString);
    				}
    				catch (Exception e)
    				{
    					entryAccepted = false;
    					System.out.println("Invalid entry...");
    				}
    			}
    			if (entryAccepted && validateInput)
    			{
    				if ((result != defaultResult) && ((result < minAllowableResult) || (result > maxAllowableResult)))
    				{
    					entryAccepted = false;
    					System.out.println("Invalid entry. Allowable range is " + minAllowableResult + "..." + maxAllowableResult + " (default = " + defaultResult + ").");
    				}
    			}
    		}
    		return result;
    	}
    	//**********************************************************************************************************************
    	//Method:		getDouble
    	//Description:	Gets a double (int) from the keyboard. If validateInput=true, the routine loops until the user entry
    	//				matches defaultResult (which may be obtained just by pressing the ENTER key without entering anything)
    	//				or falls within the range specified by minAllowableResult and maxAllowableResult.
    	//Parameters:	validateInput		- true=make sure the entered double equals the default or is in in the allowable
    	//										range specified by minAllowableResult and maxAllowableResult; false=accept any
    	//										double that is entered
    	//				defaultResult		- double to be returned if the user enters nothing (i.e., just presses (ENTER).
    	//										If validateInput=true, the method assumes that this is a valid entry
    	//										even if it is not explicitly included in the specified range.
    	//				minAllowableResult	- the minimum allowable value for the user entry (if validateEntries=true)
    	//				maxAllowableResult	- the maximum allowable value for the user entry (if validateEntries=true)
    	//										Note: if validateInput=false,these values are ignored
    	//				prompt				- descriptive text prompting the user for an entry
    	//Returns:		result				- the double entered by the user or defaultResult if no double was entered
    	//Calls:		getKeyboardInput
    	public double getDouble(boolean validateInput, double defaultResult, double minAllowableResult, double maxAllowableResult, String prompt)
    	{
    		String inputString = "";
    		double result = defaultResult;
    		boolean entryAccepted = false;
    		while (!entryAccepted)
    		{
    			result = defaultResult;
    			entryAccepted = true;
    			inputString = getKeyboardInput(prompt);
    			if (inputString.length() > 0)
    			{
    				try
    				{
    					result = Double.parseDouble(inputString);
    				}
    				catch (Exception e)
    				{
    					entryAccepted = false;
    					System.out.println("Invalid entry...");
    				}
    			}
    			if (entryAccepted && validateInput)
    			{
    				if ((result != defaultResult) && ((result < minAllowableResult) || (result > maxAllowableResult)))
    				{
    					entryAccepted = false;
    					System.out.println("Invalid entry. Allowable range is " + minAllowableResult + "..." + maxAllowableResult + " (default = " + defaultResult + ").");
    				}
    			}
    		}
    		return result;
    	}
    	//**********************************************************************************************************************
    	//Method:		
    	//Description:	
    	//Parameters:	
    	//Returns:		
    	//Calls:		
    	//**********************************************************************************************************************
    }
    //**************************************************************************************************************************
    //**************************************************************************************************************************
    
    //Here is how to use it... (remove the comments!)
    //KeyboardInputClass keyboardInput = new KeyboardInputClass();
    //String userInput="";
    //userInput=keyboardInput.getKeyboardInput("Specify the string to be processed");

  8. #8
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Does that not work then?
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  9. #9
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    If it doesn't work... use the chess row/column labels for your board, and let the user decide what to move and where from them.
    Java Code:
          WHITE
       A B C D E F G H
    1 |_|_|_|_|_|_|_|_| 1
    2 |_|_|_|_|_|_|_|_| 2
    3 |_|_|_|_|_|_|_|_| 3
    4 |_|_|_|_|_|_|_|_| 4
    5 |_|_|_|_|_|_|_|_| 5
    6 |_|_|_|_|_|_|_|_| 6
    7 |_|_|_|_|_|_|_|_| 7
    8 |_|_|_|_|_|_|_|_| 8
       A B C D E F G H
            BLACK
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •