三子棋
主函数和菜单
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
printf("********0.exit********\n");
printf("********1.play********\n");
printf("**********************\n");
}
void game()
{
int ret = 0;
char board[3][3] = { 0 };
InitBoard(board, ROW, COL);
//打印棋盘
ShowBoard(board, ROW, COL);
while (1)
{
PlayerMove(board, ROW, COL);
//每次硬都要判断是否赢了比赛
ret = IsWin(board, ROW, COL);
if (ret == ' ')
{
break;
}
ShowBoard(board, ROW, COL);
ComputerMove(board, ROW, COL);
if (ret == ' ')
{
break;
}
ShowBoard(board, ROW, COL);
}
if (ret == 'x')
{
printf("玩家获胜\n");
}
else if (ret == 'O')
{
printf("电脑获胜\n");
}
else if (ret == 'Q')
{
printf("平局\n");
}
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择--》\n");
scanf("%d", &input);
switch (input)
{
case 1:
printf("游戏开始\n");
game();
break;
case 0:
printf("游戏退出\n");
break;
default:
printf("请输入正确的数字\n");
break;
}
} while (input);
return 0;
}
头文件
#ifndef __GAME_H__
#define __GAME_H__
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#define ROW 3
#define COL 3
//初始化棋盘
void InitBoard(char board[][ROW], int row, int col);
//打印棋盘
void ShowBoard(char board[][ROW], int row, int col);
//玩家移动
void PlayerMove(char board[][ROW], int row, int col);
//是否赢得了胜利
int IsWin(char board[][ROW], int row, int col);
//是否满了
int IsFull(char board[][ROW], int row, int col);
//电脑移动
void ComputerMove(char board[][ROW], int row, int col);
#endif //!__GAME_H__
游戏函数
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void InitBoard(char board[][ROW], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
board[i][j] =' ';
}
}
}
void ShowBoard(char board[][ROW], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
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 < col; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
}
printf("\n");
}
}
void PlayerMove(char board[][ROW], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("玩家移动\n");
printf("请输入坐标:\n");
scanf("%d %d", &x, &y);
if (x >= 1&& x <=3 && y>=1 && y <= 3)
{
if (board[x - 1][y - 1] == ' ')//当前位置没下棋子
{
board[x - 1][y - 1] = 'x';
break;
}
else
{
printf("此位置有棋子了\n");
}
}
else
{
printf("输入格式不正确\n");
}
}
}
int IsFull(char board[][ROW], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
int IsWin(char board[][ROW], int row, int col)
{
int i = 0;
for (i = 0; i <= row; i++)
{
//行的比较
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
{
return board[i][0];
}
//列的比较
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
{
return board[0][i];
}
}
//主对角线
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
{
return board[0][0];
}
//副对角线
if (board[0][2] == board[1][1] && board[1][1] == board[0][0] && board[0][2] != ' ')
{
return board[0][2];
}
if (IsFull( board ,row,col))
{
return 'Q';//平局
}
return ' ';//继续
}
void ComputerMove(char board[][ROW], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑移动\n");
while (1)
{
x = rand()%row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '0';
break;
}
}
}