2018/9/18 Assignment 1.0 – CS 242 – Illinois Wiki
https://wiki.illinois.edu/wiki/display/cs242/Assignment+1.0 1/6
页面 / Home / Assignments
由 Triphol “Pao” Nilkuha (admin)创建, 最终由 Chen, Chen修改于昨天12:27 下午
Assignment 1.0
Assignment 1.0 Chess Library
Overview
This week, we will be focusing on unit testing and ObjectOriented design by
implementing the core objects and data structures for a chess game application. This
will be the first of several weeks in building this application. By the end of the week,
you should have implemented the tests and the corresponding implementation for the
pieces and board of a chess game.
For this assignment, you are required to use either Eclipse or IntelliJ IDEA. Both
are free and have powerful refactoring tools available.
Read this entire page before beginning your assignment, and post on Piazza if
anything is still unclear.
What am I actually turning in?
Many students find our concrete expectations for this first assignment confusing, and
that’s perfectly normal. There are a few key things to note here:
Your task is to design, test, and implement (more or less in that order) a chess
library from scratch. This is intentionally vague, and we expect you to spend
some time making design decisions for your library.
For this assignment, your deliverables are simply your unit test suite and your
implementation. It is not part of the requirements to add a GUI or main game
loop for this week.
If something is still unclear, ask on Piazza, chances are someone else is also
confused. In general, we are flexible with interpretations of the assignment, as long
as it does not trivialize any component of the assignment.
Background
For this assignment, we assume that you have some knowledge of the chess board
game. We will assume a simple 8 x 8 board, with two players, and the standard board
pieces. Each player alternately moves and strategically captures the others’ pieces,
until the game ends when one player is in checkmate (the king is about to be
captured, and cannot move), or there is a stalemate (neither player can move).
From Wikipedia:
Each chess piece has its own style of moving.
The king moves one square in any direction.
The rook can move any number of squares along any rank or file, but
may not leap over other pieces.
Summary
Table of Contents
Assignment 1.0 Chess Library
Overview
What am I actually turning in?
Background
Part I: Game Logic
Part II: Chess Pieces
ObjectOriented Design in Java
10 ObjectOriented Design Principles
Programmers should Know
Unit Testing
Introduction
Example
Technique
This assignment
jUnit in Eclipse and IntelliJ IDE
Introduction to JUnit
Last bit of help
Still confused or need help?
Table of Contents
Reading
Submission
Objectives
Resources
Grading
Reading
Code Complete Chapter 22: Developer Testin
Submission
This assignment is due at the beginning of your dis
section the week of September 17th, 2018. Please
submit in Gitlab, and ask your moderator or TA befor
deadline if you have any questions. Also, make sure
the naming convention: https://piazza.com/class/jl4kd
cid=52
Objectives
Practice designing a library
Learn how to write unit tests
Resources
Eclipse vs. IntelliJ IDEA
You are likely already familiar with Eclipse from earlier programming courses
here at UIUC. A few of our course staff, prefer IntelliJ. If you’ve never tried it
out, consider using it for this project, besides, it’s free for UIUC students when
you register with your @illinois email!
Assignment Format
This course is likely very different from previous courses you have taken, in
that we typically reuse your code from the previous week for each
assignment. As such, don’t waste your time with messy code: focus on
maintainability.
Refer to https://wiki.illinois.edu/wiki/display/cs242/Style+Conventions for style
and conventions.
Questions that appear in lecture quizzes
come from the assigned reading, so it is
best interest to complete it.
https://wiki.illinois.edu/wiki/collector/pages.action?key=cs242&src=breadcrumbs-collector
https://wiki.illinois.edu/wiki/display/cs242/Home?src=breadcrumbs
https://wiki.illinois.edu/wiki/display/cs242/Assignments?src=breadcrumbs-parent
https://wiki.illinois.edu/wiki/display/~a_tnilkuha
https://wiki.illinois.edu/wiki/display/~chen228
https://wiki.illinois.edu/wiki/pages/diffpagesbyversion.action?pageId=528356500&selectedPageVersions=33&selectedPageVersions=34
https://wiki.illinois.edu/wiki/display/cs242/Assignment+1.0
http://www.eclipse.org/
http://www.jetbrains.com/idea/
http://en.wikipedia.org/wiki/Chess
http://en.wikipedia.org/wiki/Chess_piece
http://en.wikipedia.org/wiki/King_(chess)
http://en.wikipedia.org/wiki/Rook_(chess)
https://piazza.com/class/jl4kd0fparb1fk?cid=52
https://wiki.illinois.edu/wiki/display/cs242/Style+Conventions
2018/9/18 Assignment 1.0 – CS 242 – Illinois Wiki
https://wiki.illinois.edu/wiki/display/cs242/Assignment+1.0 2/6
The bishop can move any number of squares diagonally, but may not
leap over other pieces.
The queen combines the power of the rook and bishop and can move
any number of squares along rank, file, or diagonal, but it may not
leap over other pieces.
The knight moves to any of the closest squares that are not on the
same rank, file, or diagonal, thus the move forms an “L”shape: two
squares vertically and one square horizontally, or two squares
horizontally and one square vertically. The knight is the only piece
that can leap over other pieces.
The pawn may move forward to the unoccupied square immediately
in front of it on the same file; or on its first move it may advance two
squares along the same file provided both squares are unoccupied;
or it may move to a square occupied by an opponent’s piece which is
diagonally in front of it on an adjacent file, capturing that piece.
We do not expect you to deal with special moves like castling for the king & rooks, or
en passant or promotion for the pawn, however, if you want to have a complete Chess
game at the end of the project, you might want to implement them for playability’s
sake.
Part I: Game Logic
First, select data structures and test and implement logic for the chess board itself.
Since there are many types of chess pieces that will be involved in the game, we
suggest you deal with this first, dealing with a general chess piece.
Specifically, you should implement the logic for components such as:
The board shape/size
Piece movement on the board
Pieces capturing/killing other pieces
Putting the king in check
Game ending conditions (detecting when a player has won or lost)
You can safely assume that your game will be played with two players and that
this number of players will not change in future weeks. You do not need to
implement the game loop, but a single for loop handling logic for turns and a
GUI should be the only missing pieces for a working chess game in this week’s
implementation.
Part II: Chess Pieces
Your library should be able to handle movement for all of the standard chess pieces.
This includes where pieces can and cannot move. Imagine that some other
component of your code (the players / game loop) need to play the game. What
methods do you need to implement for your chess pieces?
Logic for all of the following pieces should be implemented this week:
Rook
Bishop
Knight
King
Queen
Pawn
Again, you only need to worry about the most basic movements for each of these
pieces, namely (1) moving across the board and (2) capturing another piece. You do
not need to consider special cases such as castling for the king & rooks, or en passant
or promotion for the pawn (unless you want to).
Eclipse vs. IntelliJ IDEA
How is IntelliJ better than Eclipse?
Eclipse vs. IntelliJ Verdict is IntelliJ
Pointbypoint Comparison
Another Pointbypoint Comparison
ObjectOriented Design
Java Polymorphism Tutorial
Polymorphism
Java Abstract Classes
Java Interfaces
Testing
JUnit API JavaDoc
JUnit Assert
JUnit (Wikipedia)
JUnit Homepage
TestDriven development (Wikipedia)
jUnit Integration
Eclipse: Tutorial
IntelliJ: Super Simple Example
IntelliJ: Basic Configuration
IntelliJ: Fancier Use
Workshop Slides
Workshop1.pptx
Grading
We will bias clarity over cleverness in evaluating
You should adopt a “teaching” mindset, always askin
“How would I change what I have written to make it a
possible for someone else to understand my code wi
direct assistance?”
Refer to the standard Grading rubric if any portion of
is unclear. Note that the standard rubric assumes a s
for each category.
Category Weight Scoring Notes
Basic
Preparation
2 01 Ready
the sta
section
Cleverness 2 02 The ha
points o
rubric
Code
Submission
4 02 Submit
correct
on time
the cor
location
reposit
Decomposition 4 02 Project
adequa
decom
differen
and me
Documentation 4 02 Comme
each c
each fu
are cle
followin
guides
Effort 2 02 Perform
conside
amoun
Extensibility
How much effort would it require you to change your library to support a
rectangular (not necessarily square) board?
Design your game logic with considerations such as this in mind. Remember,
we will be asking you to further extend your code next week.
Extensibility
How much effort would it require you to add new types of chess pieces to your
library? Design your chess with considerations such as this in mind.
http://en.wikipedia.org/wiki/Bishop_(chess)
http://en.wikipedia.org/wiki/Queen_(chess)
http://en.wikipedia.org/wiki/Knight_(chess)
http://en.wikipedia.org/wiki/Pawn_(chess)
http://programmers.stackexchange.com/questions/21987/how-is-intellij-better-than-eclipse
A sequel to Netbeans vs. Eclipse comparison. Verdict is Intellij
http://developer4life.blogspot.com/2012/01/intellij-vs-eclipse.html
http://www.javacodegeeks.com/2012/01/intellij-vs-eclipse.html
http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html
http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming
http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
http://docs.oracle.com/javase/tutorial/java/concepts/interface.html
http://junit.sourceforge.net/javadoc/
http://junit.org/junit/javadoc/4.3/org/junit/Assert.html
http://en.wikipedia.org/wiki/JUnit
http://junit.org/
http://en.wikipedia.org/wiki/Test-driven_development
http://www.vogella.com/articles/JUnit/article.html#eclipse_usingjunit
http://stackoverflow.com/questions/320092/need-an-example-for-using-junit-in-intellij-idea
http://www.jetbrains.com/idea/webhelp/configuring-testing-libraries.html
http://stackoverflow.com/questions/4757800/configuring-intellij-idea-for-unit-testing-with-junit
https://wiki.illinois.edu/wiki/download/attachments/528356500/Workshop1.pptx?version=1&modificationDate=1441982019000&
https://wiki.illinois.edu/wiki/display/cs242/Grading
2018/9/18 Assignment 1.0 – CS 242 – Illinois Wiki
https://wiki.illinois.edu/wiki/display/cs242/Assignment+1.0 3/6
ObjectOriented Design in Java
One of the reasons we start CS 242 off with an assignment in Java is that it is fairly
easy to follow good objectoriented design practices in Java, such as inheritance and
polymorphism. We will expect you to understand and use these concepts, and they
will make your life much easier for assignments like this.
If you are unfamiliar with these concepts or could use a refresher, here is a relatively
simple tutorial for using polymorphism in Java:
http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html. We
also suggest you familiarize yourself with abstract classes and interfaces in Java if you
have never used them before.
10 ObjectOriented Design Principles Java Programmers
should Know
https://jelastic.com/blog/10objectorienteddesignprinciplesjavaprogrammershould
knowguestpost/
Unit Testing
Unit testing is a great way to ensure that, given an implementation, the code works
correctly. This is enforced by creating small (unit) tests for each function. Each test
should be able to call the function it is testing, and determine if the function worked
according to its specification.
Introduction
This can be a strange concept at first, but it is really quite simple: If you know the input
to a function, and you know how the function works, you should know exactly what the
output of the function is. Since you know what the output should be before even
calling the function, you can compare the output you get from making the function call
to the expected output. If they match up, then the test has succeeded. Otherwise, it
fails.
One test is rarely sufficient to be confident in the correctness of a function. A complete
test suite would try all cornercases, as well as the common case. For instance, if a
function operates on positive integers, and it is called with a negative parameter, does
it do the right thing? The success and failure of tests are completely defined by the
person designing the application. Therefore, it is necessary to clearly understand what
a function should do in order to test it.
Each test should be named in a fashion that clearly says what it does. Each test
should be written in such a way that it is immediately apparent from the prose of its
statements:
the objects it works on,
specific methods it tests,
an exception it calls or handles,
any preconditions and postconditions that must be met.
If it doesn’t, the test must include comments that fully explain what is missing.
Example
An example of a valid unit test would look like (for an imaginary Phone object):
Category Weight Scoring Notes
Naming 2 02 Variabl
and me
names
readab
followin
conven
Overall Design 4 02 Have n
approa
structu
overall
Participation 5 02.5 Interac
group 2
(ask a
make a
comme
answer
questio
(0 poin
partici
the stu
more t
minute
Presentation 4 02 Presen
clearly
Requirements
Chess Pieces
5 02.5 2 p
imp
bas
mo
rule
cap
beh
all c
pie
2.5
spe
mo
or e
Requirements
Data
Structures
4 02 2 p
bas
gam
stru
incl
boa
two
etc
Requirements
Game End
Conditions
4 02 2 p
imp
gam
con
like
che
and
sta
det
Remember, we will be asking you to further extend your code next week.
public class PhoneTests {
//note these are jUnit 4 style tests, the naming
convention is different for jUnit 3 style tests
/**
* The constructor is supplied with valid values so no
exceptions should be thrown.
http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html
http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
http://docs.oracle.com/javase/tutorial/java/concepts/interface.html
https://jelastic.com/blog/10-object-oriented-design-principles-java-programmer-should-know-guest-post/
2018/9/18 Assignment 1.0 – CS 242 – Illinois Wiki
https://wiki.illinois.edu/wiki/display/cs242/Assignment+1.0 4/6
Technique
Good testing requires you to test for the unexpected. Every function should have at
least one test. However, to get full credit, you should write tests which cover all corner
cases and other possibilities that your library may eventually encounter. You do not
need to write tests for extremely trivial, small functions (5 lines or less).
A common misconception is that more tests are always better. While good test
coverage of your code is important, you can make the most of your time when unit
testing by strategically selecting the test cases to implement. For example, if we are
testing a function that adds two positive integers, we could test inputs 5, 0, 5, 6, 7,
INT_MAX, etc., but testing all of 5, 6, and 7 may be redundant, since they are all fairly
small positive numbers and so are very similar, likely redundant test cases.
This assignment
For your assignment, we expect you to thoroughly test both the game logic and chess
pieces for your library. This is extremely important because the errors will propagate
into future assignments!
For example, you may want to test:
What happens when a player tries to move a piece to an empty space on the
board?
What happens when a player tries to move a piece to an invalid space (off the
board)?
What happens when a player captures another piece? Does the captured piece
disappear?
What happens when a player tries to move to space already containing one of
his/her pieces?
What happens when the player’s king is put in “check”?
Which player moves first?
Some common checkmate/stalemate scenarios (Extra Credit)
Checkmate: https://en.wikipedia.org/wiki/Checkmate#Examples
Stalemate: https://en.wikipedia.org/wiki/Stalemate#Simple_examples
Remember, testdriven development means writing your tests before you write
their implementation!
Category Weight Scoring Notes
Testing
Chess Pieces,
Game Logic
5 02.5 2 p
whe
tes
che
beh
imp
hav
cov
abo
2.5
whe
cov
95%
AN
tes
inc
che
and
sta
sce
Testing Data
Structures
5 02.5 2 p
whe
tes
imp
gam
stru
hav
cov
abo
2.5
whe
cov
95%
Full Score 56
*/
@Test
public void ValidConstructor1() throws Exception {
String number = “15555555555”
String name = “John Smith”
Phone phone = new Phone(name,number);
assertEquals(number, phone.getNumber());
assertEquals(name, phone.getName());
}
/**
* This test checks the case where the constructor is
supplied a phone number with too many dashes.
*/
@Test(expected=InvalidPhoneNumberException.class)
public void TooManyDashes() throws Exception {
String number = “999999999999″;
String name = “John Smith”;
Phone phone = new Phone(name,number);
}
//more tests below…
}
https://en.wikipedia.org/wiki/Checkmate#Examples
https://en.wikipedia.org/wiki/Stalemate#Simple_examples
2018/9/18 Assignment 1.0 – CS 242 – Illinois Wiki
https://wiki.illinois.edu/wiki/display/cs242/Assignment+1.0 5/6
When writing your functions always keep in mind writing them so that they are easy to
test. The more focused each individual function is, the easier it will be to test, and the
more correct your code will be. Modularity is key to unittesting. It may be a good idea
to split your library into multiple classes based on the data structure(s) you use as an
internal representation for the chessboard.
There are some resources which aid in the unit testing process, such as JUnit for
Eclipse and IntelliJ. You are free to use these if you would like. Eclipse and JUnit are
available on the CSIL Linux computers, and an introduction to using JUnit with Eclipse
is provided in this assignment description.
jUnit in Eclipse and IntelliJ IDEA
One of the reasons we require you to use either EclipseofIntelliJ for this assignment
isjUnitintegration. Both IDEs have had support forjUnitintegration for some time.
Eclipse
There is a thorough tutorial for using jUnit+ Eclipse here:
http://www.vogella.com/articles/JUnit/article.html#eclipse_usingjunit
IntelliJ IDEA
There is some documentation for usingjUnitwith IntelliJ here as well:
Super Simple Example
Basic Configuration
Fancier Use
Introduction to JUnit
http://www.vogella.com/tutorials/JUnit/article.html (A comprehensive JUnit
tutorial page)
An example of jUnit4 tests was shown above. Generally, developers put all unit tests
for one type in one file, with multiple files of tests composing a test suite for the library
or application. It will be much easier for you to keep things organized if you follow a
similar pattern. Although Eclipse and IntelliJ will let you run a single file containing
JUnit tests directly, it takes a bit more effort to run all tests in all files at once (your test
suite).
For example, say I had tests for a Phone object in a file called PhoneTests and tests
for an Address object in a file called AddressTests, I could have the IDEjUnitrunner run
them both by creating a file called something like ContactInfoTests and have it contain:
The following examples will work for both Eclipse and IntelliJ but are
unnecessary for IntelliJ. It is possible to simply create “Run Configurations” in
IntelliJ IDEA to accomplish this without adding any code, rather than create
additional classes as we describe below.
package contactInfo.tests;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
/**
* Runs all tests for classes in the
contactInfo
package
* @author Jerome Bell
*
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({AddressTests.class,
PhoneTests.class})
public class ContactInfoTests {
}
http://www.vogella.com/articles/JUnit/article.html#eclipse_usingjunit
http://stackoverflow.com/questions/320092/need-an-example-for-using-junit-in-intellij-idea
http://www.jetbrains.com/idea/webhelp/configuring-testing-libraries.html
http://stackoverflow.com/questions/4757800/configuring-intellij-idea-for-unit-testing-with-junit
http://www.vogella.com/tutorials/JUnit/article.html
2018/9/18 Assignment 1.0 – CS 242 – Illinois Wiki
https://wiki.illinois.edu/wiki/display/cs242/Assignment+1.0 6/6
If I was to then instruct my IDE to run ContactInfoTests as ajUnittest, it would run all
the tests in both the PhoneTests class and the AddressTests class. You might not
need to, but you can chain it up even further if you divide your tests into subcategories
and have a master suite file like:
Last bit of help
When we solve these assignments, we do in fact write all of the tests before the
methods they test. After writing the tests, we write stub implementations of the tests
that call them. The code then compiles without errors, but the tests fail since they
didn’t do anything yet. This is normal. Then one by one, we write the implementation
for each of the methods until all of the tests pass.
Still confused or need help?
First, ask questions on Piazza. If you have a question, there is a pretty good chance
someone else has an even better chance that someone else in the class or one of the
TAs will be able to answer it for you. If you are still having a problem, email your
moderator or one of the TAs to get advice. Remember, its best to ask questions early
on so they have time to be answered. Don’t wait until the last second to get started
then realize that you are confused.
package allTests;
import junit.framework.TestSuite;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import contactInfo.tests.ContactInfoTests;
import persistence.tests.DatabaseInterfaceTests;
@RunWith(Suite.class)
@Suite.SuiteClasses({ContactInfoTests.class,
DatabaseInterfaceTests.class})
public class RunAllTests extends TestSuite{
}
Sample code and tests
To assist you in your efforts, here are two sample files SquareTest.java and
Square.java. You do not need to use this class, but feel free to take portions
of it to use in your chess data structures.
无标签
https://wiki.illinois.edu/wiki/download/attachments/528356500/SquareTest.java?version=1&modificationDate=1441464235000&
https://wiki.illinois.edu/wiki/download/attachments/528356500/Square.java?version=1&modificationDate=1441464190000&
http://www.atlassian.com/