Giter VIP home page Giter VIP logo

guess-number-raypeng's Introduction

总览

该Workshop部分会采用TDD的方式进行开发,但是TDD并不是本活动的重点,重点是如何写好单元测试。 写好单元测试与TDD相辅相成,很难完全分离。同样的题目也可以用于TDD的练习,但是更侧重于TDD的细节,且时间要更长。

用TDD(Test Driven Development)开发一个简单的猜数字游戏,基本业务逻辑如下:

  • 游戏开始后,系统会随机给出一个四位,每位都不重复的数字作为答案。由用户输入自己猜测的四个数字。
  • 系统会将两个数字进行对比,并给形出xAxB的提示, 比如"2A1B"。
  • 如果数字猜对而且位置也对,就是一个A。
  • 如果数字猜对但位置不对,就是一个B。

例如:

  • 系统给出"1234",用户输入"1234"
  • 返回"4A0B"
  • 系统给出"1234",用户输入"4321"
  • 返回"0A4B"

第一问 xAxB

写一个CompareNumber类,只有一个函数,该函数接受两个参数,一个是答案,一个是用户输入的四位数。返回值是xAxB的字符串 这里我们假定答案和用户输入都是合法的四位数。不用考虑数字合法性问题。 请对这个类写测试(思考要写几个测试)

第二问 随机数生成

写一个AnswerGenerator类,只有一个函数,返回一个四位,每位都不重复随机数。 请对这个类写测试。(思考测试哪些)

第三问

写一个Guess类,还是只有一个函数,但是只有一个参数。把前两问做的类集成起来,写一个集成的单元测试,写一个集成测试。

第四问

完成整个游戏。整个游戏是以命令行方式进行的。每回游戏有六次机会。每输入一次数字就会减少一次机会并打印xAxB。

  • 当游戏开始时,打印“Welcome!”空一行打印 "Please input your number(6): "
  • 每次输入完并敲击回车,就会在下面打印出xAxB,然后空一行打印出新的"Please input your number(x): "
  • 当输入的数字包含重复数字并回车时,在下面打印"Cannot input duplicate numbers!"
  • 当6次都没有猜中的时候,打印"Game Over"并退出
  • 当猜中的时候,不要打印4A0B,而是打印"Congratulations!"并退出

guess-number-raypeng's People

Contributors

raypeng avatar jtong avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.