设计一个五子棋,实现基本的落子和判断输赢功能
- 初始化一个棋盘,并创建输出棋盘方法
- 选择谁先落子
- 创建落白子和黑子的方法
- 判断输入的棋子坐标是否超出棋盘或此处已有棋子
- 每次落完子输出棋盘并且判断输赢
判断输赢基本思路:
- 遍历每一个点,从每个点出发从横竖斜三个方向判断棋子是否有连续的五个(代码简洁但用时较多)
- 以刚刚落的棋子出发,从横竖斜三个方向判断棋子是否有连续的五个(代码繁琐但用时较少)这里采用后者
代码实现:
import java.util.Scanner;
public class Renju {//创建棋盘类
private Scanner scan = new Scanner(System.in);
private char[][] qp = new char[15][15];
private char[] num = new char[]{'⒈', '⒉', '⒊', '⒋', '⒌', '⒍', '⒎', '⒏', '⒐', '⒑', '⒒', '⒓', '⒔', '⒕', '⒖'};
public Renju() {//初始化棋盘
for (int i = 0; i < qp.length-1; i++) {
for (int j = 0; j < qp[i].length - 1; j++) {
qp[i][j] = '十';
}
qp[i][14] = num[i];
}
for (int i = 0; i < qp.length ; i++)
qp[14][i] = num[i];
System.out.println("开始下棋");
}
public void printQp() {//输出棋盘
for (int i = 0; i < qp.length; i++) {
for (int j = 0; j < qp[i].length; j++)
System.out.print(qp[i][j] + " ");
System.out.println();
}
}
public boolean whoFirst() {//判断谁先开始下棋
System.out.println("请选择谁先开始:1.黑 2.白");
if (scan.nextInt() == 1)
return true;
else
return false;
}
public boolean incomeBlankC() {//下黑子
while (true) {
System.out.print("请输入黑棋坐标:");
int bcy = scan.nextInt();
int bcx = scan.nextInt();
if (memoryCoordinates(bcx, bcy)) {
qp[bcy - 1][bcx - 1] = '★';
printQp();
if (AcrossWin('★',bcy-1,bcx-1))
return true;
break;
}
}
return false;
}
public boolean incomeWhiteC() {//下白子
while (true) {
System.out.print("请输入白棋坐标:");
int wcy = scan.nextInt();
int wcx = scan.nextInt();
if (memoryCoordinates(wcx, wcy)) {
qp[wcy - 1][wcx - 1] = '☆';//落白子
printQp();//输出棋盘
if (AcrossWin('☆',wcy-1,wcx-1))//判断输赢
return true;//若赢则返回true
break;
}
}
return false;
}
public boolean memoryCoordinates(int x, int y) {//判断输入的位置有无落子或超出棋盘
if (x < 1 || y > 14) {//判断是否超出棋盘
System.out.println("坐标错误,请重新输入");
return false;
} else {
if (qp[y - 1][x - 1] == '十') {//判断此位置是否已经落子
return true;
} else {
System.out.println("此处已有棋子,请重新选择位置");
return false;
}
}
}
public boolean AcrossWin(char qz,int y,int x) {//判断输赢
int kh=0,kl=0,kx1=0,kx2=0,kx3=0,kx4=0;
for(int j=0;j<qp.length-1;j++){
if(qz==qp[y][j]) {//横方向
kh++;
if(kh==5) return true;//若够五个,则返回true
}
else kh=0;
if(qz==qp[j][x]){//竖方向
kl++;
if(kl==5)return true;
}
else kl=0;
if(Math.abs(y-x)+j<qp.length&&qz==qp[Math.abs(y-x)+j][j]){//主对角及以上区域
kx1++;
if(kx1==5) return true;
} else kx1=0;
if(Math.abs(y-x)+j< qp.length&&qz==qp[j][Math.abs(y-x)+j]){//主对角以下区域
kx2++;
if(kx2==5) return true;
}else kx2=0;
if(x+y<qp.length&&x+y>=j&&qz==qp[j][x+y-j]){//次对角及以上区域
kx3++;
if(kx3==5) return true;
}else kx3=0;
if(x+y- (qp.length-2)+j< qp.length&&x+y>qp.length&&qz==qp[x+y- (qp.length-2)+j][qp.length-2-j]){//次对角以下区域
kx4++;
if(kx4==5) return true;
}else kx4=0;
}
return false;
}
}
主程序:
public class RenjuFive {
public static void main(String[] args) {
Renju rj = new Renju();
rj.printQp();
if(rj.whoFirst()){//判断谁先落子
while(true) {
if(rj.incomeBlankC()) {
System.out.println("黑棋胜");
break;
}
if(rj.incomeWhiteC()) {
System.out.println("白棋胜");
break;
}
}
}
else{
while(true){
if(rj.incomeWhiteC()){
System.out.println("白棋胜");
break;
}
if(rj.incomeBlankC()){
System.out.println("黑棋胜");
break;
}
}
}
}
}
运行结果: