简易“三子棋”游戏,以“五子棋”游戏为模板,率先在棋盘上三子成线的一方获胜。
共有三个文件,分别是game.h、game.c和test.c。
1、game.h
#ifndef __GAME_H__
#define __GAME_H__
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define ROWS 3
#define COLS 3
void init_board(char board[ROWS][COLS], int row, int col);
void display_board(char board[ROWS][COLS], int row, int col);
void player_move(char board[ROWS][COLS], int row, int col);
void computer_move(char board[ROWS][COLS], int row, int col);
char check_win(char board[ROWS][COLS], int row, int col);
#endif //__GAME_H__
2、game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void init_board(char board[ROWS][COLS], int row, int col)
{
memset(board, ' ', col*row*sizeof(char));//初始化棋盘
}
void display_board(char board[ROWS][COLS], int row, int col)//设计并展示棋盘
{
int i = 0;
for (i = 0; i < row; i++)
{
printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
if (i != 2)
{
printf("---|---|---\n");
}
}
}
void player_move(char board[ROWS][COLS], int row, int col)//玩家走
{
int x = 0;
int y = 0;
while (1)
{
printf("请输入坐标:>");
scanf("%d%d", &x, &y);
x--;
y--;
if (((x >= 0) && (x <= 2)) && ((y >= 0) && (y <= 2)))
{
if (board[x][y] == ' ')
{
board[x][y] = 'X';
break;
}
else
{
printf("下标有误,请重新输入。");
}
}
else
{
printf("下标有误,请重新输入。");
}
}
}
void computer_move(char board[ROWS][COLS], int row, int col)//电脑走
{
while (1)
{
int x = rand() % 3;
int y = rand() % 3;
if (board[x][y] == ' ')
{
board[x][y] = '0';
break;
}
}
}
static int is_full(char board[ROWS][COLS], 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 check_win(char board[ROWS][COLS], 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][0]) &&
(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 (is_full(board, row, col))
{
return 'q';
}
return ' ';
}
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include "game.h"
void game()
{
char board[ROWS][COLS];
char ret = 0;
init_board(board, ROWS, COLS);
display_board(board, ROWS, COLS);
srand((unsigned int)time(NULL));//建立随机数生成器
while (1)
{
player_move(board, ROWS, COLS);
if ((ret = check_win(board, ROWS, COLS)) != ' ')
{
break;
}
computer_move(board, ROWS, COLS);
if ((ret = check_win(board, ROWS, COLS)) != ' ')
{
break;
}
display_board(board, ROWS, COLS);
}
if (ret == 'X')
{
printf("玩家赢\n");
}
else if (ret == '0')
{
printf("呵呵,电脑赢\n\n");
}
else if (ret == 'q')
{
printf("平局\n");
}
display_board(board, ROWS, COLS);
}
void menu()
{
printf("1.play\n");
printf("0.exit\n");
}
int main()
{
int input = 0;
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
break;
default:
printf("选择错误\n");
break;
}
} while (input);
return 0;
}
a、玩家赢
b、电脑赢
c、平局