# 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 ;
}