java

profileaplzoh
project1.pdf

COMP 182 Project 1: Review (30 points)

Due: Thursday September 4 at 23:55

Purpose: There is a famous puzzle called the 15-puzzle (feel free to look it up on the internet - eg

en.wikipedia.org/wiki/15 puzzle). In this puzzle there is a frame that could hold 16 tiles in a 4x4 configuration,

but the frame contains only 15 tiles where each tile is labeled with a number between 1 and 15. Since one of the

locations is empty, it is possible to slide another tile (from above/below/right/left of the empty location) into this

location. The goal of the puzzle is to reach the point where the numbers are organized so that they read (top to

bottom, left to right) 1, 2, 3, . . . , 15, space. The diagram below shows a very simple example with the final solution on the right.

1 2 3 4

5 6 7 8

9 10 12

13 14 1511

1 2 3 4

5 6 7 8

9 10 11 12

13 14

1 2 3 4

5 6 7 8

9 10 11 12

13 14 1515

1 2 3 4

5 7 8

9 12

13 1511

1 2 3 4

5 6 7 8

9 12

13 14 1511

1010

6

14

You will be writing a Java program to allow people to play the 24-puzzle (the obvious variant where the frame is

5x5) puzzle) in a text based context. My goal is that you recall how to enter programs (probably using jGrasp),

how to write a small/simple program, how to compile and debug your work, and learn how to submit your work

via moodle. This is meant to be a review project.

Requirements: To complete this project you will write two classes and one text file.

Create a file called Board.java which will be the “guts” of the project. This class will likely contain a field int[][]

theFrame (a 5x5 integer array), a method readInitialBoard(String fileName), a method makeMove(int number),

a method showBoard(), and a method isCorrect(). Each method does exactly what a reasonable person would

expect. You are welcome to have other fields and methods.

Create a file called Driver1.java which will interact with the user and “run” your program. There is a sample

interaction below. You should have your program match the style/sytax as closely as possible. Due to class size,

some of the grading of projects gets automated which means deciding to go with a di↵erent format for input or

output will likely result in losing points even if you think “my way is just as good as his”. Typically, this class

would create an instance of Board.java, fill the board with the information from the file input.txt (a sample is

below: 0 indicates the empty position). Then it would repeatedly ask the user for a command and perform that

command. Legal commands would include: help, quit, and move. It ends if the user quits or the problem is solved.

Create a file called status.txt which contains your name and a short (2-10 sentence) description of the status of your

program. This file should be an ascii file. Though you may create it with MS Word (or notepad/wordpad/jGrasp/etc),

you should be certain that it is a text file. A sample status.txt file is below.

Comments:

You should be using good style. See moodle for more details, but at a minimum, a java file should always: begin

with a comment containing your name and the purpose of the class, contain appropriate comments, be limited to

80 character lines, be limited to 30 line methods (usually shorter), and be properly indented.

Submission: Prior to the deadline upload your 3 files to moodle (class files and data files are neither necessary

nor wanted). I would suggest uploading long before the deadline and updating/replacing as you go (work on it

today and upload, work on it tomorrow and replace, work on it the next day and replace, . . ..

Sample input.txt file:

1 2 3 4 16

5 6 7 8 17

9 10 11 12 18

13 14 15 19 20

0 21 22 23 24

Sample interaction 1:

Welcome to Project 1: 24-Puzzle.

Loading board from input.txt

Current board

--------------------------

| 1 | 2 | 3 | 4 | 16 |

|------------------------|

| 5 | 6 | 7 | 8 | 17 |

|------------------------|

| 9 | 10 | 11 | 12 | 18 |

|------------------------|

| 13 | 14 | 15 | 19 | 20 |

|------------------------|

| | 21 | 22 | 23 | 24 |

--------------------------

Next command

> help

Legal commands are:

help - prints this helpful information

quit - ends the game

move # - which attempts to move the tile with that number into the empty position

Current board

--------------------------

| 1 | 2 | 3 | 4 | 16 |

|------------------------|

| 5 | 6 | 7 | 8 | 17 |

|------------------------|

| 9 | 10 | 11 | 12 | 18 |

|------------------------|

| 13 | 14 | 15 | 19 | 20 |

|------------------------|

| | 21 | 22 | 23 | 24 |

--------------------------

Next command

> move 2

Not a legal move.

Current board

--------------------------

| 1 | 2 | 3 | 4 | 16 |

|------------------------|

| 5 | 6 | 7 | 8 | 17 |

|------------------------|

| 9 | 10 | 11 | 12 | 18 |

|------------------------|

| 13 | 14 | 15 | 19 | 20 |

|------------------------|

| | 21 | 22 | 23 | 24 |

--------------------------

Next command

> move 21

Current board

--------------------------

| 1 | 2 | 3 | 4 | 16 |

|------------------------|

| 5 | 6 | 7 | 8 | 17 |

|------------------------|

| 9 | 10 | 11 | 12 | 18 |

|------------------------|

| 13 | 14 | 15 | 19 | 20 |

|------------------------|

| 21 | | 22 | 23 | 24 |

--------------------------

Next command

> move 14

Current board

--------------------------

| 1 | 2 | 3 | 4 | 16 |

|------------------------|

| 5 | 6 | 7 | 8 | 17 |

|------------------------|

| 9 | 10 | 11 | 12 | 18 |

|------------------------|

| 13 | | 15 | 19 | 20 |

|------------------------|

| 21 | 14 | 22 | 23 | 24 |

--------------------------

Next command

> move 13

Current board

--------------------------

| 1 | 2 | 3 | 4 | 16 |

|------------------------|

| 5 | 6 | 7 | 8 | 17 |

|------------------------|

| 9 | 10 | 11 | 12 | 18 |

|------------------------|

| | 13 | 15 | 19 | 20 |

|------------------------|

| 21 | 14 | 22 | 23 | 24 |

--------------------------

Next command

> move 21

Current board

--------------------------

| 1 | 2 | 3 | 4 | 16 |

|------------------------|

| 5 | 6 | 7 | 8 | 17 |

|------------------------|

| 9 | 10 | 11 | 12 | 18 |

|------------------------|

| 21 | 13 | 15 | 19 | 20 |

|------------------------|

| | 14 | 22 | 23 | 24 |

--------------------------

Next command

> quit

Writing to file. Goodbye.

Sample status.txt file:

John Noga - Project 1

The program works as required. It compiles/runs and the output matches the

correct format to the letter. However, the style and formatting is incorrect

because I DIDN’T: comment it (didn’t even put my name in the file), keep the

length of lines to 80 characters, and keep the length of methods to 30 lines.

Cheating: This project is an individual project. You can discuss this project with other students. You can explain

what needs to be done and give suggestions on how to do it. You cannot share source code. If two projects are

submitted which show significant similarity in source code then both students will receive an F in the course. Note

a person who gives his code to another student also fails the class (you are facilitating the dishonest actions of

another).