讲解:JavaProject BlackBoxJava、Java

Introduction目标- 练习使用二维数组- 练习使用方法来模块化代码- 练习使用对象来表示抽象概念(例如游戏)介绍您的第二个项目是BlackBox游戏的实施。请参考我们的游戏视频,常见问题解答和本手册的其余部分,以便准确说明预期内容。BlackBox介绍BlackBox的目标是推断多个对象(以下称为“球”)的位置。 玩家从网格边缘的有效位置投影一条线,称为入口。 该线根据一组规则(在下面定义)与游戏板上的球进行交互。 利用他们对这些相互作用的了解,玩家的目标是尽可能少地预测这些球的位置。BlackBox游戏板BlackBox是用正方形,n×n矩阵来演奏的。 行数和列数根据玩家在开始游戏前选择的难度级别(不区分大小写)设置。 难度级别如下图所示。游戏板将有三个隐藏的球放置在任何位置。 下面显示了您的初始板应该看起来像一个“简单”游戏的样本。请注意,有5行和5列未知空间可能会隐藏球。 每个“|” |“可能包含其中一个球。在这个游戏板中,’#’字符是用户将选择从其中触发的输入端口。 该线将遵循从’#’字符开始朝向另一侧’#’字符的轨迹,遵循为球和线定义的交互规则。 当到达游戏板的任何一侧时,射击入口端口和接收入口端口可以呈现以下任何一种状态:命中(H):命中被定义为不离开游戏板的光束,这只有在光束与含有球的空间相交时才会发生。请注意,球仅可用于此演示。 在该图中,从(1,5),(3,1),(3,7)或(7,5)任何一个端口发射都将导致命中。 到达球后,射击入口将被替换为’H’字符。反射(R):反射被定义为通过射击它的同一入口离开游戏板的光束。 有两种情况可能会发生反射。如果球位于射击入口轴的旁边(轴和球之间没有空间),并且位于距入口点45度的空间内。 在上面的图片中,入口端口(4,1)和(6,1)将满足这个条件,如果发射,则用’R’字符代替。如果两个球彼此相距一个空间,并且相对于另一个球直接水平或垂直,则任何进入间隙之前的光束将返回到其入口端口。 在上面的图片中,进入端口(5,1)将满足这个条件,并且发射,则被替换为’R’字符。直射线(整数):直线射线定义为在从原始端口沿直线前进到退出游戏板的光束。在上面的图像中,从(7,6)或(1,6)发射的光束将被认为是直的,并且发射和接收端口将被一个整数代替,表明这是第n个直射线或偏转。 第一个直线射线或偏转将用’1’代替相应的端口,下一个用’2’代替,等等。偏转(整数):偏转被定义为以上述以外的任何方式离开游戏板的光束。在上面的图像中,任何一个直接进入球的空间的光束都会改变方向,朝着既不是球的方向也不是原来的入口的入口移动。(2,1),(1,4),(1,6),(2,7),(4,7),(7,4),(7,6)或(4, 1)将导致偏转,并且按照与直线相同的规则,发射和接收端口将被整数代替。 请注意,偏转和直线光线不会分开计算。相互作用规则光线和球之间的相互作用规则可以从上面的图像中确定,但是下面定义这些规则可以帮助您决定交互方式。 这些规则按照优先级递减的顺序显示,并在每个路径的每个方块上顺序解释,包括光线起源端口。1.当光束的路径上有一个球时,光束不会通过一个端口离开。2.当网格的对角线边缘有球时,它会阻止射线进入网格,并会反射回其进入的端口。3.当光束到达与所示光线相邻的两个球(见反射)时,光束将返回到发射射束的入口。4.如果从光束左侧或右侧立起一个球,光线将偏离球90度。为了简化分配,您应该假定对于给定的波束,这些规则中只有一个将被执行一次。一个光束的第一个相互作用将被考虑,其他任何球将被忽略的其余部分的轨迹。例如,一束通常会偏离球1并因此击中球2的光束应该忽略球2(如果适用的话,球3)并击中进入口。揣测玩家必须正确猜测棋盘上三个球的位置才能获得分数。 在任何给定的时间内,最多可以选择三个位置进行猜测。 玩家猜测输入猜测的坐标。 例如,在本文末尾的演示中会显示一个猜测(2,3)处出现球。 ‘‘字符将被放置在相应的空间中。在用户输入“提交”之前,猜测还没有最终确定。 一旦用户进入“提交”,程序必须检查是否有三次猜测。 如果为true,则用户未能找到的球的位置将标记为’0’(零)字符。 否则,游戏将不允许玩家提交。评分用户的分数将等于用户进行的移动次数,包括猜测和重复输入(输入提交将不会增加他们的分数)。 在提交三次猜测后,游戏将根据猜测的准确性确定玩家是否获得分数。 没有正确猜测所有球的玩家不会得分。 正确猜测所有球的玩家将获得一个分数。高分将在每场比赛结束后显示。 如果用户未能在以前的每场比赛中正确识别每个球的位置,则高分将是“无”。 否则,高分将成为最低分数(注意命名方案中的不匹配,仅限于街机惯例)。输入您将不需要执行输入验证:您可能会假设用户将输入逗号分隔的整数对,并且不输入空格,然后按Enter键。用户必须输入“quit”来结束程序,输入“submit”来提交他们的猜测(不区分大小写)。格式化你的游戏板应该被格式化,以便玩家可以合理地解释游戏。 将不会有测试用例检查格式:我们将手动测试您的程序以评估游戏的运行方式,但会有一些方法的自动测试,可以在不打印的情况下轻松进行评估。安置球在每场游戏中球应该随机放置。您不必为了放置这些程序而匹配特定的过程,因为测试不会指定布局。骨架纲要和要写的方法您将负责在您的程序中实施和使用以下方法。要写的构造函数方法名 描述BlackBox() 将所有整型变量设置为0的默认构造函数,初始化box[][]为大小为0的char数组,并将布尔端设置为false。BlackBox(int size, int numball,int numlink,boolean end,int score)参数化构造函数,用于将变量的值设置为作为参数传递的变量,并用行和列的大小初始化box [][]。我们将使用此方法在Vocareum上测试您的解决方案。getter和setter方法名 描述char[][] getbox() 返回box,包含’#’、’X’、球位置、用户猜想球位置的n×n数组,请注意,对于“简单”,box应该是7乘7以容纳’#’和’X’字符。int getscore() 返回当前得分,即玩家进行的移动次数。int getNumball() 返回numball,玩家猜测剩余的球数(最初为3)。int getNumlink() 返回numlink,直射线和偏转次数的计数器(最初为0)boolean getend() 返回end,一个变量表示当前游戏是否已结束(用户已提交三次猜测)int getSize() 返回游戏板的大小,这是任何一边的长度。void setbox(char box[][]) 将框设置为参数中给定的值。void setSize(int size) 将大小设置为参数中给出的值。void setNumball(int Numball) 将numball设置为参数中给出的值。void setNumlink(int Numlink) 将numlink设置为参数中给出的值void setEnd(boolean end) 将end设置为参数中给出的值。void setScore(int score) 将score设置为参数中给出的值将被写的函数方法名 描述public static void main() 主函数接受难度级别的输入,创建BlackBox实例,并调用方法initialize()和playgame()void initialize() 初始化为游戏准备了box[][]。它放置3个随机球。void playgame() palygame方法是游戏的主要控制者和输入地点。它在需要时调用check()和printbox()。void printbox() printbox方法按照示例中给出的特定格式打印矩阵。void check(int i,int j) 检查方法需要矩阵中的行和列。如果行和列与入口相对应,则检查命中,反射,偏转或直射线(调用相应的方法)。 否则,它会放置一个球。boolean placeball(int i,int j) placeball方法检查猜测球是否可以放置在给定的行和列。如果球已经存在,则返回false,否则返回true。boolean hitcheck(int i,int j) hitcheck方法检查从第i行j列投影的光束是否会导致击中。如果光束击中,返回true。否则,返回false。如果发生击中,用’H’替换’#’入口端口。boolean reflectionCheck(int i,int j) 反射检查方法检查从第i行j列投影的光束是否导致反射。如果光束以反射结束,则返回true。否则,返回false。 如果发生反射,则用’R’替换’#’入口端口。boolean deflectionCheck(int i,int j) 偏转检查方法检查从第i行,第j列投影的光束是否导致偏转。如果光束导致偏转,则返回true。否则,返回false。 如果发生偏转,则用当前值numlink替换投影和退出的入口端口以代替’#’。boolean straightRay(int i,int j) straightRay方法检查从第i行,第j列投影的光束是否直射。如果光束直射,则返回true。否则,返回false。如果发生直射,则用当前值numlink替换’#’。提交通过Blackboard提交需要的项目:BlackBox.java专栏这个任务将会有一个自动评分和一个人工评分组件。 在截止日期之后,允许对自动分级部分进行手动分级(尽管通过Vocareum获得前50分将保证这些分数)。自动分级(50分)•(5分)为每个难度级别正确初始化游戏板(测试初始化方法)•(5分)游戏只允许放置三个球(测试初始化和检查方法)•(10分)游戏正确检查输入端口的命中(测试检查方法)•(10分)游戏正确检查入口端口的反射(测试检查方法)•(10分)游戏正确跟踪直射线和偏转的数量(测试检查方法)•(10分)游戏正确计算玩家得分(测试检查方法)手动分级(50分)•(50分)游戏玩法遵循讲义中指定的准则程序演示RequirementPurdue University Problem Solving and Object-OrientedCS18000 ProgrammingProject 02: BlackBoxRelease date: 2/16Due date: 3/2 by 11:59pmGoals- Practice using two-dimensional arrays- Practice using methods to modularize code- Practice using objects to represent abstract concepts (e.g. games)IntroductionYour second project will be an implementation of the BlackBox game. For those unfamiliar withthe game, you can read more about its rules on Wikipedia or try playing the game here. Pleasereference our gameplay video, the FAQ, and the rest of this handout for an accurate account ofwhat is expected.Introduction to BlackBoxThe objective of BlackBox is to deduce the location of multiple objects (referred to henceforthas “balls”). The player projects a line from a valid location on the edge of the grid, referred to asan entry port. This line interacts with the balls on the game board based on a set of rules(defined below). Using their knowledge of these interactions, the player’s goal is to accuratelyguess the location of these balls using as few projections as possible.The BlackBox Game BoardBlackBox is played with a square, n-by-n matrix. The numberof rows and columns is set according to a difficulty level(case insensitive) chosen by the player before beginningplay. The difficulty levels are shown here on the right.The game board will have three hidden balls placed in any location. A sample of what yourinitial board should look like for an “easy” game is shown below.Difficulty DimensionsEasy 5 rows, 5 columnsMedium 7 rows, 7 columnsHard 8 rows, 8 columnsNote there are 5 rows and 5 columns of unknown space where the balls may be hiding. Each “||” potentially contains one of the balls.In this game board, the ‘#’ characters are the entry ports the user will select to fire from. Theline will follow a trajectory beginning at that ‘#’ character toward the ‘#’ character on the otherside, subject to the rules of interaction defined for balls and lines. Upon reaching any side of thegame board, the firing entry port and the receiving entry port can take on any one of thefollowing states:• Hit (H): A hit is defined as a beam that does not leave the game board, which can onlyhappen if a beam intersects a space containing a ball.Note that the balls are visible only for the purpose of this demonstration. In thisdiagram, firing from any of the (1,5), (3,1),(3,7) or (7,5) ports will result in a hit. Uponreaching the ball, the firing entry port will be replaced with an ‘H’ character.• Reflection (R): A reflection is defined as a beam that exits the game board through thesame entry port that fired it. There are two scenarios in which a reflection may occur.If a ball is located next to the axis of the firing entry port (no space between the axis and theball) and is located in a space 45 degrees from the entry point. In the image above, entry ports(4,1) and (6,1) would satisfy this condition and be replaced by an ‘R’ character if fired from.If two balls are one space away from each other and are either directly horizontal or verticalwith respect to the other, any beam that enters the space immediately before the gap will bereturned to its entry port. In the image above, entry port (5,1) would satisfy this condition andbe replaced by an ‘R’ character if fired from.• Straight ray (integer): A straight ray is defined as a beam that exits the game board afterfollowing a straight line from the original port.In the image above, either beam fired from (7,6) or (1,6) would be considered straightand both the firing and receiving ports would be replaced by an integer showing thatthis was the nth straight ray or deflection. The first straight ray or deflection wouldreplace the corresponding ports with a ‘1’, the next with a ‘2’, and so on.• Deflection (integer): A deflection is defined as a beam that exits the game board in anymanner other than those mentioned above.In the image above, any beam that enter the space directly diagonal to a ball willchange direction and move toward the entry port that is neither in the direction of theball nor the original entry port. Any beam fired from (2,1), (1,4), (1,6), (2,7), (4,7), (7,4),(7,6), or (4,1) will result in a deflection and the firing and receiving ports will be replacedby an integer following the same rules for the straight line. Note Java代写二维数组Project BlackBox代写Java编程、Java编程作业帮做that deflections andstraight rays are not counted separately.Beam Rules of InteractionThe rules of interaction between beams and balls can be determined from the above images,but to help you determine the interactions these rules are defined below. These rules aredisplayed in order of decreasing precedence, interpreted sequentially at each square in theeach’s path including the port where the beam originates.1. When there is a ball on the beam’s path, the beam never exits through a port.2. When there is a ball at the diagonal edge of the grid it prevents a ray from entering thegrid and will be reflected back to the port from which it entered.3. When the beam reaches two balls adjacent to the ray shown (see Reflection) in thebeam will return to the entry port from which it was fired.4. When from the left or from the right side in front of the beam stands a ball, the beamwill be deflected by 90 degrees away from the ball.To simplify the assignment, you should assume that only one of these rules will be enactedonce for a given beam. A beam’s first interaction will be considered, and any other balls willbe ignored for the rest of its trajectory. For example, a beam that would normally deflect offBall 1 and consequently hit Ball 2 should ignore Ball 2 (and Ball 3, if applicable) and hit an entryport.GuessingThe player must correctly guess the location of the three balls on the board in order to receivea score. Up to three locations may be selected for guesses at any given time in play. A playerguesses by inputting the coordinates of the guess. For example, a player guessing that a ballexists at (2,3) is shown in the demonstration at the end of this document. An ‘’ character willbe placed in the corresponding space.Guesses are not finalized until the user enters “submit”. Once the user enters “submit”, theprogram must check if three guesses have been made. If true, the location of the balls the userfailed to find will be marked with a ‘0’ (zero) character. Otherwise, the game will not allow theplayer to submit.ScoringThe user’s score will be equal to the number of moves made by the user, including guesses andrepeated entries (entering submit will not add to their score). After submitting with threeguesses made, the game will determine whether the player receives a score based on theaccuracy of their guessing. Players that did not guess all the balls correctly will not receive ascore. Players that do guess all the balls correctly will receive a score.The high score will be displayed after the completion of each game. If the user failed tocorrectly identify the location of each ball in each of the previous games, the high score will be“none”. Otherwise, the high score will be the lowest score achieved (note the mismatch innaming schemes, just following arcade conventions).InputYou will not be required to perform. input validation: you may assume the user will enter acomma separated integer pair with no spaces followed by pressing the enter key.The user must be able to enter “quit” to end the program and “submit” to submit their guesses(case insensitive).FormattingYour game board should be formatted such that a player can reasonably interpret the game.There will be no test cases examining formatting: we will test your program manually forevaluating how the game plays, but there will be some automatic testing of methods that canbe easily evaluated without printing.Ball PlacementBalls should be placed randomly for each playthrough. You do not have to match a specificprocedure for placing these, as the tests will not specify layouts.Skeleton Outline and Methods to Be WrittenYou will be responsible for the implementation and usage of the following methods in yourprogram.Constructors to be writtenMethod Name DescriptionBlackBox() The default constructor to set all the integervariables to 0, initialize box[][] to a char arrayof size 0 and to set the Boolean end to false.BlackBox(int size, int numball,intnumlink,boolean end,int score)The parameterized constructor to set thevalue of the variables to variables passed asparameters and to initialize the box[][] withsize rows and columns. We will use thismethod to test your solution on Vocareum.Getters and setters to be writtenMethod Name Descriptionchar[][] getbox() Returns box, an n-by-n array containing the‘#’ and ‘X’ characters, the location of theballs, and the location of the player guesses.Note that for “easy”, box should be 7-by-7 toaccommodate the ‘#’ and ‘X’ characters.int getscore()Returns the current score, the number ofmoves the player has made.int getNumball()Returns numball, the number of ballsremaining for the player to guess (initially 3).int getNumlink()Returns numlink, the counter for the numberof straight rays and deflections (initially 0)boolean getend()Returns end, a variable signaling whether thecurrent game has ended (user has submittedwith three guesses)int getSize() Returns the size of the game board, which isthe length of either side.void setbox(char box[][])Sets the box to the value given in theparameter.void setSize(int size)Sets the size to the value given in theparameter.void setNumball(int Numball)Sets the numball to the value given in theparameter.void setNumlink(int Numlink)Sets the numlink to the value given in theparameter.void setEnd(boolean end)Sets the end to the value given in theparameter.void setScore(int score)Sets the score to the value given in theparameter.Functions to be writtenMethod Name Descriptionpublic static void main() The main function takes input for thedifficulty level, creates an instance ofBlackBox, and calls the methods initialize()and playgame()void initialize() The initialize prepares the box[][] for thegame. It Places 3 random balls.void playgame() The playgame method is the main controllerand input place for the game. It calls check()and printbox() when needed.void printbox() The printbox method prints out the matrix ina particular format as given in the example.void check(int i,int j) The check method takes in the row andcolumn in the matrix. If the row and columncorresponds to an entry port, it checks for ahit, reflection, deflection or straight ray(calling the corresponding methods).Otherwise, it places a ball.boolean placeball(int i,int j) The placeball method checks if a guess ballcan be placed at a given row and column.Return false if a ball is already there, else itreturns true.boolean hitcheck(int i,int j) The hitcheck method checks if a beamprojected from from row i, column j results ina hit. Return true if the beam ends in a hit.Otherwise, return false. If it is a hit occurs,replace the projecting entry port with an ‘H’in place of ‘#’.boolean reflectionCheck(int i,int j) The reflectioncheck method checks if a beamprojected from from row i, column j results ina reflection. Return true if the beam ends in areflection. Otherwise, return false. If it is areflection occurs, replace the projecting entryport with an ‘R’ in place of ‘#’.boolean deflectionCheck(int i,int j) The deflectioncheck method checks if a beamprojected from from row i, column j results ina deflection. Return true if the beam resultsin a deflection. Otherwise, return false. If it isa deflection occurs, replace the projectingand exiting entry ports with the current valueof numlink in place of ‘#’.boolean straightRay(int i,int j) The straightRay method checks if a beamprojected from from row i, column j results ina straightRay. Return true if the beam resultsin a straightRay. Otherwise, return false. If itis a straightRay occurs, replace the projectingand exiting entry ports with the current valueof numlink in place of ‘#’.SubmissionItems required for submission via Blackboard:• BlackBox.javaRubricThere will be an automatic grading and a manual grading component for this assignment.Manual grading for automatically graded portions will be allowed after the deadline (althoughreceiving the first 50 points through Vocareum will guarantee those points).Automatically Graded (50 points)• (5 points) Game board is properly initialized for each difficulty level (testing the initializemethod)• (5 points) Game allows the placement of only three balls (testing the initialize and checkmethods)• (10 points) Game properly checks entry ports for hits (testing the check method)• (10 points) Game properly checks entry ports for reflections (testing the check method)• (10 points) Game properly tracks the number of straight rays and deflections (testingthe check method)• (10 points) Game properly calculates player score (testing the check method)Manually Graded (50 points)• (50 points) Gameplay follows guidelines specified here in the handout.Program Demonstration& 转自:http://ass.3daixie.com/2018052516413972.html

你可能感兴趣的:(讲解:JavaProject BlackBoxJava、Java)