标签:col char exit eof 屏幕 efault system boa chess
第一步:建立头文件game.h
#ifndef __GAME_H__
#define __GAME_H__
#include<stdio.h>
#define Row 3
#define Col 3
void ChessBoard(char arr[Row][Col], int row, int col);
void DisplayBoard(char arr[Row][Col], int row, int col);
void PlayerMove(char arr[Row][Col], int row, int col);
void ComputerMove(char arr[Row][Col], int row, int col);
char CheckWin(char arr[Row][Col], int row, int col);
int IsFull(char arr[Row][Col], int row, int col);
#endif//__GAME_H__
第二步:写game.c,包含可调用函数。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "game.h"
#include <string.h>
void ChessBoard(char arr[Row][Col], int row, int col)
{
memset(arr, ‘ ‘, row*col*sizeof(arr[0][0]));
}
void DisplayBoard(char arr[Row][Col], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf(" %c ",arr[i][j]);
if (j != (col - 1))
{
printf("|");
}
}
printf("\n");
if (i != (row - 1))
{
for (j = 0; j < col; j++)
{
printf("___", arr[i][j]);
if (j != (col - 1))
{
printf("|");
}
}
printf("\n");
}
else
{
for (j = 0; j < col; j++)
{
printf(" ");
if (j != (col - 1))
{
printf("|");
}
}
printf("\n");
}
}
}
void PlayerMove(char arr[Row][Col], 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 <= row) && (y >= 1) && (y <= col))
{
if (arr[x - 1][y - 1] == ‘ ‘)
{
arr[x - 1][y - 1] = ‘X‘;
break;
}
else
{
printf("坐标被占用\n");
}
}
else
{
printf("坐标非法\n");
}
}
}
void ComputerMove(char arr[Row][Col], int row, int col)
{
printf("电脑走\n");
while (1)
{
int x = rand() % row;
int y = rand() % col;
if (arr[x][y] == ‘ ‘)
{
arr[x][y] = ‘0‘;
break;
}
}
}
char CheckWin(char arr[Row][Col], int row, int col)
{
int i = 0;
int j = 0;
int flag = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col-1; j++)
{
if ((arr[i][j] == arr[i][j+1]) && (arr[i][0]!=‘ ‘))
{
flag = 1;
}
else
{
flag = 0;
break;
}
}
if (flag == 1)
{
return arr[i][0];
}
}
for (j = 0; j < col; j++)
{
for (i = 0; i < row-1; i++)
{
if ((arr[i][j]==arr[i+1][j]) && (arr[0][j]!=‘ ‘))
{
flag = 1;
}
else
{
flag = 0;
break;
}
}
if (flag == 1)
{
return arr[0][j];
}
}
for (i = 0; i < row-1; i++)
{
if ((arr[i][i]== arr[i+1][i+1]) && (arr[0][0]!=‘ ‘))
{
flag = 1;
}
else
{
flag = 0;
break;
}
}
if (flag == 1)
{
return arr[0][0];
}
for (i = 0; i < row-1; i++)
{
if ((arr[i][row-i-1]==arr[i+1][row-i-2]) && (arr[0][row-1]!=‘ ‘))
{
flag = 1;
}
else
{
flag = 0;
break;
}
}
if (flag == 1)
{
return arr[0][row-1];
}
if (IsFull(arr, row, col) == 1)
{
return ‘q‘;
}
return ‘ ‘;
}
int IsFull(char arr[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 (arr[i][j] == ‘ ‘)
{
return 0;
}
}
}
return 1;
}
第三步:写测试函数main.c
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "game.h"
void menu()
{
printf("********************\n");
printf("***** 1.Play *****\n");
printf("***** 0.Exit *****\n");
printf("********************\n");
}
void game()
{
char arr[Row][Col] = {0};
char ret = 0;
ChessBoard(arr, Row, Col);//初始化棋盘
DisplayBoard(arr, Row, Col);//在屏幕上打印棋盘
do
{
PlayerMove(arr, Row, Col);//玩家走
ret = CheckWin(arr, Row, Col);//判断输赢
DisplayBoard(arr, Row, Col);//打印棋盘
if (ret != ‘ ‘)
{
break;
}
ComputerMove(arr, Row, Col);//电脑走
ret = CheckWin(arr, Row, Col);
DisplayBoard(arr, Row, Col);//打印棋盘
} while (ret == ‘ ‘);
if (ret == ‘X‘)
{
printf("玩家赢\n");
}
if (ret == ‘0‘)
{
printf("电脑赢\n");
}
if (ret == ‘q‘)
{
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();
system("pause");
return 0;
}
运行结果如下:
标签:col char exit eof 屏幕 efault system boa chess
原文地址:https://www.cnblogs.com/GO-lq/p/8977325.html