CSCI3180作业代做
2.3 Input/Output SpecificationThe input/output specification is detailed in this section.22.3.1 Input SpecificationIn this exercise, you are required to use the command line to get input information. Since this isa simple program, there are just two operations requiring user-input.Type of PlayersUsers can choose Player X and Player O to be either human- or computer-controlled. You haveto use the command line to request for the types of the two players before starting the game (asshown in Figure 3).Figure 3: Players’ typesHuman-Controlled Player’s MoveHuman-controlled players require user-inputs to determine their next moves. When it is humancontrolledplayer’s turn during the game, your program should request for the row and column ofthe next move. The input should be integers from 1 to 8. During each turn, the system will firstcheck whether there is a legal move for current player. If not, the system will switch to anotherplayer’s turn automatically, as shown in Figure 4(a). Moreover, any invalid input or moves shouldbe forbidden and the user is requested for input again until a valid move is given. See Figure 4(b)for an example.Computer-Controlled PlayerA computer-controlled player just randomly chooses an unfilled cell to make a valid move. Duringeach turn, the system will first check whether there is a legal move for current player. If not, thesystem will switch to another player’s turn automatically.2.4 Python ClassesPlease follow the classes Othello, GameBoard, Player, Human and Computer defined below in yourPython implementation. You are free to add other variables, methods or classes. We would startthe program by running the command: python Othello.py1. Class OthelloA game object which holds the game board, players’ turns and controls game logic. You haveto implement it with the following components: Instance Variable(s)gameBoard- This is an object variable representing the game board.player1- This is an object variable representing Player O.player2- This is an object variable representing Player X.turn3- This is an integer variable (0 for player1, 1 for player2 ) indicating which playershould play in the current turn.Figure 4: Example gameplay output. (a) No legal movement; (b) Invalid inputs Instance Method(s)__init__(self)- Create a game board and initialize other instance variables.createPlayer(self,symbol,playerNum)- Create a player (human- or computer-controlled) with the corresponding symbol(’O’ or ’X’) and player number (1 or 2) by prompting the user.startGame(self)- Create two players, initialize the game board, and then start a new game and playuntil winning/losing or draw.2. Class GameBoardYou have to implement it with the following components: Instance Variable(s)board- This is an 8 × 8 two-dimensional character array representing the game board. Instance Method(s)__init__(self)- Define all instance variables.4init_gameBoard(self)- Initialize the board. Its initial state is referred to Section 2.2.execute_flip(self,pos,symbol)- Flip over all opponent’s symbols in between given position and symbol.check_legal_move(self,symbol)- Check whether their is a legal move given symbol.check_ending(self)- Check if the game is over.check_winner(self)- Count the number for each symbol and return a list, like [17,3]. The first one isthe total number of character ‘O’ and the second one is for character ‘X’.3. Class PlayerAn superclass representing a player object. You have to implement it with the followingcomponents: Instance Variable(s)playerSymbol- This is a variable indicating the symbol of the player (X or O). Instance Method(s)__init__(self, symbol)- Initialize the player with its symbol X or O.nextMove(self, board)- A method to be implemented in subclasses, which returns a list, like [1,2]. Thefirst one is the row and the second one is the column (1-8 for each) of the next validmove. The parameter board represents a two-dimensional character array that representsthe cell conditions in the game board and your function should not modify the arraycontents.4. Class Human and ComputerClasses extending the superclass Player to represent a human- and a computer-controlledplayer object respectively.2.5 TournamentFor this bonus part, you can implement a more brilliant AI player to compete with others. Wewill arrange for an automated tournament environment to test your implementations. If you wantto join the tournament, you should submit an additional Python file, named AI_[your SID].py,like AI_1155123456.py, and the class name is “AI”, which is also extended from the class Player.The AI strategy should be built in the instance method nextMove(self,board): The basic requirements have already been mentioned above. The function must produce an output within one second on the lab’s computers. You need to test carefully to make sure your code could not damage our test system, e.g.,the array out of bounds exception!!!Please note that if your program does not follow any one of the above requirements, you cannotearn extra points for this project. We will run a round robin tournament. A win is worth 3 pointsand a tie is worth 1 point, while a loss score 0 points. The top five entire will receive additionalmarks for the assignment: 30, 25, 20, 15, 10, 5 respectively.53 Task 2: Demonstrating Advantages of Dynamic TypingThere are commonly claimed advantages of Dynamic Typing:1. More generic code can be written. In other words, functions can be defined to apply onarguments of different types.2. Possibilities of mixed type collection data structures.Please provide concise example codes to demonstrate the advantages of Dynamic Typing mentionedabove. You are welcome to provide other advantages and disadvantages along with codesegment for extra bonus points.Dynamic Typing makes coding more flexible and convenient, but you should bear in mind thattype checking can only be carried out at runtime, incurring time overhead and reliability issues.4 Task 3: The JourneyThis task is also to build a game. A Java program of the game is given. You need to understandits behavior and re-implement it with Python. After finishing this task, you will have experienceda special feature of Python called Duck Typing, which is possible only in a dynamically typedlanguage. And you will see a difference between Java and Python, the former of which does notsupport Duck Typing.4.1 Duck TypingThe following synopsis of Duck Typing is summarized from:http://en.wikipedia.org/wiki/Ducktypinghttp://www.sitepoint.com/making-ruby-quack-why-we-love-duck-typingIn standard statically-typed object-oriented languages, objects’ classes (which determine an objectcharacteristic and behavior) are essentially interpreted as the objects’ types. Duck Typing is a newtyping paradigm in dynamically-typed (late binding) languages that allow us to dissociate typingfrom objects’ characteristics. It can be summarized by the following motto by the late poet JamesWhitcombe Riley:When I see a bird that walks like a duck and swims like a duck and quacks like a duck,I call that bird a duck.The basic premise of Duck Typing is simple. If an entity looks, walks, and quacks like a duck, forall intents and purposes it is fair to assume that one is dealing with a member of the species anasplatyrhynchos. In practical Python terms, this means that it is possible to try calling any methodon any object, regardless of its class.An important advantage of Duck Typing is that we can enjoy polymorphism without inheritance.An immediate consequence is that we can write more generic codes, which are cleaner and moreprecise.4.2 BackgroundIn the world of Kafustrok, most of the land is covered in dense forest, populated by bizarre monstersand a small number of elves. The only safe haven is a small number of villages where humans live.In order to protect the villages from monster attacks, a selected warrior set foot on the forest towipe out all monsters. Every monster has its own territory, and the elves also live in some specificareas. The warrior can seek help from the elves to heal himself, but he needs to use a certainamount of magic crystals in exchange.64.3 Task DescriptionAt the beginning, there exists a warrior, e elves and m monsters. All of them will be distributedin different positions.? At each iteration, the warrior will try to teleport to a targeted location.– If meeting a monster, the monster will talk and ask the warrior whether to fight or not. Yes, if his health is sufficient to kill this monster, with losing certain health, he willoccupy this position and get some magic crystals. Otherwise, he will be killed. No, enter next iteration.– If meeting an elf, the elf will talk and ask the warrior whether to make a deal or not. Yes, he will pay some magic crystals and his health increases with the elf’s magicalpower, but cannot exceed the health cap. No, enter next iteration.– If nothing is encountered, he will end up in that position. If all monsters have been killed or the warrior is dead, the game will be overYou should read and execute the given Java program to understand its behavior and design.Please replicate all the behavior of the given Java Program in Python with Duck Typing, followingthe same class design. You should not introduce extra instance variables or instance methods.Your program should run by calling python the_journey.py. You will also be evaluated on yourprogramming style.5 Task 4: More Warriors!A warrior falls but more warriors stand up. The warriors from different villages form an allianceand join the journey to destroy the monsters together. Based on the alliance’s agreement, a warriorneeds to share some crystals if the other has a request. Moreover, some life potions appear in theforest, but their positions always change from time to time, and the warriors can get treatmentif they find the potion. The game still ends when all monsters are eliminated or all warriors aredead.5.1 Task DescriptionAt the beginning, there exists w warriors, e elves, m monsters and p life potions. All of them willbe distributed in different positions. At each iteration, all life potions will teleport into different positions randomly. At each iteration, all warriors will try to teleport to a targeted location. For each warrior,– If meeting a monster, the monster will talk and ask the warrior whether to fight or not. Yes, if his health is sufficient to kill this monster, with losing certain health, he willoccupy this position and get some magic crystals. Otherwise, he will be killed. No, enter next iteration.– If meeting an elf, the elf will talk and ask the warrior whether to make a deal or not. Yes, he will pay some magic crystals and his health increases with the elf’s magicalpower, but cannot exceed the health cap. No, enter next iteration.– If meeting a life potion, the warrior will occupy the position and have some healthincreased directly. That life potion will disappear.– If meeting another warrior, then the other warrior will talk and ask the warrior whetherhe needs more crystals7 Yes, he will get some magic crystals for free. No, enter next iteration.– If nothing is encountered, he will end up in that position. If all monsters or warriors have been killed, the game will be overYou are now required to modify/extend both the Java and Python implementation of Task 3. Youwill also be evaluated on your programming style.Figure 5: Example gameplay output. (a) when meet a life potion; (b) when meet another warrior6 ReportYour simple report should answer the following questions within TWO A4 pages.1. Providing example code and necessary elaborations for demonstrating advantages of DynamicTyping as specified in Task 2.2. Using codes for Task 3, give two scenarios in which the Python implementation is betterthan the Java implementation. Given the reasons.3. Using codes for Task 4, illustrate further advantages of Dynamic Typing and Duck Typing.7 Submission GuidelinesPlease read the guidelines CAREFULLY. If you fail to meet the deadline because of submissionproblem on your side, marks will still be deducted. So please start your work early!1. Your submissions will be accepted latest by 11:59 p.m. on Mar 13, but submissions madeafter the original deadline would be considered as LATE submissions and penalties will beimposed in the following manner: Late submissions before 11:59 p.m. on Mar 12: marks will be deducted by 20% Late submissions before 11:59 p.m. on Mar 13: marks will be deducted by 50%2. In the following, SUPPOSE8your name is Chan Tai Man,your student ID is 1155234567,your username is tmchan, andyour email address is tmchan@cse.cuhk.edu.hk.3. In your source files, insert the following header. REMEMBER to insert the header accordingto the comment rule of Python./ CSCI3180 Principles of Programming Languages --- Declaration --- I declare that the assignment here submitted is original except for source material explicitly acknowledged. I also acknowledge that I am aware of University policy and regulations on honesty in academic work, and of the disciplinary guidelines and procedures applicable to breaches of such policy and regulations, as contained in the website http://www.cuhk.edu.hk/policy/academichonesty/ Assignment 2 Name : Chan Tai Man Student ID : 1155234567 Email Addr : tmchan@cse.cuhk.edu.hk/The sample file header is available athttp://course.cse.cuhk.edu.hk/~csci3180/resource/header.txt4. Task 2 report and the final report should be merged into one report file and submitted toVeriGuide, which will generate a submission receipt. The report should be named “report.pdf”.The VeriGuide receipt of report should be named “receipt.pdf”. The report andreceipt should be submitted together with codes in the same ZIP archive.5. Tar your source files to username.tar bytar cvf tmchan.tar Othello.py task3 python.zip task4 java.zip \task4 python.zip report.pdf receipt.pdf6. Gzip the tarred file to username.tar.gz bygzip tmchan.tar7. Uuencode the gzipped file and send it to the course account with the email title “HW2studentID yourName” byuuencode tmchan.tar.gz tmchan.tar.gz \| mailx -s "HW2 1155234567 Chan Tai Man" csci3180@cse.cuhk.edu.hk8. Please submit your assignment using your Unix accounts.9. An acknowledgement email will be sent to you if your assignment is received. DO NOTdelete or modify the acknowledgement email. You should contact your TAs for help if you donot receive the acknowledgement email within 5 minutes after your submission. DO NOTre-submit just because you do not receive the acknowledgement email.10. You can check your submission status athttp://course.cse.cuhk.edu.hk/~csci3180/submit/hw2.html.11. You can re-submit your assignment, but we will only grade the latest submission.12. Enjoy your work :>因为专业,所以值得信赖。如有需要,请加QQ:99515681 或邮箱:
微信:codinghelp