首页 > > 详细

代做data编程课程、代写java程序设计、Java编程实验调试调试Matlab程序|代写Python编程

Project 1
Due Date: Tuesday, November 17
THE KING IS DEAD, LONG LIVE THE KING!!!!
A castle starts with a certain number of defense points (number of defense points is
explained later in the ELSE IF section). If the castle defense value ever goes to <= 0, the
castle falls. There are G gates to a castle. For each entrance there are S free spaces.
The number of spaces is equal to the number of people that can hold the gate closed on
one side and equal to the number of people trying to break through the gate on the other
side.
Villagers have gotten angry at the sudden increase in taxes the King has set. They have
breached the outer castle walls and have taken over the armory. The attackers will, in
FCFS (first come first served) order, take a weapon from the armory and be assigned an
attack random integer value between 1 and 10 at the time the weapon is obtained.
(Note: Have the attackers block on the same object – armory). The defenders of the
castle can be assigned this value at runtime because they are already armed.
Upon receiving a weapon an attacker will head towards a gate and prepare to attack
(Note: In the beginning every attacker/defender searches for the least attacked/defended
gate to help out his friends. That is, an attacker compares every gates’ attackers and
picks the gate with the least attackers). No thread should be allowed to get to any of the
gates that are fully occupied. If no space to defend/attack is available at any gate, then
defenders/attackers just wait for free space. (Note: attackers/defenders wait in the order
of their arrival (FCFS). Use similar code to rwcv.java. Each attacker/defender gets an
object to wait on in that data structure - vectors). Upon arrival at the gate, the attacker
will wait for all of the defenders to arrive at the appropriate gate.
Likewise, the defenders will each choose a gate and wait for all of the attackers to arrive
at their gate.
Note: use the Gate as an object that both defenders and attackers will wait on. For each
gate there is an object named “Gate”.
Once all of the defenders and attackers are at the appropriate gate the battle can began.
The combined attack/defend values will be compared with the following conditions
below. The last thread to arrive at a gate notifies all waiting threads on the gate. While
the last thread will also execute sumUpAtackersDefendersValues() and do the
comparisons, the other threads will wait for the result on one object.
We will name a group of attackers/defenders gathered on a gate – company.
There may be other companies of threads that just finished attacking/defending and
want to do the comparison too. Make sure that multiple companies can do comparison
concurrently at company level but comparison is still synchronized within the same
company. You should use some identification for the same company (for example,
timestamp all company threads and assign company gate number, timestamp+gate#
would serve as unique id for company; each thread would carry that id within itself).
When a company is done with the comparison their gate status is either breached or
open for the king to try to escape (if attack/defend values matched, gate is as it was
before the siege began). Once the comparison is done and the threads start notifying
their respective reserve threads(first on line) and then rest. After rest they join the
reserves themselves.
IF:
Total defend value > total attack value: attackers are overwhelmed by defenders and
the gate is considered clear of attackers.
The King is a very sneaky person, when a gate is cleared of attackers, he will try to
escape. First he will need to gather his precious belongings and get ready. The King will
use the wait(long) method instead of the sleep method to simulate the packing time. If an
attacker arrives during this time, then the King will be signaled on the monitor wait(long)
and will retreat back into the castle.
If the gate at which the King is escaping through doesn’t have a new attacker when the
escape time elapses, then the King has succeeded in escaping and the attackers lose.
The time the king takes to get ready should be some factor less than the time an
attacker takes to rest.
Whenever an attacker that is coming to the siege sees a gate that was cleared of his
fellow attackers, he will try to attack that gate first. As soon as that attacker gets to that
gate, the defenders barricade it once more and the king has to wait for another
opportunity.
ELSE IF:
Total attack value > total defend value: attackers overwhelm defenders.
There's a number which represents the total castle defense. It is equal to the sum of
every single defender’s defense value. When attackers breach a gate, the sum of their
attack value is subtracted from the total castle defense (For example, if we have 100
defenders, their defense variable’s sum may equal 500, which means the castle’s
defense is also 500. If several attackers (with a total attack value of 50) breach one of
the gates, then the castle defense value is down to 450.)
Those attackers that were able to breach the gate and the defenders that failed to
protect the gate start attacking/defending again from the beginning. (Essentially, their
threads run in an infinite while (true) loop).
ELSE:
the battle is postponed.
If the castle’s defense value is at or below 0, the defenders lose.
---------------------------------------------------------------------------------------------------------------------
Develop a monitor(s) that will synchronize the threads (defenders, attackers, king) in the
context of the story described below. Closely follow the details of the story and the
synchronization requirements.
You can create any other methods/objects you may need. Make sure that any shared
variable/structure is accessed in a mutual exclusion fashion.
The number of attackers, defenders, gates to the castle, and free spaces for each
entrance can be read as command line arguments using the following format where a =
attackers, d = defenders, G = castle gates and S = free spaces:
Note that all command line arguments are optional. If they are not provided at run time,
your program should default to:
G*S+4 attackers, G*S+4 defenders; where G=3, and S=2.
Do NOT use busy waiting. If a thread needs to wait, it must wait on an object (class
object or notification object).
Use only basic monitors as the ones discussed in class (synchronized methods,
synchronized blocks, wait(), notify and notifyAll). Wait(long timeout) should be use once
only. NO other synchronization tools like locks, concurrent collections…..or even volatile
variables.
Use the age( ) method and appropriate println statements to show how synchronization
works. It is important to have print statements before, inside, and after critical events.
State clearly what the current thread executing the print statement is doing. Also, be
sure to include the name of the thread and a timestamp relative to the start time of the
program.
Suggestions:
The King’s thread should call
Method by which he’s waiting for the opportunity of escaping
Method by which he’s packing belongings.
Method by which he checks for a safe escape (if an attacker arrives during
that time the king should retreat back into the castle).
Attackers/Defenders call Castle’s methods:
Method(s) to find a defending/attacking position
or wait until all spaces are filled with attackers/defenders on both sides of the
gate
attack()/defend() should be simulated by sleep. Make sure that you give info
about the name of the thread and gate.
A method that will compute the attackers or defenders value
Between major events make sure you insert a sleep of random time. Also make sure
you give the information necessary to understand what that event is.
Choose the appropriate amount of time(s) that will agree with the content of the story. I
haven’t written the code for this project yet, but from the experience of grading previous
semesters’ projects, a project should take somewhere between 50 seconds and at most
2 minutes to run and complete. Set the time in such way that you don’t create only
exceptional situations.
Do not submit any code that does not compile and run. If there are parts of the code that
contain bugs, comment it out and leave the code in. A program that does not compile
nor run will not be graded.
Follow the story closely and cover the requirements of the project’s description. Besides
the synchronization details provided there are other synchronization aspects that need to
be covered. You can use synchronized methods or additional synchronized blocks to
make sure that mutual exclusion over shared variables is satisfied.
The Main class is run by the main thread. The other threads must be specified by either
implementing the Runnable interface or extending the Thread class. Separate the
classes into separate files. Do not leave all the classes in one file. Create a class for
each type of thread.
Add the following lines to all the threads you make:
public static long time = System.currentTimeMillis();
public void msg(String m) {
System.out.println("["+(System.currentTimeMillis()-time)+"] "+getName()+": "+m);
}
There should be printout messages indicating the execution interleaving. Whenever you
want to print something from that thread use: msg("some message here");
NAME YOUR THREADS or the above lines that were added would mean nothing.
Here's how the constructors could look like (you may use any variant of this as long as
each thread is unique and distinguishable):
// Default constructor
public RandomThread(int id) {
setName("RandomThread-" + id);
}
Design an OOP program. All thread-related tasks must be specified in their respective
classes, no class body should be empty.
DO NOT USE System.exit(0); the threads are supposed to terminate naturally by
running to the end of their run methods.
Javadoc is not required. Proper basic commenting explaining the program flow, selfexplanatory
variable names, correct whitespace and indentations are required.
Name your project as follows: LASTNAME_FIRSTNAME_CSXXX_PY
where LASTNAME is your last name, FIRSTNAME is your first name, XXX is your
course, and Y is the current project number.
For example: Fluture_Simina_CS344_p1
zip only the source files. Upload the project on BlackBoard.

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp
热点文章
程序代写更多图片

联系我们 - QQ: 99515681 微信:codinghelp
© 2014 www.7daixie.com
程序代写网!