编写三子棋所需的步骤:
1)首先,我们得先要有一个棋盘,那我们就得先编写一个函数来将棋盘初始化,再编写一个函数负责打印我们的棋盘。
我们打印的棋盘为:
2)有了棋盘后,就可以下棋了。我们可以编写两个函数,一个是玩家下棋子,一个是电脑随机下棋子。
3)最后就是判断是谁赢得了比赛,还是平局。
这些就是三子棋的大致步骤了。
以下就是实现三子棋的程序:
头文件
#include<sanziqi.h>
#ifndef __SANZIQI_H__
#define __SANZIQI_H__
#define ROWS 3
#define COLS 3
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void print_board(char board[ROWS][COLS], int rows, int cols);//打印棋盘
void init_board(char board[ROWS][COLS], int rows, int cols);//清空棋盘
void play_game(char board[ROWS][COLS]);//开始游戏
void com_move(char board[ROWS][COLS]);//电脑走
void player_move(char board[ROWS][COLS]);//玩家走
char check_win(char board[ROWS][COLS]);//检测看谁获胜
#endif //__SANZIQI_H__
主函数
#include"sanziqi.h"
void menu()
{
printf("**************************\n");
printf("******* 1.开始游戏 *******\n");
printf("******* 0.退出游戏 *******\n");
printf("**************************\n");
}
void game()
{
char board[ROWS][COLS] = { 0 };
print_board(board, ROWS, COLS);
init_board(board, ROWS, COLS);
play_game(board);
com_move(board);
player_move(board);
check_win(board);
}
int main()
{
do
{
int input = 0;
srand((unsigned)time(NULL));
menu();
printf("请选择:");
scanf_s("%d", &input);
switch(input)
{
case 1:
game();
break;
case 0:
printf("退出游戏!!!\n");
break;
default:
printf("输入错误,请重新输入!\n");
break;
}
} while (1);
return 0;
}
功能实现
#include<game.c>
#include"sanziqi.h"
void print_board(char board[ROWS][COLS], int rows, int cols)
{
int i = 0;
for (i = 0; i < ROWS; i++)
{
printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
if (i < ROWS-1)
{
printf("---|---|---\n");
}
}
}
void init_board(char board[ROWS][COLS], int rows, int cols)
{
int i = 0;
int j = 0;
for (i=0; i<rows; i++)
{
for (j=0;j<cols; j++)
{
board[i][j] = ' ';
}
}
}
void com_move(char board[ROWS][COLS])
{
int x = 0;
int y = 0;
while (1)
{
x = rand() % 3;
y = rand() % 3;
if (board[x][y] == ' ')
{
board[x][y] = 'O';
return;
}
}
}
void player_move(char board[ROWS][COLS])
{
int x = 0;
int y = 0;
while (1)
{
printf("请输入坐标:");
scanf_s("%d%d", &x, &y);
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = 'X';
return;
}
else
{
printf("输入错误");
}
}
}
char check_win(char board[ROWS][COLS])
{
int i = 0;
for (i = 0; i < 3; i++)
{
if ((board[i][0] == board[i][1]) && (board[i][1] == board[i][2]) && (board[i][1] != ' '))
{
return 'p';
}
}
for (i = 0; i < 3; i++)
{
if ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]) && (board[1][i] != ' '))
{
return 'p';
}
}
if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2]) && (board[1][1] != ' '))
{
return 'p';
}
if ((board[0][2] == board[1][1]) && (board[1][1] == board[2][0]) && (board[1][1] != ' '))
{
return 'p';
}
}
void play_game(char board[ROWS][COLS])
{
char ret;
int count = 0;
while (1)
{
if (count >= 9)
{
printf("平局!!!!\n");
break;
}
player_move(board);
count++;
print_board(board, ROWS, COLS);
if ((ret = check_win(board)) == 'p')
{
printf("您获胜了!!!\n");
break;
}
if (count >= 9)
{
printf("平局!!!\n");
break;
}
count++;
printf("电脑走:\n");
com_move(board);
print_board(board, ROWS, COLS);
if ((ret = check_win(board)) == 'p')
{
printf("电脑获胜!!!\n");
break;
}
}
}