简简单单编写了一个五子棋的小游戏,运行还比较正常!
代码如下,想要的直接复制哦!
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int ROW = 15;
const int COL = 15;
void drawBoard(char board[ROW][COL]);
void playerMove(char board[ROW][COL], char player, int &row, int &col);
bool checkWin(char board[ROW][COL], char player, int row, int col);
int main()
{
char board[ROW][COL] = { {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '} };
char player1 = 'X';
char player2 = 'O';
char winner = ' ';
int row, col, moveCount = 0;
bool player1Turn = true;
// 随机决定先手玩家
srand(time(0));
player1Turn = (rand() % 2 == 0);
// 游戏主循环
do
{
system("cls"); // 清屏
drawBoard(board);
if (player1Turn)
{
cout << "玩家1(X)请下棋:";
playerMove(board, player1, row, col);
}
else
{
cout << "玩家2(O)请下棋:";
playerMove(board, player2, row, col);
}
// 放置棋子
board[row][col] = player1Turn ? player1 : player2;
moveCount++;
// 检查是否有胜者
if (checkWin(board, player1Turn ? player1 : player2, row, col))
{
winner = player1Turn ? player1 : player2;
break;
}
// 切换下一位玩家
player1Turn = !player1Turn;
} while (moveCount < ROW*COL);
// 游戏结束,显示胜者或平局
system("cls");
drawBoard(board);
if (winner == player1)
{
cout << "恭喜玩家1获胜!" << endl;
}
else if (winner == player2)
{
cout << "恭喜玩家2获胜!" << endl;
}
else
{
cout << "平局!" << endl;
}
return 0;
}
void drawBoard(char
board[ROW][COL])
{
cout << " ";
for (int i = 0; i < COL; i++)
{
cout << i+1 << " ";
}
cout << endl;
for (int i = 0; i < ROW; i++)
{
cout << i+1 << " ";
for (int j = 0; j < COL; j++)
{
cout << "|" << board[i][j];
}
cout << "|" << endl;
}
}
void playerMove(char board[ROW][COL], char player, int &row, int &col)
{
char buffer[256];
do
{
cout << "请输入行号和列号(用空格隔开):";
cin.getline(buffer, 256);
row = buffer[0] - '1';
col = buffer[2] - '1';
} while (row < 0 || row >= ROW || col < 0 || col >= COL || board[row][col] != ' ');
}
bool checkWin(char board[ROW][COL], char player, int row, int col)
{
int count;
// 横向检查
count = 1;
for (int j = col-1;j>=0;j--)
{
if (board[row][j] == player)
{
count++;
}
else
{
break;
}
}
for (int j = col+1;j<COL;j++)
{
if (board[row][j] == player)
{
count++;
}
else
{
break;
}
}
if (count >= 5) return true;
// 纵向检查
count = 1;
for (int i = row-1;i>=0;i--)
{
if (board[i][col] == player)
{
count++;
}
else
{
break;
}
}
for (int i = row+1;i<ROW;i++)
{
if (board[i][col] == player)
{
count++;
}
else
{
break;
}
}
if (count >= 5) return true;
// 斜向检查(\)
count = 1;
for (int i = row-1, j = col-1;i>=0 && j>=0;i--, j--)
{
if (board[i][j] == player)
{
count++;
}
else
{
break;
}
}
for (int i = row+1, j = col+1;i<ROW && j<COL;i++, j++)
{
if (board[i][j] == player)
{
count++;
}
else
{
break;
}
}
if (count >= 5) return true;
// 斜向检查(/)
count = 1;
for (int i = row-1, j = col+1;i>=0 && j<COL;i--, j++)
{
if (board[i][j] == player)
{
count++;
}
else
{
break;
}
}
for (int i = row+1, j = col-1;i<ROW && j>=0;i++, j--)
{
if (board[i][j] == player)
{
count++;
}
else
{
break;
}
}
if (count >= 5) return true;
// 没有胜者
return false;
}