How To Write A Program In Java 26B (Program) With A Knight'S Tour (Program 2) And A Knight (Game) (Program 1) (For Students) (Permanent) (Win2) (Seminar) (

Similar documents
AP Computer Science Java Mr. Clausen Program 9A, 9B

12-6 Write a recursive definition of a valid Java identifier (see chapter 2).

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Object-Oriented Programming in Java

WRITING DATA TO A BINARY FILE

Using Files as Input/Output in Java 5.0 Applications

J a v a Quiz (Unit 3, Test 0 Practice)

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

In this Chapter you ll learn:

Two-Dimensional Arrays Summer 2010 Margaret Reid-Miller

IRA EXAMPLES. This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account).

AP Computer Science A 2011 Free-Response Questions

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Advanced Java Client API

AP Computer Science Java Subset

public static void main(string[] args) { System.out.println("hello, world"); } }

Java Server Pages and Java Beans

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

JAVA - FILES AND I/O

Recursion and Recursive Backtracking

Building a Multi-Threaded Web Server

Java Interview Questions and Answers

Continuous Integration Part 2

Coding Standard for Java

COSC 111: Computer Programming I. Dr. Bowen Hui University of Bri>sh Columbia Okanagan

Section 6 Spring 2013

Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

Two-Dimensional Arrays. Multi-dimensional Arrays. Two-Dimensional Array Indexing

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Introduction to Java

AP Computer Science A 2004 Free-Response Questions

13 File Output and Input

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

This loop prints out the numbers from 1 through 10 on separate lines. How does it work? Output:

Introduction to Object-Oriented Programming

Algorithms and Data Structures Written Exam Proposed SOLUTION

Using Two-Dimensional Arrays

Part 3: GridWorld Classes and Interfaces

The Snake Game Java Case Study

AP Computer Science A Picture Lab Student Guide

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

CS114: Introduction to Java

Chapter 5. Recursion. Data Structures and Algorithms in Java

The Interface Concept

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Reading Input From A File

1.4 Arrays Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright /6/11 12:33 PM!

The resulting tile cannot merge with another tile again in the same move. When a 2048 tile is created, the player wins.

Software Development with UML and Java 2 SDJ I2, Spring 2010

Lecture J - Exceptions

Arrays in Java. Working with Arrays

Introduction to Java. CS 3: Computer Programming in Java

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Reduced echelon form: Add the following conditions to conditions 1, 2, and 3 above:

What is Microsoft Excel?

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

Grundlæggende Programmering IT-C, Forår Written exam in Introductory Programming

Answers to Review Questions Chapter 7

The following program is aiming to extract from a simple text file an analysis of the content such as:

Massachusetts Institute of Technology 6.005: Elements of Software Construction Fall 2011 Quiz 1 October 14, Instructions

Stacks. Linear data structures

Paillier Threshold Encryption Toolbox

Iteration CHAPTER 6. Topic Summary

Teach Yourself Java in 21 Minutes

Contents. 9-1 Copyright (c) N. Afshartous

Homework/Program #5 Solutions

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

INPUT AND OUTPUT STREAMS

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

4.5 Symbol Table Applications

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Lab 4: 26 th March Exercise 1: Evolutionary algorithms

CS 111 Classes I 1. Software Organization View to this point:

HW3: Programming with stacks

LAB4 Making Classes and Objects

Report Vertiefung, Spring 2013 Constant Interval Extraction using Hadoop

1. What are Data Structures? Introduction to Data Structures. 2. What will we Study? CITS2200 Data Structures and Algorithms

COMPUTER SCIENCE. Paper 1 (THEORY)

Decision-making Computer Science Lesson to Prepare for UIL Computer Science Contest

West Virginia University College of Engineering and Mineral Resources. Computer Engineering 313 Spring 2010

CS170 Lab 11 Abstract Data Types & Objects

Mail User Agent Project

AP Computer Science. Marine Biology Case Study. Advanced Placement Program

AVRO - SERIALIZATION

Database Access from a Programming Language: Database Access from a Programming Language

Database Access from a Programming Language:

Socket Programming in Java

Part I. Multiple Choice Questions (2 points each):

Exception Handling. Overloaded methods Interfaces Inheritance hierarchies Constructors. OOP: Exception Handling 1

Komumaldata Barcoded PDF Forms PDF417 Barcode Generator Reference XFA (LiveCycle) Version Generator Version: 1.12

Chapter 1 Java Program Design and Development

CMSC 202H. ArrayList, Multidimensional Arrays

Remote Method Invocation

Lab 5: Bank Account. Defining objects & classes

Transcription:

AP Computer Science II Java Unit Lab Assignment # 26b The "Knight's Tour" Program 90 & 100 Point Versions Assignment Purpose: The purpose of this assignment is to review and practice Java program coding and problem solving for a challenging assignment with Java two-dimensional arrays. Write a program that tries to solve the famous Knight s Tour problem. This problem concerns the movements of a Knight on a chessboard. The Knight moves only in a unique L-shaped pattern. The chessboard below shows eight possible moves for a knight from position [4][4] on the board. 8 1 7 2 K 6 3 5 4 Consider any one of 64 starting positions on the chessboard. Call that position 1 of the Knight s Tour. Now try to move the knight to every position on the chessboard without visiting any square a second time. An example of a completed knight s tour is shown below from position [2][2]. 31 46 15 60 29 44 13 10 16 1 30 45 14 11 28 43 47 32 59 52 61 64 9 12 2 17 50 63 56 53 42 27 33 48 55 58 51 62 23 8 18 3 36 49 54 57 26 41 37 34 5 20 39 24 7 22 4 19 38 35 6 21 40 25 Exposure Java 2007 Lab 26b Assignments Page 1 07-25-08

