Java奥赛罗游戏_用面向对象思想设计奥赛罗游戏

这是一个Java实现的奥赛罗棋盘游戏,通过面向对象设计,包括初始化棋盘、判断游戏结束条件、检查是否可以进行有效移动、获取玩家合法移动等核心功能。游戏将持续进行直到某方获胜或平局。
摘要由CSDN通过智能技术生成

public class Question {

private final int white = 1;

private final int black = 2;

private int[][]  board;

/* Sets up the board in the standard othello starting positions,

* and starts the game */

public void start () { ... }

/* Returns the winner, if any. If there are no winners, returns

* 0 */

private int won() {

if (!canGo (white) && !canGo (black)) {

int count = 0;

for (int i = 0; i < 8; i++) {

for (int j = 0; j < 8; j++) {

if (board [i] [j] == white) {

count++;

}

if (board [i] [j] == black) {

count--;

}

}

}

if (count > 0) return white;

if (count < 0) return black;

return 3;

}

return 0;

}

/* Returns whether the player of the specified color has a valid

* move in his turn. This will return false when

* 1. none of his pieces are present

* 2. none of his moves result in him gaining new pieces

* 3. the board is filled up

*/

private boolean canGo(int color) { ... }

/* Returns if a move at coordinate (x,y) is a valid move for the

* specified player */

private boolean isValid(int color, int x, int y) { ... }

/* Prompts the player for a move and the coordinates for the move.

* Throws an exception if the input is not valid or if the entered

* coordinates do not make a valid move. */

private void getMove (int color) throws Exception { ... }

/* Adds the move onto the board, and the pieces gained from that

* move. Assumes the move is valid. */

private  void add (int x, int y, int  color) { ... }

/* The actual game: runs continuously until a player wins */

private void game() {

printBoard();

while (won() == 0) {

boolean valid = false;

while (!valid) {

try {

getMove(black);

valid = true;

} catch (Exception e) {

System.out.println (“Enter a valid coordinate!”);

}

}

valid = false;

printBoard();

while (!valid) {

try {

getMove(white);

valid = true;

} catch (Exception e) {

System.out.println (“Enter a valid coordinate!”);

}

}

printBoard ();

}

if (won()!=3) {

System.out.println (won () == 1 ? “white” : “black” +

“ won!”);

} else {

System.out.println(“It’s a draw!”);

}

}

}

参考:http://wenku.baidu.com/view/47eda066f8c75fbfc67db27f.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值