import java.io.*;
import java.util.*;

public class TestsGen {
	static int testcount = 0;

	static Random random = new Random(7356352);

	static int maxn = 100000;

	static String figs = "KQRNBPkqrnbp";

	static void write(char[][] tab, int[][] moves, int k) throws IOException {
		PrintWriter out = new PrintWriter(String.format("%02d", ++testcount));
		System.out.println("Generating test #" + testcount);
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				out.print(tab[i][j]);
			}
			out.println();
		}
		out.println(k);
		for (int i = 0; i < k; i++) {
			out.print((char) ('a' + moves[i][1]));
			out.print((char) ('0' + 8 - moves[i][0]));
			out.print((char) ('a' + moves[i][3]));
			out.print((char) ('0' + 8 - moves[i][2]));
			out.println();
		}
		out.close();
	}

	static char[][] randBoard(double free) {
		char[][] ret = new char[8][8];
		for (int i = 0; i < ret.length; i++) {
			for (int j = 0; j < ret[i].length; j++) {
				if (random.nextDouble() < free) {
					ret[i][j] = '.';
				} else {
					ret[i][j] = figs.charAt(random.nextInt(figs.length()));
				}
			}
		}
		return ret;
	}

	static int nx, ny;

	static int[] kdx = new int[] { -1, 0, 1, -1, 1, -1, 0, 1 };
	static int[] kdy = new int[] { -1, -1, -1, 0, 0, 1, 1, 1 };

	static int[] rdx = new int[] { 0, -1, 1, 0 };
	static int[] rdy = new int[] { -1, 0, 0, 1 };

	static int[] bdx = new int[] { -1, -1, 1, 1 };
	static int[] bdy = new int[] { -1, 1, 1, -1 };

	static int[] ndx = new int[] { -1, -2, -1, -2, 1, 2, 1, 2 };
	static int[] ndy = new int[] { -2, -1, 2, 1, -2, -1, 2, 1 };

	static int[] Pdy = new int[] { -1, -1, -1 };
	static int[] Pdx = new int[] { 0, -1, 1 };

	static int[] pdy = new int[] { 1, 1, 1 };
	static int[] pdx = new int[] { 0, -1, 1 };

	static int letterCase(char ch) {
		if ('a' <= ch && ch <= 'z')
			return 0;
		if ('A' <= ch && ch <= 'Z')
			return 1;
		return -1;
	}

	static boolean getMove(char[][] tab, int x, int y, boolean noeat) {
		ArrayList<Integer> mx = new ArrayList<Integer>();
		ArrayList<Integer> my = new ArrayList<Integer>();
		switch (tab[x][y]) {
		case 'K':
		case 'k':
			for (int dir = 0; dir < kdx.length; dir++) {
				int xx = x + kdx[dir];
				int yy = y + kdy[dir];
				if (xx < 0 || xx >= 8 || yy < 0 || yy >= 8)
					continue;
				if (letterCase(tab[x][y]) == letterCase(tab[xx][yy]))
					continue;
				mx.add(xx);
				my.add(yy);
			}
			break;
		case 'Q':
		case 'q':
			for (int dir = 0; dir < kdx.length; dir++) {
				int xx = x + kdx[dir];
				int yy = y + kdy[dir];
				while (true) {
					if (xx < 0 || xx >= 8 || yy < 0 || yy >= 8)
						break;
					if (letterCase(tab[x][y]) == letterCase(tab[xx][yy]))
						break;
					mx.add(xx);
					my.add(yy);
					xx += kdx[dir];
					yy += kdy[dir];
				}
			}
			break;
		case 'R':
		case 'r':
			for (int dir = 0; dir < rdx.length; dir++) {
				int xx = x + rdx[dir];
				int yy = y + rdy[dir];
				while (true) {
					if (xx < 0 || xx >= 8 || yy < 0 || yy >= 8)
						break;
					if (letterCase(tab[x][y]) == letterCase(tab[xx][yy]))
						break;
					mx.add(xx);
					my.add(yy);
					xx += rdx[dir];
					yy += rdy[dir];
				}
			}
			break;
		case 'B':
		case 'b':
			for (int dir = 0; dir < bdx.length; dir++) {
				int xx = x + bdx[dir];
				int yy = y + bdy[dir];
				while (true) {
					if (xx < 0 || xx >= 8 || yy < 0 || yy >= 8)
						break;
					if (letterCase(tab[x][y]) == letterCase(tab[xx][yy]))
						break;
					mx.add(xx);
					my.add(yy);
					xx += bdx[dir];
					yy += bdy[dir];
				}
			}
			break;
		case 'N':
		case 'n':
			for (int dir = 0; dir < ndx.length; dir++) {
				int xx = x + ndx[dir];
				int yy = y + ndy[dir];
				if (xx < 0 || xx >= 8 || yy < 0 || yy >= 8)
					continue;
				if (letterCase(tab[x][y]) == letterCase(tab[xx][yy]))
					continue;
				mx.add(xx);
				my.add(yy);
			}
			break;
		case 'P':
			for (int dir = 0; dir < Pdx.length; dir++) {
				int xx = x + Pdx[dir];
				int yy = y + Pdx[dir];
				if (xx < 0 || xx >= 8 || yy < 0 || yy >= 8)
					continue;
				if ((dir == 0 && letterCase(tab[xx][yy]) == -1)
						|| (dir != 0 && letterCase(tab[xx][yy]) == 1 - letterCase(tab[x][y]))) {
					mx.add(xx);
					my.add(yy);
				}
			}
			break;
		case 'p':
			for (int dir = 0; dir < pdx.length; dir++) {
				int xx = x + pdx[dir];
				int yy = y + pdx[dir];
				if (xx < 0 || xx >= 8 || yy < 0 || yy >= 8)
					continue;
				if ((dir == 0 && letterCase(tab[xx][yy]) == -1)
						|| (dir != 0 && letterCase(tab[xx][yy]) == 1 - letterCase(tab[x][y]))) {
					mx.add(xx);
					my.add(yy);
				}
			}
			break;
		}
		if (noeat) {
			ArrayList<Integer> mmx = new ArrayList<Integer>();
			ArrayList<Integer> mmy = new ArrayList<Integer>();
			for (int i = 0; i < mx.size(); i++) {
				if (tab[mx.get(i)][my.get(i)] == '.') {
					mmx.add(mx.get(i));
					mmy.add(my.get(i));
				}
			}
			if (mmx.size() == 0) {
				return false;
			}
			int k = random.nextInt(mmx.size());
			nx = mmx.get(k);
			ny = mmy.get(k);
			return true;
		}
		if (mx.size() == 0) {
			return false;
		}
		int k = random.nextInt(mx.size());
		nx = mx.get(k);
		ny = my.get(k);
		return true;
	}

	static void writeRand(int n, boolean noeat) throws IOException {
		char[][] tab = randBoard(random.nextDouble());
		char[][] ot = new char[8][8];
		for (int i = 0; i < ot.length; i++) {
			ot[i] = tab[i].clone();
		}
		int[][] moves = new int[n][4];
		int c = random.nextInt(2);
		int k = 0;
		all: for (int i = 0; i < n; i++) {
			int iter = 0;
			while (true) {
				int x = random.nextInt(8);
				int y = random.nextInt(8);
				while (letterCase(tab[x][y]) != c) {
					x = random.nextInt(8);
					y = random.nextInt(8);
					iter++;
					if (iter > 10000) {
						break all;
					}
				}
				if (iter > 10000) {
					break all;
				}
				if (getMove(tab, x, y, noeat)) {
					moves[i][0] = x;
					moves[i][1] = y;
					moves[i][2] = nx;
					moves[i][3] = ny;
					if (tab[nx][ny] != '.' && noeat) {
						System.out.println(nx + " " + ny);
						iter += 0;
					}
					tab[nx][ny] = tab[x][y];
					tab[x][y] = '.';
					iter = 0;
					break;
				}
			}
			c = 1 - c;
			k++;
		}
		write(ot, moves, k);
	}

	public static void main(String[] args) throws IOException {
		while (new File(String.format("%02d.t", testcount + 1)).exists()) {
			testcount++;
		}
		for (int i = 0; i < 5; i++) {
			writeRand(10, false);
			writeRand(10, true);
		}
		for (int i = 0; i < 5; i++) {
			writeRand(1000, false);
			writeRand(1000, true);
		}
		for (int i = 0; i < 5; i++) {
			writeRand(maxn, false);
			writeRand(maxn, true);
		}
	}

}
