/**
* Kings
* A puzzle using a chess board
* Provides practice programming 2-dimenstional arrays
*
* @author Dave Mulkey
* @version April 2005
*/
public class Kings
{
public static void main(String[] args)
{
new Kings();
}
int max = 5;
char[][] board ;
public Kings()
{
max = inputInt("Size of board");
board = new char[max][max];
clearBoard();
play();
}
public void play()
{
int row = 0;
int col = 0;
do
{
showBoard();
row = inputInt("row (-1 to clear, 99 to quit)");
if (row == 99)
{ System.exit(0); }
else if(row == 88)
{ clearBoard();
}
else // default
{
col = inputInt("column");
char there = board[row][col];
if (there == 'O')
{
board[row][col] = 'K';
}
else
{
System.out.println("Already occupied");
}
}
} while (true);
}
public void clearBoard()
{
for (int row = 0; row < max; row++)
{
for (int col=0; col < max; col++)
{
board[row][col] = 'O';
}
}
}
public void showBoard()
{
for (int row=0; row < max; row = row+1)
{
for (int col=0; col < max; col = col + 1)
{
System.out.print( board[row][col] + " ");
}
System.out.println();
}
System.out.println("==========");
}
public static int inputInt(String prompt)
{ int result=0;
try{ System.out.println(prompt);
String info = new java.io.BufferedReader(
new java.io.InputStreamReader(System.in)).readLine();
result=Integer.valueOf(info).intValue();
}
catch (Exception e){result = 0;}
return result;
}
}