三子棋c语言
时间: 2023-06-21 10:19:57 浏览: 96
三子棋是一种简单的棋类游戏,下面是一个简单的使用C语言编写的三子棋游戏程序:
```
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 3
#define COL 3
void Menu()
{
printf("*****************************\n");
printf("**********三子棋游戏**********\n");
printf("*****************************\n");
}
void Init(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
void Display(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j != col - 1)
{
printf("|");
}
}
printf("\n");
if (i != row - 1)
{
printf("---|---|---\n");
}
}
}
void PlayerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("请输入您要下的位置:");
scanf_s("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("该位置已经有棋子,请重新输入!\n");
}
}
else
{
printf("您输入的位置不合法,请重新输入!\n");
}
}
}
void ComputerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
char IsWin(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
if (board[i][0] != ' ' && board[i][0] == board[i][1] && board[i][1] == board[i][2])
{
return board[i][0];
}
}
for (i = 0; i < col; i++)
{
if (board[0][i] != ' ' && board[0][i] == board[1][i] && board[1][i] == board[2][i])
{
return board[0][i];
}
}
if (board[0][0] != ' ' && board[0][0] == board[1][1] && board[1][1] == board[2][2])
{
return board[0][0];
}
if (board[0][2] != ' ' && board[0][2] == board[1][1] && board[1][1] == board[2][0])
{
return board[0][2];
}
if (IsFull(board, row, col))
{
return 'Q';
}
return 'C';
}
int IsFull(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
void Game()
{
char board[ROW][COL] = { 0 };
char result = 0;
srand((unsigned int)time(NULL));
Init(board, ROW, COL);
Display(board, ROW, COL);
while (1)
{
PlayerMove(board, ROW, COL);
Display(board, ROW, COL);
result = IsWin(board, ROW, COL);
if (result != 'C')
{
break;
}
ComputerMove(board, ROW, COL);
Display(board, ROW, COL);
result = IsWin(board, ROW, COL);
if (result != 'C')
{
break;
}
}
if (result == '*')
{
printf("恭喜您,您赢了!\n");
}
else if (result == '#')
{
printf("很遗憾,电脑赢了!\n");
}
else
{
printf("平局!\n");
}
}
int main()
{
int input = 0;
do
{
Menu();
printf("请选择:");
scanf_s("%d", &input);
switch (input)
{
case 1:
Game();
break;
case 0:
printf("退出游戏!\n");
break;
default:
printf("选择错误,请重新输入!\n");
break;
}
} while (input);
return 0;
}
```
这个程序中,使用了一个二维数组来表示棋盘,其中'*'表示玩家下的棋子,'#'表示电脑下的棋子。程序中实现了玩家下棋、电脑下棋、判断胜负等功能。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)