鹏哥C语言三子棋实现
关键在于创造棋盘二维数组
#include "game.h"
void meun()
{
printf("***************\n");
printf("****play: 1/***\n");
printf("****exit: 2/***\n");
}
void play()
{
int ret = 0;
//初始化棋盘
char board[ROW][COL] = { 0 };
InitBoard(board, ROW, COL);
Display(board, ROW, COL);
//下棋
while (1)
{
//玩家下棋
PlayerMove(board, ROW, COL);
//判断输赢
ret=IsWin(board, ROW, COL);
if (ret != 4)
{
break;
}
Display(board, ROW, COL);
//电脑下棋
PcMove(board, ROW, COL);
//判断输赢
ret = IsWin(board, ROW, COL);
if (ret != 4)
{
break;
}
Display(board, ROW, COL);
}
if (ret == 1)
{
printf("玩家赢\n");
}
else if (ret == 2)
{
printf("电脑赢\n");
}
else if (ret == 3)
{
printf("平局\n");
}
Display(board, ROW, COL);
}
int main()
{
srand((unsigned int)time(NULL));
int output = 0;
do
{
meun();//菜单
printf("请选择>");
scanf("%d", &output);
switch (output)
{
case 1:
play();
break;
case 2:
printf("退出游戏");
break;
default:
printf("选择错误");
break;
}
} while (output != 2);
return 0;
}
将下棋函数的主要功能和函数声明放在其他.c和头文件.h中
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 3
#define COL 3
//初始化棋盘
void InitBoard(char board[ROW][COL], int row, int col);
//显示棋盘
void Display(char board[ROW][COL], int row, int col);
//玩家下棋
void PlayerMove(char board[ROW][COL], int row, int col);
//电脑下棋
void PcMove(char board[ROW][COL], int row, int col);
//判断输赢
int IsWin(char board[ROW][COL], int row, int col);
#include "game.h"
void InitBoard(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
void Display(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
if (i < row - 1)
{
for (int k = 0; k < col; k++)
{
printf("---");
if (k < col - 1)
{
printf("|");
}
}
printf("\n");
}
}
}
void PlayerMove(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
while (1)
{
printf("玩家输入落棋坐标>");
scanf("%d %d", &i, &j);
if (i >= 1 && i <= row && j >= 1 && j <= col)
{
if (board[i - 1][j - 1] == ' ')
{
board[i - 1][j - 1] = '*';
break;
}
else
{
printf("坐标已被占用,请重新输入\n");
}
}
else
{
printf("坐标错误,请重新输入\n");
}
}
}
void PcMove(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
printf("电脑下棋\n");
while (1)
{
i = rand() % row;
j = rand() % col;
if (board[i][j] == ' ')
{
board[i][j] = '#';
break;
}
}
}
int IsWin(char board[ROW][COL], int row, int col)
{
int flag = 0;
// flag=1 玩家赢
// flag=2 电脑赢
// flag=3 平局
// flag=4 游戏继续
// 检查每一行
for (int i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][0] == board[i][2])
{
if (board[i][0] == '*')
{
flag = 1;
break;
}
else if (board[i][0] == '#')
{
flag = 2;
break;
}
}
}
//检查每一列
for (int j = 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[0][j] == board[2][j])
{
if (board[0][j] == '*')
{
flag = 1;
break;
}
else if (board[0][j] == '#')
{
flag = 2;
break;
}
}
}
//检查对角线
if ((board[0][0] == board[1][1] && board[0][0] == board[2][2]) || (board[0][2] == board[1][1] && board[1][1] == board[2][0]))
{
if (board[1][1] == '*')
{
flag = 1;
}
else if(board[1][1] == '#')
{
flag = 2;
}
}
//检查平局
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (board[i][j] == ' ' && flag == 0)
{
flag = 4;
break;
}
}
}
if (flag == 0)
{
flag = 3;
}
return flag;
}