You are on page 1of 6

Project 3: Peg Solitaire (a.k.a.

Brainvita)
This is a team project to be done in groups of 3-4
Milestone Due Thursday, March 4th, 9:00 pm
Final Project Due Thursday, March 11th, 9:00 pm
TA: Mohammad Kazi
For this project you will develop a simplified version of the Peg Solitaire game using your knowledge of arrays, loops and conditional
statements.

• Background

• Course Material

• Setup

• Project Specification

• Divide and Conquer - Turning in a MileStone

• Final Project Implementation and Turnin

m

er as
Grading Criteria

co
eH w
Background

o.
Peg Solitaire is a board game for one player involving movement of pegs on a board with holes.Some sets use marbles in a board with
indentations. The game is known simply as Solitaire in the United Kingdom where the card games are called Patience. It is also referred to

rs e
as Brainvita (especially in India), while one popular commercial version is also used generically, Hi-Q.
ou urc
The standard game fills the entire board with pegs except for the central hole. The objective is, making valid moves, to empty the entire
board except for a solitary peg in the central hole. There are two traditional boards, graphically depicted as follows ('.' as an initial peg, 'o' as
an initial hole):
o
aC s
vi y re
ed d
ar stu
is
Th
sh

In this project, we would be focussing on an updated implementation of the English version of this game, which is displayed in the right hand
side figure above.

This study source was downloaded by 100000829368906 from CourseHero.com on 12-09-2021 08:59:45 GMT -06:00

https://www.coursehero.com/file/6811221/Project-3/
A valid move is to jump a peg horizontally or vertically over an adjacent peg into a hole two positions away and then to remove the jumped
peg.
In the diagrams which follow, a green * indicates the peg that is chosen to be moved and a green * indicates any other peg in a hole and a
green o indicates an empty hole. A green ¤ is the hole the current peg moved from; a red * is the final position of that peg and a red o is the
hole of the peg that was jumped and removed.
Thus valid moves in each of the four orthogonal directions are:
Board Configuration before the Board Configuration after the
Move
move move
Jump to right
* * o ¤ o *

Jump to left
o * * * o ¤

Jump down
* ¤

* o

m
er as
o *

co
Jump up

eH w
o *

o.
rs e * o
ou urc
* ¤
To get a feel of the game before you begin coding for this project, you are welcome to play the game online using the following link.
o
aC s

Course Material
vi y re

Before you start this project, you must understand:

• Conditionals (section 1.3)

• Loops (section 1.3)


ed d

• 2-D Arrays (section 1.4)


ar stu

• StdIn Library(section 1.5)

• CS 177 Java Coding Standards


is

Setup
Th

1. In your console or terminal window enter the cd command to ensure that you are in your home directory.

2. Enter the cd cs177 command to go to your cs177 directory.


3. Create a directory for this project: mkdir proj3. You should create a new directory for each project.
sh

4. Change to the directory created just now using the cd proj3 command. You will be storing the files for this project
in this directory.

5. Enter the drjava & command to start working on Project3.java.

Project Specification

This study source was downloaded by 100000829368906 from CourseHero.com on 12-09-2021 08:59:45 GMT -06:00

https://www.coursehero.com/file/6811221/Project-3/
Overview
For this project, you need a 9 by 9 two dimensional array of characters, each element of which represents a location on the Solitaire board.
The elements of the array will have one of the three values :

• 'Z' indicates an undisplayed location which basically isnt a part of the board and will not be a part of the output.

• 'O' indicates a location with no peg.

• 'X' indicates a location with a peg.


Choose the bottom-most left-most element as the location of the array[0][0] element and index the other elements accordingly. The array
should be initialized to look like this. However, what you should display on the console as the initial setup should resemble the figure shown
on the left :

m
er as
co
eH w
and when the board reaches the combination displayed on the right, the player wins and the game ends.

o.
Don't worry, the aim of this project is not to find a set of moves that wins you the game !! What is required is that after you display the initial

rs e
board setup, the user should enter the X and Y coordinates (which are equivalent to their indices in the 2D array) of the peg which he/she
wants to move and also the coordinates of the position where the user wants to move the peg to. After this is done, you should print exactly
ou urc
5 blank lines and then re-display the board after making the changes caused by the user's last move. Now the user enters the coordinates
again and this goes on till you reach the winning configuration or when your code detects that no further moves are possible. The latter case
occurs when you have no two pegs adjacent to each other or when there is no hole for a peg to jump to). If you have reached the winning
combination, you should print "User wins the game. Exiting the game..." and then end the game. If no further moves are possible, you should
print"User loses the game. Exiting the game..." and then end the game.
o

In this project, you need to do the following error checking each time the user input's the coordinates:
aC s

• The source and destination points should lie on the ACTUAL Solitaire board. This involves two checks:
vi y re

1. Both the X coordinate and the Y coordinate should be between 0 and 8.


2. The element specified by the X-Y coordinates should not contain a 'Z'.

• The source and destination points should lie on a line (Eg: source: (3,3) & destination (3,5) lie on the same line
but source: (4,4) & destination: (5,5) dont).


ed d

The source should be exactly two hops away from the destination.(Eg: (3,3) to (5,3) is valid but (4,4) to (7,4) isnt).


ar stu

The source point should have a peg ( i.e. the letter 'X').

• The destination point should have a hole ( i.e. the letter 'O').

• The element between the source and the destination should have a peg ( i.e. the letter 'X').
If there is an error in the user's input based on the above rules, then ask for the input again without printing the 5 blank lines and dont
is

