import java.awt.Color;
import java.util.Vector;
import hsa.Console;

class Bingo
{
    static Console c = new Console ();
    public static void main (String[] args)
    {
	int i, j;
	int max = 50;
	int[] [] value = newSheet (max);
	String[] [] cell = new String [value.length] [value [0].length];
	for (i = 0 ; i < cell.length ; i++)
	    for (j = 0 ; j < cell [i].length ; j++)
		cell [i] [j] = "" + value [i] [j];
	cell [2] [2] = "FREE";

	boolean gameon = true, tryagain;
	int input;
	while (gameon)
	{
	    c.clear ();
	    for (i = 0 ; i < cell.length ; i++)
	    {
		for (j = 0 ; j < cell [i].length ; j++)
		    c.print (cell [i] [j], 6);
		c.println ("");
	    }
	    c.print ("\nCall a number: ");
	    input = c.readInt ();
	    tryagain = true;
	    while (true)
	    {
		if (input < 1 || input > max)
		    c.print ("Value out of range (1-" + max + ").");
		else
		{
		    for (i = 0 ; i < value.length ; i++)
		    {
			for (j = 0 ; j < value [i].length ; j++)
			{
			    if (input == value [i] [j])
			    {
				tryagain = false;
				value [i] [j] = 0;
				cell [i] [j] = "0";
				gameon = check4bingo (i, j, value);
				break;
			    }
			}
			if (!tryagain)
			    break;
		    }
		    if (!tryagain)
			break;
		    c.print ("No match.");
		}
		c.print (" Try again: ");
		input = c.readInt ();
	    }
	}
	c.print ("BINGO!!  Congratulation");
	c.setTextColor (Color.red);
	c.print ("s"); // lol
	c.setTextColor (Color.black);
	c.println ("!");
    }


    static int[] [] newSheet (int range)
    {
	int i, j;
	int[] [] value = new int [5] [5];
	Vector vector = new Vector ();
	for (i = 0 ; i < range ; i++)
	    vector.add (new Integer (i + 1));
	for (i = 0 ; i < value.length ; i++)
	    for (j = 0 ; j < value [i].length ; j++)
		value [i] [j] = ((Integer) (vector.remove ((int) (Math.random () * vector.size ())))).intValue ();
	value [2] [2] = 0;
	return value;
    }


    static boolean check4bingo (int y, int x, int[] [] value)
    {
	int i;
	boolean gameon = false;
	for (i = 0 ; i < value.length ; i++)
	    if (value [i] [x] != 0)
	    {
		gameon = true;
		break;
	    }
	if (!gameon)
	    return gameon;
	gameon = false;
	for (i = 0 ; i < value [0].length ; i++)
	    if (value [y] [i] != 0)
	    {
		gameon = true;
		break;
	    }
	if (!gameon)
	    return gameon;
	if (y == x)
	{
	    for (i = 0 ; i < value.length ; i++)
		if (value [i] [i] != 0)
		    return true;
	}
	else if (y + x == value.length - 1)
	{
	    for (i = 0 ; i < value.length ; i++)
		if (value [i] [value.length - 1 - i] != 0)
		    return true;
	}
	else
	    return true;
	return false;
    }
}
