通过手动输入坐标落子。
package Fivespot;
public class ChessBoard {
char [][] board;
int n;
ChessBoard(int n){
this.n = n;
board = new char[n][n];
}
public void setBoard() {
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
board[i][j] = '+';
}
}
}
public void setBlack(int x,int y) {
board[x][y] = '●';
}
public void setWhite(int x,int y) {
board[x][y] = '○';
}
public void Print() {
System.out.println("ok");
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
System.out.print(board[i][j]);
if(j==n-1)
System.out.println();
}
}
}
public boolean Judge(int x, int y) {
int wl = 0;//白子水平方向上五连贯判断值
int wh = 0;//白子竖直方向上五连贯判断值
int wld = 0;//白子主对角线上五连贯判断值
int wcd = 0;//白子副对角线上五连贯判断值
int bl = 0;//黑子水平方向上五连贯判断值
int bh = 0;//黑子竖直方向上五连贯判断值
int bld = 0;//黑子主对角线上五连贯判断值
int bcd = 0;//黑子副对角线上五连贯判断值
int lnx = x;//主对角线
int lny = y;//主对角线
int cnx = x;//副对角线
int cny = y;//副对角线
if(board[x][y]=='○') {//判白子;
//判断水平和竖直方向上的棋子;
for(int i=0;i<n;i++) {
if(i+1>=n) break;
if(board[x][i]=='○'&&board[x][i+1]=='○') {//水平
wl = wl+1;
if(wl==4) break;
}
if(board[i][y]=='○'&&board[i+1][y]=='○') {//竖直
wh = wh+1;
if(wh==4) break;
}
}
//判断主对角线上的棋子
if(x+y<=2*x) {
if(x+y<n) {
while(lny!=0) {
lnx = lnx-1;
lny = lny-1;
cnx = cnx+1;
}
cny = cnx;
cnx = 0;
for(;lnx<n;lnx++) {
if(lnx+1>=n||lny+1>=n) break;
if(board[lnx][lny]=='○'&&board[lnx+1][lny+1]=='○') {
wld = wld+1;
}
lny = lny+1;
}
for(;cny>=0;cny--) {
if(cnx+1>=n||cny-1<0) break;
if(board[cnx][cny]=='○'&&board[cnx+1][cny-1]=='○') {
wcd = wcd+1;
}
cnx = cnx+1;
}
}
else {
while(lny!=0) {
lnx = lnx-1;
lny = lny-1;
}
while(cnx!=n-1) {
cnx = cnx+1;
cny = cny-1;
}
for(;lnx<n;lnx++) {
if(lnx+1>=n||lny+1>=n) break;
if(board[lnx][lny]=='○'&&board[lnx+1][lny+1]=='○') {
wld = wld+1;
}
lny=lny+1;
}
for(;cny<n;cny++) {
if(cnx-1<0||cny+1>=n) break;
if(board[cnx][cny]=='○'&&board[cnx-1][cny+1]=='○') {
wcd = wcd+1;
}
cnx=cnx-1;
}
}
}
}
if(board[x][y]=='●') {//判黑子;
//判断水平和竖直方向上的棋子;
for(int i=0;i<n;i++) {
if(i+1>=n) break;
if(board[x][i]=='●'&&board[x][i+1]=='●') {//水平
bl = bl+1;
if(bl==4) break;
}
if(board[i][y]=='●'&&board[i+1][y]=='●') {//竖直
bh = bh+1;
}
}
//判断主对角线上和副对角线上的棋子
if(x+y<=2*x) {
if(x+y<n) {
while(lny!=0) {
lnx = lnx-1;
lny = lny-1;
cnx = cnx+1;
cny = cny-1;
}
for(;lnx<n;lnx++) {
if(lnx+1>=n||lny+1>=n) break;
if(board[lnx][lny]=='●'&&board[lnx+1][lny+1]=='●') {
bld = bld+1;
}
lny = lny+1;
}
for(;cnx>=0;cnx--) {
if(cnx-1<0||cny+1>=n) break;
if(board[cnx][cny]=='●'&&board[cnx-1][cny+1]=='●') {
bcd = bcd+1;
}
cny = cny+1;
}
}
else {
while(lny!=0) {
lnx = lnx-1;
lny = lny-1;
}
while(cnx!=n-1) {
cnx = cnx+1;
cny = cny-1;
}
for(;lnx<n;lnx++) {
if(lnx+1>=n||lny+1>=n) break;
if(board[lnx][lny]=='●'&&board[lnx+1][lny+1]=='●') {
bld = bld+1;
}
lny=lny+1;
}
for(;cny<n;cny++) {
if(cnx-1<0||cny+1>=n)break;
if(board[cnx][cny]=='●'&&board[cnx-1][cny+1]=='●') {
bcd = bcd+1;
}
cnx=cnx-1;
}
}
}
else {
if(x+y<n) {
while(lnx!=0) {
lnx = lnx-1;
lny = lny-1;
cnx = cnx-1;
cny = cny+1;
}
for(;lny<n;lny++) {
if(board[lnx][lny]=='●'&&board[lnx+1][lny+1]=='●') {
bld = bld+1;
}
lnx = lnx+1;
}
for(;cny>=0;cny--) {
if(board[cnx][cny]=='●'&&board[cnx+1][cny-1]=='●') {
bcd = bcd+1;
}
cnx = cnx+1;
}
}
else {
while(lnx!=0) {
lnx = lnx-1;
lny = lny-1;
}
while(cnx!=n-1) {
cnx = cnx+1;
cny = cny-1;
}
for(;lny<n;lny++) {
if(board[lnx][lny]=='●'&&board[lnx+1][lny+1]=='●') {
bld = bld+1;
}
lnx = lnx+1;
}
for(;cny<n;cny++) {
if(board[cnx][cny]=='●'&&board[cnx-1][cny+1]=='●') {
bcd = bcd+1;
}
cnx = cnx+1;
}
}
}
}
// System.out.println(wl+" "+wh+" "+wld+" "+wcd);
if((wl>=4)||(wh>=4)||(wld>=4)||(wcd>=4)) {
System.out.println("白胜");
return true;
}
else if((bl>=4)||(bh>=4)||(bld>=4)||(bcd>=4)) {
System.out.println("黑胜");
return true;
}
else
return false;
}
}
实现package Fivespot;
import java.util.*;
public class Realized {
public static void main(String args[]) {
ChessBoard chess = new ChessBoard(9);
chess.setBoard();
chess.Print();
int i=1;//i为0时白子先,i为1时,黑子先
while(true) {
System.out.println("输入您的落棋坐标:");
int x = new Scanner(System.in).nextInt();
int y = new Scanner(System.in).nextInt();
// System.out.println(x+"ok"+y);
if(x==-1&&y==-1)
break;
i = i+1;
if(i%2!=0) {
chess.setWhite(x, y);
}
else{
chess.setBlack(x, y);
}
chess.Print();
if(i>=9) {
// System.out.println("iok");
if(chess.Judge(x, y)) {
// System.out.println("Jok");
break;
}
}
}
}
}