您的位置:首页 > 编程语言 > C语言/C++

C语言实现三子棋程序

2018-04-07 23:38 351 查看
实现三子棋程序,首先打印菜单,选择游戏或者退出,然后进行棋盘初始化,用符号表示出三子棋棋盘,然后实现玩家走,电脑走,电脑走的时候用rand随机位置,最后检查输赢。(可以说这个游戏的难度是输给电脑,哈哈哈)

game.h头文件

#ifndef __GAME_H__
#define __GAME_H__

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ROWS 3
#define COLS 3

void init_board(char board[ROWS][COLS], int rows, int cols);
void display_board(char board[ROWS][COLS], int rows, int cols);
void computer_move(char board[ROWS][COLS], int rows, int cols);
void player_move(char board[ROWS][COLS], int rows, int cols);
char check_win(char board[ROWS][COLS], int rows, int cols);

#endif __GAME_H__


test.c源文件

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "game.h"

void game()
{
char board[ROWS][COLS];
double ret = 0.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;
}
display_board(board, ROWS, COLS);
printf("\n");
computer_move(board, ROWS, COLS);
if((ret = check_win(board, ROWS, COLS))!=' ')
{
break;
}
display_board(board, ROWS, COLS);
printf("\n");
}
if(ret == 'X')
{
printf("恭喜你,赢了此次比赛\n");
}
if(ret == '0')
{
printf("电脑胜,你输了\n");
}
else if(ret == 'p')
{
printf("平局\n");
}
display_board(board, ROWS, COLS);
}

void menu()
{
printf("********************************\n");
printf("********     1.play      *******\n");
printf("********     0.exit     ********\n");
printf("********************************\n");

}
int main()
{
int input = 0;
do
{
menu();
printf("请选择:");
scanf("%d", &input);
switch(input)
{
case 1:
game();
break;
case 0:
break;
default:
printf("输入错误,请重新选择:\n");
}
}
while(input);
return 0;
}


game.c源文件

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

void init_board(char board[ROWS][COLS], int rows, int cols)
{
memset(board, ' ', rows*cols*sizeof(char));
}

void display_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)
{
printf("---|---|---\n");

}
}
}

void player_move(char board[ROWS][COLS], int rows, int cols)
{
int x = 0;
int y = 0;
while(1)
{
printf("请输入坐标>");
scanf("%d%d", &x, &y);
x--;
y--;
if(((x>=0) && (x<rows)) && ((y>=0) && (y<=cols)))
{
if(board[x][y] == ' ')
{
board[x][y] = 'X';
break;
}
else
{
printf("输入有误,请重新输入\n");
}
}
else
{
printf("输入有误,请重新输入\n");
}
}
}

void computer_move(char board[ROWS][COLS], int rows, int cols)
{
int x = 0;
int y = 0;
x = rand()%3;
y = rand()%3;
while(1)
{
x = rand()%3;
y = rand()%3;
if(board[x][y] == ' ')
{
board[x][y] = '0';
break;
}
}
}
int check_full(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++)
{
if(board[i][j] == ' ')
return 0;
}
}
return 1;
}

char check_win(char board[ROWS][COLS], int rows, int cols)
{
int i = 0;
int j = 0;
for(i=0; i<rows; i++)
{
if((board[i][0]==board[i][1]) && (board[i][1]==board[i][2]) && (board[i][0]!= ' '))
{
return board[i][0];
}
}
for(j=0; j<cols; i++)
{
if((board[0][i]==board[1][i]) && (board[1][i]==board[2][i]) && (board[0][i]!= ' '))
{
return board[0][i];
}

}
if((board[0][0]==board[1][1]) && (board[1][1]==board[2][2]) && (board[0][0]!= ' '))
{
return board[0][0];
}
if((board[2][0]==board[1][1]) && (board[1][1]==board[0][2]) && (board[0][2]!= ' '))
{
return board[0][0];
}
if(check_full(board, rows, cols))
{
return 'p';
}
return ' ';
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: