/**
* 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();
}
char[][] board = new char[5][5];
public Kings()
{
clearBoard();
showBoard();
play();
}
public void play()
{
int row = 0;
int col = 0;
do
{
row = inputInt("row");
col = inputInt("column");
board[row][col] = 'K';
showBoard();
} while (true);
}
public void clearBoard()
{
board[0][0] = 'O'; board[0][1] = 'O'; board[0][2] = 'O'; board[0][3] = 'O'; board[0][4] = 'O';
board[1][0] = 'O'; board[1][1] = 'O'; board[1][2] = 'O'; board[1][3] = 'O'; board[1][4] = 'O';
board[2][0] = 'O'; board[2][1] = 'O'; board[2][2] = 'O'; board[2][3] = 'O'; board[2][4] = 'O';
board[3][0] = 'O'; board[3][1] = 'O'; board[3][2] = 'O'; board[3][3] = 'O'; board[3][4] = 'O';
board[4][0] = 'O'; board[4][1] = 'O'; board[4][2] = 'O'; board[4][3] = 'O'; board[4][4] = 'O';
}
public void showBoard()
{
for (int row=0; row < 5; row = row+1)
{
for (int col=0; col < 5; 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;
}
}
// Kings is a puzzle based on chess. In chess, it is never permitted
// to have two kings standing next to each other. There must be at
// least one empty space between them.
//
// The puzzle involves placing many kings on the board, but legally
// with space between them. There are several puzzles possible:
//
// Maximum : what is the maximum number of kings that can
// be placed on the board?
//
// Minimum : what is the minimum number of kings which can "fill up"
// the board, so that no more can be added legally?
//
// Two player game:
// Two players take turns placing kings on the board legally,
// until the board is "full". The last player to place a king wins.
//
// The program above is a VERY SIMPLE version that keeps track of where
// the kings have been placed. It doesn't know any rules, so it
// will allow kings to be placed illegally. It also allows a king
// to be placed where another is already standing. It also attempts
// to place kings in positions using negative number or large numbers -
// this causes the program to crash.
//
// The purpose of this assignment is to get some practice programming
// 2-dimensional arrays. Do each of the following:
//
// (1) Change play() so that before a move is placed on the board,
// it checks that row and col are legal numbers, between 0 and 4.
//
// (2) Change play() so that before placing a king on the board,
// it checks that the square is empty - that it contains '-' .
//
// (3) Rewrite clearBoard() using nested loops instead of 25 separate
// assignment commands.
//
// (4) Change the program so that the user can decide how big the board is.
// This means the number 5 should be replaced by a variable, e.g. MAX.
//
// (5) Fill in the following table:
//
// Size of Board Maximum Kings Minimum Kings
// 3 4 1
// 4 4 2
// 5 ? ?
// 6 ? ?
// 7 ? ?
// 8 ? ?
//
// (6) Change the play() method so it prevents illegal moves.
// It needs some if.. commands similar to these:
// if (board[row][col] = 'K') { bad = true; }
// if (board[row+1][col] = 'K') { bad = true; }
// if (board[row][col-1] = 'K') { bad = true; }
// ...
// Be careful that you don't check [row+1] when row == MAX,
// and don't check [row-1] when row == 0.
//
// (7) Play the two-player game with a partner, and search for a winning
// strategy for the 5x5 board. Is it like tic-tac-toe - you want
// to play in the middle first? Or is it more complicated?