DOS版五子棋

本文介绍了一个使用Java实现的五子棋游戏程序。该程序通过数组来表示棋盘,并实现了棋子放置、显示棋盘状态等功能。特别关注了游戏胜负判断算法的设计,确保能够正确判断出何时一方获胜。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在搞五子棋,使用数组实现的,初始化,数组的修改(即放置棋子),功能都很快实现,但是这个输赢的算法纠结了好久,到现在才有点头绪,之前的思路走到一半发现不对,因为是数组,所以经常出现数组越界的错误。等我的五子棋做好了,贴上来,保存下,毕竟是自己的心血。

等写好了会上传,争取今天下午搞定!

怎么说呢,可能自己太笨了,算法始终没有想出来,只好借鉴了下本站的高手的算法,总算是完成了这个游戏程序,代码如下:

 

 

import java.util.Scanner;

 

public class FiveChessGame {

public static void main(String[] args) {

char table[][] = new char[17][17];

initialTable(table);

printTable(table);

setChess(table);

}

 

public static void initialTable(char table[][]) {

// 初始化棋盘

char firstline[] = { ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8',

'9', 'a', 'b', 'c', 'd', 'e', 'f' };

table[0][0] = ' ';

for (int i = 1; i < 17; i++) {

table[i][0] = firstline[i];

for (int j = 1; j < 17; j++) {

table[0][i] = firstline[i];

table[i][j] = '+';

}

 

}

}

 

public static void printTable(char table[][]) {

System.out.println("FiveChessGame" + "       " + "Version1.0");

System.out.println(" by DavidSoft@AHUT");

for (int i = 0; i < table.length; i++) {

for (int j = 0; j < table[i].length; j++) {

System.out.print(table[i][j] + " ");

}

System.out.println();

}

}

 

public static void setChess(char table[][]) {

Scanner sc = new Scanner(System.in);

for (int i = 1;; i++) {

boolean black = true;

if (i % 2 == 0) {

System.out.println("请白方放子:");

black = false;

} else {

System.out.println("请黑方放子:");

black = true;

}

 

String s = sc.next();

char c1 = s.charAt(0);

char c2 = ' ';

try {

c2 = s.charAt(1);

} catch (java.lang.StringIndexOutOfBoundsException e) {

System.out.println("请输入两个位置");

i--;

continue;

}

// 将c1和c2转换成对应的棋盘位置位置

short x = (short) (c1 - 48);

short y = (short) (c2 - 48);

if (c1 <= 102 && c1 >= 97) {

x = (short) (c1 - 87);

}

if (c2 >= 97 && c2 <= 102) {

y = (short) (c2 - 87);

}

// a: 97 a如果相对应10那么a的ascii-87

// 0: 48

 

// 判断坐标是否超出范围

if (x < 0 || x > 15 || y > 15 || y < 0) {

System.out.println("超出棋盘范围,请重新输入:");

continue;

}

if (table[x + 1][y + 1] != '+') {

System.out.println("此处已经有子,请重新输入:");

i--;

continue;

}

if (black) {

table[x + 1][y + 1] = '@';

if (judgeWin(table, x + 1, y + 1)) {

printTable(table);

System.out.println("黑方胜");

break;

}

} else {

table[x + 1][y + 1] = 'O';

if (judgeWin(table, x + 1, y + 1)) {

printTable(table);

System.out.println("白方胜");

break;

}

}

printTable(table);

}

}

 

public static boolean judgeWin(char table[][], int x, int y) {

boolean win = false;

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

if (judgeWinLine(i, table, x, y)) {

win = true;

break;

}

}

 

return win;

}

 

public static boolean judgeWinLine(int direction, char table[][], int x,

int y) {

int count = 1;

int posx = 0, posy = 0;

boolean win = false;

switch (direction) {

case 0:

posx = 1;

posy = 0;

break;

case 1:

posx = 0;

posy = 1;

break;

case 2:

posx = 1;

posy = 1;

break;

case 3:

posx = 1;

posy = -1;

break;

}

for (int i = 1; i <= 4; i++) {

if (x + i * posx < 0 && x + i * posx > 1 && y + i * posy < 0

&& y + i * posy > 16)

break;

if (table[x + i * posx][y + i * posy] == table[x][y])

count++;

else

break;

}

for (int i = -1; i >= -4; i--) {

if (x + i * posx < 0 && x + i * posx > 16 && y + i * posy < 0

&& y + i * posy > 16)

break;

if (table[x + i * posx][y + i * posy] == table[x][y])

count++;

else

break;

}

if (count == 5)

win = true;

return win;

}

}

 

 

运行结果为:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值