五子棋程序
程序员文章站
2022-06-30 21:30:53
java编程五子棋程序package chapter_3;import java.util.Arrays;import java.util.Scanner;public class demo7_2 { public static void main(String[] args) { // 初始化棋盘 a[15][15] String [][]a=new String[15][15]; for (int i = 0; i < a.le...
java编程五子棋程序
package chapter_3;
import java.util.Arrays;
import java.util.Scanner;
public class demo7_2 {
public static void main(String[] args) {
// 初始化棋盘 a[15][15]
String [][]a=new String[15][15];
for (int i = 0; i < a.length-1; i++) {
for (int j = 0; j < a.length-1; j++) { a[i][j]="十"; }
}
String []b={"⒈","⒉","⒊","⒋","⒌","⒍","⒎","⒏","⒐","⒑","⒒","⒓","⒔","⒕","⒖"};
for (int i = 0; i < a.length; i++) {
a[i][a.length-1]=a[a.length-1][i]=b[i];
}
stamp(a);
int i=0;
do { // 进程开始
boolean mn=true;
i=i%2;
System.out.println();
if (i==0) {
System.out.println("P1 ★ 请下棋");
}else {
System.out.println("P2 ● 请下棋");
}
mn = Next_step(a,i,mn); //下棋
if (mn==true){
stamp(a);
i=i+1;
}else {
i=i;
}
}while (ture_false(a)==true);
}
public static void stamp(String a[][]){ //打印棋盘
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length; j++) {
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
public static boolean Next_step(String a[][],int t,boolean z){ // i为到谁下棋了,并下棋
z=true;
String []player=new String[2];
player[0]="★"; // p1 的棋
player[1]="●"; // p2 的棋
Scanner c=new Scanner(System.in);
System.out.print("输入你要下的棋。行数:");
int hang=c.nextInt(); //接收下一步棋的行数
System.out.print("输入你要下的棋。列数:");
int lie=c.nextInt(); // 接收下一步棋的列数
if (a[hang-1][lie-1]!="十" | (hang<=0 && hang>a.length-1) | (lie<=0 && lie>a.length-1)) //判断下一步棋是否有效
{ System.out.println("下棋位置无效或已经有棋");
z=false;
}
else a[hang-1][lie-1]=player[t]; //赋值(棋下在相应位置上)
return z;
}
public static boolean ture_false(String [][]a){ //判断是否结束
boolean srr=true;
for (int i = 0; i < a.length-1;i++) { //判断行 是否满足规则赢
for (int j = 0; j < a.length-1-4; j++) {
if (a[i][j]=="十"){ continue;}
if (a[i][j]==a[i][j+1] &a[i][j]==a[i][j+2] &a[i][j]==a[i][j+3] & a[i][j]==a[i][j+4])
{ srr=false;
if(a[i][j]=="★") System.out.println("P1 赢");
else System.out.println("P2 赢");
}
}
}
for (int i = 0; i < a.length-1-4;i++) { //判断列 是否满足规则赢
for (int j = 0; j < a.length-1; j++) {
if (a[i][j]=="十"){ continue;}
if (a[i][j]==a[i+1][j] &a[i][j]==a[i+2][j] &a[i][j]==a[i+3][j] & a[i][j]==a[i+4][j])
{ srr=false;
if(a[i][j]=="★") System.out.println("P1 赢");
else System.out.println("P2 赢");
}
}
}
for (int i = 0; i < a.length-1-4;i++) { //判断正对角线 是否满足规则赢
for (int j = 0; j < a.length-1-4; j++) {
if (a[i][j]=="十"){ continue;}
if (a[i][j]==a[+1][j+1] &a[i][j]==a[i+2][j+2] &a[i][j]==a[i+3][j+3] & a[i][j]==a[i+4][j+4])
{ srr=false;
if(a[i][j]=="★") System.out.println("P1 赢");
else System.out.println("P2 赢");
}
}
}
for (int i = 5; i < a.length-1;i++) { //判断反对角线 是否满足规则赢
for (int j = 0; j < a.length-1-4; j++) {
if (a[i][j]=="十"){ continue;}
if (a[i][j]==a[i-1][j+1] &a[i][j]==a[i-2][j+2] &a[i][j]==a[i-3][j+3] & a[i][j]==a[i-4][j+4])
{ srr=false;
if(a[i][j]=="★") System.out.println("P1 赢");
else System.out.println("P2 赢");
}
}
}
return srr;
}
}
本文地址:https://blog.csdn.net/weixin_52551903/article/details/109846000