1.制作框架
2.利用函数以及循环完成井字棋的编程
3.开始和呆呆的机器人下棋吧
//game.h
#define row 3
#define col 3
#include<stdlib.h>;
#include<time.h>
#include<Windows.h>
void qpcsh(char arr[row][col], int a, int b);
void qpprint(char arr[row][col], int a, int b);
void wjxq(char arr[row][col], int a, int b);
void dnxq(char arr[row][col], int a, int b);
char win(char arr[row][col], int a, int b);
int isfull(char arr[row][col], int a, int b);
//game.c
#include"game.h"
int isfull(char arr[row][col], int a, int b)
{
int i = 0;
int j = 0;
for (i = 0; i < a; i++)
{
for (j = 0; j < b; j++)
{
if(arr[i][j] != ' ');
return 0;
}
}
return 1;
}
void qpcsh(char arr[row][col], int a, int b)
{
int i = 0;
for (i = 0; i < a; i++)
{
int j = 0;
for (j = 0; j < b; j++)
{
arr[i][j] = ' ';
}
}
}
void qpprint(char arr[row][col], int a, int b)
{
int i = 0;
for (i = 0; i < a; i++)
{
int j = 0;
for (j = 0; j < b; j++)
{
printf(" %c ", arr[i][j]);
if (j < b - 1)
printf("|");
}
printf("\n");
if (i < a - 1)
{
for (j = 0; j < b; j++)
{
printf("---");
if (j < b - 1)
printf("|");
}
printf("\n");
}
}
}
void wjxq(char arr[row][col], int a, int b)
{
int x = 0;
int y = 0;
while (1)
{
printf("请输入坐标\n");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= b && y >= 1 && y <= a)
{
if (arr[x - 1][y - 1] == ' ')
{
arr[x - 1][y - 1] = '*';
break;
}
else
{
printf("该坐标已经被占用\n");
}
}
else
{
printf("坐标非法重新输入\n");
}
}
}
void dnxq(char arr[row][col], int a, int b)
{
int x = 0;
int y = 0;
printf("\n电脑下棋\n\n");
while (1)
{
x = rand() % 2;
y = rand() % 2;
if (arr[x][y] == ' ')
{
Sleep(1000);
arr[x][y] = '#';
break;
}
}
}
char win(char arr[row][col], int a, int b)
{
int i = 0;
int j = 0;
for (i = 0; i < a; i++)
{
if (arr[i][0] == arr[i][1] && arr[i][1] == arr[i][2] && arr[i][0] != ' ')
return arr[i][0];
}
for(j=0;j<b;j++)
{
if (arr[0][j] == arr[1][j] && arr[1][j] == arr[2][j] && arr[0][j] != ' ')
{
return arr[0][j];
}
}
if (arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2] && arr[0][0] != ' ')
{
return arr[0][0];
}
if (arr[0][2] == arr[1][1] && arr[1][1] == arr[0][2] && arr[2][0] != ' ')
{
return arr[0][2];
}
if (1 == isfull(arr, a, b))
{
return 'q';
}
return 'c';
}
//test.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void game()
{
int ret = 0;
int c = 0;
char arr[row][col] = {0};
qpcsh(arr, row, col);
qpprint(arr, row, col);
while (1)
{
wjxq(arr, row, col);
qpprint(arr, row, col);
ret=win(arr, row, col);
if (ret != 'c')
break;
dnxq(arr, row, col);
qpprint(arr, row, col);
ret=win(arr, row, col);
if (ret != 'c')
break;
}
if (ret == '*')
printf("玩家赢\n");
else if (ret == '#')
printf("电脑赢\n");
else
printf("平局\n");
}
//玩家赢*
//电脑赢#
//平局q
//继续c
void menu()
{
printf("***************\n");
printf("*****1.play****\n");
printf("*****0.exit****\n");
printf("***************\n");
}
void test()
{
int input = 0;
do
{
menu();
printf("请选择\n");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,重新输入\n");
break;
}
} while (input);
}
int main()
{
srand((unsigned int)time(NULL));
test();
return 0;
}
怎么样???看完这个教程是不是也觉得有手就行???自己打一遍试试吧!