新手java五子棋完整代码判断落子落在线上_求一个简单的JAVA五子棋代码!! 网上复制的别来了!...

展开全部

32313133353236313431303231363533e59b9ee7ad9431333262373939以下是现写的 实现了两人对战 自己复制后运行把 没什么难度 类名 Games

import java.util.Scanner;

public class Games {

private String board[][];

private static int SIZE = 17;

private static String roles = "A玩家";

//初始化数组

public void initBoard() {

board = new String[SIZE][SIZE];

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

for (int j = 0; j < SIZE; j++) {

// if(i==0){

// String str = "";

// str += j+" ";

// board[i][j]= str;

// }else if(i!=0&&j==0){

// String str = "";

// str += i+" ";

// board[i][j]= str;

// }else{

board[i][j] = "╋";

// }

}

}

}

//输出棋盘

public void printBoard() {

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

for (int j = 0; j < SIZE; j++) {

System.out.print(board[i][j]);

}

System.out.println();

}

}

//判断所下棋子位置是否合理

public boolean isOk(int x, int y) {

boolean isRight = true;

if (x >= 16 || x < 1 || y >= 16 | y < 1) {

//System.out.println("输入错误,请从新输入");

isRight = false;

}

if (board[x][y].equals("●") || board[x][y].equals("○")) {

isRight = false;

}

return isRight;

}

//判断谁赢了

public void whoWin(Games wz) {

// 从数组挨个查找找到某个类型的棋子就从该棋子位置向右,向下,斜向右下 各查找5连续的位置看是否为5个相同的

int xlabel;// 记录第一次找到某个棋子的x坐标

int ylabel;// 记录第一次找到某个棋子的y坐标

// ●○╋

// 判断人是否赢了

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

for (int j = 0; j < SIZE; j++) {

if (board[i][j].equals("○")) {

xlabel = i;

ylabel = j;

// 横向找 x坐标不变 y坐标以此加1连成字符串

String heng = "";

if (i + 5 < SIZE && j + 5 < SIZE) {

for (int k = j; k < j + 5; k++) {

heng += board[i][k];

}

if (heng.equals("○○○○○")) {

System.out.println(roles+"赢了!您输了!");

System.exit(0);

}

// 向下判断y不变 x逐增5 连成字符串

String xia = "";

for (int l = j; l < i + 5; l++) {

xia += board[l][j];

// System.out.println(xia);

}

if (xia.equals("○○○○○")) {

System.out.println(roles+"赢了!您输了!");

System.exit(0);

}

// 斜向右下判断

String youxia = "";

for (int a = 1; a <= 5; a++) {

youxia += board[xlabel++][ylabel++];

}

if (youxia.equals("○○○○○")) {

System.out.println(roles+"赢了!您输了!");

System.exit(0);

}

}

}

}

}

// 判断电脑是否赢了

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

for (int j = 0; j < SIZE; j++) {

if (board[i][j].equals("●")) {

xlabel = i;

ylabel = j;

// 横向找 x坐标不变 y坐标以此加1连成字符串

String heng = "";

if (j + 5 < SIZE && i + 5 < SIZE) {

for (int k = j; k < j + 5; k++) {

heng += board[i][k];

}

if (heng.equals("●●●●●")) {

System.out.println(roles+"赢输了!您输了!");

System.exit(0);

}

// 向下判断y不变 x逐增5 连成字符串

String xia = "";

for (int l = i; l < i + 5; l++) {

xia += board[l][ylabel];

// System.out.println(xia);

}

if (xia.equals("●●●●●")) {

System.out.println(roles+"赢了!您输了!");

System.exit(0);

}

// 斜向右下判断

String youxia = "";

for (int a = 1; a <= 5; a++) {

youxia += board[xlabel++][ylabel++];

}

if (youxia.equals("●●●●●")) {

System.out.println(roles+"赢了!您输了!");

System.exit(0);

}

}

}

}

}

}

public static void main(String[] args) {

Games wz = new Games();

Scanner sc = new Scanner(System.in);

wz.initBoard();

wz.printBoard();

while (true) {

System.out.print("请"+roles+"输入X,Y坐标,必须在0-15范围内,xy以空格隔开,输入16 16结束程序");

int x = sc.nextInt();

int y = sc.nextInt();

if (x == SIZE && y == SIZE) {

System.out.println("程序结束");

System.exit(0);

}

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

System.out.println("输入错误,请从新输入");

continue;

}

//如果roles是A玩家 就让A玩家下棋,否则就让B玩家下棋。

if (wz.board[x][y].equals("╋")&&roles.equals("A玩家")) {

wz.board[x][y] = "○";

wz.printBoard();

//判断输赢

wz.whoWin(wz);

}else if(wz.board[x][y].equals("╋")&&roles.equals("B玩家")){

wz.board[x][y] = "●";

wz.printBoard();

//判断输赢

wz.whoWin(wz);

} else {

System.out.println("此处已经有棋子,从新输入");

continue;

}

if(roles.equals("A玩家")){

roles = "B玩家";

}else if(roles.equals("B玩家")){

roles = "A玩家";

}

}

}

}

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值