Java 控制台五子棋游戏
思路
这是疯狂java第四章习题:
难点:
- 如何遍历左斜线和右斜线的棋子
- 怎么判断5个棋子连在一起了
这里通过设置两个计数器(countBlack和countWhite)遇到黑子countBlack加1,清零countWhite,如果countBlack=5退出计数,黑子胜利。遇到白子countWhite加1,清零countBlack,如果countWhite=5退出计数,白子胜利。
效果
代码
package FourPointSeven;
import javax.xml.namespace.QName;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigDecimal;
public class Gobang {
//定义棋盘的大小
private static int BOARD_SIZE = 15;
private static int countBlack = 0;
private static int countWhite = 0;
private static Gobang gobang;
//定义一个二维数组来充当棋盘
private String[][] board;
public void initBoard(){
board = new String[BOARD_SIZE][BOARD_SIZE];
for (var i= 0; i < BOARD_SIZE; i++){
for (var j = 0; j < BOARD_SIZE; j++){
board[i][j]="┼";
}
}
}
public void printBoard(){
for (var i= 0; i < BOARD_SIZE; i++){
for (var j = 0; j < BOARD_SIZE; j++){
System.out.print(board[i][j]);
}
System.out.print('\n');
}
}
public static void main(String[] args) throws Exception{
String[] posStrArr = new String[1];
int xPos=0,yPos=0;
gobang = new Gobang();
gobang.initBoard();
gobang.printBoard();
//键盘输入
var br = new BufferedReader(new InputStreamReader(System.in));
String inputStr = null;
while (true){
System.out.println("请输入您的下棋坐标,应以x y的格式");
inputStr = br.readLine();
if (inputStr.equals("q")){
break;
}
try{
posStrArr = inputStr.split(" ");
//把字符串转换成用户下棋坐标
xPos = Integer.parseInt(posStrArr[0]);
yPos = Integer.parseInt(posStrArr[1]);
if (xPos<=0||yPos<=0||xPos>15||yPos>15){
throw new Exception("输入的坐标超出范围");
}
//把对应数组元素赋为“⚫”
while (true){
if (gobang.board[xPos - 1][yPos - 1].equals("┼")){
gobang.board[xPos - 1][yPos - 1] = "●";
break;
}else {
System.out.println("这里已经有棋子了,请重新输入:");
inputStr = br.readLine();
posStrArr = inputStr.split(" ");
xPos = Integer.parseInt(posStrArr[0]);
yPos = Integer.parseInt(posStrArr[1]);
}
}
//电脑下棋
while (true){
var cxPos = (int)(Math.random()*BOARD_SIZE);//生成一个1~15的整数
var cyPos = (int)(Math.random()*BOARD_SIZE);
if (gobang.board[cxPos][cyPos].equals("┼")){
gobang.board[cxPos][cyPos] = "○";
break;
}
}
gobang.printBoard();
}catch (Exception e){
System.out.println("您输入的格式有误!请重新输入!");
}
//扫描判断谁赢了
//按每一行扫描
for (var i = 0; i < BOARD_SIZE;i++) {
for (var j = 0; j < BOARD_SIZE; j++){
if (gobang.board[i][j].equals("●")){
if (blackAdd())break;
}else if (gobang.board[i][j].equals("○")){
if(whiteAdd())break;
}
}
if (isVictory()==1||isVictory()==2){
break;
}
}
//按列扫描
for (var j = 0; j < BOARD_SIZE;j++) {
for (var i = 0; i < BOARD_SIZE; i++) {
if (gobang.board[i][j].equals("●")) {
if (blackAdd())break;
} else if (gobang.board[i][j].equals("○")) {
if(whiteAdd())break;
}
}
if (isVictory()==1 || isVictory()==2){
break;
}
}
//按 “/”扫描
for (var i = 0; i < BOARD_SIZE;i++){
for (var j = 0;j <= i;j++){
if (gobang.board[j][i-j].equals("●")){
if (blackAdd())break;
}else if (gobang.board[j][i-j].equals("○")){
if(whiteAdd())break;
}
}
if (isVictory()==1||isVictory()==2){
break;
}
}
for (var i = 0; i < BOARD_SIZE; i++){
for (var j =0 ;j <=i ;j++){
if (gobang.board[14-i+j][14-j].equals("●")){
if (blackAdd())break;
}else if (gobang.board[14+j-i][14-j].equals("○")){
if(whiteAdd())break;
}
}
if (isVictory()==1||isVictory()==2){
break;
}
}
//按“\”扫描
for (var i = 0; i < BOARD_SIZE;i++){
for (var j = 0; j <= i;j++){
if (gobang.board[14-i+j][j].equals("●")){
if (blackAdd())break;
}else if (gobang.board[14-i+j][j].equals("○")){
if(whiteAdd())break;
}
}
if (isVictory()==1||isVictory()==2){
break;
}
}
for (var i = 0; i < BOARD_SIZE; i++){
for (var j = 0; j <= i; j++){
if (gobang.board[j][14-i+j].equals("●")){
if (blackAdd())break;
}else if (gobang.board[j][14-i+j].equals("○")){
if(whiteAdd())break;
}
}
if (isVictory()==1||isVictory()==2){
break;
}
}
}
}
private static int isVictory(){
if (countBlack == 5 ){
System.out.println("黑子胜利!!!劈里啪啦!");
//重新初始化棋盘
gobang.initBoard();
gobang.printBoard();
emptyCount();
return 1;
}else if (countWhite ==5){
System.out.println("白子胜利!!!劈里啪啦!");
//重新初始化棋盘
gobang.initBoard();
gobang.printBoard();
emptyCount();
return 2;
} else {
emptyCount();
}
return -1;
}
private static void emptyCount(){
countBlack = 0;
countWhite = 0;
}
private static boolean blackAdd(){
countBlack++;
if (countBlack == 5){
return true;
}else {
countWhite = 0;
}
return false;
}
private static boolean whiteAdd(){
countWhite++;
if (countWhite == 5)
{
return true;
}else {
countBlack = 0;
}
return false;
}
}