# 下落棋

+关注继续查看

import java.util.Scanner;

public class practice7Arrays{

//悬挂的四子棋

public static void main(String[]args){

Scanner input = new Scanner(System.in);

String[][] qi = new String[6][7];

String yuanShiYuanSu = "|   ";

String yellow = "| Y ";

String red = "| R ";

int count = 0;//谁下子及下了多少子的判断计数器

//初始化棋子数组

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

for(int j = 0;j < qi[0].length;j++)

qi[i][j] = yuanShiYuanSu;

//画出原始棋盘

System.out.println("-----------------------------");

for(int i = qi.length - 1;i >= 0;i--){

for(int j = 0;j < qi[0].length;j++)

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

System.out.println("|");

System.out.println("-----------------------------");

}

for(int i = 0;i < qi[0].length;i++)

System.out.print("* "+(i+1)+" ");

System.out.println("*");

int row = 0,column = 0;//行列下标初始化

//下棋循环开始

while (true) {

row = 0;

column = 0;

if (count % 2 == 0) {//黄方下子

System.out.print("\n\nY player please drop a yellow disk at column(1~7):");

while (true) {//黄方下子循环开始

column = input.nextInt() - 1;

if (column >= 0 && column <= 6) {// 输入合法进行下子

for (row = 0; row < qi.length; row++) {

if (qi[row][column] == yuanShiYuanSu) {

qi[row][column] = yellow;

break;

}

}

if (row == qi.length)//该列棋子满，重新输入

System.out.print("The column of you enter is full,"

else//棋子没满，下子结束

break;

}

else// 输入不合法，重新下子

System.out.print("You enter the wrong column,"

}//黄方下子循环结束

}//if(count % 2 == 0)

else{//红方下子

System.out.print("\n\nR player please drop a yellow disk at column(1~7):");

while (true) {//红方下子循环开始

column = input.nextInt() - 1;

if (column >= 0 && column <= 6) {// 输入合法进行下子

for (row = 0; row < qi.length; row++) {

if (qi[row][column] == yuanShiYuanSu) {

qi[row][column] = red;

break;

}

}

if (row == qi.length)//该列棋子满，重新输入

System.out.print("The column of you enter is full,"

else//棋子没满，下子结束

break;

}

else// 输入不合法，重新下子

System.out.print("You enter the wrong column,"

}//红方下子循环结束

}//if(count % 2 != 0)

//画出棋盘

System.out.println("-----------------------------");

for(int i = qi.length - 1;i >= 0;i--){

for(int j = 0;j < qi[0].length;j++)

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

System.out.println("|");

System.out.println("-----------------------------");

}

for(int i = 0;i < qi[0].length;i++)

System.out.print("* "+(i+1)+" ");

System.out.println("*");

//输赢标志位

boolean flagForYWin = false;

boolean flagForRWin = false;

//只需对当前下子的周围情况进行判断来决定棋局结果

if (count % 2 == 0) {//yellow player is win?

//行检测开始

if (column <= 3) {

for (int jj = 0; jj <= column; jj++)

if (qi[row][jj] == yellow

&& qi[row][jj + 1] == yellow

&& qi[row][jj + 2] == yellow

&& qi[row][jj + 3] == yellow) {

flagForYWin = true;

break;

}

}

else {

for (int jj = column - 3; jj <= 3; jj++)

if (qi[row][jj] == yellow

&& qi[row][jj + 1] == yellow

&& qi[row][jj + 2] == yellow

&& qi[row][jj + 3] == yellow) {

flagForYWin = true;

break;

}

}

if (flagForYWin) {

System.out.println("The yellow player win the game!");

break;

}

//列检测开始

if (row >= 3) {

if (qi[row][column] == yellow

&& qi[row - 1][column] == yellow

&& qi[row - 2][column] == yellow

&& qi[row - 3][column] == yellow)

flagForYWin = true;

}

if (flagForYWin) {

System.out.println("The yellow player win the game!");

break;

}

//正反对角检测

if(row >= 3){

if(column < 3){

if (qi[row][column] == yellow

&& qi[row - 1][column + 1] == yellow

&& qi[row - 2][column + 2] == yellow

&& qi[row - 3][column + 3] == yellow)

flagForYWin = true;

}

else if(column > 3){

if (qi[row][column] == yellow

&& qi[row - 1][column - 1] == yellow

&& qi[row - 2][column - 2] == yellow

&& qi[row - 3][column - 3] == yellow)

flagForYWin = true;

}

else{

if ((qi[row][column] == yellow

&& qi[row - 1][column + 1] == yellow

&& qi[row - 2][column + 2] == yellow

&& qi[row - 3][column + 3] == yellow)

|| (qi[row][column] == yellow

&& qi[row - 1][column - 1] == yellow

&& qi[row - 2][column - 2] == yellow

&& qi[row - 3][column - 3] == yellow))

flagForYWin = true;

}

}

if (flagForYWin) {

System.out.println("The yellow player win the game!");

break;

}

}//yellow player is win?

else{//red player is win?

//行检测开始

if (column <= 3) {

for (int jj = 0; jj <= column; jj++)

if (qi[row][jj] == red

&& qi[row][jj + 1] == red

&& qi[row][jj + 2] == red

&& qi[row][jj + 3] == red) {

flagForRWin = true;

break;

}

}

else {

for (int jj = column - 3; jj <= 3; jj++)

if (qi[row][jj] == red

&& qi[row][jj + 1] == red

&& qi[row][jj + 2] == red

&& qi[row][jj + 3] == red) {

flagForRWin = true;

break;

}

}

if (flagForRWin) {

System.out.println("The red player win the game!");

break;

}

//列检测开始

if (row >= 3) {

if (qi[row][column] == red

&& qi[row - 1][column] == red

&& qi[row - 2][column] == red

&& qi[row - 3][column] == red)

flagForRWin = true;

}

if (flagForRWin) {

System.out.println("The red player win the game!");

break;

}

//正反对角检测

if(row >= 3){

if(column < 3){

if (qi[row][column] == red

&& qi[row - 1][column + 1] == red

&& qi[row - 2][column + 2] == red

&& qi[row - 3][column + 3] == red)

flagForRWin = true;

}

else if(column > 3){

if (qi[row][column] == red

&& qi[row - 1][column - 1] == red

&& qi[row - 2][column - 2] == red

&& qi[row - 3][column - 3] == red)

flagForRWin = true;

}

else{

if ((qi[row][column] == red

&& qi[row - 1][column + 1] == red

&& qi[row - 2][column + 2] == red

&& qi[row - 3][column + 3] == red)

|| (qi[row][column] == red

&& qi[row - 1][column - 1] == red

&& qi[row - 2][column - 2] == red

&& qi[row - 3][column - 3] == red))

flagForRWin = true;

}

}

if (flagForRWin) {

System.out.println("The red player win the game!");

break;

}

}

count++;//棋子数加1，并用于谁下棋子的判断

//棋盘下满棋子，是平局

if(count == 6*7){

System.out.println("棋盘棋子已经下满，是平局！");

break;

}

}//下棋循环结束

}//方法块

}//类块

26795 0

10092 0

11633 0

10886 0

11819 0

7365 0

4510 0

22409 0
+关注
121

0

《2021云上架构与运维峰会演讲合集》

《零基础CSS入门教程》

《零基础HTML入门教程》