#pragma once
#include<stdio.h>
#define ROW 3//利用宏来实现棋盘的大小
#define COL 3
void InitBoard(char board[ROW][COL], int row, int col);//棋盘初始化
void DisplayBoard(char board[ROW][COL],int row, int col);//棋盘打印函数
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<Windows.h>
#define ROW 3//利用宏来实现棋盘的大小
#define COL 3
void InitBoard(char board[ROW][COL], int row, int col);//棋盘初始化
void DisplayBoard(char board[ROW][COL], int row, int col);//棋盘打印函数
void PlayerMove(char board[ROW][COL], int row, int col);//玩家下棋
void ComputerMove(char board[ROW][COL], int row, int col);//电脑下棋
char ISWIN(char board[ROW][COL], int row, int col);//判断输赢
int ISFULL(char board[ROW][COL], int row, int col);//判断棋盘是否已满
game.h
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void InitBoard(char board[ROW][COL], int row, int col)//棋盘初始化
{
int i = 0, j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
//void DisplayBoard(char board[3][3], int row, int col)//棋盘打印函数
//{
// for (int i = 0; i < 3; i++)
// {
// printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
// if (i < 2)
// {
// printf("---|---|---\n");
// }
// }
//}
void DisplayBoard(char board[ROW][COL], int row, int col)//棋盘打印函数
{
int i = 0, 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)
{
for (j = 0; j < 3; j++)
{
printf("---");//分割行
if (j < col - 1)
{
printf("|");
}
}
}
printf("\n");
}
}
void PlayerMove(char board[ROW][COL], int row, int col)//玩家下棋
{
int x, y;
printf("玩家走:\n");
while (1)
{
printf("请输入你所要落子的坐标:");
scanf("%d%d", &x, &y);
if (!((x > 0 && x <= row) && (y > 0 && y <= col)))
{
printf("该坐标为非法坐标,请重新输入!\n");//坐标非法
}
else if (board[x - 1][y - 1] != ' ')//坐标被占用
{
printf("该坐标已被占用,请重新下子!\n");
}
else
{
board[x - 1][y - 1] = '*';//玩家落子,暂时用 * 来表示
break;
}
}
system("cls");
}
void ComputerMove(char board[ROW][COL], int row, int col)//电脑下棋
{
int x, y;
printf("电脑走:\n");
Sleep(1000);
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';//这里我们用 # 来表示电脑下棋
break;
}
}
system("cls");
}
int ISFULL(char board[ROW][COL], int row, int col)
{
int i = 0, j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
return 0;
}
}
return 1;
}
char ISWIN(char board[ROW][COL], int row, int col)
{
int i = 0, j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col - 2; j++)
{
//判断横行
if (board[i][j] == board[i][j + 1] && board[i][j] == board[i][j + 2] && board[i][j] != ' ')
return board[i][j];
//判断主对角线
else if (board[i][j] == board[i + 1][j + 1] && board[i + 2][j + 2] == board[i][j] && board[i][j] != ' ')
return board[i][j];
//判断副对角线
else if (board[col - 1 - i][j] == board[col - 2 - i][j + 1] && board[col - 3 - i][j + 2] == board[col - 1 - i][j] && board[col - 1 - i][j] != ' ')
return board[col - 1 - i][j];
}
}
for (i = 0; i < row - 2; i++)
{
for (j = 0; j < col; j++)
{
//判断竖行
if (board[i][j] == board[i + 1][j] && board[i][j] == board[i + 2][j] && board[i][j] != ' ')
return board[i][j];
}
}
//判断是否满盘---放在最后是因为最后一步的判断
if (1 == ISFULL(board, row, col))
{
return 'F';
}
return 'C';
}
game.c
#define _CRT_SECURE_NO_WARNINGS 1//加这一句话是因为笔者采用的是 VS 编译器,为了防止一些不必要的错误出现
#include "game.h"
void menu()//列出可供玩家选择的模式
{
printf("**************************************************************\n");
printf("***************** 1.play ****************\n");
printf("***************** 0.exit ****************\n");
printf("**************************************************************\n");
}
void play()
{
int ret;
char board[ROW][COL] = { 0 };
InitBoard(board, ROW, COL);
DisplayBoard(board, ROW, COL);
while (1)
{
PlayerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = ISWIN(board, ROW, COL);
{
if (ret != 'C')
break;
}
ComputerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = ISWIN(board, ROW, COL);
{
if (ret != 'C')
break;
}
}
switch (ret)
{
case 'F':
printf("和局!\n");
system("pause");
system("cls");
break;
case '#':
printf("电脑赢!\n");
system("pause");
system("cls");
break;
case '*':
printf("玩家赢!\n");
system("pause");
system("cls");
break;
default:
break;
}
}
int main()
{
srand((unsigned)time(NULL));//随机种子的初始化
int input;//在这里,我们利用玩家选择的模式来控制循环的终止
do
{
menu();
printf("请输入你的选择:");
scanf("%d", &input);
switch (input)
{
case 1://play
system("cls");
play();
break;
case 0://退出
printf("欢迎下次再来!\n");
break;
default://当玩家输入了非法字符,让其重新选择
system("cls");
printf("输入错误,请重新输入!\n");
}
} while (input);//当input为0时,停止循环
return 0;
}
#pragma once
#include <stdio.h>
#include<stdlib.h>
#include<time.h>
#include<Windows.h>
#define ROW 10
#define COL 10
//初始化棋盘
void InitBoard(char board[ROW][COL], int row, int col);
//打印棋盘
void DisplayBoard(char board[ROW][COL], int row, int col);
//玩家走
void Player1Move(char board[ROW][COL],int row, int col);
//玩家2走
void Player2Move(char board[ROW][COL], int row, int col);
//电脑走
void ComputerMove(char board[ROW][COL], int row, int col);
//判断输赢
char ISWIN(char board[ROW][COL], int row, int col);
//判断棋盘是否已满
int ISFULL(char board[ROW][COL], int row, int col);
game.h
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void InitBoard(char board[ROW][COL], int row, int col)
{
int i = 0, j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
void DisplayBoard(char board[ROW][COL], int row, int col)
{
int i = 0, j = 0;
for (i = 0; i < col; i++)
printf(" %d", i + 1);
printf("\n");
for (i = 0; i < row; i++)
{
printf("%2d", i + 1);
for (j = 0; j < col; j++)
{
printf(" %c ",board[i][j]);
if (j < col - 1)
printf("|");
}
printf("\n");
//打印分割行
if (i < row - 1)
{
printf(" ");
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
printf("\n");
}
}
}
void Player1Move(char board[ROW][COL], int row, int col)
{
int i = 0, j = 0;
while (1)
{
printf("请输入玩家1要下的棋的坐标:");
scanf("%d%d", &i, &j);
//检查是否越界
if (1 <= i && i <= row && j >= 1 && j <= col)
{
if (board[i-1][j-1] != ' ')
printf("此坐标已被占用!\n");
else
{
board[i-1][j-1] = '*';
break;
}
}
else
printf("坐标非法访问!\n");
}
system("cls");
}
void Player2Move(char board[ROW][COL], int row, int col)
{
int i = 0, j = 0;
while (1)
{
printf("请输入玩家2要下的棋的坐标:");
scanf("%d%d", &i, &j);
//检查是否越界
if (1 <= i && i <= row && j >= 1 && j <= col)
{
if (board[i - 1][j - 1] != ' ')
printf("此坐标已被占用!\n");
else
{
board[i - 1][j - 1] = '#';
break;
}
}
else
printf("坐标非法访问!\n");
}
system("cls");
}
//void ComputerMove(char board[ROW][COL], int row, int col)
//{
// int i = 0, j = 0;
// printf("电脑走:\n");
// while (1)
// {
// i = rand() % row;
// j = rand() % col;
// if (board[i][j] == ' ')
// {
// board[i][j] = '#';
// break;
// }
// }
// Sleep(1000);
// system("cls");
//}
int ISFULL(char board[ROW][COL], int row, int col)
{
int i = 0, j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
return 0;
}
}
return 1;
}
char ISWIN(char board[ROW][COL], int row, int col)
{
int i = 0, j = 0;
for (i = 0; i < col; i++)
{
for (j = 0; j < col - 4; j++)
{
//判断横行
if (board[i][j] == board[i][j + 1] && board[i][j] == board[i][j + 2] && board[i][j] == board[i][j + 3] && board[i][j] == board[i][j + 4] && board[i][j] != ' ')
return board[i][j];
//判断对角线
else if (board[i][j] == board[i + 1][j + 1] && board[i + 2][j + 2] == board[i][j] && board[i][j] == board[i + 3][j + 3] && board[i][j] == board[i + 4][j + 4] && board[i][j] != ' ')
return board[i][j];
else if (board[col - 1 - i][j] == board[col - 2 - i][j + 1] && board[col - 3 - i][j + 2] == board[col - 1 - i][j] && board[col - 4 - i][j + 3] == board[col - 2 - i][j + 1] && board[col - 5 - i][j + 4] == board[col - 2 - i][j + 1] && board[col - 1 - i][j] != ' ')
return board[col - 1 - i][j];
}
}
for (i = 0; i < col - 4; i++)
{
for (j = 0; j < col ; j++)
{
//判断竖行
if (board[i][j] == board[i + 1][j] && board[i][j] == board[i + 2][j] && board[i][j] == board[i + 3][j] && board[i][j] == board[i + 4][j] && board[i][j] != ' ')
return board[i][j];
}
}
//判断是否满盘
if (1 == ISFULL(board, row, col))
{
return 'f';
}
return 'c';
}
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
printf("*********************************************\n");
printf("******* 1.play *********\n");
printf("******* 0.quit *********\n");
printf("*********************************************\n");
}
int choice()
{
int input = 0;
printf("请输入你的选择:");
scanf("%d", &input);
return input;
}
void game()
{
char ret;
char board[ROW][COL] = { 0 };
InitBoard(board, ROW, COL);
DisplayBoard(board, ROW, COL);
while (1)
{
Player1Move(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = ISWIN(board, ROW, COL);
{
if (ret != 'c')
break;
}
Player2Move(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = ISWIN(board, ROW, COL);
{
if (ret != 'c')
break;
}
}
switch (ret)
{
case 'f':
printf("和局!\n");
break;
case '#':
//printf("电脑赢!\n");
printf("玩家2赢!\n");
break;
case '*':
printf("玩家1赢!\n");
break;
default:
break;
}
}
void play()
{
int input;
do
{
menu();
input = choice();
system("cls");
switch (input)
{
case 1:
game();
break;
case 0:
printf("谢谢使用,欢迎下次再来!\n");
break;
default:
system("cls");
printf("输入错误,请重新输入!\n");
break;
}
} while (input);
}
int main()
{
play();
return 0;
}