三子棋是一个比较容易写的小游戏,大概思路如下:
打印菜单——选择先行——打印初始棋盘——玩家下棋或者电脑随机下棋——判断输赢——结束游戏
接下来看看代码
如果可以,我们应该将电脑变得更智能,这个代码还没有将电脑变得智能,只是一个简单的三子棋的思路。
- 头文件(game.h)
#ifndef __GAME_H__
#define __GAME_H__
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#pragma warning(disable:4996)
#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 ComputerMove(char board[ROW][COL], int row, int col);//电脑走
void PlayerMove(char board[ROW][COL], int row, int col);//玩家走
char CheckWin(char board[ROW][COL], int row, int col);//判断输赢
#endif // __GAME_H__
- 测试代码(test.c)
#include "game.h"
//菜单
void meau()
{
printf("*******************************\n");
printf("****** 1. play 0.exit ******\n");
printf("*******************************\n");
}
void play()
{
printf("*********************************************\n");
printf("****** 1. playerplay 2.computerplay ******\n");
printf("*********************************************\n");
}
//游戏
void game()
{
char board[ROW][COL] = { 0 };
char ret = 0;
int move = 0;
play(); //选择先行玩家
printf("请选择先行玩家>:");
scanf("%d",&move);
InitBoard(board, ROW, COL); //初始化棋盘
DisPlayBoard(board, ROW, COL);//打印空棋盘
switch (move) //判断谁先走 1.玩家先走 2.电脑先走
{
case 1:
{
while (1)
{
PlayerMove(board, ROW, COL);
ret = CheckWin(board, ROW, COL);
if (ret != ' ')
break;
DisPlayBoard(board, ROW, COL);
ComputerMove(board, ROW, COL);
ret = CheckWin(board, ROW, COL);
if (ret != ' ')
break;
DisPlayBoard(board, ROW, COL);
}
break;
}
case 2:
{
while (1)
{
ComputerMove(board, ROW, COL);
ret = CheckWin(board, ROW, COL);
if (ret != ' ')
break;
DisPlayBoard(board, ROW, COL);
PlayerMove(board, ROW, COL);
ret = CheckWin(board, ROW, COL);
if (ret != ' ')
break;
DisPlayBoard(board, ROW, COL);
}
break;
}
default:
printf("输入错误,请重新选择!\n");
break;
}
if (ret == 'X') //判断输赢
printf("玩家赢\n");
else if (ret == '0')
printf("电脑赢\n");
else if (ret == 'q')
printf("平局\n");
DisPlayBoard(board, ROW, COL);
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
meau();
printf("请选择:>");
scanf_s("%d",&input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,重新输入\n");
break;
}
} while (input);
}
int main()
{
test();
system("pause");
return 0;
}
- 游戏函数
#include "game.h"
//初始化棋盘,开始初始化为空格
void InitBoard(char board[ROW][COL], int row, int col)
{
memset(board, ' ', row*col * sizeof(board[0][0]));
}
//打印棋盘
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++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
printf("|");
}
printf("\n");
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
}
printf("\n");
}
}
//电脑先走
void ComputerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[1][1] == ' ') //先占中间位置
{
board[1][1] = '0';
break;
}
else if (board[x][y] == ' ')
{
board[x][y] = '0';
break;
}
}
}
//玩家先走
void PlayerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("请输入坐标:>(1-3)");
scanf("%d%d", &x, &y);
if ((x >= 1 && x <= row) && (y >= 1 && y <= col) && board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = 'X';
break;
}
if (x > row || y > col)
{
printf("超出范围\n");
}
else
{
printf("输入位置占用\n");
}
}
}
//判断棋盘是否填满
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 CheckWin(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][1] == board[i][2])
&& (board[i][1] != ' '))
return board[i][1];
}
for (i = 0; i < col; i++) //检查列
{
if ((board[0][i] == board[1][i])
&& (board[1][i] == board[2][i])
&& (board[1][i] != ' '))
return board[1][i];
}
if ((board[0][0] == board[1][1]) //检查对角线
&& (board[1][1] == board[2][2])
&& (board[1][1] != ' '))
return board[1][1];
if ((board[0][2] == board[1][1])
&& (board[1][1] == board[2][0])
&& (board[1][1] != ' '))
return board[1][1];
if (IsFull(board, row, col)) //棋盘填满
return 'q';
return ' ';
}