帮我一段 c++ 的五子棋
时间: 2023-05-23 18:05:17 浏览: 86
好的,以下是一段 C 语言的五子棋代码:
```c
#include<stdio.h>
#define LEN 15
#define BLACK 'x'
#define WHITE 'o'
#define EMPTY ' '
char board[LEN][LEN];
int main(){
int i,j;
//初始化棋盘
for(i=0;i<LEN;i++){
for(j=0;j<LEN;j++){
board[i][j] = EMPTY;
}
}
//打印棋盘
printf(" ");
for(i=0;i<LEN;i++){
printf("%d",i+1);
}
printf("\n");
for(i=0;i<LEN;i++){
printf("%c",i+'a');
for(j=0;j<LEN;j++){
printf("%c",board[i][j]);
}
printf("\n");
}
return 0;
}
```
这段代码实现了一个简单的五子棋游戏棋盘,并在控制台输出了棋盘。其中 `board` 数组存储了棋盘上每个位置的状态(黑子、白子或空), `BLACK` 、 `WHITE` 和 `EMPTY` 分别表示黑子、白子和空位的符号。代码通过 `for` 循环填充棋盘并打印输出。
相关问题
帮我写一个五子棋的c++代码
当然,下面是一个简单的五子棋游戏的C++代码示例:
```cpp
#include <iostream>
#include <vector>
const int BOARD_SIZE = 15;
const char EMPTY = '-';
const char PLAYER_ONE = 'X';
const char PLAYER_TWO = 'O';
std::vector<std::vector<char>> board(BOARD_SIZE, std::vector<char>(BOARD_SIZE, EMPTY));
void printBoard() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
std::cout << board[i][j] << " ";
}
std::cout << std::endl;
}
}
bool isFull() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == EMPTY) {
return false;
}
}
}
return true;
}
bool checkWin(char player) {
// 检查行
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j <= BOARD_SIZE - 5; j++) {
bool win = true;
for (int k = 0; k < 5; k++) {
if (board[i][j + k] != player) {
win = false;
break;
}
}
if (win) {
return true;
}
}
}
// 检查列
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j <= BOARD_SIZE - 5; j++) {
bool win = true;
for (int k = 0; k < 5; k++) {
if (board[j + k][i] != player) {
win = false;
break;
}
}
if (win) {
return true;
}
}
}
// 检查主对角线
for (int i = 0; i <= BOARD_SIZE - 5; i++) {
for (int j = 0; j <= BOARD_SIZE - 5; j++) {
bool win = true;
for (int k = 0; k < 5; k++) {
if (board[i + k][j + k] != player) {
win = false;
break;
}
}
if (win) {
return true;
}
}
}
// 检查副对角线
for (int i = 4; i < BOARD_SIZE; i++) {
for (int j = 0; j <= BOARD_SIZE - 5; j++) {
bool win = true;
for (int k = 0; k < 5; k++) {
if (board[i - k][j + k] != player) {
win = false;
break;
}
}
if (win) {
return true;
}
}
}
return false;
}
bool makeMove(int row, int col, char player) {
if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE || board[row][col] != EMPTY) {
return false;
}
board[row][col] = player;
return true;
}
int main() {
char currentPlayer = PLAYER_ONE;
while (true) {
std::cout << "当前棋盘:" << std::endl;
printBoard();
int row, col;
std::cout << "玩家 " << currentPlayer << ",请输入要落子的坐标(行 列):";
std::cin >> row >> col;
if (makeMove(row, col, currentPlayer)) {
if (checkWin(currentPlayer)) {
std::cout << "玩家 " << currentPlayer << " 获胜!" << std::endl;
break;
} else if (isFull()) {
std::cout << "游戏结束,平局!" << std::endl;
break;
}
currentPlayer = (currentPlayer == PLAYER_ONE) ? PLAYER_TWO : PLAYER_ONE;
} else {
std::cout << "无效的落子位置,请重新输入!" << std::endl;
}
}
return 0;
}
```
这段代码实现了一个简单的五子棋游戏。玩家可以交替输入要落子的坐标(行和列),程序会根据玩家输入的位置进行落子判断,并打印出当前棋盘状态。当有一方玩家获胜或者棋盘已满时,游戏结束。注意,此代码只实现了基本的功能,还可以根据需要进行扩展和优化。
写一段五子棋C++代码
#include<stdio.h>
int main()
{
int chessboard[15][15],i,j;
//初始化棋盘
for(i=0;i<15;i++)
{
for(j=0;j<15;j++)
{
chessboard[i][j]=0;
}
}
//游戏过程
while(1)
{
//黑棋落子
printf("请黑方下棋:\n");
scanf("%d%d",&i,&j);
chessboard[i][j]=1;
//判断输赢
if(checkwin(1))
{
printf("黑方赢了!\n");
break;
}
//白棋落子
printf("请白方下棋:\n");
scanf("%d%d",&i,&j);
chessboard[i][j]=2;
//判断输赢
if(checkwin(2))
{
printf("白方赢了!\n");
break;
}
}
return 0;
}
//检查是否胜利
int checkwin(int chessman)
{
int i,j,count;
//横向检查
for(i=0;i<15;i++)
{
count=0;
for(j=0;j<15;j++)
{
if(chessboard[i][j]==chessman)
count++;
else
count=0;
if(count==5)
return 1;
}
}
//纵向检查
for(j=0;j<15;j++)
{
count=0;
for(i=0;i<15;i++)
{
if(chessboard[i][j]==chessman)
count++;
else
count=0;
if(count==5)
return 1;
}
}
//左斜检查
for(i=0;i<11;i++)
{
for(j=0;j<11;j++)
{
count=0;
int k;
for(k=0;k<5;k++)
{
if(chessboard[i+k][j+k]==chessman)
count++;
else
count=0;
if(count==5)
return 1;
}
}
}
//右斜检查
for(i=0;i<11;i++)
{
for(j=4;j<15;j++)
{
count=0;
int k;
for(k=0;k<5;k++)
{
if(chessboard[i+k][j-k]==chessman)
count++;
else
count=0;
if(count==5)
return 1;
}
}
}
return 0;
}我不明白你的问题,但是如果你在问如何用C语言编写一段五子棋的代码,那么我可以给你一个例子:
阅读全文