re-print the board . Only when the user enters a correct input, should five blank lines be printed and the new board be displayed.
Th

To sum up, the major tasks you need to perform are :


1. Initialize the array and display the initial setup board.
sh

2. Keep asking the user for input till he enters a valid input.
3. Print 5 blank lines on the screen and display the new board.
4. Check if this board is a winning combination or if no more moves are
possible. If either of the conditions are met, display the appropriate message
and exit.

This study source was downloaded by 100000829368906 from CourseHero.com on 12-09-2021 08:59:45 GMT -06:00

https://www.coursehero.com/file/6811221/Project-3/
5. Else, if none of the conditions in point number 4 are met, then keep
repeating steps 2 to 4.
User Input and Code Output
You will use StdIn to input in the following format.
Enter the coordinates
2444

• The first number represents the X coordinate of the source.

• The second number represents the Y coordinate of the source.

• The third argument represents the X coordinate of the destination.

• The fourth argument represents the Y coordinate of the destination.


After this, you clear the screen and display the new board. If this board matches the winning combination, print the following :

User wins the game. Exiting the game...

If this board falls into the category of a losing combination, then print:

User loses the game. Exiting the game...

m
Else clear the screen and display the new board and ask for user input again as described in this section.

er as
co
Divide and Conquer - Turning in a Milestone

eH w
This project is a lot more involved than the previous two projects. You have three weeks to complete the project. To get you started, let us

o.
consider a subtask you should aim to complete within two weeks.
You should write a program that initializes the 9 X 9 two dimensional array, asks the user for input, performs the error check at each input

rs e
and works correctly for any number of moves. However, at this milestone you DO NOT need to check for the finishing conditions (winning
ou urc
or losing) .

Start Your Project


Enter some comments at the top of your file listing your name, CS 177, your lab section, and today's date. Remember that multi-line
comments are surrounded by /* and */. For example:
o

/*
aC s

* Project 3 : Peg Solitaire


vi y re

* Name : xxx1, xxx2, xxx3, xxx4 (name all the group members)
* Course : CS 177
* Section : xxxx1, xxxx2, xxxx3, xxx4 (write the corresponding sections)
* Date : 03/xx/2010
*/
ed d

Name your class Project3 as shown:


ar stu

public class Project3


{
public static void main(String[] args)
{
is

}
}
Th

You only need to write code in the main method.

Sample I/O (Input/Output)


Here is a sample I/O for Milestone submission. As mentioned before, the leftmost, bottommost element is considered as the [0][0] element
of the array.
sh

Turning in Your Milestone Code


Only one person per group must turn in the Milestone code using the following steps:

1. In your console window, make sure that you are in the directory containing Project3.java. To make sure, you
can run:
cd ~/cs177/proj3
2. Make sure that your name, CS 177, lab section, and date are in comments at the top of your Project3.java file.
3. To turn in the assignment, type:

This study source was downloaded by 100000829368906 from CourseHero.com on 12-09-2021 08:59:45 GMT -06:00

https://www.coursehero.com/file/6811221/Project-3/
turnin -c cs177=xxxx -p project3M Project3.java
Substitute the xxxx with your lab section number such as 0301, or 0401, or 0501, etc. For example, if you are in lab section
0501, you should type:
turnin -c cs177=0501 -p project3M Project3.java

Final Project Implementation and Turnin


The only task left after the Milestone is that after each new board is displayed, the code should test if this is a winning combination or a losing
combination or whether the game should continue hereafter.

Sample I/O (Input/Output)


Here are two partial sample I/O's : One for the Winning Case and one for the Losing case.

Turning in Your Final Code


Only one person per group must turn in the Final code using the following steps:

1. In your console window, make sure that you are in the directory containing Project3.java. To make sure, you
can run:
cd ~/cs177/proj3
2. Make sure that your name, CS 177, lab section, and date are in comments at the top of your Project3.java file.

m
3.

er as
To turn in the assignment, type:
turnin -c cs177=xxxx -p project3 Project3.java

co
Substitute the xxxx with your lab section number such as 0301, or 0401, or 0501, etc. For example, if you are in lab section

eH w
0501, you should type:
turnin -c cs177=0501 -p project3 Project3.java

o.
Verifying your submission
rs e
ou urc
You should verify that your assignment was turned in by typing
turnin -v -p project3
You will be prompted to enter the course name, and it will list the assignments that you have turned in and the date and time each
assignment was last modified.
o
aC s

Grading Criteria
vi y re

!!! WARNING !!!


If your program does not compile and run, you will receive zero points.

Milestone Submission
ed d
ar stu

Requirement Points

Able to print the initial board. 10


is

All Error checks enforced. 20


Th

Works correctly for 20 moves. 20

Coding style (Coding standard). 5


sh

Total 55
Final Submission

Requirement Points

This study source was downloaded by 100000829368906 from CourseHero.com on 12-09-2021 08:59:45 GMT -06:00

https://www.coursehero.com/file/6811221/Project-3/
Correctly identifies and stops at Winning board. 15

Correctly identifies and stops at the first evident losing combination. 25

Coding style (Coding standard). 5

Total 45
Citations : 1. http://en.wikipedia.org/wiki/Peg_solitaire
2. http://www.flash-game.net/game/1738/brainvita.html

m
er as
co
eH w
o.
rs e
ou urc
o
aC s
vi y re
ed d
ar stu
is
Th
sh

This study source was downloaded by 100000829368906 from CourseHero.com on 12-09-2021 08:59:45 GMT -06:00

https://www.coursehero.com/file/6811221/Project-3/
Powered by TCPDF (www.tcpdf.org)

You might also like