帮我写一个c++编写的五子棋项目
时间: 2023-08-31 18:26:33 浏览: 84
### 回答1:
我可以为您提供一个简单的C语言编写的五子棋项目的代码: #include <stdio.h> int main(void)
{
//定义棋盘
int board[8][8]={0};
//定义玩家
int player=1;
//定义变量判断游戏是否结束
int flag=1;
//游戏开始
printf("游戏开始:\n");
//游戏主循环
while(flag==1)
{
//输出棋盘
printf("当前棋盘:\n");
//遍历棋盘
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
printf("%d\t",board[i][j]);
}
printf("\n");
}
//输入下棋位置
int x,y;
printf("请玩家%d输入棋子坐标:",player);
scanf("%d %d",&x,&y);
board[x][y]=player;
//判断游戏是否结束
if(check(x,y,player,board)==1)
{
printf("游戏结束,玩家%d胜!\n",player);
flag=0;
}
else
{
//换棋手
if(player==1)
{
player=2;
}
else
{
player=1;
}
}
}
return 0;
} //判断输赢函数
int check(int x,int y,int player,int board[8][8])
{
int flag=1;
//判断横向是否赢
for(int i=0;i<8;i++)
{
if(board[x][i]!=player)
{
flag=0;
break;
}
}
if(flag==1)
{
return 1;
}
else
{
flag=1;
}
//判断纵向是否赢
for(int i=0;i<8;i++)
{
if(board[i][y]!=player)
{
flag=0;
break;
}
}
if(flag==1)
{
return 1;
}
else
{
flag=1;
}
//判断左上到右下是否赢
if(x<y)
{
int i=x;
int j=0;
while(i<8&&j<8)
{
if(board[i][j]!=player)
{
flag=0;
break;
}
i++;
j++;
}
}
else
{
int i=0;
int j=y;
while(i<8&&j<8)
{
if(board[i][j]!=player)
{
flag=0;
break;
}
i++;
j++;
}
}
if(flag==1)
{
return 1;
}
else
{
flag=1;
}
//判断右上到左下是否赢
if(x+y<7)
{
int i=x;
int j=7;
while(i<8&&j>=0)
{
if(board[i][j]!=player)
{
flag=0;
break;
}
i++;
j--;
}
}
else
{
int i=7;
int j=x+y-7;
while(i>=0&&j<8)
{
if(board[i][j]!=player)
{
flag=0;
break;
}
i--;
j++;
}
}
if(flag==1)
{
return 1;
}
else
{
return 0;
}
}
### 回答2:
五子棋是一种古老而受欢迎的策略游戏。下面是一个简单的C编写的五子棋项目示例。
```c
#include <stdio.h>
#define SIZE 15
#define EMPTY 0
#define BLACK 1
#define WHITE 2
int board[SIZE][SIZE] = {0}; // 棋盘状态
int currentPlayer = BLACK; // 当前玩家
// 判断胜利条件
int checkWin(int player) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE - 4; j++) {
// 水平连续五子
if (board[i][j] == player && board[i][j + 1] == player && board[i][j + 2] == player && board[i][j + 3] == player && board[i][j + 4] == player) {
return 1;
}
// 垂直连续五子
if (board[j][i] == player && board[j + 1][i] == player && board[j + 2][i] == player && board[j + 3][i] == player && board[j + 4][i] == player) {
return 1;
}
}
}
for (int i = 0; i < SIZE - 4; i++) {
for (int j = 0; j < SIZE - 4; j++) {
// 左上到右下连续五子
if (board[i][j] == player && board[i + 1][j + 1] == player && board[i + 2][j + 2] == player && board[i + 3][j + 3] == player && board[i + 4][j + 4] == player) {
return 1;
}
// 右上到左下连续五子
if (board[SIZE - 1 - i][j] == player && board[SIZE - 1 - i - 1][j + 1] == player && board[SIZE - 1 - i - 2][j + 2] == player && board[SIZE - 1 - i - 3][j + 3] == player && board[SIZE - 1 - i - 4][j + 4] == player) {
return 1;
}
}
}
return 0;
}
// 显示棋盘
void displayBoard() {
printf("\n");
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
switch (board[i][j]) {
case EMPTY:
printf("+ ");
break;
case BLACK:
printf("X ");
break;
case WHITE:
printf("O ");
break;
}
}
printf("\n");
}
printf("\n");
}
// 下子
void makeMove(int x, int y) {
if (board[x][y] == EMPTY) {
board[x][y] = currentPlayer;
currentPlayer = (currentPlayer == BLACK) ? WHITE : BLACK;
} else {
printf("此位置已有棋子,请重新输入\n");
}
}
int main() {
int x, y;
displayBoard();
while (1) {
printf("请 %s 下子,输入坐标(x y):", (currentPlayer == BLACK) ? "黑方" : "白方");
scanf("%d %d", &x, &y);
makeMove(x, y);
displayBoard();
if (checkWin(BLACK)) {
printf("黑方胜利!\n");
break;
}
if (checkWin(WHITE)) {
printf("白方胜利!\n");
break;
}
}
return 0;
}
```
这个示例程序实现了一个基本的五子棋游戏。通过使用二维数组表示棋盘状态,判断胜利条件,显示棋盘和处理玩家输入,实现了一个简单的游戏循环。玩家可以轮流下子,程序会在每次下子后检查是否达到胜利条件。如果有一方满足胜利条件,游戏结束并宣布胜利者。
### 回答3:
五子棋是一种以黑白两色棋子在棋盘上进行对战的策略游戏。以下是一个简单的C程序实现五子棋项目的示例。
```c
#include <stdio.h>
// 定义棋盘大小
#define SIZE 15
// 定义棋盘和玩家
char chessboard[SIZE][SIZE];
char player1 = 'X';
char player2 = 'O';
// 初始化棋盘
void initChessboard() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
chessboard[i][j] = '-';
}
}
}
// 打印棋盘
void printChessboard() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
printf("%c ", chessboard[i][j]);
}
printf("\n");
}
}
// 判断是否胜利
int isWin(int row, int col) {
char player = chessboard[row][col];
// 判断横向是否连成五子
int count = 1;
int i = 1;
while (col - i >= 0 && chessboard[row][col - i] == player) {
count++;
i++;
}
i = 1;
while (col + i < SIZE && chessboard[row][col + i] == player) {
count++;
i++;
}
if (count >= 5) {
return 1;
}
// 判断纵向是否连成五子
count = 1;
i = 1;
while (row - i >= 0 && chessboard[row - i][col] == player) {
count++;
i++;
}
i = 1;
while (row + i < SIZE && chessboard[row + i][col] == player) {
count++;
i++;
}
if (count >= 5) {
return 1;
}
// 判断左斜向是否连成五子
count = 1;
i = 1;
while (row - i >= 0 && col - i >= 0 && chessboard[row - i][col - i] == player) {
count++;
i++;
}
i = 1;
while (row + i < SIZE && col + i < SIZE && chessboard[row + i][col + i] == player) {
count++;
i++;
}
if (count >= 5) {
return 1;
}
// 判断右斜向是否连成五子
count = 1;
i = 1;
while (row - i >= 0 && col + i < SIZE && chessboard[row - i][col + i] == player) {
count++;
i++;
}
i = 1;
while (row + i < SIZE && col - i >= 0 && chessboard[row + i][col - i] == player) {
count++;
i++;
}
if (count >= 5) {
return 1;
}
return 0;
}
int main() {
initChessboard();
int currentPlayer = 1;
while (1) {
int row, col;
printf("请输入落子位置(行 列):");
scanf("%d %d", &row, &col);
// 检查输入是否合法
if (row < 0 || row >= SIZE || col < 0 || col >= SIZE || chessboard[row][col] != '-') {
printf("输入不合法,请重新输入!\n");
continue;
}
// 玩家落子
if (currentPlayer == 1) {
chessboard[row][col] = player1;
} else {
chessboard[row][col] = player2;
}
// 打印棋盘
printChessboard();
// 判断是否胜利
if (isWin(row, col)) {
if (currentPlayer == 1) {
printf("玩家1(X)胜利!\n");
} else {
printf("玩家2(O)胜利!\n");
}
break;
}
// 切换玩家
currentPlayer = (currentPlayer == 1) ? 2 : 1;
}
return 0;
}
```
这个五子棋项目中,主要包括棋盘的初始化、打印,玩家落子,判断胜利等功能。玩家1使用X棋子,玩家2使用O棋子,玩家可以通过输入行和列来落子。每次落子后会打印当前的棋盘,并判断是否有一方胜利。若存在胜利,会打印获胜玩家的信息,并结束游戏。若没有胜利,则切换到另一玩家继续游戏,直到有玩家胜利或结束游戏。
希望这个简单的五子棋C程序对你有所帮助!