package com.txt;
import java.util.Scanner;
class Gobang
{
char[][] chessboard;//生成新的二维数组
Gobang() //构建五子棋函数
{
chessboard = new char[15][15]; //15x15的棋盘[0~14]
for(int i=0;i<15;i++)
{
for(int j=0;j<15;j++)
chessboard[i][j] = '+'; //循环体构建+型棋盘
}
}
//输赢判断
boolean judge() { //布尔类型的判断
boolean result = false;
//左右方向判断
for(int i=0;i<15;i++) {
for(int j=4;j<15;j++) {
char ch = chessboard[i][j];
if(ch == '+') {
continue;
}
if(ch=='*'
&& chessboard[i][j-1] == ch
&& chessboard[i][j-2] == ch
&& chessboard[i][j-3] == ch
&& chessboard[i][j-4] == ch) {
System.out.println("您赢了");
result=true;
}
}
}
//上下方向判断
for(int i=4;i<15;i++) {
for(int j=0;j<15;j++) {
char ch = chessboard[i][j];
if(ch == '+') {
continue;
}
if(ch=='*'
&& chessboard[i-1][j] == ch
&& chessboard[i-2][j] == ch
&& chessboard[i-3][j] == ch
&& chessboard[i-4][j] == ch) {
System.out.println("您赢了");
result=true;
}
}
}
//左上右下判断
for(int i=4;i<15;i++) {
for(int j=4;j<15;j++) {
char ch = chessboard[i][j];
if(ch == '+') {
continue;
}
if(ch=='*'
&& chessboard[i-1][j-1] == ch
&& chessboard[i-2][j-2] == ch
&& chessboard[i-3][j-3] == ch
&& chessboard[i-4][j-4] == ch) {
System.out.println("您赢了");
result=true;
}
}
}
//左下右上判断
for(int i=4;i<15;i++) {
for(int j=0;j<15;j++) {
char ch = chessboard[i][j];
if(ch == '+') {
continue;
}
if(ch=='*'
&& chessboard[i-1][j+1] == ch
&& chessboard[i-2][j+2] == ch
&& chessboard[i-3][j+3] == ch
&& chessboard[i-4][j+4] == ch) {
System.out.println("您赢了");
result=true;
}
}
}
return result;
}
void start ()
{
Scanner input = new Scanner(System.in);
while(true)
{
System.out.println("游戏开始!");
int x=0,y=0;
while(true)
{
System.out.println("请输入落子位置:x,y");
String p_input = input.nextLine();
String[] p_inputs = p_input.split(",");
x = Integer.parseInt(p_inputs[0]);
y = Integer.parseInt(p_inputs[1]);
if( x >= 0 && x < 15 && y >= 0 && y < 15) {}
if(chessboard[x][y] == '+')
{
chessboard[x][y] = '*';
break;
}
}
for(int i=0;i<15;i++)
{
for(int j=0;j<15;j++)
System.out.print(chessboard[i][j]+" ");
System.out.println();
}
if(judge()) break;
while(true)
{
System.out.println("机器落子!");
x = (int)(Math.random()*15);
y =(int)(Math.random()*15);
if(x>=0 && x<15 && y>=0 && y<15)
if(chessboard[x][y] == '+')
{
chessboard[x][y] = '$';
break;
}
for(int i=0;i<15;i++)
{
for(int j=0;j<15;j++)
System.out.print(chessboard[i][j]);
System.out.println();
}
if(judge()) break;
}
}
}
public static void main(String[] args)
{
Gobang gb = new Gobang();
gb.start();
}
}