- Test-Driven Java Development(Second Edition)
- Alex Garcia Viktor Farcic
- 136字
- 2021-06-24 18:31:55
Implementation
To fulfill this test, we need to check whether any horizontal line is filled by the same mark as the current player. Until this moment, we didn't care what was put on the board array. Now, we need to introduce not only which board boxes are empty, but also which player played them:
public String play(int x, int y) { checkAxis(x); checkAxis(y); lastPlayer = nextPlayer(); setBox(x, y, lastPlayer); for (int index = 0; index < 3; index++) { if (board[0][index] == lastPlayer
&& board[1][index] == lastPlayer
&& board[2][index] == lastPlayer) { return lastPlayer + " is the winner"; } } return "No winner"; }
private void setBox(int x, int y, char lastPlayer) { if (board[x - 1][y - 1] != '\0') { throw new RuntimeException("Box is occupied"); } else { board[x - 1][y - 1] = lastPlayer; } }
推薦閱讀
- Oracle WebLogic Server 12c:First Look
- C語言程序設計實踐教程(第2版)
- 深入淺出Prometheus:原理、應用、源碼與拓展詳解
- Python漫游數學王國:高等數學、線性代數、數理統計及運籌學
- 算法訓練營:提高篇(全彩版)
- Learning Zurb Foundation
- 深入分布式緩存:從原理到實踐
- 蘋果的產品設計之道:創建優秀產品、服務和用戶體驗的七個原則
- Go語言開發實戰(慕課版)
- Building Serverless Architectures
- Learning JavaScript Data Structures and Algorithms(Second Edition)
- Python數據可視化之美:專業圖表繪制指南(全彩)
- Python青少年趣味編程
- Xamarin Cross-Platform Development Cookbook
- Python 快速入門(第3版)