? c语言实现三子棋? ?
一、关键思想?
用字符二维数组来实现“棋子”存储。
二、基本步骤
1.菜单界面
void menu()
{
printf("******************\n");
printf("******1.play******\n");
printf("******0.exit******\n");
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择->");
scanf("%d", &input);
switch(input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default :
printf("输入错误,重新输入\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
?2.二维数组的初始化,将二维数组里的元素都初始化为‘空格’? ?.
void InitBoard(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++)
{
board[i][j] = ' ';
}
}
}
3.棋盘打印?
void DisplayBoard(char board[][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if(j<col-1)
printf("|");
}
printf("\n");
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
}
printf("\n");
}
}
?4.下棋的流程?
void game()
{
char board[ROW][COL] = { 0 };
InitBoard(board, ROW, COL); //将棋盘都初始化为空格
DisplayBoard(board, ROW, COL); //打印棋盘
char ret = 0;
while (1)
{
player_move(board, ROW, COL); //玩家先下棋
DisplayBoard(board, ROW, COL); //每下一步棋都打印棋盘
ret = is_win(board, ROW, COL); //判断输赢,如果返回的是*,代表玩家赢
if (ret != 'C') //如果返回C,则代表还需继续下棋
{
break;
}
computer_move(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = is_win(board, ROW, COL); //如果返回的是#代表电脑赢
if(ret!='C')
{
break;
}
}
if (ret == '*')
printf("玩家赢");
else if (ret == '#')
printf("电脑赢");
else
printf("平局");
}
5.玩家下棋的函数实现
void player_move(char board[][COL], int row, int col)
{
printf("玩家下棋:");
int x= 0;
int y = 0;
while (1)
{
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)//要求输入的横纵坐标在
//1~3之间
{
if (board[x - 1][y - 1] == ' ') //如果输入的坐标的位置为空,则可以下棋
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("该位置已被占用,重新输入其他坐标\n");
}
}
else
{
printf("输入的坐标不合法,请重新输入\n");
}
}
}
6.电脑下棋函数的实现
void computer_move(char board[][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑下棋>\n");
while (1)
{
x = rand() % ROW; //利用rand()生成随机数,并模上行的大小
y = rand() % COL;
if (board[x][y] == ' ') //不需再判断坐标是否合法,随机数%3得到的数:0~2
{
board[x][y] = '#';
break;
}
}
}
7.判断输赢的函数: 判断三行或三列或两条对角线上是否有三个相邻位置都相同的棋子,且不是空,如果有则返回该种棋子,例如再第一行上有三个*,则函数返回*.
规定:
玩家赢返回*
电脑赢返回#
平局返回Q
还需继续下棋返回C
char is_win(char board[][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][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][2] && 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 (1 == is_full(board, row, col))
{
return 'Q';
}
//继续
return 'C';
}
?is_win中调用的一个函数,判断棋盘是否全部下满
int is_full(char board[][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;
}
三、完整代码
test.h
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
printf("******************\n");
printf("******1.play******\n");
printf("******0.exit******\n");
}
void game()
{
char board[ROW][COL] = { 0 };
InitBoard(board, ROW, COL);
DisplayBoard(board, ROW, COL);
char ret = 0;
while (1)
{
player_move(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'C')
{
break;
}
computer_move(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = is_win(board, ROW, COL);
if(ret!='C')
{
break;
}
}
if (ret == '*')
printf("玩家赢");
else if (ret == '#')
printf("电脑赢");
else
printf("平局");
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择->");
scanf("%d", &input);
switch(input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default :
printf("输入错误,重新输入\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
game.h
#pragma once
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define ROW 3 //3行
#define COL 3 //3列
void InitBoard(char board[][COL], int row, int col);
void DisplayBoard(char board[][COL], int row, int col);
void player_move(char board[][COL], int row, int col);
void computer_move(char board[][COL], int row, int col);
char is_win(char board[][COL], int row, int col);
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void InitBoard(char board[][COL], int row, int 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[][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if(j<col-1)
printf("|");
}
printf("\n");
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
}
printf("\n");
}
}
void player_move(char board[][COL], int row, int col)
{
printf("玩家下棋:");
int x= 0;
int y = 0;
while (1)
{
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)//要求输入的横纵坐标在
//1~3之间
{
if (board[x - 1][y - 1] == ' ') //如果输入的坐标的位置为空,则可以下棋
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("该位置已被占用,重新输入其他坐标\n");
}
}
else
{
printf("输入的坐标不合法,请重新输入\n");
}
}
}
void computer_move(char board[][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑下棋>\n");
while (1)
{
x = rand() % ROW; //利用rand()生成随机数,并模上行的大小
y = rand() % COL;
if (board[x][y] == ' ') //不需再判断坐标是否合法,随机数%3得到的数:0~2
{
board[x][y] = '#';
break;
}
}
}
int is_full(char board[][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;
}
char is_win(char board[][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][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][2] && 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 (1 == is_full(board, row, col))
{
return 'Q';
}
//继续
return 'C';
}
四、演示过程
|