2014-04-24 23:28

题目:玩象棋游戏,你要设计一个bool型的方法来检测一个棋子能否移动到指定位置。

解法:不同的棋子有不同的移动规则,那么应该采取棋子基类实现接口,各个棋子子类来实现的方式。棋子除了类型之外,还必须要记录当前位置,作为判断的依据。而要移到的位置,则作为参数传给方法。不过这个题目出现在软件测试的章节里,应该是要描述如何测试吧。没学过系统的测试方法,遇到这种题的话还真没办法。

代码:

 // 12.3 You're testing a method canMoveTo(int x, int y), which is a member method of a class Piece. Piece represents a piece in the chess game. How would you perform the testing?
// Answer:
// 1. Apparently every kind of pieces in chess has its own rule of moving. So the class Piece must be an abstract class, which will inherited by various kinds of specific pieces.
// 2. You always have to check if (x, y) is out of border. So this will be included in base class Piece.
// 3. Every piece has its own rule of moving, so the derived class will have to implement its own rule(), which will be executed by canMoveTo().
// 4. The class Piece should look like this:
// 5. The base class will have an interface named rule(), which will be implemented by derived class.
class Piece {
public:
virtual bool canMoveTo(int x, int y);
private:
// ...
virtual bool rule(int x, int y) = ;
};
// 6. Thus, the test should cover two parts:
// 6.a. The coordinates (x, y) will cover in-bound and out-of-bound cases.
// 6.b. Every rule() function will be executed inside canMoveTo(), thus the test will also be included here.

《Cracking the Coding Interview》——第12章:测试——题目3的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  6. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  7. 《Cracking the Coding Interview》——第12章:测试——题目6

    2014-04-25 00:53 题目:你要如何测试一个分布式银行系统的ATM机? 解法:ATM是Automatic Teller Machine,取钱的.我想了半天,没找到什么很清晰的思路,也许是因 ...

  8. 《Cracking the Coding Interview》——第12章:测试——题目5·

    2014-04-25 00:41 题目:怎么测试一支笔?(Pen?您老说的是钢笔?) 解法:这种简约而不简单的题目,实在是面试官最喜欢,面试者最头疼的类型了.面试官可以只花三秒,以一种灰常高贵冷艳的语 ...

  9. 《Cracking the Coding Interview》——第12章:测试——题目4

    2014-04-25 00:35 题目:没有专门的测试工具,你要如何对一个网页进行压力测试? 解法:拼手速,拼电脑数量呗.快捷键+复制粘贴网址,狂搞一番.话说回来,有脚本语言的情况下,直接写个脚本来模 ...

随机推荐

  1. failed: No module named 'catkin_pkg' Make sure that you have installed "catkin_pkg"

    https://stackoverflow.com/questions/43024337/why-this-error-when-i-try-to-create-workspaces-in-ros# ...

  2. Radix Sort

    为了完成二维数据快速分类,最先使用的是hash分类. 前几天我突然想,既然基数排序的时间复杂度也不高,而且可能比hash分类更稳定,所以不妨试一下. 在实现上我依次实现: 1.一维数组基数排序 基本解 ...

  3. POJ-1469 COURSES---二分图最大匹配--匈牙利算法

    题目链接: https://vjudge.net/problem/POJ-1469 题目大意: 给你p门课程和n个学生,一个学生可以选0门,1门,或者多门课程,现在要求一个由p个学生组成的集合,满足下 ...

  4. 后缀数组入门(二)——Height数组与LCP

    前言 看这篇博客前,先去了解一下后缀数组的基本操作吧:后缀数组入门(一)--后缀排序. 这篇博客的内容,主要建立于后缀排序的基础之上,进一步研究一个\(Height\)数组以及如何求\(LCP\). ...

  5. 转:SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)

    转:https://www.cnblogs.com/zyw-205520/p/4771253.html 1.基本概念   1.1.Spring Spring是一个开源框架,Spring是于2003 年 ...

  6. .nettiers和SQLite搅合到一块之后遇到的问题

    第一步 用SQLiteStudio生成一个新的数据库,sqlitetest,新建一张表test,建立一个主键字段ID,一个字符字段Name,建立完成,留待后用. 第二步 用VS2010建立一个sqli ...

  7. python模块之正则

    re模块 可以读懂你写的正则表达式 根据你写的表达式去执行任务 用re去操作正则 正则表达式 使用一些规则来检测一些字符串是否符合个人要求,从一段字符串中找到符合要求的内容.在线测试网站:http:/ ...

  8. Excle 常用函数

    1. 查找函数VLOOKUP 使用示例: =VLOOKUP(A1,$C$1:$D$19,2,0) 表示以A1单元格为基准,匹配C1开始到D19的范围数据,在公式行显示D单元格文本,也就是匹配范围的第二 ...

  9. ES6初识-Proxy和Reflect

    { let obj={ time:'2017-03-11', name:'net', _r:123 };   let monitor=new Proxy(obj,{ // 拦截对象属性的读取 get( ...

  10. Qt.5.9.6移植

    工具及软件包 交叉编译工具链 arm-2014.05-29-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2 软件包 dbus-1.10.0.tar.g ...