// The "Assi10" class.
import java.awt.*;
import hsa.Console;
public class Assi10
{
static Console c; // The output console
public static void main (String[] args)
{
c = new Console (28, 78, 16, "Magic Square Game");
c.setTextBackgroundColor (Color.darkGray);
c.clear ();
c.setTextColor (Color.green);
final int SIZE = 3;
boolean exit;
boolean checkValid;
String grid[] [] = new String [SIZE] [SIZE];
int[] starPosition = new int [2];
String cont;
String close;
exit = instructions ();
while (exit != true)
{
int move = 0;
int moves = 0;
boolean finish = false;
starPosition = starPosition ();
grid = printInitialGrid (starPosition);
while (finish != true && move != 123)
{
checkValid = false;
while (checkValid != true && move != 123)
{
move = move ();
checkValid = checkValid (move, starPosition, grid);
}
switchStar (move, grid, starPosition);
moves++;
c.clear ();
printGrid (grid, moves);
finish = checkFinish (grid);
}
if (finish == true)
{
c.setTextColor (Color.blue);
c.println ("You won!! You took " + moves + " moves.(enter any key to continue)");
cont = c.readLine ();
c.setTextColor (Color.green);
}
c.clear ();
exit = instructions ();
}
for (int s = 0 ; s < 4 ; s++)
c.println ();
c.setTextColor (Color.blue);
c.print ("", 32);
c.println ("Thank You For Playing");
c.print ("", 31);
c.println ("(Enter any key to close)");
close = c.readLine ();
c.close ();
} // main method
public static int[] switchStar (int move, String[] [] grid, int[] starPosition)
{
final int SIZE = 3;
String temp;
for (int i = 0 ; i < SIZE ; i++)
{
for (int x = 0 ; x < SIZE ; x++)
{
if (grid [i] [x].equals ("" + move))
{
grid [i] [x] = "*";
grid [starPosition [0]] [starPosition [1]] = "" + move;
starPosition [0] = i;
starPosition [1] = x;
return starPosition;
}
}
}
return starPosition;
}
public static boolean instructions ()
{
final int SIZE = 3;
String[] [] grid = new String [SIZE] [SIZE];
int instruction = 0;
int moves = 0;
int value = 0;
String menu;
boolean exit = false;
while (instruction != 1 && exit != true)
{
c.print ("", 30);
c.println ("Welcome to Magic Square Game!!");
c.print ("\n");
c.println ("What would you like to do?");
c.println ("1) START GAME!!!!!!");
c.println ("2) Instructions");
c.println ("3) Exit");
c.println ("Choose wisely");
instruction = c.readInt ();
c.clear ();
if (instruction == 2)
{
for (int i = 0 ; i < SIZE ; i++)
{
for (int x = 0 ; x < SIZE ; x++)
{
value = value + 1;
grid [i] [x] = "" + (value);
}
}
grid [2] [2] = "*";
c.println ("The object of the game is to get all the numbers so that");
c.println ("it looks like this:");
printGrid (grid, moves);
c.print ("\n");
c.println ("The '*' represents the empty spot which can be switched");
c.println ("by entering the number you want to switch with.");
c.println ("You can only switch '*' with the number either");
c.println ("on top, on the bottom, to the right, or to the left");
c.println ("of the '*'");
c.println ("\n");
c.println ("Enter in any key to go back to the menu");
menu = c.readLine ();
c.clear ();
}
else if (instruction == 3)
exit = true;
else if (instruction != 1)
c.println ("Enter either 1, 2, or 3!!");
}
return exit;
}
public static boolean checkFinish (String[] [] grid)
{
final int SIZE = 3;
int value = 0;
int last;
for (int i = 0 ; i < SIZE ; i++)
{
if (i == 2)
last = 2;
else
last = SIZE;
for (int x = 0 ; x < last ; x++)
{
value = value + 1;
if (!grid [i] [x].equals ("" + value))
return false;
}
}
return true;
}
public static int move ()
{
int move;
c.println ("Where would you like to move?(123 to exit)");
move = c.readInt ();
return move;
}
public static boolean checkValid (int move, int[] starPosition, String[] [] grid)
{
final int SIZE = 3;
for (int i = 0 ; i < SIZE ; i++)
{
for (int x = 0 ; x < SIZE ; x++)
{
if (grid [i] [x].equals ("" + move))
{
if (i == 0 || i == 1)
{
if (x == 0 || x == 1)
if (grid [i + 1] [x].equals ("*"))
return true;
else if (grid [i] [x + 1].equals ("*"))
return true;
if (x == 2 || x == 1)
if (grid [i + 1] [x].equals ("*"))
return true;
else if (grid [i] [x - 1].equals ("*"))
return true;
}
if (i == 1)
{
if (x == 0 || x == 1)
if (grid [i - 1] [x].equals ("*"))
return true;
if (x == 2 || x == 1)
if (grid [i - 1] [x].equals ("*"))
return true;
}
if (i == 2)
{
if (x == 0 || x == 1)
if (grid [i - 1] [x].equals ("*"))
return true;
else if (grid [i] [x + 1].equals ("*"))
return true;
if (x == 2 || x == 1)
if (grid [i - 1] [x].equals ("*"))
return true;
else if (grid [i] [x - 1].equals ("*"))
return true;
}
}
}
}
c.println ("You can't move there!!!");
return false;
}
public static int[] starPosition ()
{
double rawRow;
double rawColum;
int starPosition[] = new int [2];
int row;
int colum;
rawRow = Math.random ();
row = (int) (rawRow * 2);
rawColum = Math.random ();
colum = (int) (rawColum * 2);
starPosition [0] = row;
starPosition [1] = colum;
return starPosition;
}
public static String[] [] printInitialGrid (int[] starPosition)
{
final int SIZE = 3;
String grid[] [] = new String [SIZE] [SIZE];
double rawRow1;
double rawColum1;
int colum1 = 0;
int row1 = 0;
int moves = 0;
for (int w = 0 ; w < 3 ; w++)
{
for (int x = 0 ; x < 3 ; x++)
grid [w] [x] = "";
}
grid [starPosition [0]] [starPosition [1]] = "*";
for (int i = 1 ; i <= 8 ; i++)
{
while (!grid [row1] [colum1].equals (""))
{
rawRow1 = Math.random ();
row1 = (int) (rawRow1 * 3);
rawColum1 = Math.random ();
colum1 = (int) (rawColum1 * 3);
}
grid [row1] [colum1] = "" + i;
}
printGrid (grid, moves);
return grid;
}
private static void printGrid (String[] [] grid, int moves)
{
c.setTextColor (Color.yellow);
// Print first row
c.print ("", 35);
c.print ("Moves taken: " + moves);
c.println ();
c.println ();
for (int currElement = 0 ; currElement < 3 ; currElement++)
{
c.print ("", 35);
for (int x = 0 ; x < 3 ; x++)
c.print (grid [currElement] [x], 5);
//c.print ("Moves taken: " + moves);
c.println ();
c.println ();
}
c.setTextColor (Color.green);
}
} // Assi10 class
|