三字棋可是很多人的童年回忆呢,那么身为一个程序员,我们如何把它变成代码呢??只需要三步!!!
1. game.c
#include "game.h"
void InitBoard(char board[ROW][COL], int row, int col)//创建一个ROW行 COL列的二维数组
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
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-1; j++)
{
printf(" %c ┊", board[i][j]);
}
printf(" %c \n", board[i][row - 1]);
if (i != row - 1)
{
for (j = 0; j < row - 1; j++)
{
printf("━━━╃");
}
printf("━━\n");
}
}
}
void ComputerMove(char board[ROW][COL], int row, int col)//电脑走
{
while (1)
{
int x = rand() % row;
int y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] ='O';
break;
}
}
}
void PlayerMove(char board[ROW][COL], int row, int col)//玩家走
{
int x = 0;
int y = 0;
printf("该你走了\n");
while (1)
{
printf("请输入一个坐标: ");
scanf("%d%d", &x, &y);
if ((x >= 1) && (x <= row) && (y >= 1) && (y <= col))
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = 'X';
break;
}
else
printf("你输入的坐标已经被使用了\n");
}
else
printf("你输入的坐标错了,这是一个3*3的格子\n");
}
}
char IsWin(char board[ROW][COL], int row, int col)//判断输赢,返回一个值
{
int i = 0;
int j = 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][0];
}
for (j = 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] != ' ')
return board[1][j];
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
return board[1][1];
else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
return board[1][1];
else if (IsFull(board, ROW, COL))
return 'q';
else
return ' ';
}
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;
}
2.three——chess.c
#include "game.h" //首先引入自己的头文件
void mune()
{
printf("********************************\n");
printf("*********** 1 play *********\n");
printf("*********** 0 exit *********\n");
printf("********************************\n");
//打印一个菜单,选择进入或者退出
}
void game()
{
char board[ROW][COL] = {0};//创建一个ROW COL二维数组
char ret = '0';
InitBoard(board, ROW, COL);//初始化棋盘
DisplayBoard(board, ROW, COL);//打印整个空棋盘
//开始下棋
do
{
printf("\n电脑已经走了一步\n");
ComputerMove(board, ROW, COL);//电脑走
DisplayBoard(board, ROW, COL);//玩家走
if (ret = IsWin(board, ROW, COL) != ' ') //判断输赢,返回一个值,如果是空格,则继续下棋,如果返回O,电脑胜;如果返回X,玩家胜
{ //嵌套一个判断棋盘是否满的函数,如果棋盘满,没人胜利,则返回q,平局
break;
}
PlayerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = IsWin(board, ROW, COL);
}
while (ret = ' '); //如果返回值是空格,一直循环
if (IsWin(board, ROW, COL) == 'q') //返回q,平局
{
printf("\n平局\n");
}
else if (IsWin(board, ROW, COL) == 'X') //返回X,玩家胜
{
printf("\n玩家胜利\n");
}
else if (IsWin(board, ROW, COL) == 'O') //返回O,电脑胜;
{
printf("\n电脑胜利\n");
}
}
int main()
{
int input = 0;
do
{
srand((unsigned int)time(NULL)); //生成一个随机数
mune();
printf("请选择 ");
scanf("%d", &input);
switch(input)
{
case 1:
game();
break;
case 0:
printf("你选择了退出游戏.");
break;
default:
printf("请重新输入数字0或1\n");
}
}
while (input);
return 0;
}
3.game.h
#ifndef __GAME_H__
#define __GAME_H__
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#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 PlayerMove(char board[ROW][COL], int row, int col);
void ComputerMove(char board[ROW][COL], int row, int col);
int IsFull(char board[ROW][COL], int row, int col);
char IsWin(char board[ROW][COL], int row, int col);
#endif