程序实现了菜单显示,棋盘打印,电脑与玩家信息记录,判断输赢(盘满为平),选择先走一方等功能。
在头文件中定义了行列变量,方便改变棋盘大小;判断输赢有不足,因为未想到如何用行列变量去写判断输赢的代码部分。
在头文件中进行了一部分的函数声明。
只在游戏文件自定义函数中有部分注释说明。
运行结果并未完全截图,运行环境Visual Studio 2013。
头文件(game.h):
#ifndef _GAME_H
#define _GAME_H
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <windows.h>
#include <time.h>
#define ROW 3
#define COL 3
void print_board(char arr1[ROW][COL], int row1, int col1);
int first_choose();
void computer(char arr2[ROW][COL], int row2, int col2);
void player(char arr3[ROW][COL], int row3, int col3);
int judge_win(char arr4[ROW][COL], int row4, int col4);
#endif //_GAME_H
游戏文件(game.c):
#include "game.h"
void print_board(char arr1[ROW][COL], int row1, int col1) //打印棋盘
{
int i = 0, j = 0;
printf(" ___ ___ ___ \n");
for (i = 0; i < row1 ; i++)
{
printf("| %c | %c | %c |\n", arr1[i][0], arr1[i][1],arr1[i][2]);
printf("|___|___|___|\n");
}
}
int first_choose() //选择先走的一方
{
int first1 = 0;
printf("Please choose the one who is the first to move:\n");
printf(" 1.Computer 2.Player ");
scanf("%d", &first1);
if (first1 == 1)
return 1;
else
return 2;
}
void computer(char arr2[ROW][COL], int row2, int col2) //电脑随机走
{
int x = 0, y = 0;
srand((unsigned int)time(NULL));
while (1)
{
x = rand() % row2;
y = rand() % col2;
if ((arr2[x][y] == '*') || (arr2[x][y] == '#'))
continue;
else
{
arr2[x][y] = '*';
break;
}
}
print_board(arr2, ROW, COL);
}
void player(char arr3[ROW][COL], int row3, int col3) //玩家选择坐标走
{
int x1 = 0, y1 = 0;
while (1)
{
printf("Please input the coordinate:\n");
scanf("%d%d", &x1, &y1);
if ((arr3[x1-1][y1-1] == '*') || (arr3[x1-1][y1-1] == '#'))
continue;
else
{
arr3[x1-1][y1-1] = '#';
break;
}
}
print_board(arr3, ROW, COL);
}
int judge_win(char arr4[ROW][COL], int row4, int col4) //判断输赢
{
int i = 0, j = 0;
for (i = 0; i < row4; i++)
{
if (arr4[0][i] == arr4[1][i] && arr4[1][i] == arr4[2][i])
return arr4[0][i];
if (arr4[i][0] == arr4[i][1] && arr4[i][1] == arr4[i][2])
return arr4[i][0];
}
if (arr4[0][0] == arr4[1][1] && arr4[1][1] == arr4[2][2])
return arr4[0][0];
if (arr4[0][2] == arr4[1][1] && arr4[1][1] == arr4[2][0])
return arr4[0][2];
for (i = 0; i < row4; i++)
{
for (j = 0; j < col4; j++)
{
if ((arr4[i][j] == '#') || (arr4[i][j] == '*'))
continue;
else
return 1;
}
}
return 0;
}
主程序文件(main-body.c):
#include "game.h"
void test();
void menu();
void game();
int main()
{
printf("Welcome to the Tic Tac Toe!\n");
test();
return 0;
}
void test()
{
int choose = 0;
while (1)
{
menu();
printf("Please choose 1 or 2:\n");
scanf("%d", &choose);
if (choose == 1)
game();
else if (choose == 2)
break;
else if (choose!=1&&choose!=2)
{
printf("Choose wrong!\n");
continue;
}
}
}
void menu()
{
printf("*************************\n");
printf("******1.Play 2.Exit******\n");
printf("*************************\n");
}
void game()
{
char arr[ROW][COL] = { 0 };
int i = 0, j = 0;
int first = 0;
char result = 0;
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
arr[i][j] = ' ';
}
print_board(arr, ROW, COL);
first = first_choose();
if (first == 1)
{
while (1)
{
computer(arr, ROW, COL);
result = judge_win(arr, ROW, COL);
if (result == '*')
{
printf("The computer wins!\n");
break;
}
if (result == '#')
{
printf("The player wins!\n");
break;
}
if (result == 0)
{
printf("It equalize!\n");
break;
}
player(arr, ROW, COL);
result = judge_win(arr, ROW, COL);
if (result == '*')
{
printf("The computer wins!\n");
break;
}
if (result == '#')
{
printf("The player wins!\n");
break;
}
if (result == 0)
{
printf("It equalize!\n");
break;
}
}
}
else
{
while (1)
{
player(arr, ROW, COL);
result = judge_win(arr, ROW, COL);
if (result == '*')
{
printf("The computer wins!\n");
break;
}
if (result == '#')
{
printf("The player wins!\n");
break;
}
if (result == '0')
{
printf("It equalize!\n");
break;
}
computer(arr, ROW, COL);
result = judge_win(arr, ROW, COL);
if (result == '*')
{
printf("The computer wins!\n");
break;
}
if (result == '#')
{
printf("The player wins!\n");
break;
}
if (result == 0)
{
printf("It equalize!\n");
break;
}
}
}
}
程序主界面截图: