C:井字棋基础版
井字棋在C语言中并不算太难,它对于初级小白来说是一次对于历史的作结,也可以是对未来的预习,下面看具体代码:
首先需要打印一个游戏的菜单,游戏菜单应该十分容易,包括一个主函数,一个自定义函数:
void menu()
{
printf("*******************\n");
printf("***** 1.0 Play ****\n");
printf("***** 0.0 Exit ****\n");
printf("*******************\n");
}
void text()
{
srand((unsigned int)time(NULL));
int input = 0;
//scanf("%d", &input);
do
{
menu();
printf("请输入1或0,0结束,1开始\n");
scanf("%d", &input);
//system("cls");
switch (input)
{
case 1:
//game();
printf("开始游戏\n");
game();
break;
case 0:
printf("结束游戏\n");
break;
default:
printf("输入错误,请重输\n");
break;
}
} while (input);
}
int main()
{
text();
return 0;
}
Text函数包括比赛函数和菜单函数,菜单函数十分容易,在这里不做详解,下面来讲text函数,比赛一定是一个循环,所以函数包括一个循环,但这个循环不知道结束条件,但需要先开始,所以用do循环,循环中包含比赛内容:
void game()
{
char board[ROW][COL];
char ret;
init_board(board, ROW, COL);
print_board(board, ROW, COL);
while (1)
{
play_move(board, ROW, COL);
print_board(board, ROW, COL);
ret = win_in(board, ROW, COL);
if (ret != 'C')
{
break;
}
computer_move(board, ROW, COL);
print_board(board, ROW, COL);
ret = win_in(board, ROW, COL);
if (ret !='C')
{
break;
}
}
if (ret == '#')
{
printf("电脑赢\n");
}
else if (ret == '*')
{
printf("玩家赢\n");
}
else if(ret=='Q')
{
printf("平局\n");
}
}
上面的比赛函数,也就是game函数包含多个函数内容,下面我们一个一个分析:
init_board(board, ROW, COL);
这个函数init_board是初始化棋盘的函数,包含三个参数,这个函数初始化棋盘为“空格”函数内容为
void init_board(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
ROW表示行,COL表示列,由于只是一个二维数组所以使用嵌套循环,接下来看第二个函数打印函数
void print_board(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
printf("|");
}
printf("\n");
for (j = 0; j < col; j++)
{
printf("---");
if(j<col-1)
printf("|");
}
printf("\n");
}
}
接下来是玩家移动棋子,玩家移动主要是在3x3棋盘中移动,为了便于玩家更容易理解,所以数组从下标为一的开始,也就是arr[i+1]开始,下面是代码:
void play_move(char board[ROW][COL], int row, int col)
{
printf("玩家下棋:");
while (1)
{
printf("输入坐标:");
int x = 0;
int y = 0;
//while(board[x][y]==' ')
scanf("%d %d", &x, &y);
if (x > 0 && x <= row && y>0 && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("请重输\n");
}
}
else
printf("输入错误,请重输\n");
}
然后是电脑端移动,电脑移动需要判断棋盘中是否有棋子,如果有棋子,则要重新输入,如果输入的大于过大,侧要重新输入。
然后是判断输赢,判断输赢的内容并不十分复杂,这是代码
char win_in(char board[ROW][COL], int row, int col)
{
int x = 0;
for (x = 0; x < row; x++)
{
if (board[x][0] == board[x][1] && board[x][1] == board[x][2] && board[x][0] != ' ')
return board[x][0];
}
for (x = 0; x < col; x++)
{
if (board[0][x] == board[1][x] && board[1][x] == board[2][x] && board[0][x] != ' ')
return board[0][x];
}
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[0][2] && board[1][1] != ' ')
{
return board[1][1];
}
if (is_full(board, row, col) == 1)
{
return 'Q';
}
return 'C';
}
其中包含一个判断是否结束的函数,is_full(board, row, col),这个函数用来判断是否棋盘已满或未满,为了避免is_full(char board[ROW][COL]函数影响其他内容,我们在函数中加入:
static int is_full(char board[ROW][COL], int row, int col);
int is_full(char board[ROW][COL], int row, int col)
{
int x = 0;
int j = 0;
for (x = 0; x < row; x++)
{
for (j = 0; j < col; j++)
{
if (board[x][j] == ' ')
return 0;
}
}
return 1;
}
已满返回1,未满返回0,
这是整个井字棋的代码,当然,为了方便,我们用一个头文件来写函数名:
#pragma once
#include
#include
#include
#define ROW 3
#define COL 3
void init_board(char board[ROW][COL], int row, int col);
void print_board(char board[ROW][COL], int row, int col);
void play_move(char board[ROW][COL], int row, int col);
void computer_move(char board[ROW][COL], int row, int col);
char win_in(char board[ROW][COL], int row, int col);
下面是整个代码,分成了三个部分:
头文件
#pragma once
#include
#include
#include
#define ROW 3
#define COL 3
void init_board(char board[ROW][COL], int row, int col);
void print_board(char board[ROW][COL], int row, int col);
void play_move(char board[ROW][COL], int row, int col);
void computer_move(char board[ROW][COL], int row, int col);
char win_in(char board[ROW][COL], int row, int col);
比赛部分
#include "game.h"
//#define ROW,COL
static int is_full(char board[ROW][COL], int row, int col);
void init_board(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
void print_board(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
printf("|");
}
printf("\n");
for (j = 0; j < col; j++)
{
printf("---");
if(j<col-1)
printf("|");
}
printf("\n");
}
}
void play_move(char board[ROW][COL], int row, int col)
{
printf("玩家下棋:");
while (1)
{
printf("输入坐标:");
int x = 0;
int y = 0;
//while(board[x][y]==' ')
scanf("%d %d", &x, &y);
if (x > 0 && x <= row && y>0 && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("请重输\n");
}
}
else
printf("输入错误,请重输\n");
}
}
void computer_move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑下\n");
while (1)
{
//printf("电脑下/n");
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] ='#';
break;
}
}
}
int is_full(char board[ROW][COL], int row, int col)
{
int x = 0;
int j = 0;
for (x = 0; x < row; x++)
{
for (j = 0; j < col; j++)
{
if (board[x][j] == ' ')
return 0;
}
}
return 1;
}
char win_in(char board[ROW][COL], int row, int col)
{
int x = 0;
for (x = 0; x < row; x++)
{
if (board[x][0] == board[x][1] && board[x][1] == board[x][2] && board[x][0] != ' ')
return board[x][0];
}
for (x = 0; x < col; x++)
{
if (board[0][x] == board[1][x] && board[1][x] == board[2][x] && board[0][x] != ' ')
return board[0][x];
}
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[0][2] && board[1][1] != ' ')
{
return board[1][1];
}
//
if (is_full(board, row, col) == 1)
{
return 'Q';
}
return 'C';
}
测试部分
#include "game.h"
void menu()
{
printf("*******************\n");
printf("***** 1.0 Play ****\n");
printf("***** 0.0 Exit ****\n");
printf("*******************\n");
}
void game()
{
char board[ROW][COL];
char ret;
init_board(board, ROW, COL);
print_board(board, ROW, COL);
//system("cls");
while (1)
{
play_move(board, ROW, COL);
print_board(board, ROW, COL);
ret = win_in(board, ROW, COL);
if (ret != 'C')
{
break;
}
computer_move(board, ROW, COL);
print_board(board, ROW, COL);
ret = win_in(board, ROW, COL);
if (ret !='C')
{
break;
}
}
if (ret == '#')
{
printf("电脑赢\n");
}
else if (ret == '*')
{
printf("玩家赢\n");
}
else if(ret=='Q')
{
printf("平局\n");
}
//computer_move(board, ROW, COL);
}
void text()
{
srand((unsigned int)time(NULL));
int input = 0;
//scanf("%d", &input);
do
{
menu();
printf("请输入1或0,0结束,1开始\n");
scanf("%d", &input);
//system("cls");
switch (input)
{
case 1:
//game();
printf("开始游戏\n");
game();
break;
case 0:
printf("结束游戏\n");
break;
default:
printf("输入错误,请重输\n");
break;
}
} while (input);
}
int main()
{
text();
return 0;
}