90 Point Version Your first concern is to write a program that tries to make a Knight s Tour to determine the next move. Start at any location on the chessboard and start moving the knight until you cannot move any further. The next example shows a partial knight s tour that starts from position [4][4]. 00 10 00 00 00 00 00 00 00 00 00 00 01 00 25 00 00 00 08 00 26 00 02 21 24 16 27 00 09 22 19 12 03 07 00 17 14 05 10 23 20 28 15 06 00 18 13 04 11 Lab26b 90/100-point Versions // Lab26bst.java // This is the student version of the knight's tour lab assignment. Student Provided File import java.io.*; import java.text.decimalformat; public class Lab26bst public static void main (String args[]) throws IOException System.out.println("\nLab26b 90/100 Point Version\n"); Knight knight = new Knight(); knight.getstart(); knight.solvetour(); knight.displayboard(); class Knight private int board[][]; private int startrow; private int startcol; private int rowpos; private int colpos; private int moves; // stores the sequence of knight moves // row location where the knight starts // col location where the knight starts // current row position of the knight // current col position of the knight // number of location visited by the knight /* USED WITH THE 100 POINT VERSION final private int ACCESS[][] = 0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0, 0,0,2,3,4,4,4,4,3,2,0,0, 0,0,3,4,6,6,6,6,4,3,0,0, 0,0,4,6,8,8,8,8,6,4,0,0, 0,0,4,6,8,8,8,8,6,4,0,0, 0,0,4,6,8,8,8,8,6,4,0,0, 0,0,4,6,8,8,8,8,6,4,0,0, 0,0,3,4,6,6,6,6,4,3,0,0, 0,0,2,3,4,4,4,4,3,2,0,0, 0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0; */ Exposure Java 2007 Lab 26b Assignments Page 2 07-25-08

public Knight() // constructor used to initializes the data attributes public void getstart() throws IOException // input method to get starting row and col from keyboard entry public void displayboard() // displays the chessboard after the tour is concluded private boolean getmove() // computes the next available knight's move. Alters RowPos and ColPos and // returns true if move is possible, otherwise returns false public void solvetour() // primary method that drives the knight's tour solution Exposure Java 2007 Lab 26b Assignments Page 3 07-25-08

Lab26b 90 Point Version Two Separate Required Outputs Lab26b 90 Point Version Enter row start ===>> 4 Enter col start ===>> 4 00 00 00 01 00 25 00 00 00 08 00 26 00 02 21 24 16 27 00 09 22 19 12 03 07 00 17 14 05 10 23 20 28 15 06 00 18 13 04 11 The knight has toured 28 locations. Enter row start ===>> 2 Enter col start ===>> 2 00 32 00 00 00 00 00 00 00 01 00 33 00 00 00 00 31 00 00 02 39 34 00 00 00 41 26 00 00 03 38 35 15 30 00 40 25 36 21 04 42 09 16 27 12 19 24 37 29 14 07 10 17 22 05 20 08 43 28 13 06 11 18 23 The knight has toured 43 locations. Exposure Java 2007 Lab 26b Assignments Page 4 07-25-08

100 Point Version Using the Accessibility Algorithm The problem with the "no algorithm" approach is that it is easy to drive yourself in a corner with no place to go. The center of the chessboard has many options with potentially eight moves for the next location. The four corners of the chessboard only provide two possible moves. A clever approach to improve Knight s Tour considerably, is to first occupy positions with smaller access, and then move to areas of greater access. Essentially, this means to fill up the corners and the borders first and then end up in the center. For the 100-point version you need to instantiate a two-dimensional array called ACCESS, which stores the accessibility values for each position on the chessboard. With the Access array available, the next Knight move is no longer a matter of Is there space? Now you check all the available positions and move to the square that has the least access first. The chessboard below shows the accessibility values for each square on the chessboard. 2 3 4 4 4 4 3 2 3 4 6 6 6 6 4 3 3 4 6 6 6 6 4 3 2 3 4 4 4 4 3 2 The chessboard is an 8 X 8 matrix. Make the ACCESS matrix 12 X 12. This allows room for two rows and two columns around the chessboard. Place a zero in these extra border spaces to identify a move that is outside the chess board area. The object is to move the smallest, non-zero location. It is also suggested that you create a separate GetMove method. This method is not shown in the main method. Call GetMove from the SolveTour method to get the location of the next knight move, if it is possible. Exposure Java 2007 Lab 26b Assignments Page 5 07-25-08

Lab26b 100 Point Version Two Separate Required Outputs Lab26b 100 Point Version Enter row start ===>> 4 Enter col start ===>> 4 33 46 17 60 31 44 15 12 18 49 32 45 16 13 30 43 47 34 61 00 59 00 11 14 50 19 48 01 56 53 42 29 35 02 51 54 00 58 25 10 20 05 38 57 52 55 28 41 03 36 07 22 39 26 09 24 06 21 04 37 08 23 40 27 The knight made 61 moves Lab27b 100 Point Version Enter row start ===>> 2 Enter col start ===>> 2 31 46 15 60 29 44 13 10 16 01 30 45 14 11 28 43 47 32 59 52 61 64 09 12 02 17 50 63 56 53 42 27 33 48 55 58 51 62 23 08 18 03 36 49 54 57 26 41 37 34 05 20 39 24 07 22 04 19 38 35 06 21 40 25 The knight made 64 moves Exposure Java 2007 Lab 26b Assignments Page 6 07-25-08