/*
* Note for teachers:
*
* Below is one possible way you could use this lab with students. Depending on how
* much you do/don't want them to do on their own, you can add back in code from the
* solution files or you can delete more code from this lab before you hand it out.
* The philosophy taken here is to have them working mainly with the World, Grid, and
* Location classes - therefore, the code that takes care of the structure of the game,
* graphics, etc. has been left intact.
*
* Directions for students:
*
* If a method contains "*** complete this method ***" on the first line, complete
* it where the comments direct you. Otherwise, the methods are already complete.
*/
/*
* AP(r) Computer Science GridWorld Case Study:
* Copyright(c) 2002-2006 College Entrance Examination Board
* (http://www.collegeboard.com).
*
* This code is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
import info.gridworld.grid.Grid;
import info.gridworld.grid.BoundedGrid;
import info.gridworld.grid.Location;
import info.gridworld.world.World;
import java.util.ArrayList;
import java.util.Collections;
/**
* The GameOfFifteen
class is the main application.
*
* This class is not tested on the AP CS A and AB exams.
*
* copyright© 2007 Dave Wittry (http://apcomputerscience.com)
* @author Dave Wittry
*/
public class GameOfFifteen extends Worldtrue
if the world consumes the key press, false
* @see World
class
*/
public boolean keyPressed(String desc, Location loc)
{
/*
*** complete this method ***
*** where you see the "..." and a comment afterward, complete the stmt.
if ( winner )
return true; // game over; no more play
if ( !("A".compareTo(desc)<=0 && "Z".compareTo(desc)>=0) ) // not a legal keyboard entry
return true;
Gridtrue
if there is a winning situation, false
otherwise
*/
private boolean determineWinner()
{
// *** complete this method ***
/*
* Students, one possible way to write this method would be to get all of the objects out of the Grid
* in row-major order and place them into an ArrayList. Then, go through that ArrayList making sure
* that adjacent elements are ordered based on getText() (or your teacher may want you to make the
* ColorTextPiece class implement Comparable as a nice additional exercise).
* Keep in mind that somewhere there will be a null location! You're hoping the null location ends up
* as the 16th cell, with all other cells being ordered - then you have your winning situation! Try
* this algorithm with a piece of graph paper and a friend - see if it makes sense to the both of you
* before you start coding. Or, come up with your own algorithm.
*/
return true; // remove this stmt when completing this method
}
/**
* The method is called by the GUI when a mouse event takes place. There are no mouse
* events for this game - so this method immediately returns true;
* @param loc the selected location in the grid at the time the mouse was pressed
* @return true
if the world consumes the key press, false
* @see World
class
*/
public boolean locationClicked(Location loc)
{
return true; // do nothing when mouse-clicked
}
public static void main(String[] args)
{
World mw = new GameOfFifteen();
System.setProperty("info.gridworld.gui.selection", "hide");
mw.show();
}
}