game.h
#ifndef __GAME_H__
#define __GAME_H__
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.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);
#endif //__GAME_H__
//
//函数实现
void InitBoard(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 DisplayBoard(char board[ROW][COL], int row, int col) //打印棋盘
{
int i=0;
for(i=0;i<row;i++)
{
printf(" %c | %c | %c \n",board[i][0],board[i][1],board[i][2]);
if(i<row-1)
{
printf("---|---|---\n");
}
}
}
void PlayerMove(char board[ROW][COL], int row, int col) //玩家走
{
int x=0;
int y=0;
do
{
printf("你走>:");
scanf("%d %d",&x,&y);
if(x<1||x>row||y<1||y>col)
{
printf("输入有误!请重新输入\n");
}
}while(!(x>=1&&x<=row&&y>=1&&y<=col));
if(board[x-1][y-1]==' ')
{
board[x-1][y-1]='x';
}
else
{
printf("走错地方了!\n");PlayerMove(board,ROW,COL);
}
}
void Computermove(char board[ROW][COL], int row, int col) //电脑走,电脑会判断,会设法让"它"赢
{ //此处代码过于臭长
int x=0;
int y=0;
//判断第一行'o'
if ((board[0][2]==' ')&&(board[0][0]=='o'&&board[0][1]=='o'))
{ x=0;
y=2;
}
else if ((board[0][1]==' ')&&(board[0][0]=='o'&&board[0][2]=='o'))
{
x=0;
y=1;
}
else if ((board[0][0]==' ')&&(board[0][1]=='o'&&board[0][2]=='o'))
{
x=0;
y=0;
}
//判断第二行'o'
else if ((board[1][2]==' ')&&(board[1][0]=='o'&&board[1][1]=='o'))
{
x=1;
y=2;
}
else if ((board[1][1]==' ')&&(board[1][0]=='o'&&board[1][2]=='o'))
{
x=1;
y=1;
}
else if ((board[1][0]==' ')&&(board[1][1]=='o'&&board[1][2]=='o'))
{
x=1;
y=0;
}
//判断第三行'o'
else if ((board[2][2]==' ')&&(board[2][0]=='o'&&board[2][1]=='o'))
{
x=2;
y=2;
}
else if ((board[2][1]==' ')&&(board[2][0]=='o'&&board[2][2]=='o'))
{
x=2;
y=1;
}
else if ((board[2][0]==' ')&&(board[2][1]=='o'&&board[2][2]=='o'))
{
x=2;
y=0;
}
//判断第一列'o'
else if ((board[2][0]==' ')&&(board[0][0]=='o'&&board[1][0]=='o'))
{
x=2;
y=0;
}
else if ((board[1][0]==' ')&&(board[0][0]=='o'&&board[2][0]=='o'))
{
x=1;
y=0;
}
else if ((board[0][0]==' ')&&(board[1][0]=='o'&&board[2][0]=='o'))
{
x=0;
y=0;
}
//判断第二列'o'
else if ((board[2][1]==' ')&&(board[0][1]=='o'&&board[1][1]=='o'))
{
x=2;
y=1;
}
else if ((board[1][1]==' ')&&(board[0][1]=='o'&&board[2][1]=='o'))
{
x=1;
y=1;
}
else if ((board[0][1]==' ')&&(board[1][1]=='o'&&board[2][1]=='o'))
{
x=0;
y=1;
}
//判断第三列'o'
else if ((board[2][2]==' ')&&(board[0][2]=='o'&&board[1][2]=='o'))
{
x=2;
y=2;
}
else if ((board[1][2]==' ')&&(board[0][2]=='o'&&board[2][2]=='o'))
{
x=1;
y=2;
}
else if ((board[0][2]==' ')&&(board[1][2]=='o'&&board[2][2]=='o'))
{
x=0;
y=2;
}
//判断右斜对角↖'o'
else if ((board[2][2]==' ')&&(board[0][0]=='o'&&board[1][1]=='o'))
{
x=2;
y=2;
}
else if ((board[1][1]==' ')&&(board[0][0]=='o'&&board[2][2]=='o'))
{
x=1;
y=1;
}
else if ((board[0][0]==' ')&&(board[1][1]=='o'&&board[2][2]=='o'))
{
x=0;
y=0;
}
//判断左斜对角↗'o'
else if ((board[2][0]==' ')&&(board[0][2]=='o'&&board[1][1]=='o'))
{
x=2;
y=0;
}
else if ((board[1][1]==' ')&&(board[0][2]=='o'&&board[2][0]=='o'))
{
x=1;
y=1;
}
else if ((board[0][2]==' ')&&(board[1][1]=='o'&&board[2][0]=='o'))
{
x=0;
y=2;
}
//判断第一行'x'
else if ((board[0][2]==' ')&&(board[0][0]=='x'&&board[0][1]=='x'))
{ x=0;
y=2;
}
else if ((board[0][1]==' ')&&(board[0][0]=='x'&&board[0][2]=='x'))
{
x=0;
y=1;
}
else if ((board[0][0]==' ')&&(board[0][1]=='x'&&board[0][2]=='x'))
{
x=0;
y=0;
}
//判断第二行'x'
else if ((board[1][2]==' ')&&(board[1][0]=='x'&&board[1][1]=='x'))
{
x=1;
y=2;
}
else if ((board[1][1]==' ')&&(board[1][0]=='x'&&board[1][2]=='x'))
{
x=1;
y=1;
}
else if ((board[1][0]==' ')&&(board[1][1]=='x'&&board[1][2]=='x'))
{
x=1;
y=0;
}
//判断第三行'x'
else if ((board[2][2]==' ')&&(board[2][0]=='x'&&board[2][1]=='x'))
{
x=2;
y=2;
}
else if ((board[2][1]==' ')&&(board[2][0]=='x'&&board[2][2]=='x'))
{
x=2;
y=1;
}
else if ((board[2][0]==' ')&&(board[2][1]=='x'&&board[2][2]=='x'))
{
x=2;
y=0;
}
//判断第一列'x'
else if ((board[2][0]==' ')&&(board[0][0]=='x'&&board[1][0]=='x'))
{
x=2;
y=0;
}
else if ((board[1][0]==' ')&&(board[0][0]=='x'&&board[2][0]=='x'))
{
x=1;
y=0;
}
else if ((board[0][0]==' ')&&(board[1][0]=='x'&&board[2][0]=='x'))
{
x=0;
y=0;
}
//判断第二列'x'
else if ((board[2][1]==' ')&&(board[0][1]=='x'&&board[1][1]=='x'))
{
x=2;
y=1;
}
else if ((board[1][1]==' ')&&(board[0][1]=='x'&&board[2][1]=='x'))
{
x=1;
y=1;
}
else if ((board[0][1]==' ')&&(board[1][1]=='x'&&board[2][1]=='x'))
{
x=0;
y=1;
}
//判断第三列'x'
else if ((board[2][2]==' ')&&(board[0][2]=='x'&&board[1][2]=='x'))
{
x=2;
y=2;
}
else if ((board[1][2]==' ')&&(board[0][2]=='x'&&board[2][2]=='x'))
{
x=1;
y=2;
}
else if ((board[0][2]==' ')&&(board[1][2]=='x'&&board[2][2]=='x'))
{
x=0;
y=2;
}
//判断右斜对角↖'x'
else if ((board[2][2]==' ')&&(board[0][0]=='x'&&board[1][1]=='x'))
{
x=2;
y=2;
}
else if ((board[1][1]==' ')&&(board[0][0]=='x'&&board[2][2]=='x'))
{
x=1;
y=1;
}
else if ((board[0][0]==' ')&&(board[1][1]=='x'&&board[2][2]=='x'))
{
x=0;
y=0;
}
//判断左斜对角↗'x'
else if ((board[2][0]==' ')&&(board[0][2]=='x'&&board[1][1]=='x'))
{
x=2;
y=0;
}
else if ((board[1][1]==' ')&&(board[0][2]=='x'&&board[2][0]=='x'))
{
x=1;
y=1;
}
else if ((board[0][2]==' ')&&(board[1][1]=='x'&&board[2][0]=='x'))
{
x=0;
y=2;
}
//随机赋值
else
{
x=rand()%row;
y=rand()%col;
}
if(board[x][y]==' ')
{
board[x][y]='o';
}
else
{
Computermove(board,ROW,COL);
}
}
int ful_is(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;
}
char IsWin(char board[ROW][COL], int row, int col)// x 你赢 o 电脑赢 ‘p’平局 1继续
{
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<row;i++)
{
if((board[0][i]!=' ')&&(board[0][i]==board[1][i])&&(board[1][i]==board[2][i]))
{
return board[0][i];
}
}
if((board[1][1]!=' ')&&(board[0][0]==board[1][1])&&(board[1][1]==board[2][2]))
return board[1][1];
else if((board[1][1]!=' ')&&(board[0][2]==board[1][1])&&(board[1][1]==board[2][0]))
return board[1][1];
if(ful_is(board,row,col))
{
return 'p';
}
else
return '1';
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void game()
{
char board[ROW][COL]={0};
char win=0;
int flag=1;
int first=0;
do
{
printf("选择:>1.电脑先走 2.你先走\n");
scanf("%d",&first);
if ((first==1)||(first==2))
{
flag=0;
}
else
printf("输入有误,重新输入!\n");
} while (flag);
InitBoard(board,ROW,COL);
//DisplayBoard(board,ROW,COL);
do{
flag=1;
if (first==1)
{
printf("电脑走>:\n");
Computermove(board,ROW,COL);
DisplayBoard(board,ROW,COL);
PlayerMove(board,ROW,COL);
DisplayBoard(board,ROW,COL);
}
else if (first==2)
{
DisplayBoard(board,ROW,COL);
PlayerMove(board,ROW,COL);
//system("cls"); // 清屏
DisplayBoard(board,ROW,COL);
}
first=0;
system("cls"); // 清屏
printf("电脑走>:\n");
Computermove(board,ROW,COL);
DisplayBoard(board,ROW,COL);
win=IsWin(board,ROW,COL);
if(win=='o')
{
flag=0;
printf("电脑赢了!\n");
break;
}
else if(win=='p')
{
flag=0;
printf("平局!\n");
break;
}
PlayerMove(board,ROW,COL);
//system("cls"); // 清屏
DisplayBoard(board,ROW,COL);
win=IsWin(board,ROW,COL);
if(win=='x')
{
flag=0;
printf("你赢了!\n");
break;
}
else if(win=='p')
{
flag=0;
printf("平局!\n");
break;
}
}while(flag);
}
void menu()
{
printf("******************************\n");
printf("*** 1.play 0.exit ***\n");
printf("******************************\n");
}
void test()
{
int input;
do{
menu();
printf("请选择>:");
scanf("%d",&input);
switch(input)
{
case 1:
game();break;
case 0:
printf("退出!\n");break;
default:
break;
}
}while(input);
}
int main()
{
srand((unsigned int)time(NULL));
test();
return 0;
}