Midterm 2 数据结构考试代考 Question 1 25 pts Your task for this question is to create a Node class by implementing an interface. To begin, the following interface is provided Question 1 ...View details
Java编程作业代写 General coursework requirements: Considering the program requirements and the marking criteria: • Produce a Java program for a Reversi/Othello
General coursework requirements:
Considering the program requirements and the marking criteria:
• Produce a Java program for a Reversi/Othello game which meets the program requirements and marking criteria specified in this document.
• Identify and use appropriate classes in your program.
• Use interfaces and sub-classing appropriately in your program.
• Use anonymous inner classes if/where appropriate to do so.
• Only your main() function can be static. You must have no other static member functions! (Not doing this will penalise marks!)
• Use standard Swing classes, any other standard class library classes, any classes that I have given you already. And your own classes (which may be subclasses of Swing classes), where appropriate.
• Draw a class diagram to represent your solution, including your own classes, any immediate superclasses for your own classes. (See marking criteria.) Include aggregation and inheritance relationships where they exist. Include all attributes and operations for your own classes. You can draw this by hand or using any software package that you wish to use. It must be clear and unambiguous. You must be able to explain your class diagram when asked questions in the demo.
• You will need to understand how to create objects, and how to have references to other objects within your objects.
• Your finished program should look like the display below at the start, should allow players to place pieces when it is their turn, should automatically change the colour of pieces which are taken, and should detect when a player wins. See marking criteria for more information.
Program requirements (see also marking criteria!!!): Java编程作业代写
Read the information about how to play Othello/Reversi first. Wikipedia has a summary of the rules: https://en.wikipedia.org/wiki/Reversi
You must have two frames which display views of the board, with a label at the top (specifying which player the board is for) and a button at the bottom (to make an AI move), as shown in the pictures. You need to display the board on each, where one shows the board upside down from the other. (actually a 180 degree rotation, see example on the next page) Think of each as representing the view of the board for one player, and the players are sitting at opposite ends of the table, so that they see the board upside down (actually rotated 180 degrees) compared to the other player.
Create a class to draw and handle a square of the board. Consider what I did previously in ColorLabel and and do something appropriate for the board square – probably a button rather than a label though. Hint: you will need a
green square, a black border, and white/black circles.
You need to maintain a board state somewhere (a class for this is recommended), which should represent an 8×8 grid of squares, as shown above. Each square can contain a white piece, a black piece or be empty. You should put four initial pieces in the centre as show above.
When a piece is played, any pieces between it and another piece of colour in a straight line up to the first piece of your colour in that direction are captured. One way to do this for every time a piece is played, search vertically, horizontally and diagonally (8 directions in total):
• If you find an empty square or the edge of the board then stop – no pieces can be captured in that direction.
• If you find a piece of the opposing colour then continue in that direction and check the next square.
• If you find a piece of your own colour then stop and capture all of the pieces of the opposing colour between it and the piece you just played. If no opposing pieces would be captured then you cannot play there.
Players take it in turns to play. The active player should be obvious to the player(s) and should be indicated in the label along the top of the board, as shown in the earlier pictures. (where it was white’s turn) and in the top figure on the next page (where it is black’s turn because white has made a turn)
A player can only play a piece if it is their turn and playing the piece will capture at least one piece owned by the other player. There are only four initial places for white to play, which will be horizontally or vertically next to a black piece, see picture below for two examples. The black player can then play in a choice of three places (see diagrams below). After that things get more complex.
White should play first. The picture above shows the board states after white has made a move. Black can then make a move, as shown by the board below. Moves then continue in turn.
If a player cannot make a move then the game switches back to the other’s player’s turn – the other player can make another move. (See also point 11.)
Include a simple greedy AI, so that when a player presses the AI button and it is their turn the computer takes their turn for them. When the player presses the button, the program should check each space, calculate how many pieces will be taken if a piece is placed in that space and choose one of the space which take the most pieces. Note: If there are multiple places which take the same pieces then I don’t mind which you choose – I chose randomly but you can choose the first or last such position if you wish. This is called a greedy algorithm because it always makes the best move it can think of at the time without thinking about later consequences. Please do read the hints!
When neither player can play a piece then the game ends (this will usually be when the board is full, but may not always be). At this point the player(s) should be presented with a message telling them then who won (who had the most pieces) and how many pieces of each colour there were at the time the game ended.
To understand how to do requirement 11, please look up JOptionPane. You could look at the example code on this page: https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html. e.g. it gives the example: JOptionPane.showMessageDialog(frame, “Eggs are not supposed to be green.”);
The JOptionPane class has a static function (the equivalent of a global function in C, remember) which will display a message box for you if you wish. Just create the string to display and pass it to the function. E.g.: String st = “My message”; JOptionPane.showMessageDialog(null,st);
Note: a ‘modal’ dialog in this case means that when you call showMessageDialog() the function will not return back to your code until the dialog is closed. This actually makes things a lot simpler for you. Try it.
Marking criteria (total 30 marks – for 15% total): Java编程作业代写
The mark breakdown is shown below. We will use this to decide how many marks to give you, so you could probably use it as a check-list.
5 marks: class diagram is correct and represents the code.
• All of student’s own classes are shown, as are parent classes for these classes. Classes are shown correctly.
• Attributes are listed for your classes (ignore standard classes).
• Methods are shown for your classes (ignore standard classes).
• Relationships between objects are correct.
• Includes inheritance and aggregation correctly.
9 marks: user interface is correct:
• Two top-level windows appear and work.
(Three sections per window – label, grid, button.)
• Each frame has as its top section a label which shows which player this board is for and whether it is their turn.
• Each frame has middle section as an 8×8 grid of squares.
• Each square in grid is green with a black border.
• Each square with a piece has either a black circle with white outline or white circle with black outline on them.
Hint: drawing a slightly smaller circle inside a larger circle will leave an outline.
• Bottom section of each board/frame is a button for asking AI to make a move – see marks below.
• Boards are represented from the point of view of different players – i.e. one is a 180 degree rotation of the other.
• At the end a message is displayed which shows which player won (or a draw) and correctly counts how many pieces each side had.
• There are no problems at all on user interface/usage – it all works correctly and appears correctly.
7 marks: Basic functionality was implemented Java编程作业代写
• Only the active player can make a move. (Requirement above specifies that labels need to show which is the active player.)
• If there is no valid position for the player to make a move, control automatically moves back to the other player.
• The active player can click any valid square on the board (where opposing pieces could be captured) to place their counter and the counter appears (or could use the AI – see below for AI).
• Counters can only be placed in valid locations – when at least one opposing counter would be captured by the move.
• When a counter is placed, control goes to the other player.
• Placing a piece correctly captures (at least some) pieces between it and other friendly pieces.
• ALL appropriate pieces in all directions are captured (including horizontal, vertical or diagonal).
5 marks: Board state is maintained
• The board state is correctly stored in a board object (or squares of a board object), so the state (white, black or empty) of each square on the board is maintained.
• The correct pieces are shown on both boards for the current board state.
• The initial board state is an alternating white (top-left), black (top-right), black (bottom-left), white (bottom-right) pattern in the middle 4 squares of the board.
• When the board is filled, the game will end. (Message will be displayed – see above.)
• After completing a game, a new game can be played, with an initial board which is set up correctly, and the game can progress correctly.
4 marks: Simple Artificial Intelligence Java编程作业代写
• When the button is pressed the AI makes a decision about where to place a piece
• AI correctly counts how many pieces would be captured if it made a move into each of the empty squares.
• AI selects one of the square(s) which would capture the highest number of pieces and makes a move there.
• AI makes a move correctly, actually setting the board square. Both frames are updated correctly to show the move.
Negative marks: you will lose marks for breaking the general requirements.
E.g. if you have more than one static function (bad OO design because you didn’t make the objects do the work), or have a design which is obviously not a good decomposition into sensible OO classes. (there is significant leniency on this, so you would have to do something that we can’t understand why it could be a sensible design) I gave you huge clues for what some sensible classes/objects are in the requirements and marking criteria.
It is intended that this coursework will be marked in the labs via a demo, however you need to also submit your code to moodle in case of any issues and to allow us to verify marks if necessary. This is your evidence in case of any queries about whether the code does work as expected, whether any requirement was met, and/or that you did complete it on time in case of any queries about marks.
Since you have the marking criteria, you should be able to ensure that you can get all of the marks. So you are encouraged to have it marked early to get it out of the way before the courseworks deadlines accumulate nearer to the exams.
When you have completed your coursework, please ask one of the markers to mark it in the labs. They will ask you to demo it and ask you various questions to understand how you did things and to test your understanding of what you did. They will then give you a mark and tell you where marks were lost. The mark will later appear in moodle so that you can check it if you wish. i.e. You will receive immediate verbal feedback so will know what mark you got and why.
Counting the pieces taken:
If you create a function which counts how many pieces will be taken if you play in a location, you will find that you can use this function in multiple places. If the value is 0 for all directions then you can’t play there. If you call the function mentioned above for every empty square on the board you can work out whether a player can play at all. If neither player can play at all then the game should end. You can also work out what the best greedy move is for the AI very easily if you have developed this function (i.e. the one which takes the most pieces). i.e. this function is REALLY useful!
Checking in multiple directions:
If you want to check multiple directions, you could use a loop of offsets and either recursion. (as seen in functional programming) or iteration. E.g. continuously adding -1x,-1y to a position moves diagonally, whereas adding 0x,1y to a position moves vertically. I used two loops: e.g. for (xoffset=-1; xoffset<=1;xoffset++) and similarly for y. You need to skip the case for 0,0 – as this is not a move.
Using this offset I checked all 8 directions without me having to hardcode multiple directions. (If this doesn’t make sense to you then just hardcode the directions, but it is not as quick to code it that way, so you’ll need to write more code.) Don’t forget to check for reaching the edge of the board! You can use iteration to keep moving to the edge, but I found that recursion gave me an easier function to create in this case. You may find iteration easier until you fully understand recursion though – the comments from the FP lectures about different cases also apply to recursive functions in Java.
Where to start? Java编程作业代写
You can choose your own starting point, either starting from the diagrams and working to the code, or doing both in combination.
– If you are really stuck for a starting point then I suggest starting with the user interface, since the concepts are very similar to those that you have already used in the previous lab. This will allow you to make some progress early on, and is safe to do even before you know how to do some of the more complex things.
– Next decide how to store your data, how you will work out which pieces are taken (or could be taken) and how you will ensure that changes made to one frame are seen in the other. I tend to think about what data I need to maintain as well (i.e. what member variables do you need in each object), and what each object needs to do.
– Thinking about the responsibilities of objects, and their relationships with other objects may help you. Think also about how you will record whose turn it is as well, and how you will ensure that a player can only play a piece on their own turn. And think about where you will store information about what piece is in each board square (black, white, or none).