CS 134 Test Program #2

Similar documents
CS 134 Programming Exercise 9:

CS 51 Laboratory # 12

CS 201 Advanced Object-Oriented Programming Lab 6 - Sudoku, Part 2 Due: March 10/11, 11:30 PM

Java Foundations John Lewis Peter DePasquale Joe Chase Third Edition

CS 051 Homework Laboratory #2

CS 201 Advanced Object-Oriented Programming Lab 3, Asteroids Part 1 Due: February 17/18, 11:30 PM

Programming Project 1

CS 134 Programming Exercise 2:

To gain experience using GUI components and listeners.

Programming Languages and Techniques (CIS120)

Be sure check the official clarification thread for corrections or updates to this document or to the distributed code.

Programming Languages and Techniques (CIS120)

SAMPLE EXAM Exam 2 Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 5 November 2009

Practice Midterm 1. Problem Points Score TOTAL 50

Programming Exercise

CSE 142, Autumn 2018 Programming Assignment #9: Critters (20 points) Due Tuesday, December 4th, 9:00 PM

CS 134 Programming Exercise 3:

CIS 162 Project 4 Farkle (a dice game)

CS Programming Exercise:

SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177. Programming

CS 134 Programming Exercise 7:

CS 2110 Summer 2011: Assignment 2 Boggle

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

You Can Make a Difference! Due April 11/12 (Implementation plans due in class on 4/9)

CS 335 Lecture 02 Java Programming

IT101. Graphical User Interface

Programming Assignment IV Due Monday, November 8 (with an automatic extension until Friday, November 12, noon)

Fall 2013 Program/Homework Assignment #2 (100 points) -(Corrected Version)

Programming Assignment 4 ( 100 Points )

Practice Midterm 1 Answer Key

Programming Assignment 2 (PA2) - DraggingEmoji & ShortLongWords

H212 Introduction to Software Systems Honors

CS61BL: Data Structures & Programming Methodology Summer Project 1: Dots!

This homework has an opportunity for substantial extra credit, which is described at the end of this document.

1 Getting started with Processing

CSE 131 Introduction to Computer Science Fall Exam II

// class variable that gives the path where my text files are public static final String path = "C:\\java\\sampledir\\PS10"

BCIS 3630 Dr. GUYNES SPRING 2018 TUESDAY SECTION [JAN version] GRADER COURSE WEBSITE

GRAPHICS & INTERACTIVE PROGRAMMING. Lecture 1 Introduction to Processing

CS 134 Programming Exercise 1:

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

READ AND OBSERVE THE FOLLOWING RULES:

CSE 143: Computer Programming II Spring 2015 HW7: 20 Questions (due Thursday, May 28, :30pm)

CS Problem Solving and Object-Oriented Programming

CS 150 Lab 10 Functions and Random Numbers

First Name: AITI 2004: Exam 2 July 19, 2004

Introduction to Programming Using Java (98-388)

Programming assignment A

CS 111X - Spring Final Exam - KEY

Remaining Enhanced Labs

APCS Semester #1 Final Exam Practice Problems

First Name: AITI 2004: Exam 2 July 19, 2004

CIS 110 Introduction to Computer Programming. February 29, 2012 Midterm

EECS168 Exam 3 Review

CS164: Programming Assignment 5 Decaf Semantic Analysis and Code Generation

3 Getting Started with Objects

12/22/11. } Rolling a Six-Sided Die. } Fig 6.7: Rolling a Six-Sided Die 6,000,000 Times

CS161: Introduction to Computer Science Homework Assignment 10 Due: Monday 11/28 by 11:59pm

Programming Assignment 3 ( 100 Points ) START EARLY!

Goal of lecture. Object-oriented Programming. Context of discussion. Message of lecture

Use the scantron sheet to enter the answer to questions (pages 1-6)

School of Computer Science CPS109 Course Notes Set 7 Alexander Ferworn Updated Fall 15 CPS109 Course Notes 7

Sudoku-4: Logical Structure

} Each object in a Java program has an identifier (name) } This includes:

5.6.1 The Special Variable this

CSCI0330 Intro Computer Systems Doeppner. Lab 02 - Tools Lab. Due: Sunday, September 23, 2018 at 6:00 PM. 1 Introduction 0.

Programming Languages and Techniques (CIS120)

Computer Science II (20082) Week 1: Review and Inheritance

Practice Midterm Examination

: Principles of Imperative Computation. Fall Assignment 5: Interfaces, Backtracking Search, Hash Tables

CS 142 Style Guide Grading and Details

Lab 3: Work with data (IV)

