game.h
#define ROW 3
#define COL 3
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//声明
void Displayboard(char board[ROW][COL], int row, int col);
void Initboard(char board[ROW][COL], int row, int col);
void player1(char board[ROW][COL], int row, int col);
void player2(char board[ROW][COL],int row,int col);
char ifgamed(char board[ROW][COL], int row, int col);
text.c
#include "game.h"
void menu()
{
printf("***********************************\n");
printf("***** 1 开始游戏 0 退出游戏*****\n");
printf("***********************************\n");
printf("游戏规则:\n");
printf("1.横、竖、对角连成三个棋子获胜。\n");
printf("2.下期方法采用坐标输入,如\"2 2\" 。\n\n");
}
void game()
{
char ret = 0;
char board[ROW][COL] = { 0 };
//初始化棋盘
Initboard(board, ROW, COL);
//打印棋盘
Displayboard(board, ROW, COL);
//下棋
while (1)
{
//玩家1回合
player1(board,ROW, COL);
Displayboard(board, ROW, COL);
system("cls");
ret = ifgamed(board, ROW, COL);
if (ret != 'C')
{
break;
}
//电脑回合
//1.玩家赢,返回 'H'
//2.游戏继续,返回 'C'
//3.平局,返回 'P'
//4.电脑赢,返回 '*'
player2(board, ROW, COL);
system("cls");
Displayboard(board, ROW, COL);
ret = ifgamed(board, ROW, COL);
if (ret != 'C')
{
break;
}
}
if (ret == 'H')
printf("恭喜你赢了\n");
else if (ret == '*')
printf("很遗憾你输了\n");
else if (ret == 'P')
printf("平局\n");
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch(input)
{
case 1:
printf("游戏开始\n");
system("cls");
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入:>\n");
break;
}
} while (input);
}
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "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] = ' ';
}
}
}
// 1| | |
// --| ---|---|---
// 2| | |
// --| ---|---|---
// 3| | |
// ------------
// 1 | 2 | 3
void Displayboard(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 (j == 0)
printf(" %d | ", i+1);
printf(" %c ",board[i][j]);
if (j < col-1)
printf("|");
}
printf("\n");
if (i < row-1)
{
//if (i == 0)
printf(" ---| ");
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
printf("\n");
}
}
for (i = 0; i < 3 * (col+1); i++)
{
if (0 == i)
printf(" ");
printf("-");
}
printf("\n");
for (i = 1; i < col; i++)
{
if (1 == i)
printf(" ");
if (i < col )
{
printf(" %d |",i);
}
}
printf(" %d ",col);
printf("\n");
}
void player1(char board[ROW][COL],int row, int col)
{
int x = 0;
int y = 0;
printf("玩家回合请输入坐标:>");
while (1)
{
scanf("%d%d", &x, &y);
//判断输入合法性
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
//判断输入坐标是否已有旗子
if (board[x-1][y-1] == ' ')
{
board[x-1][y-1]='H';
break;
}
else
{
printf("该坐标有棋子,请重新输入:>");
}
}
else
{
printf("坐标非法,请重新输入:>");
}
}
}
void player2(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑回合\n");
while (1)
{
x = rand() % row;
y = rand() % col;
printf("%d %d\n", x, y);
//判断输入合法性
//判断输入坐标是否已有旗子
if (board[x][y] == ' ')
{
board[x][y] = '*';
break;
}
}
}
int IsFull(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 ifgamed(char board[ROW][COL], int row, int col)
{
int i = 0;
//行分胜负
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][0] !=' ')
{
return board[i][0];
}
}
//列分胜负
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[0][i] == board[2][i] && board[0][i] != ' ')
{
return board[0][i];
}
}
//对角分胜负
if (board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] != ' ')
{
return board[0][0];
}
if (board[0][2] == board[1][1] && board[0][0] == board[2][0] && board[0][2] != ' ')
{
return board[0][2];
}
//平局
if (1 == IsFull(board, row, col))
{
return 'P';
}
return 'C';
}
一天搞这个越敲越懵逼,总算搞出来了,还有点小bug。