C语言实现三子棋
三子棋
思路 1.用二维数组保存数据。 2.通过输入坐标的方式落子。 3.人机对战,电脑采用随机生成的坐标。
代码实现
程序多文件形式,一个头文件game.h和两个源文件main.c,game.c,其中main.c和 game.c,之间只有Game一个函数的交互,耦合低,两个文件彼此高度独立。 程序注释中对每个函数的作用有详细解释, 源码如下: 1.头文件game.h(包含头文件及宏定义等)
#ifndef __GAME_H__
#define __GAME_H__
#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<time.h>
#define ROW 3
#define COL 3
#define INIT ' '
#define WHITE 'X'
#define BLACK 'O'
#define DRAW 'D'
#define NEXT 'N'
#pragma warning(disable:4996)
extern void Game();
#endif
2.源文件main.c(主函数,程序入口)
#include"game.h"
void Maun()
{
printf("------------------------------\n");
printf("| 1.Play 0.Exit |\n");
printf("------------------------------\n");
}
int main()
{
int quit = 0;
while (!quit)
{
Maun();
printf("Please select:");
int select = 0;
scanf("%d",&select);
switch (select){
case 1:
Game();
break;
case 0:
quit = 1;
break;
default:
printf("Enter error,please try again!\n");
continue;
break;
}
}
printf("Bye bye!\n");
system("pause");
return 0;
}
3.源文件game.c(游戏具体实现)
#include"game.h"
static void InitBoard(char board[][COL], int row, int col)
{
for (int i = 0; i < row; i++){
for (int j = 0; j < col; j++){
board[i][j] = INIT;
}
}
}
static void ShowBoard(char board[][COL], int row, int col)
{
printf(" ");
for (int j = 0; j < col; j++){
printf("%3d ",j+1);
}
printf("\n--------------\n");
for (int i = 0; i < row; i++){
printf("%-2d",i+1);
for (int j = 0; j < col; j++){
printf("| %c ", board[i][j]);
}
printf("\n--------------\n");
}
printf(" \n");
}
static char IsEnd(char board[][COL], int row, int col)
{
for (int i = 0; i < row; i++){
if (board[i][0] == board[i][1] && \
board[i][1] == board[i][2] && \
board[i][0] != INIT)
{
return board[i][0];
}
}
for (int j = 0; j < col; j++){
if (board[0][j] == board[1][j] && \
board[1][j] == board[2][j] && \
board[0][j] != INIT)
{
return board[0][j];
}
}
if (board[0][0] == board[1][1] && \
board[1][1] == board[2][2] && \
board[0][0] != INIT)
{
return board[0][0];
}
if (board[0][2] == board[1][1] && \
board[1][1] == board[2][0] && \
board[1][1] != INIT)
{
return board[1][1];
}
for (int i = 0; i < row; i++){
for (int j = 0; j < col; j++){
if (board[i][j] == INIT){
return NEXT;
}
}
}
return DRAW;
}
static void PlayerMove(char board[][COL], int row, int col)
{
while (1){
printf("Please enter your position(row col):");
int x = 0;
int y = 0;
scanf("%d %d",&x,&y);
if (x<1 || x>3 || y<1 || y>3){
printf("Wrong position!\n");
continue;
}
if (board[x-1][y-1] != INIT){
printf("This position is not empty!\n");
continue;
}
board[x-1][y-1] = WHITE;
break;
}
}
static void ComputerMove(char board[][COL], int row, int col)
{
while (1)
{
int x = rand() % row;
int y = rand() % col;
if (board[x][y] != INIT){
continue;
}
board[x][y] = BLACK;
break;
}
}
void Game()
{
srand((unsigned long)time(NULL));
char board[ROW][COL];
InitBoard(board,ROW,COL);
int result = 0;
while (1)
{
system("cls");
ShowBoard(board,ROW,COL);
PlayerMove(board,ROW,COL);
result = IsEnd(board, ROW, COL);
if (result != NEXT){
ShowBoard(board, ROW, COL);
break;
}
ComputerMove(board,ROW,COL);
result = IsEnd(board, ROW, COL);
if (result != NEXT){
ShowBoard(board, ROW, COL);
break;
}
}
switch (result){
case WHITE:
printf("You win!\n");
break;
case BLACK:
printf("You lose!\n");
break;
case DRAW:
printf("Draw!\n");
break;
default:
printf("Bug\n");
}
}
|