CS159 Midterm #1 Review

Full file at Chapter 2 - Inheritance and Exception Handling

CS ) PROGRAMMING ASSIGNMENT 11:00 PM 11:00 PM

CSE115 Lab 4 Fall 2016

CompSci 105 S2 C - ASSIGNMENT TWO -

CIS 121 Data Structures and Algorithms with Java Spring 2018

n Specifying what each method does q Specify it in a comment before method's header n Precondition q Caller obligation n Postcondition

Blocky: A Game of Falling Blocks

Interlude. Object Oriented Design

CMPSCI 187 / Spring 2015 Hangman

ACORN.COM CS 1110 SPRING 2012: ASSIGNMENT A1

CS 209 Spring, 2006 Lab 11: Files & Streams Instructor: J.G. Neal

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

Practice Final Examination #2

Project 1 Computer Science 2334 Spring 2016 This project is individual work. Each student must complete this assignment independently.

CS 349 / SE 382 Design Patterns. Professor Michael Terry January 21, 2009

CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

Programming Assignment 8 ( 100 Points )

CSE 143: Computer Programming II Summer 2015 HW6: 20 Questions (due Thursday, August 13, :30pm)

CISC 1600 Lecture 3.1 Introduction to Processing

1 Getting started with Processing

Project Data: Manipulating Bits

Compilers Project 3: Semantic Analyzer

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

CS 170 Java Programming 1. Week 9: Learning about Loops

CIS 162 Project 1 Business Card Section 04 (Kurmas)

Queen s University Faculty of Arts and Science School of Computing CISC 124 Final Examination December 2004 Instructor: M. Lamb

Transcription:

CS 134 Test Program #2 Sokoban Objective: Build an interesting game using much of what we have learned so far. This second test program is a computer maze game called Sokoban. Sokoban is a challenging game to play, but its rules are quite simple. There are several marbles placed in a maze. One marble is blue and represents the player. The other marbles must be pushed into a goal area. The goal area is simply part of the maze drawn in yellow. The player marble moves the other marbles into the goal by getting behind a marble and pushing it into the goal. There is one catch. The player marble can only push one marble at a time and can only push it one square at a time. Of course, it needs to be behind the marble to push it and neither the player nor the other marble can go through walls. A level is complete when all the target marbles are in the goal area. (The number of cells in the goal area will always correspond to the number of ordinary marbles on the board.) There are many levels with different shaped mazes and different numbers of target marbles to move. In your program, you will initialize a level by reading a maze configuration from a file. The maze is then drawn on the screen. The user uses the arrow keys to move the player marble around. The user pushes a target marble simply by moving the player marble behind the target marble. The arrow keys will then move both the player and target marble in the same direction, as long as the move is legal. A picture of the game in progress is shown below. At this point one of the marbles has been pushed into the goal area. The program should also include a button labelled Restart level. If the user clicks this button, the initial configuration for that level will be drawn so the player can start over after getting stuck. If the user completes a level, the label on the button changes to Next level. Clicking on the button then removes the 1

current level and draws the maze for the next level of the game by reading in the file containing the next level. At that point, the label of the button changes back to restart level. To get you started: We have placed in the CS134 folder of Courses, a folder called Test Program 2. It contains two folders, a demo version of Sokoban and the Sokoban start-up. The start-up folder contains files describing the mazes for various levels of the game and skeletons of the four classes you need to write: Sokoban, Player, Level, and Square. The contents of these files are printed at the end of this document. The various levels of the board can be found in files named screen.i for i an integer between 0 and 90. The file screen.0 is a special very simple file that can be used for testing your program. Each level file is a text file containing at most twenty lines (most contain fewer), with each line containing at most 20 characters. Each line represents a row in the starting configuration of the level. A blank represents an open cell, a # represents a wall, a $ represents a regular marble, an @ represents the player s marble, while a. represents a goal area. Sokoban class The class Sokoban, which extends WindowController, is responsible for receiving handling all GUI input. It creates a new object, curlevel, of type Level, reads in the file screen.i using method readlevelfile(int i), and enters the data into the level by invoking the additem method of a Level object for each item in the file. It also creates player by calling the constructor for class Player when @ is read in from the file. We have provided for you the method readlevelfile and the method makebufferedreader(string filename) that it uses. Sokoban also contains methods keytyped, keyreleased, and keypressed. The first two methods should be ignored. (They are required in every class that implements KeyListener, but we will not need them in this program.) The method keypressed will be activated when the user presses any of the keys of the program. This method should invoke the appropriate method of the player to let it know which direction the user wishes it to move (though of course the player will need to determine if that move is possible). Class Sokoban will also be responsible for adding the Button and its listening methods to the window. Level class The Level class keeps track of the current state of the board via a 20 by 20 array of Squares. When a file representing a new level is being read in, the readlevelfile method of Sokoban will execute invocations of the form additem(row,col,item) for each character read from the level file, where item is the character read in. The additem method will construct a new object of type Square which will be inserted into the array in the slot corresponding to that row and column. Later, the player object will invoke methods associated with the current Level to determine whether particular cells represent walls, whether they contain a marble, etc. These queries will generally be passed on to the corresponding objects of type Square to answer. That is, the level will delegate to the appropriate Square the responsibility for answering the query. The level is also responsible for moving around marbles on the board in response to directions from player. Square class Objects of type square are responsible for keeping all relevant information about a particular cell of the board. Each needs to keep track of whether it is a wall, goal, or open, and whether it contains a marble, inculding the player s marble. Other methods may also be necessary in order to move marbles around from one Square to another. Objects of type Square are responsible for keeping the display up-to-date in response to changes requested by the Level object. Player class Methods of the Player object are invoked by Sokoban whenever an arrow key is pressed. It knows the rules of the game and is responsible for interacting with the level to enforce the rules. For example, it must determine if a move is possible (either a push or non-push) and then instruct curlevel what changes to make in response to these moves. To understand the use of objects of each of these classes, consider the following diagram: 2

