- 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).
推薦閱讀
- AngularJS Testing Cookbook
- MySQL 8從入門到精通(視頻教學(xué)版)
- 信息可視化的藝術(shù):信息可視化在英國
- Java從入門到精通(第4版)
- GitLab Repository Management
- 編寫高質(zhì)量代碼:改善C程序代碼的125個建議
- Unity 5.x By Example
- 微信小程序入門指南
- RISC-V體系結(jié)構(gòu)編程與實(shí)踐(第2版)
- iOS開發(fā)實(shí)戰(zhàn):從入門到上架App Store(第2版) (移動開發(fā)叢書)
- Mobile Device Exploitation Cookbook
- Extreme C
- Learning AWS
- ExtJS Web應(yīng)用程序開發(fā)指南第2版
- Go語言從入門到精通