- Test-Driven Java Development(Second Edition)
- Alex Garcia Viktor Farcic
- 146字
- 2021-06-24 18:31:53
Implementation
To implement the last test, we should store the location of the placed pieces in an array. Every time a new piece is placed, we should verify that the place is not occupied, or else throw an exception:
private Character[][] board = {
{'\0', '\0', '\0'}, {'\0', '\0', '\0'},
{'\0', '\0', '\0'}
}; public void play(int x, int y) { if (x < 1 || x > 3) { throw new RuntimeException("X is outside board"); } else if (y < 1 || y > 3) { throw new RuntimeException("Y is outside board"); } if (board[x - 1][y - 1] != '\0') { throw new RuntimeException("Box is occupied"); } else { board[x - 1][y - 1] = 'X'; } }
We're checking whether a place that was played is occupied and, if it is not, we're changing the array entry value from empty (\0) to occupied (X). Keep in mind that we're still not storing who played (X or O).
推薦閱讀
- The Android Game Developer's Handbook
- 軟件界面交互設計基礎
- AngularJS Web Application Development Blueprints
- Python機器學習算法與實戰
- 軟件測試技術指南
- 零基礎學單片機C語言程序設計
- Mastering JavaScript High Performance
- 劍指大數據:企業級數據倉庫項目實戰(在線教育版)
- 編程改變生活:用Python提升你的能力(進階篇·微課視頻版)
- Learning ECMAScript 6
- Python無監督學習
- Scratch編程從入門到精通
- VMware vSphere 5.5 Cookbook
- Getting Started with Windows Server Security
- Building Scalable Apps with Redis and Node.js