It shows the maze used in the first level of the game overlaid with a grid that can be used to identify locations within the maze. For example, the position of the player marble in the maze is row 8, column 11 of the grid. An object of type Player corresponding to this board would need to know the row and column where it is located. The corresponding object of type Level will contain a two dimensional array of Squares describing each of the cells in the picture above. When asked if the cell in row 2 and column 5 were a wall, the curlevel would respond negatively, but would respond positively to a query as to whether that cell held a marble. Try it out! As stated above, a demo of this assignment can be found in the CS134 folder on Courses. Please play with it until you understand the rules completely. Your program need not produce an identical display to ours, but it will be acceptable if it does. We hope you will be creative and make the game more fun to play! We have some suggestions below. Writing the program: We strongly encourage you to proceed as suggested below to assure that you can turn in a running program. While a partial program will not receive full credit, a program that does not run at all generally receives a worse grade. Moreover it is easier to debug a program if you know that some parts do run correctly. Run the demonstration program. Construct a program that opens the file and displays the first board before quitting. Expand the program to allow the player to move his or her marble around the board, one space at a time, without pushing any other marble. Make sure that the move is a legal one before allowing it. Expand the program again, this time allowing the user to push another marble with theirs. From the user s point of view, there is no difference between moving and pushing. (They just press the appropriate arrow key!) Your program will have to detect and deal with this difference. Be sure to check that there is an open space in which to push the marble. 3

Add the button to allow the user to restart the current level. Add a method to determine if all of the shaded marbles are in the goal area. This method should be called each time the user moves a gray marble to determine if they are allowed to move on to the next level. If all the marbles are in the goal area, change the button to read next level. When they click on the button, update the board level and change the button title back to restart level. You should now have a playable version of the game. Hints for keeping the board updated properly: 1. When you draw a rectangle for a goal cell, be sure to send it to the back of the canvas so that the marbles will show up on top. 2. Be sure to repaint the canvas every time you move a marble. Extra credit suggestions: Here are a few suggestions for extra credit features that you can add that will make the game more fun to play. 1. Add an undo button to undo your last move if you mess up. (It is very frustrating to have to start all over if you make one mistake.) You need not undo more than one move. [2 points maximum] 2. Design and implement a GUI component which allows the user to allow the selection of any of the available levels. [2 points maximum] 3. Keep track of where each player last left off in a file. Allow the display of an honor roll of the top 10 players (in terms of level solved). [4 points maximum] 4. Allow a player to decide to start fresh or to start at the last level not yet completed. Use of a file is necessary for this and the previous feature. You will learn how to work with files after Thanksgiving. [4 points maximum] Remember: This test program should be treated like an open-book take-home examination, as described in the Honor Code Guidelines handed out at the beginning of the semester. Specifically, any questions you have regarding this assignment or the particulars of your program should be addressed only to an instructor. The TA s can only answer questions involving incorrect functioning of your computer or Java compiler. When in doubt, they will refer you to an instructor. Please turn in a copy of your design document (worth 20% of the grade) at the beginning of your lecture section on Monday, November 20. To get full credit, the design document should include all of the basic game features. However, partial credit will be given for designs that include fewer features. The final program should be turned in electronically by 5 pm on Friday, December 1. 4

