控制台五子棋游戏
程序员文章站
2024-03-18 14:58:52
...
import java.util.*;
import java.io.*;
/**
*Description:控制台五子棋游戏,用户自定义BOARD_SIZE的大小,
*即棋盘的大小,用户为黑棋●,PC为白棋○,
*用户输入黑旗的坐标x,y PC随机产生坐标x1,y1
*注意:不能重复下棋,每次下棋,需要扫描谁赢了
*扫描:横、竖、右斜、左斜
*@author MechanicalDog
*@version 1.0
*/
public class Gobang
{
private static int BOARD_SIZE = 20;
String[][] board;
public static void main(String[] args) throws Exception
{
//无static修饰的类
Gobang gb = new Gobang();
gb.initBoard();
gb.printBoard();
Scanner s = new Scanner(System.in);
// String str = s.nextLine();
String str ;
//当检测到一方胜利时,跳出循环
outer:
while( (str = s.nextLine())!= null )
{
//将输入的字符串x,y分成两个字符串x和y,并且以","作为分界
String[] cordinate = str.split(",");
//将String字符转换为int类型,不是转换为ASCII
int x = Integer.parseInt(cordinate[0]);
int y = Integer.parseInt(cordinate[1]);
gb.board[x-1][y-1] = "●";
//电脑随机生成两个随机数,取值范围0~15
int x1 = (int)( Math.random() * BOARD_SIZE );
int y1 = (int)( Math.random() * BOARD_SIZE );
//下棋的点不能重复下棋
while( gb.board[x1][y1] != "╂" )
{
x1 = (int)( Math.random() * BOARD_SIZE );
y1 = (int)( Math.random() * BOARD_SIZE );
}
gb.board[x1][y1] = "○";
gb.printBoard();
//标记
int black = 0;
int white = 0;
int blackWin = 0;
int whiteWin = 0;
//扫描谁赢了,4次循环扫描,横、竖、右斜、左斜
//横着扫描
outer1:
for(int i = 0 ; i < BOARD_SIZE ; i++ )
{
white = 0;
black = 0;
for (int j = 0 ; j < BOARD_SIZE ; j++ )
{
if(gb.board[i][j] == "●")
{
black++;
}
else if(gb.board[i][j] == "○")
{
white++;
}
if( black == 5)
{
System.out.println("黑棋胜利");
blackWin = 1;
break outer1;
}
if( white == 5)
{
System.out.println("白棋胜利");
whiteWin = 1;
break outer1;
}
}
}
//竖着扫描
outer2:
for(int j = 0 ; j < BOARD_SIZE ; j++ )
{
white = 0;
black = 0;
for (int i = 0 ; i < BOARD_SIZE ; i++ )
{
if(gb.board[i][j] == "●")
{
black++;
}
else if(gb.board[i][j] == "○")
{
white++;
}
if( black == 5)
{
System.out.println("黑棋胜利");
blackWin = 1;
break outer2;
}
if( white == 5)
{
System.out.println("白棋胜利");
whiteWin = 1;
break outer2;
}
}
}
//右斜扫描(从右上角开始判断)
int temp = BOARD_SIZE ;
int k = 0;
int t = 0;
//斜线共有2*BOARD_SIZE - 1 条
outer3:
for( int num = 1 ; num < (2*BOARD_SIZE); num++)
{
white = 0;
black = 0;
temp--;
if( temp >= 0)
{
t = temp;
k = BOARD_SIZE - t ;
for( int i = 0 ; i < k ; i++)
{
if(gb.board[i][ t + i ] == "●")
{
black++;
}
else if(gb.board[i][ t + i ] == "○")
{
white++;
}
if( black == 5)
{
System.out.println("黑棋胜利");
blackWin = 1;
break outer3;
}
if( white == 5)
{
System.out.println("白棋胜利");
whiteWin = 1;
break outer3;
}
}
}
else
{
t = temp;
t = -t;
k = BOARD_SIZE - t ;
for( int j = 0 ; j < k ; j++ )
{
if(gb.board[ t + j ][ j ]== "●")
{
black++;
}
else if(gb.board[ t + j ][ j ] == "○")
{
white++;
}
if( black == 5)
{
System.out.println("黑棋胜利");
blackWin = 1;
break outer3;
}
if( white == 5)
{
System.out.println("白棋胜利");
whiteWin = 1;
break outer3;
}
}
}
}
//左斜扫描(从左上角开始)
temp = BOARD_SIZE ;
k = 0;
t = 0;
int count = 0 ;
//斜线共有2*BOARD_SIZE - 1 条
outer4:
for( int num = 1 ; num < (2*BOARD_SIZE); num++)
{
white = 0;
black = 0;
temp--;
if( temp >= 0)
{
t = temp;
k = BOARD_SIZE - t ;
for( int i = 0 ; i < k ; i++)
{
if(gb.board[ i ][count - i]== "●")
{
black++;
}
else if(gb.board[ i ][ count -i ] == "○")
{
white++;
}
if( black == 5)
{
System.out.println("黑棋胜利");
blackWin = 1;
break outer4;
}
if( white == 5)
{
System.out.println("白棋胜利");
whiteWin = 1;
break outer4;
}
}
}
else
{
t = temp;
t = -t;
k = BOARD_SIZE - t ;
int p = 0;
for( int j = BOARD_SIZE - 1 ; p < k ; j -- , p++ )
{
if(gb.board[ count - j ][ j ]== "●")
{
black++;
}
else if(gb.board[ count - j ][ j ] == "○")
{
white++;
}
if( black == 5)
{
System.out.println("黑棋胜利");
blackWin = 1;
break outer4;
}
if( white == 5)
{
System.out.println("白棋胜利");
whiteWin = 1;
break outer4;
}
}
}
count++;
}
//判断一方有没有获得胜利
if( blackWin != 1 && whiteWin != 1)
{
System.out.println("请输入您的下棋坐标,应以x,y格式");
}
else
{
System.out.println("游戏结束");
//跳出整个循环
break outer;
}
}
}
/**
*定义棋盘,用"+"表示
*/
public void initBoard()
{
//初始化棋盘数组,一定要有!!!
board = new String[BOARD_SIZE][BOARD_SIZE];
for( int i = 0 ; i < BOARD_SIZE ; i++ )
{
for( int j = 0 ; j < BOARD_SIZE ; j++ )
{
board[i][j] = "╂";
}
}
}
/**
*打印棋盘
*/
public void printBoard()
{
for( int i = 0 ; i < BOARD_SIZE ; i++ )
{
for( int j = 0 ; j < BOARD_SIZE ; j++ )
{
System.out.print(board[i][j]);
}
//换行
System.out.print('\n');
}
}
}
下一篇: 关于Java你不知道的十件事