# include <iostream>
# include <vector>
# include <string>
# include <iomanip>
# include <limits> using namespace std;
const int BOARD_SIZE = 15 ;
enum class Piece { EMPTY, BLACK, WHITE
} ;
enum class GameState { PLAYING, BLACK_WIN, WHITE_WIN, DRAW
} ; / * * * 棋盘类 - 管理棋盘状态和游戏规则* /
class Board {
private : vector< vector< Piece>> grid; public : Board ( ) : grid ( BOARD_SIZE, vector< Piece> ( BOARD_SIZE, Piece:: EMPTY) ) { } / * * * 重置棋盘 - 将所有位置设为空* / void reset ( ) { for ( auto & row : grid) { fill ( row. begin ( ) , row. end ( ) , Piece:: EMPTY) ; } } / * * * 在指定位置放置棋子* @param x 横坐标( 0 - 14 ) * @param y 纵坐标( 0 - 14 ) * @param piece 要放置的棋子类型* @return 放置是否成功* / bool placePiece ( int x, int y, Piece piece) { if ( x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE || grid[ x] [ y] != Piece:: EMPTY) { return false ; } grid[ x] [ y] = piece; return true ; } / * * * 获取指定位置的棋子* @param x 横坐标* @param y 纵坐标* @return 该位置的棋子类型( 无效坐标返回EMPTY) * / Piece getPiece ( int x, int y) const { if ( x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE) { return Piece:: EMPTY; } return grid[ x] [ y] ; } / * * * 检查是否五子连珠* @param x 最后落子的横坐标* @param y 最后落子的纵坐标* @return 是否形成五子连珠* / bool checkFiveInARow ( int x, int y) const { Piece current = grid[ x] [ y] ; if ( current == Piece:: EMPTY) return false ; int directions[ 4 ] [ 2 ] = { { 1 , 0 } , { 0 , 1 } , { 1 , 1 } , { 1 , - 1 } } ; for ( auto & dir : directions) { int count = 1 ; for ( int i = 1 ; i < 5 ; i++ ) { int nx = x + dir[ 0 ] * i; int ny = y + dir[ 1 ] * i; if ( getPiece ( nx, ny) != current) break ; count++ ; } for ( int i = 1 ; i < 5 ; i++ ) { int nx = x - dir[ 0 ] * i; int ny = y - dir[ 1 ] * i; if ( getPiece ( nx, ny) != current) break ; count++ ; } if ( count >= 5 ) return true ; } return false ; } / * * * 打印棋盘到控制台* / void print ( ) const { cout << " " ; for ( int i = 0 ; i < BOARD_SIZE; i++ ) { cout << setw ( 2 ) << i << " " ; } cout << endl; for ( int i = 0 ; i < BOARD_SIZE; i++ ) { cout << setw ( 2 ) << i << " " ; for ( int j = 0 ; j < BOARD_SIZE; j++ ) { switch ( grid[ i] [ j] ) { case Piece:: EMPTY: cout << " . " ; break ; case Piece:: BLACK: cout << " ● " ; break ; case Piece:: WHITE: cout << " ○ " ; break ; } } cout << endl; } }
} ; / * * * 游戏主类 - 管理游戏流程和状态* /
class GomokuGame {
private : Board board; GameState state; Piece currentPlayer; public : / * * * 构造函数 - 初始化新游戏* / GomokuGame ( ) : state ( GameState:: PLAYING) , currentPlayer ( Piece:: BLACK) { } / * * * 开始新游戏 - 重置棋盘和游戏状态* / void startNewGame ( ) { board. reset ( ) ; state = GameState:: PLAYING; currentPlayer = Piece:: BLACK; } / * * * 获取当前游戏状态* @return 当前游戏状态* / GameState getGameState ( ) const { return state; } / * * * 打印当前游戏状态* / void printStatus ( ) const { board. print ( ) ; cout << endl; switch ( state) { case GameState:: PLAYING: cout << "当前玩家: " << ( currentPlayer == Piece:: BLACK ? "黑方" : "白方" ) << endl; break ; case GameState:: BLACK_WIN: cout << "游戏结束! 黑方获胜!" << endl; break ; case GameState:: WHITE_WIN: cout << "游戏结束! 白方获胜!" << endl; break ; case GameState:: DRAW: cout << "游戏结束! 平局!" << endl; break ; } } / * * * 执行走棋操作* @param x 横坐标* @param y 纵坐标* @return 走棋是否成功* / bool makeMove ( int x, int y) { if ( state != GameState:: PLAYING || ! board. placePiece ( x, y, currentPlayer) ) { return false ; } if ( board. checkFiveInARow ( x, y) ) { state = ( currentPlayer == Piece:: BLACK) ? GameState:: BLACK_WIN : GameState:: WHITE_WIN; return true ; } currentPlayer = ( currentPlayer == Piece:: BLACK) ? Piece:: WHITE : Piece:: BLACK; return true ; }
} ; / * * * 主函数 - 游戏入口* /
int main ( ) { GomokuGame game; game. startNewGame ( ) ; while ( true ) { game. printStatus ( ) ; if ( game. getGameState ( ) != GameState:: PLAYING) { cout << "输入'r'重新开始,'q'退出: " ; char cmd; cin >> cmd; if ( cmd == 'r' ) { game. startNewGame ( ) ; continue ; } else if ( cmd == 'q' ) { break ; } } cout << "输入坐标(x y): " ; int x, y; cin >> x >> y; if ( ! game. makeMove ( x, y) ) { cout << "无效移动! 请重新输入。" << endl; cin. clear ( ) ; cin. ignore ( numeric_limits < streamsize> :: max ( ) , '\n' ) ; } } return 0 ;
}