Table 1: Grading Guidelines Value Feature Design preparation (20 pts total) 5 pts. each for the four classes Syntax Style (15 pts total) 6 pts. Descriptive and helpful comments 2 pts. Good names 2 pts. Good use of constants 2 pts. Appropriate formatting 3 pts. Appropriate use of public/private Semantic style (25 pts total) 5 pts. conditionals and loops 5 pts. General design/efficiency issues 5 pts. Parameters, variables, and scoping 5 pts. Good correct use of arrays 5 pts. Miscellaneous Correctness (40 pts total) 8 pts. Drawing board correctly 8 pts. Moving marble correctly 8 pts. Pushing marble correctly 8 pts. Recognizing winning position 8 pts. Updating board correctly with button 5

Sokoban class starter: import objectdraw.*; import java.io.*; import java.awt.event.*; import java.awt.*; public class Sokoban extends WindowController implements KeyListener public static final char WALL = # ; // file characters public static final char PLAYER = @ ; // representing cells public static final char MARBLE = $ ; public static final char GOAL =. ; private static final int MAZESIZE = 20; private static final int MAX_LEVELS = 90; // max size of maze // max number of levels in game // prefix of filenames holding different levels private static final String FILENAMEPREFIX = "screens/screen."; private int levelcounter = 0; // level to be read in next from file private Level curlevel; private Player player; // representation of game board and marbles // object representing player marble in level // Create a new level and fill it from the file. Set up GUI interface and // set up appropriate listeners. public void begin () curlevel = new Level (MAZESIZE, MAZESIZE, canvas); readlevelfile (levelcounter); addkeylistener (this); // Add GUI stuff // Initialize curlevel by reading file "screen.i" and loading info // into curlevel. // Also initializes player as position with PLAYER character. public void readlevelfile (int i) String nextrow; int rowcounter = 0; char nextcellcontents; BufferedReader levelreader = makebufferedreader (FILENAMEPREFIX + i); try for (nextrow = levelreader.readline(); nextrow!= null; nextrow = levelreader.readline()) 6

for (int colcounter = 0; colcounter < nextrow.length(); colcounter++) nextcellcontents = nextrow.charat(colcounter); curlevel.additem (rowcounter, colcounter, nextcellcontents); if (nextcellcontents == PLAYER) player = new Player (rowcounter, colcounter, curlevel); rowcounter++; canvas.repaint(); catch (IOException e) System.out.println ("Exception reading file!"); // Create a BufferedReader stream for the given file name private BufferedReader makebufferedreader(string filename) InputStream leveldata = getclass().getresourceasstream (filename); return new BufferedReader(new InputStreamReader (leveldata)); // Must be here to implement KeyListener, but should not be used. public void keytyped(keyevent e) // Must be here to implement KeyListener, but should not be used. public void keyreleased(keyevent e) // Handle the pressing of the arrow keys by sending appropriate // messages to player. e is the event generated by the keypress public void keypressed(keyevent e) switch(e.getkeycode()) case KeyEvent.VK_UP: // add code here break; case KeyEvent.VK_DOWN: // add code here break; 7

case KeyEvent.VK_LEFT: // add code here break; case KeyEvent.VK_RIGHT: // add code here break; // Add any other methods needed here. // Get rid of extraneous comments before turning in. Level class starter: import objectdraw.*; import java.awt.*; public class Level private static final int CELL_SIZE = 15; private static final int LEFT_EDGE = 50; private static final int TOP = 50; private Square [][] grid; // size of each cell of board // location of upper left corner // of board // grid represents playing area // construct new Level with grid of dimension rows x cols. // Add extra parameters if necessary public Level (int rows, int cols, DrawingCanvas canvas) // Insert code here // Add element corresponding to item to level at (row,col) public void additem(int row, int col, char item) // Insert code here // Add methods necessary to manipulate grid contents as a result of player moves. 8

Player class starter: public class Player // declarations here // construct new player object public Player (int row, int col, Level level) // code to construct player // insert appropriate methods to move player Square class starter: import objectdraw.*; import java.awt.*; public class Square // Colors of player marble, pushing marbles, walls, and goal private static final Color PLAYER_COLOR = Color.blue; private static final Color MARBLE_COLOR = Color.gray; private static final Color WALL_COLOR = Color.black; private static final Color GOAL_COLOR = Color.yellow; private static final int MARBLE_OFFSET = 2; // inset of marble w/in cell // Create new square representing information about cell of game board public Square (/* insert parameters here */) // add methods here 9